Skip to content

Commit daccf55

Browse files
committed
Merge pull request #186 from recurly/edit_restore_coupons
Support editing and restoring coupons
2 parents abbe4f5 + 1f6e9f0 commit daccf55

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Recurly PHP Client Library CHANGELOG
22

33
* Removed `nestedAttributes` [#191](https://github.com/recurly/recurly-client-php/pull/191)
4+
* Added support for coupon `update` and `restore` actions [#186](https://github.com/recurly/recurly-client-php/pull/186)
45

56
## Version 2.4.6 (September 15th, 2015)
67

Tests/Recurly/Coupon_Test.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
require_once(__DIR__ . '/../test_helpers.php');
43

54
class Recurly_CouponTest extends Recurly_TestCase
@@ -63,6 +62,28 @@ public function testDeleteCoupon() {
6362
Recurly_Coupon::deleteCoupon('special', $this->client);
6463
}
6564

65+
public function testUpdateCoupon() {
66+
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/coupons/special', 'coupons/update-201.xml');
67+
68+
$coupon = Recurly_Coupon::get('special', $this->client);
69+
70+
$this->assertEquals($coupon->name, 'Special 50% Discount');
71+
$coupon->name = 'My New Name';
72+
$coupon->update();
73+
$this->assertEquals($coupon->name, 'My New Name', 'It loads values from the returned XML');
74+
}
75+
76+
public function testRestoreCoupon() {
77+
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/coupons/special/restore', 'coupons/update-201.xml');
78+
79+
$coupon = Recurly_Coupon::get('special', $this->client);
80+
81+
$this->assertEquals($coupon->name, 'Special 50% Discount');
82+
$coupon->name = 'My New Name';
83+
$coupon->restore();
84+
$this->assertEquals($coupon->name, 'My New Name', 'It loads values from the returned XML');
85+
}
86+
6687
// Parse plan_codes array in response
6788
public function testPlanCodesXml() {
6889
$this->client->addResponse('GET', '/coupons/special', 'coupons/show-200-2.xml');
@@ -102,4 +123,26 @@ public function testXmlWithPlans() {
102123
$coupon->xml()
103124
);
104125
}
126+
127+
public function testCreateUpdateXML() {
128+
$coupon = new Recurly_Coupon();
129+
130+
// should ignore these values
131+
$coupon->coupon_code = 'fifteen-off';
132+
$coupon->discount_type = 'dollar';
133+
$coupon->discount_in_cents->addCurrency('USD', 1500);
134+
$coupon->plan_codes = array('gold', 'monthly');
135+
136+
// should serialize these values
137+
$coupon->name = '$15 Off';
138+
$coupon->invoice_description = 'Invoice description';
139+
$coupon->redeem_by_date = '2017-12-01';
140+
$coupon->max_redemptions = 100;
141+
$coupon->max_redemptions_per_account = 3;
142+
143+
$this->assertEquals(
144+
"<?xml version=\"1.0\"?>\n<coupon><name>$15 Off</name><max_redemptions>100</max_redemptions><max_redemptions_per_account>3</max_redemptions_per_account><hosted_description></hosted_description><invoice_description>Invoice description</invoice_description><redeem_by_date>2017-12-01</redeem_by_date></coupon>\n",
145+
$coupon->createUpdateXML()
146+
);
147+
}
105148
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HTTP/1.1 200 OK
2+
Content-Type: application/xml; charset=utf-8
3+
4+
<?xml version="1.0" encoding="UTF-8"?>
5+
<coupon href="https://api.recurly.com/v2/coupons/special">
6+
<redemptions href="https://api.recurly.com/v2/coupons/special/redemptions"/>
7+
<coupon_code>special</coupon_code>
8+
<name>My New Name</name>
9+
<state>redeemable</state>
10+
<description>New Description</description>
11+
<discount_type>dollars</discount_type>
12+
<discount_in_cents>
13+
<USD type="integer">1000</USD>
14+
</discount_in_cents>
15+
<invoice_description>New Invoice Description</invoice_description>
16+
<redeem_by_date type="datetime">2020-05-01T08:00:00Z</redeem_by_date>
17+
<single_use type="boolean">true</single_use>
18+
<applies_for_months type="integer">1</applies_for_months>
19+
<max_redemptions type="integer">99</max_redemptions>
20+
<applies_to_all_plans type="boolean">true</applies_to_all_plans>
21+
<applies_to_non_plan_charges type="boolean">true</applies_to_non_plan_charges>
22+
<redemption_resource>account</redemption_resource>
23+
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
24+
<duration>forever</duration>
25+
<temporal_unit></temporal_unit>
26+
<temporal_amount type="integer"></temporal_amount>
27+
<max_redemptions_per_account type="integer">3</max_redemptions_per_account>
28+
<plan_codes type="array">
29+
</plan_codes>
30+
<a name="redeem" href="https://api.recurly.com/v2/coupons/special/redeem" method="post"/>
31+
</coupon>

lib/recurly/coupon.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Recurly_Coupon extends Recurly_Resource
44
{
55
protected static $_writeableAttributes;
6+
protected static $_updatableAttributes;
67
protected $_redeemUrl;
78

89
function __construct() {
@@ -19,6 +20,9 @@ public static function init()
1920
'hosted_description','invoice_description', 'applies_to_non_plan_charges', 'redemption_resource',
2021
'max_redemptions_per_account'
2122
);
23+
Recurly_Coupon::$_updatableAttributes = array('name', 'max_redemptions',
24+
'max_redemptions_per_account', 'hosted_description', 'invoice_description', 'redeem_by_date'
25+
);
2226
}
2327

2428
public static function get($couponCode, $client = null) {
@@ -47,13 +51,41 @@ public function redeemCoupon($accountCode, $currency, $subscriptionUUID = null)
4751
}
4852
}
4953

54+
public function update() {
55+
$this->_save(Recurly_Client::PUT, $this->uri(), $this->createUpdateXML());
56+
}
57+
58+
public function restore() {
59+
$this->_save(Recurly_Client::PUT, $this->uri() . '/restore', $this->createUpdateXML());
60+
}
61+
5062
public function delete() {
5163
return Recurly_Base::_delete($this->uri(), $this->_client);
5264
}
5365
public static function deleteCoupon($couponCode, $client = null) {
5466
return Recurly_Base::_delete(Recurly_Coupon::uriForCoupon($couponCode), $client);
5567
}
5668

69+
// generates the xml needed for a coupon update
70+
// only uses the updateable attributes
71+
public function createUpdateXML() {
72+
$doc = $this->createDocument();
73+
74+
$root = $doc->appendChild($doc->createElement($this->getNodeName()));
75+
76+
foreach ($this->getUpdatableAttributes() as $attr) {
77+
$val = $this->$attr;
78+
79+
if ($val instanceof DateTime) {
80+
$val = $val->format('c');
81+
}
82+
83+
$root->appendChild($doc->createElement($attr, $val));
84+
}
85+
86+
return $this->renderXML($doc);
87+
}
88+
5789
protected function uri() {
5890
if (!empty($this->_href))
5991
return $this->getHref();
@@ -70,6 +102,9 @@ protected function getNodeName() {
70102
protected function getWriteableAttributes() {
71103
return Recurly_Coupon::$_writeableAttributes;
72104
}
105+
protected function getUpdatableAttributes() {
106+
return Recurly_Coupon::$_updatableAttributes;
107+
}
73108
protected function getRequiredAttributes() {
74109
return array();
75110
}

lib/recurly/resource.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,17 @@ public function setValues($values) {
7373
return $this;
7474
}
7575

76-
protected function _save($method, $uri)
76+
protected function _save($method, $uri, $data = null)
7777
{
7878
$this->_errors = array(); // reset errors
7979

80-
$response = $this->_client->request($method, $uri, $this->xml());
80+
if (is_null($data)) {
81+
$data = $this->xml();
82+
}
83+
84+
$response = $this->_client->request($method, $uri, $data);
8185
$response->assertValidResponse();
86+
8287
if (isset($response->body)) {
8388
Recurly_Resource::__parseXmlToUpdateObject($response->body);
8489
}

0 commit comments

Comments
 (0)