2015-09-21 38 views
1

我是WooCommerce定製支付網關(PG)集成的新手。使用重定向的Woocommerce定製支付網關工作流程

初始頁面: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)。

這是問題開始的地方。 我嘗試重定向/提交到一個獨立頁面(submitaction.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 
    

這是正確?如果只有我能得到更新付款狀態的命令,我相信這種方法會被調用,是正確的嗎?

我渴望得到一些幫助。任何環節或方向都可以。提前致謝。

回答

0

您需要解碼發送給支付提供商的有效負載。做這樣的事情。

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