BraintreeSubscriptionGateway

Braintree SubscriptionGateway module.

Defined (1)

The class is defined in the following location(s).

/includes/lib/Braintree/lib/Braintree/SubscriptionGateway.php  
  1. class SubscriptionGateway 
  2. private $_gateway; 
  3. private $_config; 
  4. private $_http; 
  5.  
  6. public function __construct($gateway) 
  7. $this->_gateway = $gateway; 
  8. $this->_config = $gateway->config; 
  9. $this->_config->assertHasAccessTokenOrKeys(); 
  10. $this->_http = new Http($gateway->config); 
  11.  
  12. public function create($attributes) 
  13. Util::verifyKeys(self::_createSignature(), $attributes); 
  14. $path = $this->_config->merchantPath() . '/subscriptions'; 
  15. $response = $this->_http->post($path, ['subscription' => $attributes]); 
  16. return $this->_verifyGatewayResponse($response); 
  17.  
  18. public function find($id) 
  19. $this->_validateId($id); 
  20.  
  21. try { 
  22. $path = $this->_config->merchantPath() . '/subscriptions/' . $id; 
  23. $response = $this->_http->get($path); 
  24. return Subscription::factory($response['subscription']); 
  25. } catch (Exception\NotFound $e) { 
  26. throw new Exception\NotFound('subscription with id ' . $id . ' not found'); 
  27.  
  28.  
  29. public function search($query) 
  30. $criteria = []; 
  31. foreach ($query as $term) { 
  32. $criteria[$term->name] = $term->toparam(); 
  33.  
  34.  
  35. $path = $this->_config->merchantPath() . '/subscriptions/advanced_search_ids'; 
  36. $response = $this->_http->post($path, ['search' => $criteria]); 
  37. $pager = [ 
  38. 'object' => $this,  
  39. 'method' => 'fetch',  
  40. 'methodArgs' => [$query] 
  41. ]; 
  42.  
  43. return new ResourceCollection($response, $pager); 
  44.  
  45. public function fetch($query, $ids) 
  46. $criteria = []; 
  47. foreach ($query as $term) { 
  48. $criteria[$term->name] = $term->toparam(); 
  49. $criteria["ids"] = SubscriptionSearch::ids()->in($ids)->toparam(); 
  50. $path = $this->_config->merchantPath() . '/subscriptions/advanced_search'; 
  51. $response = $this->_http->post($path, ['search' => $criteria]); 
  52.  
  53. return Util::extractAttributeAsArray( 
  54. $response['subscriptions'],  
  55. 'subscription' 
  56. ); 
  57.  
  58. public function update($subscriptionId, $attributes) 
  59. Util::verifyKeys(self::_updateSignature(), $attributes); 
  60. $path = $this->_config->merchantPath() . '/subscriptions/' . $subscriptionId; 
  61. $response = $this->_http->put($path, ['subscription' => $attributes]); 
  62. return $this->_verifyGatewayResponse($response); 
  63.  
  64. public function retryCharge($subscriptionId, $amount = null) 
  65. $transaction_params = ['type' => Transaction::SALE,  
  66. 'subscriptionId' => $subscriptionId]; 
  67. if (isset($amount)) { 
  68. $transaction_params['amount'] = $amount; 
  69.  
  70. $path = $this->_config->merchantPath() . '/transactions'; 
  71. $response = $this->_http->post($path, ['transaction' => $transaction_params]); 
  72. return $this->_verifyGatewayResponse($response); 
  73.  
  74. public function cancel($subscriptionId) 
  75. $path = $this->_config->merchantPath() . '/subscriptions/' . $subscriptionId . '/cancel'; 
  76. $response = $this->_http->put($path); 
  77. return $this->_verifyGatewayResponse($response); 
  78.  
  79. private static function _createSignature() 
  80. return array_merge( 
  81. 'billingDayOfMonth',  
  82. 'firstBillingDate',  
  83. 'createdAt',  
  84. 'updatedAt',  
  85. 'id',  
  86. 'merchantAccountId',  
  87. 'neverExpires',  
  88. 'numberOfBillingCycles',  
  89. 'paymentMethodToken',  
  90. 'paymentMethodNonce',  
  91. 'planId',  
  92. 'price',  
  93. 'trialDuration',  
  94. 'trialDurationUnit',  
  95. 'trialPeriod',  
  96. ['descriptor' => ['name', 'phone', 'url']],  
  97. ['options' => ['doNotInheritAddOnsOrDiscounts', 'startImmediately']],  
  98. ],  
  99. self::_addOnDiscountSignature() 
  100. ); 
  101.  
  102. private static function _updateSignature() 
  103. return array_merge( 
  104. 'merchantAccountId', 'numberOfBillingCycles', 'paymentMethodToken', 'planId',  
  105. 'paymentMethodNonce', 'id', 'neverExpires', 'price',  
  106. ['descriptor' => ['name', 'phone', 'url']],  
  107. ['options' => ['prorateCharges', 'replaceAllAddOnsAndDiscounts', 'revertSubscriptionOnProrationFailure']],  
  108. ],  
  109. self::_addOnDiscountSignature() 
  110. ); 
  111.  
  112. private static function _addOnDiscountSignature() 
  113. return [ 
  114. 'addOns' => [ 
  115. ['add' => ['amount', 'inheritedFromId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],  
  116. ['update' => ['amount', 'existingId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],  
  117. ['remove' => ['_anyKey_']],  
  118. ],  
  119. 'discounts' => [ 
  120. ['add' => ['amount', 'inheritedFromId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],  
  121. ['update' => ['amount', 'existingId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],  
  122. ['remove' => ['_anyKey_']],  
  123. ]; 
  124.  
  125. /** 
  126. * @ignore 
  127. */ 
  128. private function _validateId($id = null) { 
  129. if (empty($id)) { 
  130. throw new InvalidArgumentException( 
  131. 'expected subscription id to be set' 
  132. ); 
  133. if (!preg_match('/^[0-9A-Za-z_-]+$/', $id)) { 
  134. throw new InvalidArgumentException( 
  135. $id . ' is an invalid subscription id.' 
  136. ); 
  137.  
  138. /** 
  139. * @ignore 
  140. */ 
  141. private function _verifyGatewayResponse($response) 
  142. if (isset($response['subscription'])) { 
  143. return new Result\Successful( 
  144. Subscription::factory($response['subscription']) 
  145. ); 
  146. } else if (isset($response['transaction'])) { 
  147. // return a populated instance of Transaction, for subscription retryCharge 
  148. return new Result\Successful( 
  149. Transaction::factory($response['transaction']) 
  150. ); 
  151. } else if (isset($response['apiErrorResponse'])) { 
  152. return new Result\Error($response['apiErrorResponse']); 
  153. } else { 
  154. throw new Exception\Unexpected( 
  155. "Expected subscription, transaction, or apiErrorResponse" 
  156. );