public function testBuildingEntity() { $social = new Social(); $social->setField(Social::SOCIAL_FACEBOOK)->setValue('https://www.facebook.com/expresslyUK'); $email = new Email(); $email->setAlias('default')->setEmail('*****@*****.**'); $phone = new Phone(); $phone->setCountryCode(44)->setNumber('07951234567')->setType(Phone::PHONE_TYPE_MOBILE); $address = new Address(); $address->setFirstName('Sam')->setLastName('Pratt')->setAddress1('Address1')->setAddress2('Address2')->setCity('London')->setCompanyName('Expressly')->setZip('W2 6LG')->setPhonePosition(0)->setAlias('billing')->setStateProvince('England')->setCountry('GBR'); $entity = new Customer(); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setFirstName('Sam')); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setLastName('Pratt')); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setGender(Customer::GENDER_MALE)); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setCompany('Expressly')); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setBirthday(new \DateTime('1990-01-01'))); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setTaxNumber('tax')); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setDateUpdated(new \DateTime('2015-12-12 05:00:00'))); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setDateLastOrder(new \DateTime('2015-12-12 05:00:00'))); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->setNumberOrdered(1)); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->addSocial($social)); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->addEmail($email)); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->addPhone($phone)); $this->assertInstanceOf('Expressly\\Entity\\Customer', $entity->addAddress($address, true, Address::ADDRESS_BOTH)); $this->assertJson(json_encode($entity->toArray())); $this->assertJsonStringEqualsJsonString(json_encode($entity->toArray()), json_encode(array('firstName' => 'Sam', 'lastName' => 'Pratt', 'gender' => 'M', 'billingAddress' => 0, 'shippingAddress' => 0, 'company' => 'Expressly', 'dob' => '1990-01-01', 'taxNumber' => 'tax', 'dateUpdated' => '2015-12-12T05:00:00+0000', 'dateLastOrder' => '2015-12-12T05:00:00+0000', 'numberOrdered' => 1, 'onlinePresence' => array(array('field' => 'facebook', 'value' => 'https://www.facebook.com/expresslyUK')), 'emails' => array(array('alias' => 'default', 'email' => '*****@*****.**')), 'phones' => array(array('countryCode' => 44, 'number' => '07951234567', 'type' => 'M')), 'addresses' => array(array('firstName' => 'Sam', 'lastName' => 'Pratt', 'address1' => 'Address1', 'address2' => 'Address2', 'city' => 'London', 'companyName' => 'Expressly', 'zip' => 'W2 6LG', 'phone' => 0, 'alias' => 'billing', 'stateProvince' => 'England', 'country' => 'GBR'))))); }
public function testConstruction() { $merchant = new Merchant(); $merchant->setPath('/'); $customer = new Customer(); $customer->setFirstName('Test'); $customer->setLastName('User'); $presenter = new CustomerMigratePresenter($merchant, $customer, '*****@*****.**', '58', 'en'); return $presenter; }
public function execute() { $emailAddress = $this->_request->getParam('email'); $result = $this->resultFactory->create(ResultFactory::TYPE_JSON); try { $magentoCustomer = $this->customerRegistry->retrieveByEmail($emailAddress); if (!$magentoCustomer->isEmpty()) { $customer = new Customer(); $customer->setFirstName($magentoCustomer->getFirstname())->setLastName($magentoCustomer->getLastname()); $email = new Email(); $email->setAlias('default')->setEmail($emailAddress); $customer->addEmail($email); $defaultBilling = $magentoCustomer->getDefaultBillingAddress(); $defaultShipping = $magentoCustomer->getDefaultShippingAddress(); foreach ($magentoCustomer->getAddresses() as $magentoAddress) { $address = new Address(); $address->setFirstName($magentoAddress->getFirstname())->setLastName($magentoAddress->getLastname())->setCompanyName($magentoAddress->getCompany())->setAddress1($magentoAddress->getStreetLine(1))->setAddress2($magentoAddress->getStreetLine(2))->setCity($magentoAddress->getCity())->setStateProvince($magentoAddress->getRegion())->setZip($magentoAddress->getPostcode())->setCountry($magentoAddress->getCountry()); $phone = new Phone(); $customer->addPhone($phone); $address->setPhonePosition($customer->getPhoneIndex($phone)); $primary = false; $type = null; if ($defaultBilling->getId() == $magentoAddress->getId()) { $primary = true; $type = Address::ADDRESS_BILLING; } if ($defaultShipping->getId() == $magentoAddress->getId()) { $primary = true; $type = $type == Address::ADDRESS_BILLING ? Address::ADDRESS_BOTH : Address::ADDRESS_SHIPPING; } $customer->addAddress($address, $primary, $type); } $presenter = new CustomerMigratePresenter($this->merchant, $customer, $emailAddress, 55); $result->setData($presenter->toArray()); } } catch (\Exception $e) { // log error $result->setData([]); } return $result; }
public function showAction() { $this->getResponse()->setHeader('Content-type', 'application/json'); $route = $this->resolver->process(preg_replace('/.*(expressly\\/.*)/i', '/${1}', $_SERVER['REQUEST_URI'])); if ($route instanceof Route) { $emailAddress = $this->getRequest()->getParam('email'); $merchant = $this->app['merchant.provider']->getMerchant(); try { $mageCustomer = \Mage::getModel('customer/customer'); $mageCustomer->setWebsiteId(\Mage::app()->getWebsite()->getId()); $mageCustomer->loadByEmail($emailAddress); $reference = $mageCustomer->getId(); if ($reference) { $customer = new Customer(); $customer->setFirstName($mageCustomer->getFirstname())->setLastName($mageCustomer->getLastname())->setDateUpdated(new DateTime($mageCustomer->getCreatedAt())); if ($mageCustomer->getDob()) { $customer->setBirthday(new DateTime($mageCustomer->getDob())); } if ($mageCustomer->getTaxvat()) { $customer->setTaxNumber($mageCustomer->getTaxvat()); } $gender = $mageCustomer->getGender(); if ($gender == 1 || $gender == 2) { $customer->setGender($mageCustomer->getGender() == 1 ? 'M' : 'F'); } $email = new Email(); $email->setAlias('default')->setEmail($emailAddress); $customer->addEmail($email); $defaultBilling = $mageCustomer->getDefaultBilling(); $defaultShipping = $mageCustomer->getDefaultShipping(); foreach ($mageCustomer->getAddresses() as $mageAddress) { $address = new Address(); $address->setFirstName($mageAddress->getFirstname())->setLastName($mageAddress->getLastname())->setCompanyName($mageAddress->getCompany())->setAddress1($mageAddress->getStreet1())->setAddress2($mageAddress->getStreet2())->setCity($mageAddress->getCity())->setStateProvince($mageAddress->getRegionCode())->setZip($mageAddress->getPostcode())->setCountry($mageAddress->getCountry()); $phone = new Phone(); $phone->setType(Phone::PHONE_TYPE_HOME)->setNumber($mageAddress->getTelephone()); $customer->addPhone($phone); $address->setPhonePosition($customer->getPhoneIndex($phone)); $primary = false; $type = null; if ($mageAddress->getId() == $defaultBilling) { $primary = true; $type = Address::ADDRESS_BILLING; } if ($mageAddress->getId() == $defaultShipping) { $primary = true; $type = $type == Address::ADDRESS_BILLING ? Address::ADDRESS_BOTH : Address::ADDRESS_SHIPPING; } $customer->addAddress($address, $primary, $type); } $presenter = new CustomerMigratePresenter($merchant, $customer, $emailAddress, $reference); $this->getResponse()->setBody(json_encode($presenter->toArray())); } else { $this->getResponse()->setHttpResponseCode(404); } } catch (\Exception $e) { $this->logger->error(ExceptionFormatter::format($e)); $this->getResponse()->setBody(json_encode(array())); } } else { $this->getResponse()->setHttpResponseCode(401); } }
private function retrieveUserByEmail($emailAddr) { try { $user = get_user_by('email', $emailAddr); if ($user) { $customer = new Customer(); $customer->setFirstName($user->first_name)->setLastName($user->last_name); $email = new Email(); $email->setEmail($emailAddr)->setAlias('primary'); $customer->addEmail($email); $user_id =& $user->ID; $countryCodeProvider = $this->app['country_code.provider']; $createAddress = function ($prefix) use($user_id, $countryCodeProvider, $customer) { $address = new Address(); $address->setFirstName(get_user_meta($user_id, $prefix . '_first_name', true))->setLastName(get_user_meta($user_id, $prefix . '_last_name', true))->setAddress1(get_user_meta($user_id, $prefix . '_address_1', true))->setAddress2(get_user_meta($user_id, $prefix . '_address_2', true))->setCity(get_user_meta($user_id, $prefix . '_city', true))->setZip(get_user_meta($user_id, $prefix . '_postcode', true)); $iso3 = $countryCodeProvider->getIso3(get_user_meta($user_id, $prefix . '_country', true)); $address->setCountry($iso3); $address->setStateProvince(get_user_meta($user_id, $prefix . '_state', true)); $phoneNumber = get_user_meta($user_id, $prefix . '_phone', true); if (!empty($phoneNumber)) { $phone = new Phone(); $phone->setType(Phone::PHONE_TYPE_HOME)->setNumber((string) $phoneNumber); $customer->addPhone($phone); $address->setPhonePosition((int) $customer->getPhoneIndex($phone)); } return $address; }; $billingAddress = $createAddress('billing'); $shippingAddress = $createAddress('shipping'); if (Address::compare($billingAddress, $shippingAddress)) { $customer->addAddress($billingAddress, true, Address::ADDRESS_BOTH); } else { $customer->addAddress($billingAddress, true, Address::ADDRESS_BILLING); $customer->addAddress($shippingAddress, true, Address::ADDRESS_SHIPPING); } $merchant = $this->app['merchant.provider']->getMerchant(); $response = new CustomerMigratePresenter($merchant, $customer, $emailAddr, $user->ID); wp_send_json($response->toArray()); } } catch (\Exception $e) { $this->app['logger']->error(ExceptionFormatter::format($e)); wp_send_json(array()); } }