使用PayPal签到PHP SDK时,是否可以获取买家的地址?

发布于 2025-02-09 04:57:11 字数 4050 浏览 2 评论 0 原文

我有此代码用于处理我的网站上的PayPal付款:

$environment = new \PayPalCheckoutSdk\Core\SandboxEnvironment(PAYPAL_ID, PAYPAL_SECRET);

$client = new \PayPalCheckoutSdk\Core\PayPalHttpClient($environment);
$authorizationId = $_POST['authorizationId'];

$request = new \PayPalCheckoutSdk\Payments\AuthorizationsGetRequest($authorizationId);
$authorizationResponse = $client->execute($request);
$orderId = $authorizationResponse->result->supplementary_data->related_ids->order_id;

$request = new OrdersGetRequest($orderId);
$orderResponse = $client->execute($request);

var_dump($orderResponse->result->payer->address);

当我var_dump地址sdtobject时,我只得到这个代码,

  public 'address' => 
    object(stdClass)[74]
      public 'country_code' => string 'FR' (length=2)

我只是想在完成订单后将其地址存储在我的数据库中,因为他们是在我的数据库中,因为他们是作为“客人购买的” ”。

即使您使用PayPal的卡选项付款,但他们要求您放置您的运输地址,也会发生这种情况。

该订单是这样创建的

$order = json_encode([
                'purchase_units' => [
                    [
                        'description' => 'some desc...',
                        'items'       => array_map(function ($product) {
                            return [
                                'name'        => $product['name'] . " (n°" . $product['num'] . " | " . strtoupper($product['language']) . ")",
                                'quantity'    => $_SESSION['cart'][$product['id']],
                                'unit_amount' => [
                                    'value'         => number_format((float)$product['price'], 2, '.', ''),
                                    'currency_code' => 'USD',
                                ]
                            ];
                        }, $products),
                        'amount'      => [
                            'currency_code' => 'USD',
                            'value'         => $total > 200 ? $total : $total + 8,
                            'breakdown'     => [
                                'item_total' => [
                                    'currency_code' => 'USD',
                                    'value'         => $total
                                ],
                                'shipping' => [
                                    'currency_code' => 'USD',
                                    'value'         => $total > 200 ? '0.00' : '8.00',
                                ]
                            ]
                        ]
                    ]
                ]
            ]);

,然后像这样将其传递给PayPal的JS SDK:

paypal.Buttons({
                    createOrder: (data, actions) => {

                        return actions.order.create(<?= $order; ?>);

                    },
                    onApprove: async (data, actions) => {
                        const authorization = await actions.order.authorize()
                        const authorizationId = authorization.purchase_units[0].payments.authorizations[0].id
                        const response = await fetch('<?= base_url('cart/process'); ?>', {
                            method: 'post',
                            headers: {
                                'content-type': 'application/json'
                            },
                            body: JSON.stringify({
                                authorizationId
                            })
                        })
                        let responseText = await response.text();
                        $('.site-content').html(responseText);
                    },
                    onCancel: function(data, actions) {
                        alert("Order cancelled successfully!");
                    },
                    onError: function(err) {
                        console.log(err);
                    }
                }).render('#paypal-button-container');

创建的订单的意图是授权

有人可以帮助我吗?

I have this code for processing PayPal payments on my website :

$environment = new \PayPalCheckoutSdk\Core\SandboxEnvironment(PAYPAL_ID, PAYPAL_SECRET);

$client = new \PayPalCheckoutSdk\Core\PayPalHttpClient($environment);
$authorizationId = $_POST['authorizationId'];

$request = new \PayPalCheckoutSdk\Payments\AuthorizationsGetRequest($authorizationId);
$authorizationResponse = $client->execute($request);
$orderId = $authorizationResponse->result->supplementary_data->related_ids->order_id;

$request = new OrdersGetRequest($orderId);
$orderResponse = $client->execute($request);

var_dump($orderResponse->result->payer->address);

When I var_dump the address sdtObject I get only this

  public 'address' => 
    object(stdClass)[74]
      public 'country_code' => string 'FR' (length=2)

I am trying to get the buyer's shipping information after completing the order to store their address in my database because they are buying as a "guest".

That happens even when you pay with Card option of PayPal although they ask you to put in your shipping address.

The order is created like this

$order = json_encode([
                'purchase_units' => [
                    [
                        'description' => 'some desc...',
                        'items'       => array_map(function ($product) {
                            return [
                                'name'        => $product['name'] . " (n°" . $product['num'] . " | " . strtoupper($product['language']) . ")",
                                'quantity'    => $_SESSION['cart'][$product['id']],
                                'unit_amount' => [
                                    'value'         => number_format((float)$product['price'], 2, '.', ''),
                                    'currency_code' => 'USD',
                                ]
                            ];
                        }, $products),
                        'amount'      => [
                            'currency_code' => 'USD',
                            'value'         => $total > 200 ? $total : $total + 8,
                            'breakdown'     => [
                                'item_total' => [
                                    'currency_code' => 'USD',
                                    'value'         => $total
                                ],
                                'shipping' => [
                                    'currency_code' => 'USD',
                                    'value'         => $total > 200 ? '0.00' : '8.00',
                                ]
                            ]
                        ]
                    ]
                ]
            ]);

And is then passed like this to the PayPal's JS SDK :

paypal.Buttons({
                    createOrder: (data, actions) => {

                        return actions.order.create(<?= $order; ?>);

                    },
                    onApprove: async (data, actions) => {
                        const authorization = await actions.order.authorize()
                        const authorizationId = authorization.purchase_units[0].payments.authorizations[0].id
                        const response = await fetch('<?= base_url('cart/process'); ?>', {
                            method: 'post',
                            headers: {
                                'content-type': 'application/json'
                            },
                            body: JSON.stringify({
                                authorizationId
                            })
                        })
                        let responseText = await response.text();
                        $('.site-content').html(responseText);
                    },
                    onCancel: function(data, actions) {
                        alert("Order cancelled successfully!");
                    },
                    onError: function(err) {
                        console.log(err);
                    }
                }).render('#paypal-button-container');

The intent of the order which is created is authorize

Can someone help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ懵逼小萝莉 2025-02-16 04:57:11

$ ordersponse 包含运输地址: print_r($ ordersponse-&gt;结果

- var_dump(); 当项目离主要一个太远时,它将“ ...”:

因为这是我发现的唯一 address> array/object narray/object在 $ unerizationResponse 中,但仅包含 country_code

The $orderResponse contains the shipping address : print_r($orderResponse->result->purchase_units[0]->shipping->address);

I was using var_dump(); which when the item is too far from the main one puts "..." :

Because of that the only address array/object I found was in the $authorizationResponse but it would of contain only the country_code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文