【问题标题】:Payment gateway not sending request parameters to URL?支付网关不向 URL 发送请求参数?
【发布时间】:2016-02-10 00:44:30
【问题描述】:

这是我的自定义支付网关的以下代码。 当我试图在结帐页面下订单时,它显示连接到我创建的服务器异常。当我们在firebug中检查参数时,它只显示checkout并且响应失败。 我尝试将请求参数发送到网关 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;
}

【问题讨论】:

    标签: wordpress plugins payment-gateway


    【解决方案1】:

    我认为你混淆了这里的逻辑:

    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
    // ...
    

    【讨论】:

    • 谢谢你让我试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2020-05-17
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多