dql7588 2015-09-21 17:25
浏览 188

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

I am new to WooCommerce custom payment gateway(PG) integrations.

Initial Page: The checkout page of WooCommerce. I have created a custom plugin based on instructions from here: http://www.sitepoint.com/building-a-woocommerce-payment-extension/ and http://docs.woothemes.com/document/payment-gateway-api/

So, when I visit the checkout page, I can see my payment gateway at the bottom. My Code:

Constructor:

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

My PG vendor gave me a following process to integrate.

Page 1: The above plugin process_payment passes on to the payment gateway processing to the pgserv.php.

Page 2: Automatically my bank's page comes up (because of the credit card of my bank that I use) where I provide my OTP and all.

Page 3: Once that step is complete, the payment gateway forwards from there to another success landing page (let's call it pgresponse.php) within my website that returns the transaction result.

This is where the problem begins. I tried redirecting/submitting to an independent page (submitaction.php) where I try to mark the order complete/payment complete and empty the cart. My Code:

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

No matter what I do, the order in this case doesn't update status to payment complete. Even $payment_status doesn't return anything.

Questions:

  1. What do I do?

  2. I intend to write a hook for process_order_status to send mails. The way I am planning would be to write the following code in the plugin:

    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
    

Would that be correct? If only I could get the order to update the payment status, I am sure this method would be called, correct?

I am desperate for some help. Any link or just direction would also be fine. Thanks a lot in advance.

  • 写回答

1条回答 默认 最新

  • douxiangshi6568 2017-08-03 14:27
    关注

    You need to decode payload being sent to the payment provider. Do something like this.

    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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