【问题标题】:Custom WooCommerce Gateway - Running Local JS Function自定义 WooCommerce 网关 - 运行本地 JS 功能
【发布时间】:2019-12-21 14:56:49
【问题描述】:

概述

核心问题

我对如何将 WooCommerce 订单有效负载数据导入预加载的 JS 函数 (pru_checkout()) 有点迷茫

说明

我正在为“Payments-R-Us”构建一个扩展。 PRU 给了我一个加载到 结帐页面 的 JS sn-p。当按下 Checkout Button 时,我需要触发 pru_checkout() 函数(使用适当的 JSON 有效负载)。然后,会弹出一个模态框来指导他们完成结帐过程(模态框 + 它的所有数据点都 100% 编码并使用他们提供的 JS sn-p 运行)以完成购买。完成所有步骤后,JS sn-p 将点击我将在 WC 中创建的自定义端点,以触发 订单状态更改 + 重定向到感谢页面。


我的尝试

处理支付功能

在我的网关类中,有核心的process_payment( $order_id ) 函数。由于无法将数据发送到已加载到页面上的 JS 函数,因此这不起作用。

回调处理程序

这更多是用于流程的“第 2 步”,但我确实研究了如何实现回调处理程序,但一切都在“幕后”完成,而我需要直接在 结帐页面按下按钮时 - 事后不传递数据 (http://dominykasgel.com/woocommerce-creating-callback-url/)

自定义 JS

我正在考虑编写一些与 结帐按钮 的按钮按下挂钩的自定义 JS,但这并不能让我访问 Order 对象 或任何我需要传递到有效负载中的用户详细信息。


代码示例

网关类

/**
 * Initialize the PRU Gateway class
 */
add_action( 'plugins_loaded', 'pru_initialize_gateway_class' );
function pru_initialize_gateway_class() {

    class WC_PaymentsRUs_Gateway extends WC_Payment_Gateway {

        /**
         * Class constructor
         */
        public function __construct() {

            // Set variables that WooCommerce is expecting from any payment gateway
            $this->id                 = 'pru_gateway'; // Unique ID
            $this->icon               = 'image.jpg'; // PaymentsRUs logo
            $this->has_fields         = false; // FALSE hides the credit card fields
            $this->method_title       = 'PaymentsRUs'; // Title shown on admin screen
            $this->method_description = 'Allow checkout with PaymentsRUs modal box.  <strong>NOTE:</strong> This extension requires a newer version of jQuery than what WordPress is packaged with by default.'; // Description shown on admin screen


            // Support payments for PRODUCTS only
            $this->supports = array(
                'products'
            );

            // Initialize form fields
            $this->init_form_fields();

            // Load the settings.
            $this->init_settings();
            $this->dealer                = $this->get_option( 'dealer' );
            $this->base_plan             = $this->get_option( 'base_plan' );
            $this->program               = $this->get_option( 'program' );
            $this->promo                 = $this->get_option( 'promo' );
            $this->channel               = $this->get_option( 'channel' );
            $this->api_key               = $this->get_option( 'api_key' );
            $this->sandbox               = $this->get_option( 'sandbox' );
            $this->toggle_single_product = $this->get_option( 'toggle_single_product' );
            $this->debug_mode            = $this->get_option( 'debug_mode' );
            $this->toggle_product_pods   = $this->get_option( 'toggle_product_pods' );
            $this->toggle_cart           = $this->get_option( 'toggle_cart' );


            // This action hook saves the settings
            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array(
                $this,
                'process_admin_options'
            ) );


            // Bind the API callback URL
            add_action( 'woocommerce_api_bobrocks', array($this,'callback_handler') );


        }

        /**
         * Form field setup for Settings page
         */
        public function init_form_fields() {

            $this->form_fields = array(


                'api_key' => array(
                    'title'       => 'API Key',
                    'type'        => 'text',
                    'description' => 'This is some sample description text that can be changed',
                    //'default'     => 'Credit Card',
                    'desc_tip'    => true,
                ),

                'dealer' => array(
                    'title'       => 'Dealer ID',
                    'type'        => 'number',
                    'description' => 'This is some sample description text that can be changed',
                    //'default'     => 'Credit Card',
                    'desc_tip'    => true,
                ),

                'base_plan' => array(
                    'title'       => 'Base Plan',
                    'type'        => 'number',
                    'description' => 'This is some sample description text that can be changed',
                    //'default'     => 'Credit Card',
                    'desc_tip'    => true,
                ),

                'promo' => array(
                    'title'       => 'Promo Code',
                    'type'        => 'text',
                    'description' => 'This is some sample description text that can be changed',
                    //'default'     => 'Credit Card',
                    'desc_tip'    => true,
                ),

                'program' => array(
                    'title'       => 'Program',
                    'type'        => 'text',
                    'description' => 'This is some sample description text that can be changed',
                    //'default'     => 'Credit Card',
                    'desc_tip'    => true,
                ),

                'channel' => array(
                    'title'       => 'Channel',
                    'type'        => 'text',
                    'description' => 'This is some sample description text that can be changed',
                    //'default'     => 'Credit Card',
                    'desc_tip'    => true,
                ),

                'sandbox' => array(
                    'title'       => 'Sandbox Mode',
                    'label'       => 'Check to enable Sandbox mode',
                    'type'        => 'checkbox',
                    'description' => '',
                    'default'     => 'no'
                ),

                'debug_mode' => array(
                    'title'       => 'Debug Mode',
                    'label'       => 'Check to turn debug mode ON in the console',
                    'type'        => 'checkbox',
                    'description' => '',
                    'default'     => 'no'
                ),

                'toggle_single_product' => array(
                    'title'       => 'Show ALA on Single Product',
                    'label'       => 'Checking this box will show the ALA on a Single Product',
                    'type'        => 'checkbox',
                    'description' => '',
                    'default'     => 'yes'
                ),

                'toggle_product_pods' => array(
                    'title'       => 'Show ALA on Product Pod',
                    'label'       => 'Checking this box will show the ALA on a Product Pod (ie. the Shop page)',
                    'type'        => 'checkbox',
                    'description' => '',
                    'default'     => 'yes'
                ),

                'toggle_cart' => array(
                    'title'       => 'Show ALA on Cart',
                    'label'       => 'Checking this box will show the ALA on the Cart page',
                    'type'        => 'checkbox',
                    'description' => '',
                    'default'     => 'yes'
                ),


            );

        }

        /**
         * Credit card fields
         */
        public function payment_fields() {

            //

        }

        /*
         * Custom CSS and JS
         */
        public function payment_scripts() {

            //

        }

        /*
         * Fields validation
         */
        public function validate_fields() {

            //

        }

        /*
         * We're processing the payments here
         */
        public function process_payment( $order_id ) {

            global $woocommerce;
            $order = new WC_Order( $order_id );

            print_r($order);

        }

        /*
         * Webhooks
         */
        public function webhook() {

            //

        }




        public function callback_handler() {
            $a = array(
                'bob',
                'rocks'
            );

            echo json_encode($a);
            die();
        }
    }
}

