Woocommerce使用重定向自定义支付网关工作流


Woocommerce custom payment gateway workflow using redirection

我是WooCommerce自定义支付网关(PG)集成的新手。

Initial Page: WooCommerce的结帐页面。我根据这里的说明创建了一个自定义插件:http://www.sitepoint.com/building-a-woocommerce-payment-extension/和http://docs.woothemes.com/document/payment-gateway-api/

因此,当我访问结帐页面时,我可以在底部看到我的支付网关。我的代码:

构造函数

:

public function __construct() {
    $this->id                   = 'xxxx_payment_gateway';
    $this->title                = __( "xxxx Payment Gateway", 'woocommerce-xxxx-gateway' );
    $this->icon                 = null;
    $this->method_title         = __( 'xxxx Payment Gateway', 'woocommerce-xxxx-gateway' );
    $this->method_description   = __( 'Take payments via xxxx Payment Gateway - uses the xxxx Payment Gateway SDK.', 'woocommerce-xxxx-gateway' );
    $this->has_fields           = true;
    $this->init_form_fields();
    $this->init_settings();
    // Save settings
    if ( is_admin() ) {
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
    }
}

process_payment

function process_payment( $order_id ) {
    $customer_order = wc_get_order( $order_id );
    $country_list = array(
        "IN"=>"IND",
    );
    $environment_url = 'http://example.com/pgic/pgserv.php';
    $payload = array(
        "x_invoice_num"         => str_replace( "#", "", $customer_order->get_order_number() ),
        "x_merchant_id"         => $this->merchant_id, 
        // Order total
        "x_amount"              => 3,//$customer_order->order_total,
        // Billing Information
        "x_first_name"          => $customer_order->billing_first_name,
        ....
        // Shipping Information
        "x_ship_to_first_name"  => $customer_order->shipping_first_name,
        ....
        "x_cust_id"             => $customer_order->user_id,
        "x_customer_ip"         => $_SERVER['REMOTE_ADDR'],
    );
    $response = wp_remote_post( $environment_url, 
        array(
            'method'    => 'POST',
            'body'      => http_build_query( $payload ),
            'timeout'   => 90,
            'sslverify' => false,
        ) 
    );
    $forwardURL = trim(wp_remote_retrieve_body( $response ));
    if ( is_wp_error( $response ) ) 
    {
        // Return failure redirect
        return array(
            'result'    => 'failure',
            'redirect'  => 'failed.php'
        );
    }
    else{
        // Reduce stock levels
        // $order->reduce_order_stock();
        // Remove cart
        // WC()->cart->empty_cart();
        // Return thankyou redirect
        return array(
            'result'    => 'success',
            'redirect'  => $forwardURL //$this->get_return_url( $customer_order )
        );
    }
}//process_payment

我的PG供应商给了我以下的流程来集成。

第1页:上面的插件process_payment将支付网关处理传递给pgserv.php。

第2页:自动出现我的银行页面(因为我使用的是我的银行信用卡),在那里我提供了我的OTP和所有。

第3页:一旦这一步完成,支付网关从那里转发到我的网站内返回交易结果的另一个成功登陆页面(让我们称之为pgresponse.php)。

这就是问题开始的地方。我尝试重定向/提交到一个独立的页面(submit .php),在那里我尝试标记订单完成/支付完成并清空购物车。我的代码:

global $woocommerce, $post;
$order = new WC_Order($post->ID);
$payment_status = $order->payment_complete();

无论我做什么,在这种情况下,订单都不会将状态更新为付款完成。即使$payment_status也没有返回任何东西。

问题:

  1. 我该怎么办?

  2. 我打算为process_order_status写一个钩子来发送邮件。我计划的方式是在插件中编写以下代码:

    public function process_order_status( $order, $payment_id, $status, $auth_code ) {
        echo "Payment details :: $order, $payment_id, $status, $auth_code";
        error_log("Payment details :: $order, $payment_id, $status, $auth_code");
        if ( 'APPROVED' == $status ) {
            // Payment complete
            $order->payment_complete( $payment_id );
            // Add order note
            $order->add_order_note( sprintf( __( 'Payment approved (ID: %s, Auth Code: %s)', 'woocommerce' ), $payment_id, $auth_code ) );
            // Remove cart
            WC()->cart->empty_cart();
            return true;
        }
        return false;
    }//process_order_status
    

这样对吗?如果我能得到订单来更新付款状态,我肯定会调用这个方法,对吗?

我非常需要一些帮助。任何链接或只是方向也可以。

您需要解码发送给支付提供者的有效负载。像这样做。

public function check_ipn_response($payload)
{
    $raw_input = json_decode(file_get_contents('php://input'),TRUE);
    $order_id = $raw_input['callback_data'];
    // get order_id
    $order = new WC_Order($order_id);
    // Update order status once order is complete
    $order->update_status( 'processing', _x( 'Payment Complete', 'Check payment method', 'wc-gateway-paymentsgy' ) );
    wp_die();
}