这是我的自定义支付网关的以下代码。 当我试图在结账页面下订单时,它显示连接到我创建的服务器异常。当我们检查firebug中的参数时,它只显示结帐并且响应失败。 我尝试将请求参数发送到网关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 :(得分:0)
我认为你对这里的逻辑感到困惑:
if ( is_wp_error( $response ) )
throw new Exception(....);
else
{
throw new Exception(....);
}
因此,无论响应如何,您都会抛出异常,这样您就无法再深入了解代码。异常总是打破当前的流程,除非被"尝试... 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
// ...