Search code examples
paypal-rest-sdklaravel-5.6

Got Http response code 400 when implementing Paypal Sandbox refund


I am trying to implement Sandbox Paypal Payment Gateway refund functionality. I am using Laravel 5.6.12 and Package: "paypal/rest-api-sdk-php": "^1.13"

I have no issue in doing payment. Below is the response received after successful payment done.

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

From the above JSON, I took Sale_ID = 0KH341752J2209342 and wrote the below code to implement refund.

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

and got the below error.

Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/sale/0KH341752J2209342/refund.

enter image description here

Can you please suggest something if there is anything wrong? Please let me know if you need more info. I removed some unnecessary sections from above JSON to make it short

Error Details.

Status Code : 400 Response: {"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"developer.paypal.com/webapps/developer/docs/api/…;}PayPal\Exception\PayPalConnectionException: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/sale/3FM030155Y9829603/refund


Solution

  • I added below three lines

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

    in the below code and everything is working now.

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