简体   繁体   English

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

[英]How to send a POST Request to an external API payment Gateway in woocomerce?

I'm creating a new Payment gateway(Nequi) that it does not exist in woocommerce, I'm doing a plugin for that, but I need to know what is wrong, always comes an error in woocommerce when I place de order Error processing. Please try again 我正在创建一个新的Payment网关(Nequi),它在woocommerce中不存在,我正在为此做一个插件,但是我需要知道什么地方不对,当我下订单时总会在woocommerce中出现Error processing. Please try again Error processing. Please try again . Error processing. Please try again The payment gateway company gave me this documentation (in Spanish) about a header for authentication: 支付网关公司向我提供了有关身份验证标头的以下文档(西班牙语):

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

Example Request: 请求示例:

{
  "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"
        }
      }
    }

In this moment I'm trying with hard code, the same data is in the example. 此时,我正在尝试使用硬代码,示例中包含相同的数据。

other thing is i don't know how to debug my plugin to see the variables, i'm new in PHP and all these things of API rest, help me please! 另一件事是我不知道如何调试我的插件以查看变量,我是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),
        ];
    }
}

It look likes the request/args array isn't structured how WordPress recommends in the codex . 看起来request / args数组的结构不是WordPress 在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);

Also, I'm assuming Authorize.net wants it's payload in json, which is why I used the json_encode function and explicitly set the content type to json in the headers. 另外,我假设Authorize.net希望它是json中的有效负载,这就是为什么我使用json_encode函数并在标头中将内容类型显式设置为json的原因。 Like so: 像这样:

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

This may be a shot in the dark as I'm not familiar with Authorize.net's API, but hopefully it gets you going in the right direction. 由于我不熟悉Authorize.net的API,这可能是一个黑暗的镜头,但希望它能使您朝正确的方向前进。

Resources 资源资源

This stackoverflow article really helped. 这篇stackoverflow文章确实有帮助。

Wordpress codex WordPress的法典

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM