Support » Plugin: Membership 2 » adding a new payment gateway

  • HI,

    I issue to create new gateway in Membership 2

    Fatal error: Uncaught Error: Call to undefined method MS_Gateway_Mtc::before_load()

    I use API Membership 2

    Create your own payment gateway and hook it up with Membership 2 by using this function!
    
    Creating your own payment gateway requires good php skills. To get you started follow these steps:
    
    Copy the folder "/app/gateway/manual" to the "wp-contents/plugins" folder, and name it "membership-mygateway"
    
    Rename all files inside the "membership-mygateway" and replace the term "manual" with "mygateway"
    
    Edit all files, rename the class names inside the files to "_Mygateway" (replacing "_Manual")
    
    In class MS_Gateway_Mygateway make following changes:
    
    Set the value of const ID to "mygateway"
    
    Change the assigned name in function "after_load"
    
    Add a plugin header to the file, e.g. /*
    
    Plugin name: Membership 2 Mygateway
    /
    Add the following line to the bottom of the file: add_action( 'ms_init', 'mygateway_register' ); function mygateway_register( $api ) { $api->register_payment_gateway( MS_Gateway_Mygateway::ID, 'MS_Gateway_Mygateway' ) }
    Now you have created a new plugin that registers a custom payment gateway for Membership 2! Implementing the payment logic is up to you - you can get a lot of insight by reviewing the existing payment gateways.

    Thanks <(0_0)>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter black key

    (@fadinho)

    And this is my code

    class MS_Gateway_Mtc
    {
    
        const ID = 'mtc';
    
        /**
         * Gateway singleton instance.
         *
         * @since  1.0.0
         * @var string $instance
         */
        public static $instance;
    
        /**
         * Payment information for customer.
         *
         * The payment procedures like bank account, agency, etc.
         *
         * @since  1.0.0
         * @var string $payment_info
         */
        protected $payment_info;
    
        /**
         * Hook to show payment info.
         * This is called by the MS_Factory
         *
         * @since  1.0.0
         */
        public function after_load()
        {
            parent::after_load();
    
            $this->id = self::ID;
            $this->name = __('Maroc Telecommerce - Paiement sécurisé par Carte', 'membership2');
            $this->description = __('(Bank orders, cash, etc)', 'membership2');
            $this->group = __('Maroc Telecommerce Payment', 'membership2');
            $this->manual_payment = true; // Recurring billed/paid manually
            $this->pro_rate = true;
            /**
             * No sandbox option for mtc payment gateway
             * The mode is always set to live
             */
            $this->mode = 'live';
    
            if ($this->active) {
                $this->add_action(
                    'ms_controller_gateway_purchase_info_content',
                    'purchase_info_content'
                );
            }
        }
    
        /**
         * Show mtc purchase/payment information.
         *
         * Returns a default messsage if gateway is not configured.
         *
         * @hook ms_controller_gateway_purchase_info_content
         *
         * @since  1.0.0
         * @return string The payment info.
         */
        public function purchase_info_content()
        {
            static $Processed = false;
    
            /**
             * If some plugin calls <code>the_content()</code> multiple times then this
             * function will also run multiple times.
             * We want to process the details only once, so we have this condition!
             */
            if (!$Processed) {
                $Processed = true;
    
                do_action(
                    'ms_gateway_manual_purchase_info_content_before',
                    $this
                );
    
                if (empty($this->payment_info)) {
                    $link = MS_Controller_Plugin::get_admin_url('settings');
                    ob_start();
                    ?>
                    <?php _e('This is only an example of mtc payment gateway instructions', 'membership2'); ?>
                    <br/>
                    <?php
                    printf(
                        __('Edit it %shere%s', 'membership2'),
                        '<a href="' . $link . '">',
                        '</a>'
                    );
                    ?>
                    <br/><br/>
                    <?php _e('Name: Example name.', 'membership2'); ?>
                    <br/>
                    <?php _e('Bank: Example bank.', 'membership2'); ?>
                    <br/>
                    <?php _e('Bank account: Example bank account 1234.', 'membership2'); ?>
                    <br/>
                    <?php
                    $this->payment_info = ob_get_clean();
                }
    
                $this->payment_info = wpautop($this->payment_info);
    
                if (!empty($_POST['ms_relationship_id'])) {
                    $subscription = MS_Factory::load(
                        'MS_Model_Relationship',
                        $_POST['ms_relationship_id']
                    );
                    $invoice = $subscription->get_current_invoice();
                    $this->payment_info .= sprintf(
                        '<div class="ms-mtc-price">%s: <span class="ms-price">%s%s</span></div>',
                        __('Total value', 'membership2'),
                        $invoice->currency,
                        $invoice->total
                    );
    
                    // The user did make his intention to pay the invoice. Set status
                    // to billed.
                    $invoice->status = MS_Model_Invoice::STATUS_BILLED;
                    $invoice->save();
                }
            }
    
            return apply_filters(
                'ms_gateway_manual_purchase_info_content',
                $this->payment_info
            );
        }
    
        /**
         * Verify required fields.
         *
         * @since  1.0.0
         * @return boolean True if configured.
         */
        public function is_configured()
        {
            $is_configured = true;
            $required = array('payment_info');
    
            foreach ($required as $field) {
                if (empty($this->$field)) {
                    $is_configured = false;
                    break;
                }
            }
    
            return apply_filters(
                'ms_gateway_manual_is_configured',
                $is_configured
            );
        }
    
        /**
         * Validate specific property before set.
         *
         * @since  1.0.0
         *
         * @access public
         * @param string $property The name of a property to associate.
         * @param mixed $value The value of a property.
         */
        public function __set($property, $value)
        {
            if (property_exists($this, $property)) {
                switch ($property) {
                    case 'payment_info':
                        $this->$property = wp_kses_post($value);
                        break;
    
                    default:
                        parent::__set($property, $value);
                        break;
                }
            }
    
            do_action(
                'ms_gateway_manual__set_after',
                $property,
                $value,
                $this
            );
        }
    
    }
    
    function mtc_register($api)
    {
        $api->register_payment_gateway(MS_Gateway_Mtc::ID, 'MS_Gateway_Mtc');
    }
    add_action('ms_init', 'mtc_register');
    Plugin Support Kasia – WPMU DEV Support

    (@wpmudev-support2)

    Hello fadinho,

    Let me ping our developer on that – I’m not coding expert so I have to consult your code. Our developer is not online at the moment, but as soon I’ll get more info from him I’ll update your thread.

    kind regards,
    Kasia

    Thread Starter black key

    (@fadinho)

    thank you for your reactivity,
    I am waiting your reponse.

    For more explanation :

    I try to add a new gateway and I use API membership 2 to create a plugin like documentation.
    (MS_Gateway_Mtc)

    THanks (^_^)

    Thread Starter black key

    (@fadinho)

    Hi kasia

    Is there a solution to this problem ?

    thanks

    Thread Starter black key

    (@fadinho)

    Hi kasia

    Is there a solution to this problem ?

    thanks

    Thread Starter black key

    (@fadinho)

    Hi kasia

    Is there a solution to this problem ?

    thanks 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘adding a new payment gateway’ is closed to new replies.