33class 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 }
0 commit comments