Skip to content

Commit

Permalink
Merge pull request #289 from recurly/create_shipping_address_on_exist…
Browse files Browse the repository at this point in the history
…ing_account

Create shipping addresses on existing accounts and allow updates
  • Loading branch information
drewish authored Jan 9, 2017
2 parents a3b7a68 + f59bfa7 commit 706163b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
48 changes: 48 additions & 0 deletions Tests/Recurly/ShippingAddress_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php


class Recurly_ShippingAddressTest extends Recurly_TestCase
{
function defaultResponses() {
return array(
array('GET', '/accounts/abcdef1234567890', 'accounts/show-200.xml')
);
}

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');

$shad = new Recurly_ShippingAddress();
$shad->nickname = "Home";
$shad->first_name = "Verena";
$shad->last_name = "Example";
$shad->phone = "555-555-5555";
$shad->email = "[email protected]";
$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);
}

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.");
}
}
}

}
24 changes: 24 additions & 0 deletions Tests/fixtures/shipping_addresses/create-201.xml
Original file line number Diff line number Diff line change
@@ -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

<?xml version="1.0" encoding="UTF-8"?>
<shipping_address href="https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses/1234567">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890" />
<id type="integer">1234567</id>
<nickname>Home</nickname>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<email>[email protected]</email>
<vat_number nil="nil"></vat_number>
<address1>123 Dolores St.</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94110</zip>
<country>US</country>
<phone>555-555-5555</phone>
<created_at type="datetime">2016-12-15T21:01:51Z</created_at>
<updated_at type="datetime">2016-12-15T21:01:51Z</updated_at>
</shipping_address>
26 changes: 26 additions & 0 deletions Tests/fixtures/shipping_addresses/index-200.xml
Original file line number Diff line number Diff line change
@@ -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

<?xml version="1.0" encoding="UTF-8"?>
<shipping_addresses type="array">
<shipping_address href="https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses/1234567">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890" />
<id type="integer">1234567</id>
<nickname>Home</nickname>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<email>[email protected]</email>
<vat_number nil="nil"></vat_number>
<address1>123 Dolores St.</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94110</zip>
<country>US</country>
<phone>555-555-5555</phone>
<created_at type="datetime">2016-12-15T21:01:51Z</created_at>
<updated_at type="datetime">2016-12-15T21:01:51Z</updated_at>
</shipping_address>
</shipping_addresses>
24 changes: 24 additions & 0 deletions Tests/fixtures/shipping_addresses/update-200.xml
Original file line number Diff line number Diff line change
@@ -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

<?xml version="1.0" encoding="UTF-8"?>
<shipping_address href="https://api.recurly.com/v2/accounts/abcdef1234567890/shipping_addresses/1234567">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890" />
<id type="integer">1234567</id>
<nickname>Home</nickname>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company nil="nil"></company>
<email>[email protected]</email>
<vat_number nil="nil"></vat_number>
<address1>123 NewStreet Ave.</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>94110</zip>
<country>US</country>
<phone>555-555-5555</phone>
<created_at type="datetime">2016-12-15T21:01:51Z</created_at>
<updated_at type="datetime">2016-12-15T21:01:51Z</updated_at>
</shipping_address>
7 changes: 7 additions & 0 deletions lib/recurly/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ 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();
Expand Down
18 changes: 16 additions & 2 deletions lib/recurly/shipping_address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -13,7 +17,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';
}
}

0 comments on commit 706163b

Please sign in to comment.