From 507064669a5e4769bdfe5671d7391bc856d010ef Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 15 Dec 2016 15:24:39 -0600 Subject: [PATCH 1/2] Create shipping addresses on existing accounts --- Tests/Recurly/ShippingAddress_Test.php | 33 +++++++++++++++++++ .../shipping_addresses/create-201.xml | 24 ++++++++++++++ lib/recurly/account.php | 8 +++++ lib/recurly/shipping_address.php | 14 ++++++-- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Tests/Recurly/ShippingAddress_Test.php create mode 100644 Tests/fixtures/shipping_addresses/create-201.xml diff --git a/Tests/Recurly/ShippingAddress_Test.php b/Tests/Recurly/ShippingAddress_Test.php new file mode 100644 index 00000000..b84eed07 --- /dev/null +++ b/Tests/Recurly/ShippingAddress_Test.php @@ -0,0 +1,33 @@ +client); + $this->client->addResponse('POST', 'https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses', 'shipping_addresses/create-201.xml'); + + $shad = new Recurly_ShippingAddress(); + $shad->nickname = "Home"; + $shad->first_name = "Verena"; + $shad->last_name = "Example"; + $shad->phone = "555-555-5555"; + $shad->email = "verena@example.com"; + $shad->address1 = "123 Dolores St."; + $shad->city = "San Francisco"; + $shad->state = "CA"; + $shad->zip = "94110"; + $shad->country = "US"; + + $account->createShippingAddress($shad, $this->client); + + $this->assertEquals($shad->id, 1234567); + } + +} diff --git a/Tests/fixtures/shipping_addresses/create-201.xml b/Tests/fixtures/shipping_addresses/create-201.xml new file mode 100644 index 00000000..1557e021 --- /dev/null +++ b/Tests/fixtures/shipping_addresses/create-201.xml @@ -0,0 +1,24 @@ +HTTP/1.1 201 Created +Content-Type: application/xml; charset=utf-8 +Location: https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses/1234567 + + + + + 1234567 + Home + Verena + Example + + verena@example.com + + 123 Dolores St. + + San Francisco + CA + 94110 + US + 555-555-5555 + 2016-12-15T21:01:51Z + 2016-12-15T21:01:51Z + diff --git a/lib/recurly/account.php b/lib/recurly/account.php index 75638bce..3d28f353 100644 --- a/lib/recurly/account.php +++ b/lib/recurly/account.php @@ -70,6 +70,14 @@ public static function reopenAccount($accountCode, $client = null) { return Recurly_Base::_put(Recurly_Account::uriForAccount($accountCode) . '/reopen', $client); } + public function createShippingAddress($shippingAddress, $client = null) { + if ($client) { + $shippingAddress->_client = $client; + } + + $shippingAddress->_save(Recurly_Client::POST, $this->uri() . '/shipping_addresses'); + } + protected function uri() { if (!empty($this->_href)) return $this->getHref(); diff --git a/lib/recurly/shipping_address.php b/lib/recurly/shipping_address.php index 4307967d..057e0616 100644 --- a/lib/recurly/shipping_address.php +++ b/lib/recurly/shipping_address.php @@ -13,7 +13,17 @@ protected function getWriteableAttributes() { ); } protected function populateXmlDoc(&$doc, &$node, &$obj, $nested = false) { - $shippingAddressNode= $node->appendChild($doc->createElement($this->getNodeName())); - parent::populateXmlDoc($doc, $shippingAddressNode, $obj); + if ($this->isEmbedded($node)) { + $shippingAddressNode = $node->appendChild($doc->createElement($this->getNodeName())); + parent::populateXmlDoc($doc, $shippingAddressNode, $obj); + } else { + parent::populateXmlDoc($doc, $node, $obj); + } + } + + private function isEmbedded($node) { + $path = explode('/', $node->getNodePath()); + $last = $path[count($path)-1]; + return $last == 'shipping_addresses'; } } From f59bfa72270b2c0e53df95144c7d40ccfda5f9d3 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 15 Dec 2016 15:59:01 -0600 Subject: [PATCH 2/2] Add update() method --- Tests/Recurly/ShippingAddress_Test.php | 17 +++++++++++- .../fixtures/shipping_addresses/index-200.xml | 26 +++++++++++++++++++ .../shipping_addresses/update-200.xml | 24 +++++++++++++++++ lib/recurly/account.php | 1 - lib/recurly/shipping_address.php | 4 +++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 Tests/fixtures/shipping_addresses/index-200.xml create mode 100644 Tests/fixtures/shipping_addresses/update-200.xml diff --git a/Tests/Recurly/ShippingAddress_Test.php b/Tests/Recurly/ShippingAddress_Test.php index b84eed07..ac466e91 100644 --- a/Tests/Recurly/ShippingAddress_Test.php +++ b/Tests/Recurly/ShippingAddress_Test.php @@ -9,7 +9,7 @@ function defaultResponses() { ); } - public function testGetAccount() { + public function testCreateShippingAddressOnExistingAccount() { $account = Recurly_Account::get('abcdef1234567890', $this->client); $this->client->addResponse('POST', 'https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses', 'shipping_addresses/create-201.xml'); @@ -30,4 +30,19 @@ public function testGetAccount() { $this->assertEquals($shad->id, 1234567); } + public function testUpdateShippingAddress() { + $this->client->addResponse('GET', '/accounts/abcdef1234567890/shipping_addresses', 'shipping_addresses/index-200.xml'); + $this->client->addResponse('PUT', 'https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses/1234567', 'shipping_addresses/update-200.xml'); + + $shipping_addresses = Recurly_ShippingAddressList::get('abcdef1234567890', null, $this->client); + + foreach ($shipping_addresses as $shipping_address) { + if ($shipping_address->nickname == 'Home') { + $shipping_address->address1 = "123 NewStreet Ave."; + $shipping_address->update(); + $this->assertEquals($shipping_address->address1, "123 NewStreet Ave."); + } + } + } + } diff --git a/Tests/fixtures/shipping_addresses/index-200.xml b/Tests/fixtures/shipping_addresses/index-200.xml new file mode 100644 index 00000000..1fcfd80c --- /dev/null +++ b/Tests/fixtures/shipping_addresses/index-200.xml @@ -0,0 +1,26 @@ +HTTP/1.1 200 Created +Content-Type: application/xml; charset=utf-8 +Location: https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses + + + + + + 1234567 + Home + Verena + Example + + verena@example.com + + 123 Dolores St. + + San Francisco + CA + 94110 + US + 555-555-5555 + 2016-12-15T21:01:51Z + 2016-12-15T21:01:51Z + + diff --git a/Tests/fixtures/shipping_addresses/update-200.xml b/Tests/fixtures/shipping_addresses/update-200.xml new file mode 100644 index 00000000..c1bd69b0 --- /dev/null +++ b/Tests/fixtures/shipping_addresses/update-200.xml @@ -0,0 +1,24 @@ +HTTP/1.1 200 Updated +Content-Type: application/xml; charset=utf-8 +Location: https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses/1234567 + + + + + 1234567 + Home + Verena + Example + + verena@example.com + + 123 NewStreet Ave. + + San Francisco + CA + 94110 + US + 555-555-5555 + 2016-12-15T21:01:51Z + 2016-12-15T21:01:51Z + diff --git a/lib/recurly/account.php b/lib/recurly/account.php index 3d28f353..739ea2d4 100644 --- a/lib/recurly/account.php +++ b/lib/recurly/account.php @@ -74,7 +74,6 @@ public function createShippingAddress($shippingAddress, $client = null) { if ($client) { $shippingAddress->_client = $client; } - $shippingAddress->_save(Recurly_Client::POST, $this->uri() . '/shipping_addresses'); } diff --git a/lib/recurly/shipping_address.php b/lib/recurly/shipping_address.php index 057e0616..02c9f917 100644 --- a/lib/recurly/shipping_address.php +++ b/lib/recurly/shipping_address.php @@ -2,6 +2,10 @@ class Recurly_ShippingAddress extends Recurly_Resource { + public function update() { + $this->_save(Recurly_Client::PUT, $this->getHref()); + } + protected function getNodeName() { return 'shipping_address'; }