2

我从这里使用 Paypal PHP Rest API进行信用卡支付。我可以使用演示数据成功付款。当用户实时面临信用卡支付错误时,我需要一种以用户友好的方式而不是程序化的方式来显示它的方法。

贝宝开发者网站,我找到了返回的错误对象的格式,但不知道如何使用它。

我的代码如下:

try {
    $payment->create($apiContext);


} catch (PayPal\Exception\PPConnectionException $ex) {

echo "Exception: " . $ex->getMessage() . PHP_EOL;
    var_dump($ex->getData());

}

故意输入错误数据,出现以下错误消息:

        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)

那么我怎样才能得到上面提到的 Paypal 开发者网站中所说的错误对象,以及如何使用它向非技术人员显示错误?

4

1 回答 1

3

返回的错误是 JSON 格式。您可以使用json_decodePHP 对其进行解码:

$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;
}

将您想要的任何情况添加到您的交换机。您可以添加自己的风格,解析$e->field$e->issue显示您喜欢的任何内容,等等。

于 2014-04-30T21:45:24.233 回答