How to execute a payment with paypal/checkout-php-sdk?

1.5k Views Asked by At

I'm working with paypal/checkout-php-sdk, but am at a loss for what to do after creating an order.

I can create an order successfully:

/**
 * @Route("/create-order", name="create_order")
 */
public function createOrder(Paypal $paypal)
{
    // Construct a request object and set desired parameters
    // Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
    $request = new OrdersCreateRequest();
    $request->prefer('return=representation');
    $request->body = [
        "intent" => "CAPTURE",
        "purchase_units" => [[
            "reference_id" => "test_ref_id1",
            "amount" => [
                "value" => "100.00",
                "currency_code" => "USD"
            ]
        ]],
        "application_context" => [
            "cancel_url" => "https://google.com/",
            "return_url" => "http://127.0.0.1:8000/execute-order"
        ]
    ];

    try {
        // Call API with your client and get a response for your call
        $response = $paypal->client->execute($request);

        $approuveLink = array_values( array_filter(
            $response->result->links,
            function ($e) {
                return $e->rel == "approve";
            }
        ))[0]->href;

        return $this->redirect($approuveLink);

    }catch (HttpException $ex) {
        echo $ex->statusCode;
        return new Response($ex->getMessage());
    }

}

And as you see I put the return_url to "/execute-order",

So the PayPal redirects me to this url after being authentified :

/execute-order?token=0NH30171UR363613S&PayerID=SNVZ4E6KDYA54

But I don't know what to do next. I have to execute the payment but in the package's sample, I found only createOrder and captureOrder But there is no executeOrder

/**
 * @Route("/execute-order", name="order_execute")
 */
public function executeOrder(Request $request)
{
    echo $request->query->get('PayerID')." / ". $request->query->get('token');

    // WHAT TO PUT HERE ?
}
1

There are 1 best solutions below

2
On

For v2/checkout/orders integrations, "capture" is the final action.

It is analogous to the "execute" action in the deprecated v1/payments, which seems to be why you're expecting something with that verbiage.

There is no "execute" in v2.


For best results, do not redirect to a PayPal approval page and back to a return_url. Do not use any redirects whatsoever. Instead, use this approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server -- this is a modern "in context" experience, which keeps your site loaded in the background. It's an improved buyer experience that gives better results, increased conversion, etc