2015-11-09 36 views
0

這是我自定義支付網關的以下代碼。 當我試圖在檢出頁面下訂單時,它顯示連接到由我創建的服務器異常。當我們檢查螢火蟲中的參數時,它只顯示結帳並且響應失敗。 我嘗試發送請求參數到網關URL但沒有成功。 請告訴我我犯了什麼錯誤? 在此先感謝..支付網關沒有向URL發送請求參數?

public function process_payment($order_id) { 
    global $woocommerce; 

    // Get this Order's information so that we know 
    // who to charge and how much 
    $customer_order = new WC_Order($order_id); 

    // Are we testing right now or is it a real transaction 
    $environment = ($this->environment == "yes") ? 'TRUE' : 'FALSE'; 

    // Decide which URL to post to 
    $environment_url = ("FALSE" == $environment) 
         ? 'https://www.qpayindia.com/wwws/Payment/PaymentDetails.aspx' 
         : 'https://www.qpayindia.com/wwws/Payment/PaymentDetails.aspx'; 

    $QPayID = $this->QPayID.'`'.$this->order_total; 

    // This is where the fun stuff begins 
    $payload = array(
     // Authorize.net Credentials and API Info 
     "QPayID"   => $this->QPayID.'`'.$this->order_total, 
     "QPayPWD"    => $this->QPayPWD, 
     "CaseNumber"    => $this->CaseNumber, 

     "Currency"     => $this->Currency, 
     "TransactionType"    => $this->TransactionType, 
     "ResponseURL"    => $this->ResponseURL, 

     "Mode"     => $environment, 
     "Amount"    => $customer_order->order_total, 
     "OrderID"    => $customer_order->get_order 

    ); 

    // Send this payload to Authorize.net for processing 
    $response = wp_remote_post($environment_url, array(
     'method' => 'POST', 
     'body'  => http_build_query($payload), 
     'timeout' => 90, 
     'sslverify' => false, 
    )); 

    if (is_wp_error($response)) 
     throw new Exception(__('We are currently experiencing problems trying to connect to this payment gateway. Sorry for the inconvenience.', 'spyr-authorizenet-aim')); 
    else 
    { 
     throw new Exception(__('Connecting to server.', 'spyr-authorizenet-aim')); 
    } 
    if (empty($response['body'])) 
     throw new Exception(__('Authorize.net\'s Response was empty.', 'spyr-authorizenet-aim')); 


    // Retrieve the body's resopnse if no errors found 
    $response_body = wp_remote_retrieve_body($response); 

    // Parse the response into something we can read 
    foreach (preg_split("/\r?\n/", $response_body) as $line) { 
     $resp = explode("|", $line); 
    } 

    // Get the values we need 
    $r['ResponseCode']    = $resp[0]; 
    $r['Message']   = $resp[1]; 
    //$r['response_reason_code']  = $resp[2]; 
    //$r['Message']  = $resp[3]; 

    // Test the code to know if the transaction went through or not. 
    // 1 or 4 means the transaction was a success 
    if (($r['ResponseCode'] == 100) ) { 
     // Payment has been successful 
     $customer_order->add_order_note(__('Authorize.net payment completed.', 'spyr-authorizenet-aim')); 

     // Mark order as Paid 
     $customer_order->payment_complete(); 

     // Empty the cart (Very important step) 
     $woocommerce->cart->empty_cart(); 

     // Redirect to thank you page 
     return array(
      'result' => 'success', 
      'redirect' => $this->get_return_url($customer_order), 
     ); 
    } else { 
     // Transaction was not succesful 
     // Add notice to the cart 
     wc_add_notice($r['Message'], 'error'); 
     // Add note to the order for your reference 
     $customer_order->add_order_note('Error: '. $r['Message']); 
    } 

} 

// Validate fields 
public function validate_fields() { 
    return true; 
} 

回答

0

我想你在這裏混淆了邏輯:

if (is_wp_error($response)) 
    throw new Exception(....); 
else 
{ 
    throw new Exception(....); 
} 

所以你不管扔效應初探的異常,而這樣一來,你永遠也達不到的代碼進一步下跌。一個例外總會打破目前的流程,除非被「try ... catch」所困擾,否則它會突破程序。對於快速測試,請嘗試以下方法(請注意大括號):

if (is_wp_error($response)) { 
    throw new Exception(__('We are currently experiencing problems trying to connect to this payment gateway. Sorry for the inconvenience.', 'spyr-authorizenet-aim')); 
} else if (empty($response['body'])) { 
    throw new Exception(__('Authorize.net\'s Response was empty.', 'spyr-authorizenet-aim')); 
} 

// Retrieve the body's resopnse if no errors found 
// ... 
+0

謝謝讓我試試這個。 –