PayPal Express – Use FIRSTNAME and LASTNAME Instead of SHIPTONAME

checkoutmagento-1.9paypal-expressshipping-address

In Magento 1.9 PayPal is transferring a SHIPTONAME which is used for the shipping address. Instead of populating firstname and lastname with the correct variables, only firstname is populated with SHIPTONAME which includes both first- and lastname and the field for lastname remains empty.
I need to change this so SHIPTONAME isn't being used and instead each field is populated with the correct variable.

This function

protected function _exportAddressses($data)
{
    $address = new Varien_Object();
    Varien_Object_Mapper::accumulateByMap($data, $address, $this->_billingAddressMap);
    $address->setExportedKeys(array_values($this->_billingAddressMap));
    $this->_applyStreetAndRegionWorkarounds($address);
    $this->setExportedBillingAddress($address);
    // assume there is shipping address if there is at least one field specific to shipping
    if (isset($data['SHIPTONAME'])) {
        $shippingAddress = clone $address;
        Varien_Object_Mapper::accumulateByMap($data, $shippingAddress, $this->_shippingAddressMap);
        $this->_applyStreetAndRegionWorkarounds($shippingAddress);
        // PayPal doesn't provide detailed shipping name fields, so the name will be overwritten
        $shippingAddress->addData(array(
            'firstname'  => $data['SHIPTONAME'],
        ));
        $this->setExportedShippingAddress($shippingAddress);
    }
}

assigns SHIPTONAME to the firstname field, but simply adding a second declaration like 'lastname' => $data['LASTNAME'], has no effect, as apparently only the firstname field is being used for the shipping address for PayPal Express orders.

Best Answer

In Model/Express/Checkout.php the returnFromPaypal($token) function includes this:

$shippingAddress->setLastname(null);

If I remove this, I can change the _exportAddressses function in Model/Api/Nvp.php to include

'firstname'  => $data['FIRSTNAME'],
'lastname'  => $data['LASTNAME'],

instead of

'firstname'  => $data['SHIPTONAME'],

This will properly assign first- and lastname to their respective fields. I am still trying to figure out, why Magento is assigning the full name to the firstname field in the first place - and why nobody is bothered by this...

Edit 26.09.2017

As some have pointed out, this will force customers to use the billing address stored within PayPal without being able to select a differing shipping address. So I have chosen a different approach:

Model/Api/Nvp.php

$shiptofirstname = substr($data['SHIPTONAME'], 0, strrpos($data['SHIPTONAME'], ' '));
$shiptolastname = array_pop(explode(' ', $data['SHIPTONAME']));
$shippingAddress->addData(array(
    'firstname'  => $shiptofirstname,
    'lastname'  => $shiptolastname,
));

This will split SHIPTONAME after the last space. For my country this will most likely work 99% of the time as surnames usually don't include spaces. And even if the surname includes a space, at least I am not getting empty surname fields or double surnames (see a differenet solution further down) which will force me to edit orders manually but only incorrectly splitted surnames.

Related Topic