6

A new customer who makes a virtual product purchase through paypal has their billing address passed to Magento. The address is attached to the order but is not attached to the customer. What would be the best way to attach this to the customer and also set it as the default billing address on their account?

  • I am not aware of such built in capability, so best bet is custom module. I guess what you ask for is only option with PayPal Express – Petar Dzhambazov Jan 26 '13 at 11:50
  • 2
    It looks like this needs to be coded as an additional extension. The paypal module does not have many events to work with but it does have paypal_payment_transaction_load_by_txn_id_after At least for paypal standard this looks to be triggered when an IPN notification is received. In the observer you can access the transaction $paymentTransaction = $observer->getEvent()->getPaypalPaymentTransaction(); $orderPayment = $paymentTransaction->getOrderPaymentObject(); This should hopefully get you close enough to the order object and you can start testing if the customer has a billing address. – Kristof at Fooman Jan 26 '13 at 12:00
  • Did you get an answer? Did @fooman helped you? Then write the answer down fooman and Ryan can accept it - if it helped – Fabian Blechschmidt Feb 28 '13 at 17:51
1

You're going to want to tap into the predispatch event of the Mage_Paypal_IpnController class:

Create the xml in your custom module to handle the observer for the predispatch:

<global>
    <events>
        <controller_action_predispatch_paypal_ipn_index>
            <observers>
                <mymodule_paypal_ipn_index>
                    <type>singleton</type>
                    <class>yourmodel/observer</class>
                    <method>ipnPreDispatch</method>
                </mymodule_paypal_ipn_index>
            </observers>
        </controller_action_predispatch_paypal_ipn_index>
    </events>
</global>

Observer.php:

<?php

class YourCompany_YourModule_Model_Observer
{

    public function ipnPreDispatch($observer)
    {
        //get the request object and post data
        $data = $observer->getControllerAction()->getRequest()->getPost();

        //get the email address
        $email = $data['email_address'];

        //get the customer via the email
        $customer = Mage::getModel("customer/customer")->loadByEmail($email);

        //set the customer default address
        $default_address = array (
            'firstname' => $data['first'],
            'lastname' => $data['last'],
            'street' => array (
                '0' => $data['street1'],
                '1' => $data['street2'],
            ),
            'city' => $data['city'],
            'region_id' => $data['state'],
            'postcode' => $data['zipcode'],
            'country_id' => $data['country'],
            'telephone' => $data['telephone'],
        );
        $address = Mage::getModel('customer/address');

        $address->setData($default_address)
                    ->setCustomerId($customer->getId())
                    ->setIsDefaultBilling('1')
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1');
        try {
            $address->save();
        } catch(Exception $e) {
            Mage::logExeption($e);
        }

    }

}

I'm not certain of the format of the IPN, so the keys of $data in the observer above may need to be altered to adjust. Also, I'm not sure of the business rules, so at the moment it sets all IPN customer addresses to the default to whatever was on their most recent order. This is not production-ready, but gets you 90% of the way there.

  • 1
    Hooking into the controller_action_predispatch_paypal_ipn_index observer did the trick. Thank you for this valuable information! – Ryan Street Apr 30 '13 at 15:30
  • Fooman deserves a lot of credit here. Cheers! – philwinkle Apr 30 '13 at 15:35

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.