BraintreePaymentMethodGateway

Creates and manages Braintree PaymentMethods.

Defined (1)

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

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