如何在woocomerce中向外部API支付网关发送POST请求?

时间:2019-07-25 22:42:38

标签: php json api woocommerce payment-gateway

我正在创建一个新的Payment网关(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),
        ];
    }
}

1 个答案:

答案 0 :(得分:0)

貌似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