BraintreePaymentMethodGateway

Creates and manages Braintree PaymentMethods.

Defined (1)

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

/classes/lib/Braintree/Braintree/PaymentMethodGateway.php  
  1. class PaymentMethodGateway 
  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.  
  13. public function create($attribs) 
  14. Util::verifyKeys(self::createSignature(), $attribs); 
  15. return $this->_doCreate('/payment_methods', ['payment_method' => $attribs]); 
  16.  
  17. /** 
  18. * find a PaymentMethod by token 
  19. * @param string $token payment method unique id 
  20. * @return CreditCard|PayPalAccount 
  21. * @throws Exception\NotFound 
  22. */ 
  23. public function find($token) 
  24. $this->_validateId($token); 
  25. try { 
  26. $path = $this->_config->merchantPath() . '/payment_methods/any/' . $token; 
  27. $response = $this->_http->get($path); 
  28. if (isset($response['creditCard'])) { 
  29. return CreditCard::factory($response['creditCard']); 
  30. } else if (isset($response['paypalAccount'])) { 
  31. return PayPalAccount::factory($response['paypalAccount']); 
  32. } else if (isset($response['coinbaseAccount'])) { 
  33. return CoinbaseAccount::factory($response['coinbaseAccount']); 
  34. } else if (isset($response['applePayCard'])) { 
  35. return ApplePayCard::factory($response['applePayCard']); 
  36. } else if (isset($response['androidPayCard'])) { 
  37. return AndroidPayCard::factory($response['androidPayCard']); 
  38. } else if (isset($response['amexExpressCheckoutCard'])) { 
  39. return AmexExpressCheckoutCard::factory($response['amexExpressCheckoutCard']); 
  40. } else if (isset($response['europeBankAccount'])) { 
  41. return EuropeBankAccount::factory($response['europeBankAccount']); 
  42. } else if (isset($response['venmoAccount'])) { 
  43. return VenmoAccount::factory($response['venmoAccount']); 
  44. } else if (is_array($response)) { 
  45. return UnknownPaymentMethod::factory($response); 
  46. } catch (Exception\NotFound $e) { 
  47. throw new Exception\NotFound( 
  48. 'payment method with token ' . $token . ' not found' 
  49. ); 
  50.  
  51. public function update($token, $attribs) 
  52. Util::verifyKeys(self::updateSignature(), $attribs); 
  53. return $this->_doUpdate('/payment_methods/any/' . $token, ['payment_method' => $attribs]); 
  54.  
  55. public function delete($token) 
  56. $this->_validateId($token); 
  57. $path = $this->_config->merchantPath() . '/payment_methods/any/' . $token; 
  58. $this->_http->delete($path); 
  59. return new Result\Successful(); 
  60.  
  61. public function grant($sharedPaymentMethodToken, $allowVaulting) 
  62. return $this->_doCreate( 
  63. '/payment_methods/grant',  
  64. 'payment_method' => [ 
  65. 'shared_payment_method_token' => $sharedPaymentMethodToken,  
  66. 'allow_vaulting' => $allowVaulting 
  67. ); 
  68.  
  69. public function revoke($sharedPaymentMethodToken) 
  70. return $this->_doCreate( 
  71. '/payment_methods/revoke',  
  72. 'payment_method' => [ 
  73. 'shared_payment_method_token' => $sharedPaymentMethodToken 
  74. ); 
  75.  
  76. private static function baseSignature() 
  77. $billingAddressSignature = AddressGateway::createSignature(); 
  78. $optionsSignature = [ 
  79. 'failOnDuplicatePaymentMethod',  
  80. 'makeDefault',  
  81. 'verificationMerchantAccountId',  
  82. 'verifyCard',  
  83. 'verificationAmount' 
  84. ]; 
  85. return [ 
  86. 'billingAddressId',  
  87. 'cardholderName',  
  88. 'cvv',  
  89. 'deviceData',  
  90. 'expirationDate',  
  91. 'expirationMonth',  
  92. 'expirationYear',  
  93. 'number',  
  94. 'paymentMethodNonce',  
  95. 'token',  
  96. ['options' => $optionsSignature],  
  97. ['billingAddress' => $billingAddressSignature] 
  98. ]; 
  99.  
  100. public static function createSignature() 
  101. $signature = array_merge(self::baseSignature(), ['customerId']); 
  102. return $signature; 
  103.  
  104. public static function updateSignature() 
  105. $billingAddressSignature = AddressGateway::updateSignature(); 
  106. array_push($billingAddressSignature, [ 
  107. 'options' => [ 
  108. 'updateExisting' 
  109. ]); 
  110. $signature = array_merge(self::baseSignature(), [ 
  111. 'deviceSessionId',  
  112. 'venmoSdkPaymentMethodCode',  
  113. 'fraudMerchantId',  
  114. ['billingAddress' => $billingAddressSignature] 
  115. ]); 
  116. return $signature; 
  117.  
  118. /** 
  119. * sends the create request to the gateway 
  120. * @ignore 
  121. * @param string $subPath 
  122. * @param array $params 
  123. * @return mixed 
  124. */ 
  125. public function _doCreate($subPath, $params) 
  126. $fullPath = $this->_config->merchantPath() . $subPath; 
  127. $response = $this->_http->post($fullPath, $params); 
  128.  
  129. return $this->_verifyGatewayResponse($response); 
  130.  
  131. /** 
  132. * sends the update request to the gateway 
  133. * @ignore 
  134. * @param string $subPath 
  135. * @param array $params 
  136. * @return mixed 
  137. */ 
  138. public function _doUpdate($subPath, $params) 
  139. $fullPath = $this->_config->merchantPath() . $subPath; 
  140. $response = $this->_http->put($fullPath, $params); 
  141.  
  142. return $this->_verifyGatewayResponse($response); 
  143.  
  144. /** 
  145. * generic method for validating incoming gateway responses 
  146. * creates a new CreditCard or PayPalAccount object 
  147. * and encapsulates it inside a Result\Successful object, or 
  148. * encapsulates a Errors object inside a Result\Error 
  149. * alternatively, throws an Unexpected exception if the response is invalid. 
  150. * @ignore 
  151. * @param array $response gateway response values 
  152. * @return Result\Successful|Result\Error 
  153. * @throws Exception\Unexpected 
  154. */ 
  155. private function _verifyGatewayResponse($response) 
  156. if (isset($response['creditCard'])) { 
  157. return new Result\Successful( 
  158. CreditCard::factory($response['creditCard']),  
  159. 'paymentMethod' 
  160. ); 
  161. } else if (isset($response['paypalAccount'])) { 
  162. return new Result\Successful( 
  163. PayPalAccount::factory($response['paypalAccount']),  
  164. "paymentMethod" 
  165. ); 
  166. } else if (isset($response['coinbaseAccount'])) { 
  167. return new Result\Successful( 
  168. CoinbaseAccount::factory($response['coinbaseAccount']),  
  169. "paymentMethod" 
  170. ); 
  171. } else if (isset($response['applePayCard'])) { 
  172. return new Result\Successful( 
  173. ApplePayCard::factory($response['applePayCard']),  
  174. "paymentMethod" 
  175. ); 
  176. } else if (isset($response['androidPayCard'])) { 
  177. return new Result\Successful( 
  178. AndroidPayCard::factory($response['androidPayCard']),  
  179. "paymentMethod" 
  180. ); 
  181. } else if (isset($response['amexExpressCheckoutCard'])) { 
  182. return new Result\Successful( 
  183. AmexExpressCheckoutCard::factory($response['amexExpressCheckoutCard']),  
  184. "paymentMethod" 
  185. ); 
  186. } else if (isset($response['europeBankAccount'])) { 
  187. return new Result\Successful( 
  188. EuropeBankAccount::factory($response['europeBankAccount']),  
  189. "paymentMethod" 
  190. ); 
  191. } else if (isset($response['venmoAccount'])) { 
  192. return new Result\Successful( 
  193. VenmoAccount::factory($response['venmoAccount']),  
  194. "paymentMethod" 
  195. ); 
  196. } else if (isset($response['paymentMethodNonce'])) { 
  197. return new Result\Successful( 
  198. PaymentMethodNonce::factory($response['paymentMethodNonce']),  
  199. "paymentMethodNonce" 
  200. ); 
  201. } else if (isset($response['apiErrorResponse'])) { 
  202. return new Result\Error($response['apiErrorResponse']); 
  203. } else if (is_array($response)) { 
  204. return new Result\Successful( 
  205. UnknownPaymentMethod::factory($response),  
  206. "paymentMethod" 
  207. ); 
  208. } else { 
  209. throw new Exception\Unexpected( 
  210. 'Expected payment method or apiErrorResponse' 
  211. ); 
  212.  
  213. /** 
  214. * verifies that a valid payment method identifier is being used 
  215. * @ignore 
  216. * @param string $identifier 
  217. * @param Optional $string $identifierType type of identifier supplied, default 'token' 
  218. * @throws InvalidArgumentException 
  219. */ 
  220. private function _validateId($identifier = null, $identifierType = 'token') 
  221. if (empty($identifier)) { 
  222. throw new InvalidArgumentException( 
  223. 'expected payment method id to be set' 
  224. ); 
  225. if (!preg_match('/^[0-9A-Za-z_-]+$/', $identifier)) { 
  226. throw new InvalidArgumentException( 
  227. $identifier . ' is an invalid payment method ' . $identifierType . '.' 
  228. );