JS 函数的 JSON 负载

{
    "applicant": {
        "firstName": "BOB",
        "lastname": DOE",
        "streetaddress": "1234 N BROAD AVE ",
        "otheraddress": "Suite 1145",
        "city": "Savannah",
        "state": "GA",
        "zip": "12345",
        "country": "US",
        "phone": "123-456-7890",
        "email": "john@doe.com"
    },
    "shipping": {
        "streetaddress": "1234 N BROAD AVE ",
        "otheraddress": "Suite 1145",
        "city": "Savannah",
        "state": "GA",
        "zip": "12345",
        "country": "US",
    },
    "merchant": {
        "callbackurl": "custom/woocommerce/api/callback",
        "merchantId": "421111154",
        "planNumber": "59422348",
        "promoCode": "Empire012324123123"
    },
    "order": {
        "orderId": "104983763522",
        "totalAmount": 1005
    }
}

如果我能提供更多细节,请告诉我!

【问题讨论】:

    标签: wordpress woocommerce payment-gateway


    【解决方案1】:

    您可以使用哈希,而不是返回订单确认 URL,您可以返回一个哈希。然后在JS端,你听hashchange打开你的modal。

    return array(
        'result'   => 'success',
        'redirect' => '#some-thing-here',
    );
    

    我们还没有完成,你需要告诉 WC 完成你的订单,清空购物车,并记录付款,你可以使用 WC_Ajax 来完成。如何?客户在模式中的结帐过程中完成付款后,您将浏览器重定向到您刚刚注册的 WC_Ajax 操作的 URL。如何将该 ajax URL 传递给您的 JS 处理程序,像这样修改上面的哈希:

    return array(
        'result'   => 'success',
        'redirect' => '#some-thing-here:ajax-url-here',
    );
    

    最后,您可以完成订单,清空购物车,重定向到您的 WC_Ajax 处理程序中的感谢页面。

    Ps:我已经使用这个流程实现了Stripe Connect 网关。

    Ps2:这是 WooCommerce Stripe Gateway 处理 3D 安全支付的方式

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2017-08-05
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      相关资源
      最近更新 更多