[英]Paypal PHP Rest API for credit card payment , how to handle the errors to show it to the users
I am using Paypal PHP Rest API from here for credit card payment. 我正在从此处使用Paypal PHP Rest API进行信用卡付款。 I can pay successfully with demo data.
我可以使用演示数据成功付款。 In real time when a user faces error in credit card payment, I need a way to show it in a user-friendly way rather than in a programmatic way.
当用户实时遇到信用卡付款错误时,我需要一种以用户友好的方式而不是程序化的方式显示该错误的方法。
From the paypal developer site , I found the format of the error object returned but cannot figure out how to use that. 在贝宝开发人员网站上 ,我找到了返回的错误对象的格式,但无法弄清楚如何使用它。
My code follows: 我的代码如下:
try {
$payment->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
}
With intentional input of wrong data, following error message was there: 在故意输入错误数据的情况下,出现以下错误消息:
Exception: Got Http response code 400 when accessing
https://api.sandbox.paypal.com/v1/payments/payment.
string '{"name":"VALIDATION_ERROR","details":
[{"field":"payer.funding_instruments[0].credit_card",
"issue":"Invalid expiration (cannot be in the past)"},
{"field":"payer.funding_instruments[0].credit_card.number",
"issue":"Value is invalid"},{"field":
"payer.funding_instruments[0].credit_card.cvv2",
"issue":"Length is invalid (must be 3 or 4,
depending on card type)"}],"message":"Invalid request
- see details","information_link":
"https://developer.paypal.com/webapps
/developer/docs/api/#VALIDATION_ERROR","debug_id":
"bdcc'... (length=523)
So how can I get the error object as said in the mentioned Paypal developer site and how to use that to show errors to a non-techie person? 因此,如何获得提到的Paypal开发人员网站中所述的错误对象,以及如何使用该对象向非技术人员显示错误?
The error returned is in JSON format. 返回的错误为JSON格式。 You can use
json_decode
to decode it in PHP: 您可以使用
json_decode
在PHP中json_decode
进行解码:
$error_object = json_decode($ex->getData());
switch ($error_object->name)
{
case 'VALIDATION_ERROR':
echo "Payment failed due to invalid Credit Card details:\n";
foreach ($error_object->details as $e)
{
echo "\t" . $e->field . "\n\t" . $e->issue . "\n\n";
}
break;
}
Add whichever cases you would like to your switch. 将您想要的任何情况添加到交换机。 And you can add your own flair, parse the
$e->field
and $e->issue
to display whatever you like, etc. 您可以添加自己的风格,解析
$e->field
和$e->issue
以显示您喜欢的东西,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.