Search code examples
phppaypalpaypal-sandbox

is it possible to get buyer's address while using PayPal's Checkout PHP SDK?


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 ?


Solution

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