实施Paypal Sandbox退款时获得Http响应代码400

时间:2018-05-08 07:07:10

标签: paypal-rest-sdk laravel-5.6

我正在尝试实施Sandbox Paypal支付网关退款功能。我正在使用Laravel 5.6.12和Package:" paypal / rest-api-sdk-php":" ^ 1.13"

我付款没问题。以下是成功付款后收到的回复。

{
    "id": "PAY-94141048LX592642PLLYUVMA",
    "transactions": [
        {
            "related_resources": [
                {
                    "sale": {
                        "id": "0KH341752J2209342",
                        "state": "completed",
                        "links": [
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/0KH341752J2209342",
                                "rel": "self",
                                "method": "GET"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/0KH341752J2209342/refund",
                                "rel": "refund",
                                "method": "POST"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-94141048LX592642PLLYUVMA",
                                "rel": "parent_payment",
                                "method": "GET"
                            }
                        ],
                        "soft_descriptor": "PAYPAL *PANKAJGARGS"
                    }
                }
            ]
        }
    ]
}  

从上面的JSON中,我拿了Sale_ID = 0KH341752J2209342并写下以下代码来实现退款。

$refund = new Refund();
$refund->setAmount(200);
$sale = new Sale();
$sale->setId("0KH341752J2209342");
try {
    $apiContext = $this->_api_context;
    $refundedSale = $sale->refund($refund, $apiContext);
} catch (Exception $ex) {
    \Log::info($ex);
    exit(1);
}

并得到以下错误。

访问https://api.sandbox.paypal.com/v1/payments/sale/0KH341752J2209342/refund时获得Http响应代码400。

enter image description here

如果有什么不对的话,请你提出建议吗?如果您需要更多信息,请告诉我。我从JSON上面删除了一些不必要的部分,使其缩短

错误详情。

状态代码:400响应:{"名称":" MALFORMED_REQUEST","消息":"传入的JSON请求未映射到API请求& #34;," information_link":" developer.paypal.com/webapps/developer/docs/api/...;} PayPal \ Exception \ PayPalConnectionException:访问{{3时获得Http响应代码400 }}

2 个答案:

答案 0 :(得分:1)

您应该添加以下catch,以便您可以看到真正的错误消息:

} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode();
echo $ex->getData(); // Prints the detailed error message 
die($ex);
}

答案 1 :(得分:1)

我在下面添加了三行

$amt = new Amount();
$amt->setTotal(10)
    ->setCurrency('INR');

在下面的代码中,现在一切正常。

$refund = new Refund();
$refund->setAmount($amt);
$sale = new Sale();
$sale->setId("0KH341752J2209342");
try {
    $apiContext = $this->_api_context;
    $refundedSale = $sale->refund($refund, $apiContext);
} catch (Exception $ex) {
    \Log::info($ex);
    exit(1);
}