Home > Enterprise >  Partially refund the authorized order in Paypal
Partially refund the authorized order in Paypal

Time:04-28

I am trying to make a partial refund in PayPal v2 SDK but each time the whole amount is captured. I am just capturing the different amount so in this way the rest amount should be dropped, but the full amount is captured no matter I am requesting the partial. Workflow is like this : 1) I am creating order, 2) Authorizing the order, 3) now need to make a partial refund. The code I am using is like below :

class CaptureOrder
        {
            public static function buildRequestBody($new_Price_after_refund_requests)
            {
                return array(
                    'intent' => 'AUTHORIZE',
                    'application_context' =>
                        array(
                            'return_url' => 'https://example.com/return',
                            'cancel_url' => 'https://example.com/cancel'
                        ),
                    'purchase_units' =>
                        array(
                            0 =>
                                array(
                                    'amount' =>
                                        array(
                                            'currency_code' => 'USD',
                                            'value' =>  $new_Price_after_refund_requests
                                        )
                                )
                        )
                );
            }

            public static function orderCapture($authorizationId, $new_Price_after_refund_requests)
            {
                $request = new AuthorizationsCaptureRequest($authorizationId);
                $request->headers["prefer"] = "return=representation";
                $request->body = self::buildRequestBody($new_Price_after_refund_requests);
                $response = makeclient()->execute($request);
              if($response->status == "COMPLETED"){
                    //some codes here
            }
                echo json_encode($response->result, JSON_PRETTY_PRINT);
                return $response;
            }
        }

Is there any way to make a partial refund of authorized order? Maybe this shouldn't be like this.

CodePudding user response:

Your request body's fields are incorrect and so are not recognized. Unrecognized fields are ignored. Since everything about your body is being ignored, the original amount is being captured.

To create a body with correct fields, see the API reference for a v2/payments authorization request, which has this sample body (there are other fields in the sample, all are optional):

{
  "amount": {
    "value": "10.99",
    "currency_code": "USD"
  }
}

Convert this to PHP array syntax, and use your variable for the value.


Your function's name is a bit misleading, by the way. You aren't capturing a PayPal order (doing so would have no authorization step). Rather, you authorized an order in the previous step (at checkout time), and are now capturing that authorization.

  • Related