Magento and PayPal payment doesn’t set Delivery Address

Apr 13, 2011

MagentoI came across this minor problem when a client running a Magento website wanted to enable the paypal checkout. Once I enabled paypal express checkout and started to run through some test orders the custom module I had written to export their orders ready for their warehouse/dispatch system had a few problems. The problem is that PayPal doesn’t use the same naming for its addresses as Magento. In Magento the customer is asked for billing and delivery addresses and where they opt to use the same address the details are simply repeated. Thus my Magento module used the following two lines of code to get hold of the address information.

$shipping_address = $order->getShippingAddress();
$billing_address = $order->getBillingAddress();

Now as soon as I started testing PayPal orders the shipping address started coming back blank. A simple situation to test for and deal with – to ensure you delivery runs smoothly you just use the billing address (actually the postage address from PayPal). I went a bit overboard with the following code in case this happens differently with different providers in the future, they key is that the getShippingAddress and getBillingAddress methods return false, not null.

$shipping_address = $order->getShippingAddress();
$billing_address = $order->getBillingAddress();
if ($billing_address == false) {
    $billing_address = $shipping_address;
}
if ($shipping_address == false) {
    $shipping_address = $billing_address;
}