WooCommerce自定义付款方式

时间:2019-06-28 07:47:27

标签: wordpress woocommerce hook-woocommerce

我必须向woocommerce添加自定义付款方式。 此付款方式具有记录良好的JSON API。 如何添加自定义付款方式,并将其挂接到API?

3 个答案:

答案 0 :(得分:1)

woocommerce_payment_gateways 过滤器可让您将自定义支付网关添加到woocommerce。

add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );

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

接下来,您将创建一个扩展 WC_Payment_Gateway 类的类。在其构造函数中,您需要为付款网关设置唯一的ID,标题和说明。您还需要初始化表单字段和支付网关设置。

付款网关类需要在 plugins_loaded 钩子

调用的函数中定义

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
        }

    }
}

激活付款网关插件后,可以在 WooCommerce>设置>付款页面中启用该方法。

有关支付网关设置的详细信息,请访问:https://docs.woocommerce.com/document/payment-gateway-api/

您可以将其创建为插件,也可以将整个代码添加到主题的 function.php 文件中。

答案 1 :(得分:0)

您需要编写一个嵌入网关API的插件。

查看有关Woocomerce文档的更多信息:Payment Gateway API

您可以在Woocomerce API Doc

上查看有关代码/类/等的更多信息。

答案 2 :(得分:0)

谢谢你们!

我还添加了以下几行,否则我将无法激活它:

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

是否有可能从结帐处获取信息,例如物品,姓名,姓氏,地址等?