I am trying to implement payment using Paypal REST API using Paypal-PHP-SDK, but I am getting an error:
Connection timed out after 10000 milliseconds
Sometimes I also get error malformed. I see on debug I am using sandbox url:
https://api.sandbox.paypal.com/v1/oauth2/token
I have also set context as sandbox:
$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',
)
);
My code to make the payment is:
$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();
Here is the payload data:
{
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "https://local.tps/secure/index.php?page=order&action=confirm&session=fb3ags4k1th90aliq7cgqm1ps1&paymentAmount=1727¤cyCodeType=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"
}]
}
I found my error. I was passing $paymentAmout variable (which contains the amount value) instead of $amount Object to:
$transaction->setAmount($paymentAmount)
It should be:
$transaction->setAmount($amount)
The connection timeout error happens randomly it seems some protection due rate limit maybe?, not sure
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.