6

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.

6

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.

  • 1
    The problem with this approach is that $data['FIRSTNAME'] and $data['LASTNAME'] keeps data from Magento. This mean if shipping address name from PayPal is different from Magento it will be overwritten. I'm afraid the only solution here is to parse $data['SHIPTONAME']. – user487772 May 31 '16 at 14:00
  • Correction, $data['FIRSTNAME'] and $data['LASTNAME'] is PayPal account holder which can definitely be different from shipping address name. – user487772 May 31 '16 at 14:20
  • 2017 and we still ahve this problem ;) Is there a workaround or fix for this??? Or is the solution still to parse $data['SHIPTONAME']? thanks in advance – snh_nl Aug 28 '17 at 13:31
  • Update! I am an Magento 1934 .... if I take a look in the sales_flat_quote table ... then I can clearly see first+lastname ... can it be that the PP Exress was updated and is now sending firstname + lastname? only Magento keeps hard-resetting it? See here: imgur.com/a/ucf6U -- how else could it get there? – snh_nl Aug 28 '17 at 15:01
  • We are using this answer and see: the billing address has a firstname+lastname and the shipping address has a firstname+lastname .... only the account itself in customer/accoount in the backend nass no value for firstname+lastname. Is this fixed supposed to fix this too? Or is there another solution? thx – snh_nl Mar 2 at 16:02
1

Uncomment the SetName methods ... they are no longer needed.

Paypal does send the customer information Firstname and Lastname.

Magento 1934 28-8-17

public function returnFromPaypal($token)
{
    $this->_getApi();
    $this->_api->setToken($token)
        ->callGetExpressCheckoutDetails();
    $quote = $this->_quote;

    $this->_ignoreAddressValidation();

    // import shipping address
    $exportedShippingAddress = $this->_api->getExportedShippingAddress();
    if (!$quote->getIsVirtual()) {
        $shippingAddress = $quote->getShippingAddress();
        if ($shippingAddress) {
            if ($exportedShippingAddress) {
                $this->_setExportedAddressData($shippingAddress, $exportedShippingAddress);

                if ($quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON) == 1) {
                    // PayPal doesn't provide detailed shipping info: prefix, middlename, lastname, suffix
                    //$shippingAddress->setPrefix(null);
                    //$shippingAddress->setMiddlename(null);
                    //$shippingAddress->setLastname(null);
                    //$shippingAddress->setSuffix(null);
                }
  • So what's happening with this solution is SHIPTONAME will still insert the entire Pay Pal name into Magentos first name field while the described changes will result in the Pay Pal billing last name being inserted into Magentos last name field. Not a solution either I am afraid – loeffel Sep 26 '17 at 9:33
0
        // PayPal doesn't provide detailed shipping name fields, so the name will be overwritten
        $_arraydata = explode(' ', $data['SHIPTONAME'], 2);
        if (!empty($_arraydata[0]) && !empty($_arraydata[1])) {
            $shippingAddress->addData(array(
                'firstname'  => $_arraydata[0],
                'lastname'  => $_arraydata[1]
            ));
        } else {
            $shippingAddress->addData(array(
                'firstname'  => $data['FIRSTNAME'],
                'lastname'  => $data['LASTNAME']
            ));
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.