Paypal - PHP - Express Checkout 服务器端 Rest

Paypal - PHP - Express Checkout Server Side Rest

我正在尝试使用 Paypal-PHP-SDK 使用 Paypal REST API 实现支付,但我收到一个错误:

Connection timed out after 10000 milliseconds

有时我也会收到格式错误的错误。我在调试时看到我正在使用沙箱 url:

https://api.sandbox.paypal.com/v1/oauth2/token

我也将上下文设置为沙箱:

$ClientID = PAYPAL_CLIENT_ID;
$ClientSecret = PAYPAL_CLIENT_SECRET;

$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        $ClientID,     // ClientID
        $ClientSecret  // ClientSecret
    )
);

$apiContext->setConfig(
      array(
        'mode' => 'sandbox',
      )
);

我的付款代码是:

$payer = new Payer();
$payer->setPaymentMethod("paypal");

$amount = new Amount();
$amount->setCurrency('USD')
    ->setTotal(1727);


$transaction = new Transaction();
$transaction->setAmount($paymentAmount)
    ->setDescription("TPS")
    ->setInvoiceNumber(uniqid());

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($returnURL)
    ->setCancelUrl($cancelURL);

$payment = new Payment();
$payment->setIntent('sale')
    ->setPayer($payer)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions(array($transaction));

try {
    $payment->create($apiContext);
} catch (Exception $ex) {
    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    var_dump("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
    exit(1);
}

$approvalUrl = $payment->getApprovalLink();

这里是负载数据:

{
    "intent": "sale",
    "payer": {
        "payment_method": "paypal"
    },
    "redirect_urls": {
        "return_url": "https://local.tps/secure/index.php?page=order&action=confirm&session=fb3ags4k1th90aliq7cgqm1ps1&paymentAmount=1727&currencyCodeType=USD&paymentType=sale",
        "cancel_url": "https://local.tps/secure/index.php?page=order&action=personal_information&session=fb3ags4k1th90aliq7cgqm1ps1"
    },
    "transactions": [{
        "amount": "1727",
        "description": "TPS",
        "invoice_number": "5ad69cf30d1f0"
    }]
}

我发现了我的错误。我将 $paymentAmout 变量(包含金额值)而不是 $amount 对象传递给:

$transaction->setAmount($paymentAmount)

应该是:

$transaction->setAmount($amount)

连接超时错误是随机发生的,似乎有某种保护应有的速率限制?,不确定