【问题标题】:How to send a POST Request to an external API payment Gateway in woocomerce?如何在 woocommerce 中向外部 API 支付网关发送 POST 请求?
【发布时间】:2019-12-04 06:22:44
【问题描述】:

我正在创建一个新的支付网关(Nequi),它在 woocommerce 中不存在,我正在为此做一个插件,但我需要知道哪里出了问题,当我放置 de 时,woocommerce 中总是出现错误订购Error processing. Please try again。支付网关公司向我提供了有关身份验证标头的文档(西班牙语):

https://conecta.nequi.com/content/consumo-del-api-de-nequi-para-pagos-sin-usar-los-sdk

示例请求:

{
  "RequestMessage": {
    "RequestHeader": {
      "Channel": "PNP04-C001",
      "RequestDate": "2017-06-21T20:26:12.654Z",
      "MessageID": "1234567890",
      "ClientID": "12345",
      "Destination": {
        "ServiceName": "PaymentsService",
        "ServiceOperation": "unregisteredPayment",
        "ServiceRegion": "C001",
        "ServiceVersion": "1.0.0"
      }
    },
    "RequestBody": {
      "any": {
        "unregisteredPaymentRQ": {
          "phoneNumber": "1",
          "code": "NIT_1",
          "value": "1",
          "reference1": "reference1",
          "reference2": "reference2",
          "reference3": "reference3"
        }
      }
    }

此时我正在尝试使用硬代码,示例中的数据相同。

另一件事是我不知道如何调试我的插件来查看变量,我是 PHP 的新手,所有这些 API 都休息了,请帮助我!

public function process_payment( $order_id )
{

    global $woocommerce;

    $customer_order = new WC_Order($order_id);

    // checking for transiction
    $environment = ($this->environment == "yes") ? 'TRUE' : 'FALSE';
    // Decide which URL to post to
    $environment_url = ("FALSE" == $environment)
        ? 'https://api.sandbox.nequi.com/payments/v1/-services-paymentservice-unregisteredpayment'
        : 'https://api.sandbox.nequi.com/payments/v1/-services-paymentservice-unregisteredpayment';

    $unregistered_paymentRQ = [
        'phoneNumber' => '1',
        'code' => '1',
        'value' => '1',
        'reference1' => 'reference1',
        'reference2' => 'reference2',
        'reference3' => 'reference3'
    ];

    $any = [
        'unregisteredPaymentRQ' => $unregistered_paymentRQ
    ];

    $request_body = [
        'any' => $any
    ];

    $destination = [
        'ServiceName' => 'PaymentsService',
        'ServiceOperation' => 'unregisteredPayment',
        'ServiceRegion' => 'C001',
        'ServiceVersion' => '1.0.0'
    ];

    $request_header = [
        'Channel' => 'PNP04-C001',
        'RequestDate' => '2017-06-21T20:26:12.654Z',
        'MessageID' => '1234567890',
        'ClientID' => '12345',
        'RequestDate' => '2017-06-21T20:26:12.654Z',
        'Destination' => $destination
    ];

    $request_msg = [
        'RequestHeader' => $request_header,
        'RequestBody' => $request_body
    ];

    $payload = [
        'RequestMessage' => $request_msg
    ];
    echo $payload;

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

    if (is_wp_error($response)) {
        throw new Exception(__('There is issue for connectin payment gateway. Sorry for the inconvenience.',
            'wc-gateway-nequi'));
    }

    if (empty($response['body'])) {
        throw new Exception(__('Authorize.net\'s Response was not get any data.', 'wc-gateway-nequi'));
    }

    // get body response while get not error
    $response_body = wp_remote_retrieve_body($response);
    foreach (preg_split("/\r?\n/", $response_body) as $line) {
        $resp = explode("|", $line);
    }
    // values get
    $r['StatusCode'] = $resp[0];
    $r['StatusDesc'] = $resp[1];
    // 1 or 4 means the transaction was a success
    if (($r['StatusCode'] == 0)) {
        // Payment successful
        $customer_order->add_order_note(__('Authorize.net complete payment.', 'wc-gateway-nequi'));

        // paid order marked
        $customer_order->payment_complete();
        // this is important part for empty cart
        $woocommerce->cart->empty_cart();
        // Redirect to thank you page
        return [
            'result' => 'success',
            'redirect' => $this->get_return_url($customer_order),
        ];
    }
}

【问题讨论】:

    标签: php json api woocommerce payment-gateway


    【解决方案1】:

    看起来 request/args 数组的结构不像 WordPress 推荐的 in the codex。 (arguements)

    $request_body = [
        'any' => [
            'unregisteredPaymentRQ' => [
                'phoneNumber'  => '1',
                'code'         => '1',
                'value'        => '1',
                'reference1'   => 'reference1',
                'reference2'   => 'reference2',
                'reference3'   => 'reference3'
            ]
        ]
    ];
    
    $request_headers = [
        'content-type'  => 'application/json',
        'Channel'       => 'PNP04-C001',
        'RequestDate'   => '2017-06-21T20:26:12.654Z',
        'MessageID'     => '1234567890',
        'ClientID'      => '12345',
        'RequestDate'   => '2017-06-21T20:26:12.654Z',
        'Destination'   => [
            'ServiceName'       => 'PaymentsService',
            'ServiceOperation'  => 'unregisteredPayment',
            'ServiceRegion'     => 'C001',
            'ServiceVersion'    => '1.0.0'
        ]
    ];
    
    $args = [
        'method' => 'POST',
        'httpversion'   => '1.0',
        'user-agent'    => 'WordPress/' . $wp_version . '; ' . home_url(),
        'timeout' => 90,
        'sslverify' => false,
        'headers' => $request_headers,
        'body' => json_encode($request_body)
    ];
    
    echo $args;
    
    // Send this payload to Authorize.net for processing
    $response = wp_remote_post($environment_url, $args);
    
    print_r($response);
    

    另外,我假设 Authorize.net 希望它是 json 中的有效负载,这就是我使用 json_encode 函数并在标头中将内容类型显式设置为 json 的原因。像这样:

    'content-type' => 'application/json'   
    

    由于我不熟悉 Authorize.net 的 API,这可能是在黑暗中拍摄,但希望它能让您朝着正确的方向前进。

    资源

    这个stackoverflow article 真的很有帮助。

    Wordpress codex

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2016-12-15
      • 2019-07-07
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多