1

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

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!

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),
        ];
    }
}
Emma
  • 27,428
  • 11
  • 44
  • 69

1 Answers1

2

It look likes the request/args array isn't structured how WordPress recommends 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);

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

Resources

This stackoverflow article really helped.

Wordpress codex

Ryan
  • 510
  • 4
  • 16