Search code examples
phplaravelpaypalpaypal-rest-sdk

Laravel Paypal - pass shipping address from website


I just implemented the PayPal API for Laravel from net-shell (https://github.com/net-shell/laravel-paypal) and everything works fine so far, but now there's one thing I'm missing or I don't know how to do: In my portal, users have to register with their address. What I want is that this email (that was used for registering at my portal) is used as shipping address and is later also shown when on the PayPal site (so there is no matter what address the user entered for PayPal).

Is there any way to do this?

I know that there seems to be a PayPal:ShippingAdress, which I can use, but if this is the right one, how can I assign it to the payment then?

Edit: I now tried lots of variations with something like $transaction->setShippingAddress(); or $payer->setShippingAddress(); or $payment->setShippingAddress(); but nothing works, the method is never found. Now in Paypal/Api/ShippingAddress I found the method setDefaultAddress, so I thought about creating such an object and use this method like so:

$shippingAddress = [
            "recipient_name" => "John Johnson",
            "line1" => "Whatever street 2",
            "line2" => "Another street 2",
            "city" => "London",
            "country_code" => "UK",
            "postal_code" => "NR30 1LY",
            "state" => "England",
            "phone" => "3123123123"
        ];

        $shippingAddr = PayPal::ShippingAddresss();
        $shippingAddr->setDefaultAddress($shippingAddress);

Don't get an error using this code but also it's not setting the address, I need to assign this created shippingAddress object to the payment or transaction now, like I do it with details, creating details object and then using $amount->setDetails($details);, but I'm not finding anything for the shippingAddress.....

Edit2: Getting to the hint with the item list, this is how I create and set that list:

 $itemList = PayPal::itemList();

        foreach ($items as $item)
                $product = Product::where('id', '=', $item->product->id)->first();
                $itemName = $product->name;
                $itemPrice = $product->price;
                $itemAmount = $item->amount;
                $payPalItem = PayPal::item();
                $payPalItem->setName($itemName)
                    ->setDescription($itemName)
                    ->setCurrency('EUR')
                    ->setQuantity($itemAmount)
                    ->setPrice($itemPrice);
                $itemList->addItem($payPalItem);
            }

and then

$transaction = PayPal::Transaction();
        $transaction->setAmount($amount);
        $transaction->setItemList($itemList);

Solution

  • After chat-research done with the author of question (@nameless - greetings!), we found that the place to add another shipping address is the itemList:

    You gather items to your order

     $itemList = PayPal::itemList();
    
        foreach ($items as $item)
                $product = Product::where('id', '=', $item->product->id)->first();
                $itemName = $product->name;
                $itemPrice = $product->price;
                $itemAmount = $item->amount;
                $payPalItem = PayPal::item();
                $payPalItem->setName($itemName)
                    ->setDescription($itemName)
                    ->setCurrency('EUR')
                    ->setQuantity($itemAmount)
                    ->setPrice($itemPrice);
                $itemList->addItem($payPalItem);
            }
    

    And the final move is to attach address to $itemList

    $shippingAddress = [
                    "recipient_name" => "John Johnson",
                    "line1" => "Whatever street 2",
                    "line2" => "Another street 2",
                    "city" => "London",
                    "country_code" => "GB",
                    "postal_code" => "NR30 1LY",
                    "state" => "England",
                    "phone" => "3123123123"
                ];
    
    $itemList->setShippingAddress($shippingAddres);
    

    Be aware that country is in ISO 3166-1, so GB - not UK. If you enter wrong two letter code - the response will return 400 code malformed.