0

I'm working one project built with React.js as front-end, PHP as backend API. And running in my local pc. I tried use PayPal Checkout order API to create order in PHP. But after approve the order in PayPal, when I call capture API to complete the order, it return 400 Bad Request: invalid method error. Here is the code to create order:

    $params = [
        'intent' => 'CAPTURE',
        'purchase_units' => [
            [
                'amount' => [
                    'currency_code' => $currency,
                    'value' => (string) $amount
                ],
            ]
        ],
        'application_context' => [
            'user_action' => 'PAY_NOW',
            'shipping_preference' => 'NO_SHIPPING',
            'return_url' => $backUrl,
            'cancel_url' => $backUrl
        ],
    ];

    $data = $this->encodeData($params);
    $headers = $this->getAuthHeaders($data);
    $res = $this->execute(self::POST, '/v2/checkout/orders/', $data, $headers);

    return $res;

and Here is Order detail response from the GET /v2/checkout/order API:

  {
    id: "8D111388A5835981G",
    intent: "CAPTURE",
    status: "APPROVED",
    purchase_units: [
      {
        reference_id: "default",
        amount: { currency_code: "USD", value: "500.00" },
        payee: { email_address: "xxx@xxx.com", merchant_id: "XXXXXXXXXXX" },
      },
    ],
    payer: {
      name: { given_name: "John", surname: "Doe" },
      email_address: "sb-xxxxx@personal.example.com",
      payer_id: "XXXXXXXX",
      address: { country_code: "US" },
    },
    create_time: "2021-05-12T21:59:37Z",
    links: [
      { href: "https://api.sandbox.paypal.com/v2/checkout/orders/8D111388A5835981G", rel: "self", 
method: "GET" },
      { href: "https://api.sandbox.paypal.com/v2/checkout/orders/8D111388A5835981G", rel: "update", 
method: "PATCH" },
      {
        href: "https://api.sandbox.paypal.com/v2/checkout/orders/8D111388A5835981G/capture",
        rel: "capture", method: "POST",
      },
    ],
  };

As can see in detail, this Order APPROVED, so I can make a capture API call. But when I did so, got 400 error. Here is capture API call:

$data = json_encode([]);
$headers = [
        "Content-Type: application/json",
        'Authorization: Bearer ' . $this->token,
        "Content-length: " . mb_strlen($data)
    ];

$res = $this->execute(self::POST, '/v2/checkout/orders/' . $paymentId . '/capture', $data, $headers);

And Here is execute function:

private function execute(string $method, string $path, string $body, array $headers = [], $isNeedAuth = false)
{
    $this->requestParams = [
        'path' => $path,
        'body' => $body,
        'headers' => $headers
    ];
   
    curl_setopt($this->curl, CURLOPT_URL, $this->getUrl() . $path);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);

    if (empty($body) === false) {
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
    }

    if (count($headers)) {
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
    }

    if ($isNeedAuth) {
        curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($this->curl, CURLOPT_USERPWD, $this->clientId . ':' . $this->secret);
        
        $this->requestParams['keys'] = $this->clientId . ':' . $this->secret;
    }

    $res = curl_exec($this->curl);

    $this->response = $res;

    $resDecoded = $this->decodeData($res);

    return $resDecoded;
}

After execute the curl, $res='400 Bad Request: invalid method'.

Not sure the reason, why cant complete the order, and getting error.

ClusterH
  • 761
  • 3
  • 16
  • Your question does not include the full response body and any headers in the response, so difficult to be of specific help. What does `self::POST` evaluate to? The method should be `"POST"`. If it's not valid, because body is unset the method will default to "GET", which is not valid requests to the PayPal API that perform actions. – Preston PHX May 12 '21 at 23:44
  • `self::POST` means just string 'POST', and also just tried `echo 'Response==' . $res` after line of `$res = curl_exec($this->curl);` in execute function, and I can see only '400 Bad Request: invalid method' in php cli console – ClusterH May 12 '21 at 23:54
  • if is not full response, How to get full response body and header? – ClusterH May 12 '21 at 23:57
  • Something like https://stackoverflow.com/a/25118032 to get the headers, there should be a PayPal-Debug-Id at the least – Preston PHX May 13 '21 at 00:17
  • Only I got HTTP/1.1 400 Bad Request: invalid method Content-Type: text/plain; charset=utf-8 Connection: close – ClusterH May 13 '21 at 00:39
  • I got PayPal-Debug_id from the `GET /v2/checkout/orders/$paymentId` api response header like that : `Paypal-Debug-Id: f06be37a68e5f` – ClusterH May 13 '21 at 00:46
  • Seems there's a problem with your code then, it's not reaching PayPal. Add runtime debugging to your `execute` function. `curl_error($this->curl)` may or may not be relevant; debug all parameters and curl_setopt steps, as well as dumping the final curl config before _exec, with `curl_getinfo` – Preston PHX May 13 '21 at 01:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232322/discussion-between-clusterh-and-preston-phx). – ClusterH May 13 '21 at 05:58

0 Answers0