Paypal - PHP - 快速结账服务器侧休息

时间:2018-04-17 22:58:16

标签: php paypal paypal-rest-sdk

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

Connection timed out after 10000 milliseconds

有时我也会收到错误信息。我在调试时看到我正在使用沙盒网址:

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"
    }]
}

1 个答案:

答案 0 :(得分:0)

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

$transaction->setAmount($paymentAmount)

应该是:

$transaction->setAmount($amount)

连接超时错误是随机发生的,似乎有一些保护到期速率限制?,不确定