When I do PayPal capture Order, I keep on getting Invalid Request Connection
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I keep on getting HTTP/1.1 400 Invalid Request Connection: close Content-Length: 28 content-type: text/plain; charset=utf-8 x-served-by: cache-lon420138 broken content-length header when calling from PHP the cURL, and I do not know why, it is highly annoying,
Here is my Javascript call:
// Call your server to finalize the transaction
onApprove: function(data, actions) {
console.log('Order ID:', data.orderID);
console.log('Data:', data);
return fetch('../captureOrder.php?orderID=' + data.orderID, {
method: 'post'
})
.then(response => {
if (!response.ok) {
return response.text().then(text => {
throw new Error('Failed to capture order: ' + text);
});
}
return response.json();
})
.then(orderData => {
if (orderData.error || orderData.http_status >= 400) {
let errorMessage = orderData.error_description || "An unknown error occurred.";
console.error('Error:', orderData.error, errorMessage);
alert('Error: ' + errorMessage);
} else if (orderData.success && orderData.data && orderData.data.status === 'COMPLETED') {
console.log('Capture result', orderData.data);
alert('Transaction ' + orderData.data.id + ': ' + orderData.data.status + '\n\nSee console for all available details');
} else {
console.error('Transaction was not completed:', orderData);
alert('Transaction could not be completed. Please try again.');
}
})
.catch(error => {
console.error('Transaction failed:', error.message);
alert('There was a problem with your payment. Please try again. ' + error.message);
});
}
Here is my PHP script:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'generateAccessToken.php';
$orderID = isset($_GET['orderID']) ? $_GET['orderID'] : null;
if (empty($orderID)) {
http_response_code(400);
echo json_encode(['error' => 'Missing order ID']);
exit;
}
$accessToken = generateAccessToken();
if (empty($accessToken)) {
http_response_code(500);
echo json_encode(['error' => 'Failed to obtain access token']);
exit;
}
$url = "https://api-m.paypal.com/v2/checkout/orders/{$orderID}/capture";
$ch = curl_init($url);
$requestId = uniqid();
$uniqueOrderID = "YourUniqueOrderID"; // This should ideally come from your order system.
$paypalClientMetadataId = time() . "-" . $uniqueOrderID;
// Setting headers including Prefer and PayPal-Client-Metadata-Id
$headers = [
"Content-Type: application/json",
"Authorization: Bearer $accessToken",
"PayPal-Request-Id: $requestId",
"Prefer: return=representation", // Asking for a full response
"PayPal-Client-Metadata-Id: $paypalClientMetadataId" // Adding client metadata ID
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in output for debugging purposes
curl_setopt($ch, CURLOPT_VERBOSE, true); // To get detailed info about the request/response
$response = curl_exec($ch);
/*echo $response;
exit;*/
$curlError = curl_error($ch);
if (!$response) {
curl_close($ch);
http_response_code(500);
echo json_encode(['error' => 'Failed to capture order.', 'curlError' => $curlError]);
exit;
}
list($headers, $body) = explode("\r\n\r\n", $response, 2);
curl_close($ch);
$responseData = json_decode($body, true);
if (!$responseData) {
http_response_code(500);
echo json_encode(['error' => 'Failed to capture order. No response data from PayPal.']);
exit;
}
if (isset($responseData['id']) && isset($responseData['status']) && $responseData['status'] == 'COMPLETED') {
echo json_encode(['success' => true, 'id' => $responseData['id'], 'status' => $responseData['status']]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to capture order.', 'details' => $responseData]);
}
?>
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
- where does paypal payments go to if there is no linked phone or email to the seller ? in PayPal Payments Standard
- I get always the error: "invalid client_id or redirect_uri" from signin authorize endpoint. in Braintree Server-side Integration (PHP, Java, .NET, Ruby, Python, NodeJS SDKs)
- PayPal Redirection Issue After Successful Payment in Photobooth Integration in Sandbox Environment
- PayPal integration with Next, Lambda and API Gateway - 400 Invalid Request in Sandbox Environment
- Getting "Invalid Transaction Type" Error When Calling Payflow Gateway API in Payflow