matteo
matteo

Reputation: 2401

WooCommerce custom payment method

I have to add a custom payment method to woocommerce. this payment method has well documented JSON APIs. how do I add a custom payment method, and hook it to the API?

Upvotes: 0

Views: 8975

Answers (3)

matteo
matteo

Reputation: 2401

thanks to both of you!

I also added these lines, otherwise I couldn't activate it:

add_filter('woocommerce_payment_gateways', 'weldpay_add_gateway_class');
function weldpay_add_gateway_class($gateways) {
    $gateways[] = 'WC_Weldpay';
    return $gateways;
}

there is the possibility to take information from the checkout, like items, name, surname, address, etc?

Upvotes: 0

kggopal12
kggopal12

Reputation: 76

The woocommerce_payment_gateways filter lets you add custom payment gateways to woocommerce.

add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );

function add_your_gateway_class( $methods ) {
    $methods[] = 'WC_Custom_PG'; 
    return $methods;
}

Next you create a class that extends the WC_Payment_Gateway class. In its constructor, you need to set the unique id, title and description for your payment gateway. You also need to initialize the form fields and the payment gateway settings.

The payment gateway class needs to be defined in a function that is called by the plugins_loaded hook


add_action( 'plugins_loaded', 'init_wc_custom_payment_gateway' );

function init_wc_custom_payment_gateway(){
    class WC_Custom_PG extends WC_Payment_Gateway {
        function __construct(){
            $this->id = 'wc_custom_pg';
            $this->method_title = 'Custom Payment Gateway';
            $this->title = 'Custom Payment Gateway';
            $this->has_fields = true;
            $this->method_description = 'Your description of the payment gateway';

            //load the settings
            $this->init_form_fields();
            $this->init_settings();
            $this->enabled = $this->get_option('enabled');
            $this->title = $this->get_option( 'title' );
            $this->description = $this->get_option('description');

            //process settings with parent method
            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );

        }
        public function init_form_fields(){
            $this->form_fields = array(
                'enabled' => array(
                    'title'         => 'Enable/Disable',
                    'type'          => 'checkbox',
                    'label'         => 'Enable Custom Payment Gateway',
                    'default'       => 'yes'
                ),
                'title' => array(
                    'title'         => 'Method Title',
                    'type'          => 'text',
                    'description'   => 'This controls the payment method title',
                    'default'       => 'Custom Payment Gatway',
                    'desc_tip'      => true,
                ),
                'description' => array(
                    'title'         => 'Customer Message',
                    'type'          => 'textarea',
                    'css'           => 'width:500px;',
                    'default'       => 'Your Payment Gateway Description',
                    'description'   => 'The message which you want it to appear to the customer in the checkout page.',
                )
            );
        }

        function process_payment( $order_id ) {
            global $woocommerce;

            $order = new WC_Order( $order_id );

            /****

                Here is where you need to call your payment gateway API to process the payment
                You can use cURL or wp_remote_get()/wp_remote_post() to send data and receive response from your API.

            ****/

            //Based on the response from your payment gateway, you can set the the order status to processing or completed if successful:
            $order->update_status('processing','Additional data like transaction id or reference number');

            //once the order is updated clear the cart and reduce the stock
            $woocommerce->cart->empty_cart();
            $order->reduce_order_stock();

            //if the payment processing was successful, return an array with result as success and redirect to the order-received/thank you page.
            return array(
                'result' => 'success',
                'redirect' => $this->get_return_url( $order )
            );
        }

        //this function lets you add fields that can collect payment information in the checkout page like card details and pass it on to your payment gateway API through the process_payment function defined above.

        public function payment_fields(){
            ?>
            <fieldset>
                <p class="form-row form-row-wide">
                    <?php echo esc_attr($this->description); ?>
                </p>                        
                <div class="clear"></div>
            </fieldset>
            <?php
        }

    }
}

Once you activate your payment gateway plugin, you can enable the method in WooCommerce > Settings > Payments page.

For more information of payment gateway setup, you can visit : https://docs.woocommerce.com/document/payment-gateway-api/

You can either create this as a plugin or add the entire code to your theme's function.php file.

Upvotes: 3

Kubadev
Kubadev

Reputation: 865

You need to write a plugin who embed your gateway API.

See more information on Woocomerce Documentation : Payment Gateway API

You can see further information about code/class/etc on the Woocomerce API Doc

Upvotes: 0

Related Questions