且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PayPal 订单 V2 未处理批准 url

更新时间:2023-11-29 23:16:34

 'json' =>[意图"=>捕获",purchase_units"=>[[金额"=>[货币代码"=>美元",价值"=>100.00"]]]],

与买家登录后,点击支付,停留在同一页面,没有任何错误.

知道为什么会这样吗?


您尚未在创建正文中指定任何return_url应用上下文.它接下来无处重定向,因此它只是停留在同一页面上.

为了获得***用户体验,请勿使用任何重定向到批准网址或返回网址.而是不使用重定向.完全没有.

相反,配对 创建订单"和获取订单"的两个服务器端路由,审批流程如下:https://developer.paypal.com/demo/checkout/#/pattern/server

这让您的网站始终处于加载状态.

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 \GuzzleHttp\Client();
$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.

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?

    '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.