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.