skip to Main Content

According to PayPal orders API as document here, We have to first create order then from the response, we have to copy the approve url and run in the browser. This will open PayPal page. There buyer will approve the request. After this a capture request should be made.

Issue details

I have following code that creates the order using PayPal API:

$client = new GuzzleHttpClient();
$response = $client->request('POST', $uri, [
        'json' => [
            "intent" => "CAPTURE",
            "purchase_units" => [
                [
                    "amount" => [
                        "currency_code" => "USD",
                        "value" => "100.00"
                    ]
                ]
            ]
        ],
        'headers' => [
            'Accept' => 'application/json',
            'Accept-Language' => 'en_US',
            'Content-Type' => 'application/json',
        ],
        'auth' => [$clientId, $secret, 'basic']
    ]            
);
$data = json_decode($response->getBody(), true);
echo "<pre>";
print_r($data);
echo "</pre>";

This code works fine. This gives me 4 urls as shown in the below screenshot.

enter image description here

After this, I copy the url with rel = approve. This one: https://www.sandbox.paypal.com/checkoutnow?token=3C454469W0667862G

Now, run this url, this will open the sandbox PayPal page. After login with buyer, and click pay, it stays on same page without any error.

Any idea why this is happening?

2

Answers


  1.     'json' => [
            "intent" => "CAPTURE",
            "purchase_units" => [
                [
                    "amount" => [
                        "currency_code" => "USD",
                        "value" => "100.00"
                    ]
                ]
            ]
        ],
    

    After login with buyer, and click pay, it stays on same page without
    any error.

    Any idea why this is happening?


    You haven’t specified any return_url in the create body’s application_context. There is nowhere for it to redirect next, so it simply stays on the same page.

    For the best user experience, do not use any redirects to an approval_url or return url. Instead use no redirects. At all.

    Rather, pair your two server-side routes of ‘Create an Order’ and ‘Capture Order’ with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

    This keeps your site loaded at all times.

    Login or Signup to reply.
  2. Because return_url is missing.
    Here is the below example with array that needs to post in JSON format

    Array
    (
      [intent] => CAPTURE
      [purchase_units] => Array
        (
            [0] => Array
                (
                    [amount] => Array
                        (
                            [currency_code] => USD
                            [value] => 100
                        )
    
                    [payment_instruction] => Array
                        (
                            [disbursement_mode] => INSTANT
                        )
                )
        )
    
    [payment_source] => Array
        (
            [paypal] => Array
                (
                    [experience_context] => Array
                        (
                            [return_url] => https://example.com/return
                            [cancel_url] => https://example.com/cancel
                        )
    
                    [name] => Array
                        (
                            [given_name] => A
                            [surname] => B
                        )
    
                )
    
        )
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search