Paypal v2 PHP curl 捕获订单

问题描述 投票:0回答:1

当我尝试使用 PHP 运行它时,我的curl 似乎格式错误或标头丢失了一些元素。

我已经在 Postman 中成功运行了这个curl:

curl -v -X POST https://api-m.paypal.com/v2/checkout/orders/MyOrderId/capture
-H“内容类型:application/json”
-H“授权:不记名MyToken”
-H“PayPal-请求-Id:MyRequestId”
-H“首选:返回=表示”
-H“PayPal-客户端-元数据-Id:MyMetaDataId”

但是当我想在 PHP 中运行它时,它告诉我标头缺少信息。

这是 PHP 代码:

    <?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'generateAccessToken.php';

$orderID = $_GET['orderID'] ?? '';

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";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer " . $accessToken,
    "Content-Type: application/json",
    "PayPal-Request-Id: " . uniqid(),
    "Prefer: return=representation",
    "PayPal-Client-Metadata-Id: " . time() . "-" . $orderID
  ),
));

$response = curl_exec($curl);
$httpStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curlError = curl_error($curl);

curl_close($curl);

if ($curlError) {
    echo json_encode(['error' => 'cURL Error: ' . $curlError]);
    exit;
}

if ($response === false) {
    http_response_code(500);
    echo json_encode(['error' => 'Failed to capture order. No response data from PayPal.']);
    exit;
}

$responseData = json_decode($response, true);

if (!$responseData) {
    http_response_code(500);
    echo json_encode(['error' => 'Failed to capture order. Invalid JSON response.']);
    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($httpStatusCode);
    echo json_encode(['error' => 'Failed to capture order.', 'details' => $responseData]);
}
?>
php curl paypal
1个回答
0
投票

我实际上解决了这个问题,哈哈。 我拿了

    curl -v -X POST https://api-m.paypal.com/v2/checkout/orders/MyOrderId/capture
-H "Content-Type: application/json"
-H "Authorization: Bearer MyToken"
-H "PayPal-Request-Id: MyRequestId"
-H "Prefer: return=representation"
-H "PayPal-Client-Metadata-Id: MyMetaDataId"

命令,我把它发布在postman中,然后我将其转换为PHP -cURL,然后我拿走了整个命令,然后运行它,最终成功了!!!

源代码如下:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'generateAccessToken.php';

$orderID = $_GET['orderID'] ?? '';

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";
$requestId = uniqid();
$uniqueOrderID = $orderID; // This should ideally come from your order system.
$paypalClientMetadataId = time() . "-" . $uniqueOrderID;

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer '.$accessToken,
    'PayPal-Request-Id: '.$requestId,
    'Prefer: return=representation',
    'PayPal-Client-Metadata-Id: '.$paypalClientMetadataId
  ),
));

$response = curl_exec($curl);

curl_close($curl);


if ($response === false) {
    http_response_code(500);
    echo json_encode(['error' => 'Failed to capture order. No response data from PayPal.']);
    exit;
}

$responseData = json_decode($response, true);

if (!$responseData) {
    http_response_code(500);
    echo json_encode(['error' => 'Failed to capture order. Invalid JSON response.']);
    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($httpStatusCode);
    echo json_encode(['error' => 'Failed to capture order.', 'details' => $responseData]);
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.