/** * Update quote when returned from PayPal * rewrite billing address by paypal * save old billing address for new customer * export shipping address in case address absence * * @param string $token */ public function returnFromPaypal($token) { $this->_getApi(); $quote = $this->_quote; $getExpressCheckoutReply = $this->_api->getExpressCheckout($quote->reserveOrderId()->getReservedOrderId(), $token, Mage::app()->getStore()->getCurrentCurrencyCode()); $this->_ignoreAddressValidation(); /* Always import shipping address. We would have passed the shipping address in to begin with, so they can keep it that way at PayPal if they like - or choose their own registered shipping address at PayPal. */ $paypalShippingAddress = $getExpressCheckoutReply['shipping_address']; if (!$quote->getIsVirtual()) { $shippingAddress = $quote->getShippingAddress(); $shippingAddress->setTelephone($getExpressCheckoutReply['phone']); $shippingAddress->setStreet($paypalShippingAddress['street']); $shippingAddress->setCity($paypalShippingAddress['city']); $shippingAddress->setPostcode($paypalShippingAddress['postcode']); $shippingAddress->setCountryId($paypalShippingAddress['country_id']); // Region must be processed after country id is set for the lookup to work. $this->regionHelper->setQuoteAddressRegion($shippingAddress, $paypalShippingAddress['region_code']); $shippingAddress->setPrefix(null); $shippingAddress->setMiddlename($getExpressCheckoutReply['middlename']); $shippingAddress->setLastname($getExpressCheckoutReply['lastname']); $shippingAddress->setFirstname($getExpressCheckoutReply['firstname']); $shippingAddress->setSuffix($getExpressCheckoutReply['suffix']); $shippingAddress->setCollectShippingRates(true); $shippingAddress->setSameAsBilling(0); $quote->setShippingAddress($shippingAddress); $quote->setCustomerFirstname($getExpressCheckoutReply['firstname']); $quote->setCustomerLastname($getExpressCheckoutReply['lastname']); } // Import billing address if we are here via Button - which is to say we didn't have a billing address yet: $portBillingFromShipping = $quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON) == 1 && !$quote->isVirtual(); if ($portBillingFromShipping) { $billingAddress = clone $shippingAddress; $billingAddress->unsAddressId()->unsAddressType(); $data = $billingAddress->getData(); $data['save_in_address_book'] = 0; $quote->getBillingAddress()->addData($data); $quote->getShippingAddress()->setSameAsBilling(1); } else { $billingAddress = $quote->getBillingAddress(); } $paypalBillingAddress = $getExpressCheckoutReply['billing_address']; $billingAddress->setStreet($paypalBillingAddress['street']); $billingAddress->setCity($paypalBillingAddress['city']); $billingAddress->setPostcode($paypalBillingAddress['postcode']); $billingAddress->setCountryId($paypalBillingAddress['country_id']); $billingAddress->setEmail($getExpressCheckoutReply['email']); // Region must be processed after country id is set for the lookup to work. $this->regionHelper->setQuoteAddressRegion($billingAddress, $paypalBillingAddress['region_code']); $quote->setBillingAddress($billingAddress); // import payment info $quote->getPayment()->setAdditionalInformation(self::PAYMENT_INFO_PAYER_ID, $getExpressCheckoutReply['payer_id'])->setAdditionalInformation(self::PAYMENT_INFO_TOKEN, $token)->setAdditionalInformation(self::PAYMENT_INFO_ADDRESS_STATUS, $paypalShippingAddress['status']); $quote->collectTotals()->save(); }