PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/lib/Braintree/lib/Braintree/PaymentMethodGateway.php

https://github.com/strangerstudios/paid-memberships-pro
PHP | 363 lines | 274 code | 24 blank | 65 comment | 57 complexity | 8c731c516be93ed66a0265f95d6162e6 MD5 | raw file
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. /**
  5. * Braintree PaymentMethodGateway module
  6. *
  7. * @package Braintree
  8. * @category Resources
  9. */
  10. /**
  11. * Creates and manages Braintree PaymentMethods
  12. *
  13. * <b>== More information ==</b>
  14. *
  15. *
  16. * @package Braintree
  17. * @category Resources
  18. */
  19. class PaymentMethodGateway
  20. {
  21. private $_gateway;
  22. private $_config;
  23. private $_http;
  24. public function __construct($gateway)
  25. {
  26. $this->_gateway = $gateway;
  27. $this->_config = $gateway->config;
  28. $this->_config->assertHasAccessTokenOrKeys();
  29. $this->_http = new Http($gateway->config);
  30. }
  31. public function create($attribs)
  32. {
  33. Util::verifyKeys(self::createSignature(), $attribs);
  34. return $this->_doCreate('/payment_methods', ['payment_method' => $attribs]);
  35. }
  36. /**
  37. * find a PaymentMethod by token
  38. *
  39. * @param string $token payment method unique id
  40. * @return CreditCard|PayPalAccount
  41. * @throws Exception\NotFound
  42. */
  43. public function find($token)
  44. {
  45. $this->_validateId($token);
  46. try {
  47. $path = $this->_config->merchantPath() . '/payment_methods/any/' . $token;
  48. $response = $this->_http->get($path);
  49. if (isset($response['creditCard'])) {
  50. return CreditCard::factory($response['creditCard']);
  51. } else if (isset($response['paypalAccount'])) {
  52. return PayPalAccount::factory($response['paypalAccount']);
  53. } else if (isset($response['coinbaseAccount'])) {
  54. return CoinbaseAccount::factory($response['coinbaseAccount']);
  55. } else if (isset($response['applePayCard'])) {
  56. return ApplePayCard::factory($response['applePayCard']);
  57. } else if (isset($response['androidPayCard'])) {
  58. return AndroidPayCard::factory($response['androidPayCard']);
  59. } else if (isset($response['amexExpressCheckoutCard'])) {
  60. return AmexExpressCheckoutCard::factory($response['amexExpressCheckoutCard']);
  61. } else if (isset($response['europeBankAccount'])) {
  62. return EuropeBankAccount::factory($response['europeBankAccount']);
  63. } else if (isset($response['usBankAccount'])) {
  64. return UsBankAccount::factory($response['usBankAccount']);
  65. } else if (isset($response['venmoAccount'])) {
  66. return VenmoAccount::factory($response['venmoAccount']);
  67. } else if (isset($response['visaCheckoutCard'])) {
  68. return VisaCheckoutCard::factory($response['visaCheckoutCard']);
  69. } else if (isset($response['masterpassCard'])) {
  70. return MasterpassCard::factory($response['masterpassCard']);
  71. } else if (isset($response['samsungPayCard'])) {
  72. return SamsungPayCard::factory($response['samsungPayCard']);
  73. } else if (is_array($response)) {
  74. return UnknownPaymentMethod::factory($response);
  75. }
  76. } catch (Exception\NotFound $e) {
  77. throw new Exception\NotFound(
  78. 'payment method with token ' . $token . ' not found'
  79. );
  80. }
  81. }
  82. public function update($token, $attribs)
  83. {
  84. Util::verifyKeys(self::updateSignature(), $attribs);
  85. return $this->_doUpdate('/payment_methods/any/' . $token, ['payment_method' => $attribs]);
  86. }
  87. public function delete($token, $options=[])
  88. {
  89. Util::verifyKeys(self::deleteSignature(), $options);
  90. $this->_validateId($token);
  91. $queryString = "";
  92. if (!empty($options)) {
  93. $queryString = "?" . http_build_query(Util::camelCaseToDelimiterArray($options, '_'));
  94. }
  95. return $this->_doDelete('/payment_methods/any/' . $token . $queryString);
  96. }
  97. public function grant($sharedPaymentMethodToken, $attribs=[])
  98. {
  99. if (is_bool($attribs) === true) {
  100. $attribs = ['allow_vaulting' => $attribs];
  101. }
  102. $options = [ 'shared_payment_method_token' => $sharedPaymentMethodToken ];
  103. return $this->_doCreate(
  104. '/payment_methods/grant',
  105. [
  106. 'payment_method' => array_merge($attribs, $options)
  107. ]
  108. );
  109. }
  110. public function revoke($sharedPaymentMethodToken)
  111. {
  112. return $this->_doCreate(
  113. '/payment_methods/revoke',
  114. [
  115. 'payment_method' => [
  116. 'shared_payment_method_token' => $sharedPaymentMethodToken
  117. ]
  118. ]
  119. );
  120. }
  121. private static function baseSignature()
  122. {
  123. $billingAddressSignature = AddressGateway::createSignature();
  124. $optionsSignature = [
  125. 'failOnDuplicatePaymentMethod',
  126. 'makeDefault',
  127. 'verificationMerchantAccountId',
  128. 'verifyCard',
  129. 'verificationAmount',
  130. 'usBankAccountVerificationMethod',
  131. ['paypal' => [
  132. 'payee_email',
  133. 'payeeEmail',
  134. 'order_id',
  135. 'orderId',
  136. 'custom_field',
  137. 'customField',
  138. 'description',
  139. 'amount',
  140. ['shipping' =>
  141. [
  142. 'firstName', 'lastName', 'company', 'countryName',
  143. 'countryCodeAlpha2', 'countryCodeAlpha3', 'countryCodeNumeric',
  144. 'extendedAddress', 'locality', 'postalCode', 'region',
  145. 'streetAddress'],
  146. ],
  147. ]],
  148. ];
  149. return [
  150. 'billingAddressId',
  151. 'cardholderName',
  152. 'cvv',
  153. 'deviceData',
  154. 'expirationDate',
  155. 'expirationMonth',
  156. 'expirationYear',
  157. 'number',
  158. 'paymentMethodNonce',
  159. 'token',
  160. ['options' => $optionsSignature],
  161. ['billingAddress' => $billingAddressSignature]
  162. ];
  163. }
  164. public static function createSignature()
  165. {
  166. $signature = array_merge(self::baseSignature(), ['customerId', 'paypalRefreshToken', 'paypalVaultWithoutUpgrade']);
  167. return $signature;
  168. }
  169. public static function updateSignature()
  170. {
  171. $billingAddressSignature = AddressGateway::updateSignature();
  172. array_push($billingAddressSignature, [
  173. 'options' => [
  174. 'updateExisting'
  175. ]
  176. ]);
  177. $signature = array_merge(self::baseSignature(), [
  178. 'deviceSessionId',
  179. 'venmoSdkPaymentMethodCode',
  180. 'fraudMerchantId',
  181. ['billingAddress' => $billingAddressSignature]
  182. ]);
  183. return $signature;
  184. }
  185. private static function deleteSignature()
  186. {
  187. return ['revokeAllGrants'];
  188. }
  189. /**
  190. * sends the create request to the gateway
  191. *
  192. * @ignore
  193. * @param string $subPath
  194. * @param array $params
  195. * @return mixed
  196. */
  197. public function _doCreate($subPath, $params)
  198. {
  199. $fullPath = $this->_config->merchantPath() . $subPath;
  200. $response = $this->_http->post($fullPath, $params);
  201. return $this->_verifyGatewayResponse($response);
  202. }
  203. /**
  204. * sends the update request to the gateway
  205. *
  206. * @ignore
  207. * @param string $subPath
  208. * @param array $params
  209. * @return mixed
  210. */
  211. public function _doUpdate($subPath, $params)
  212. {
  213. $fullPath = $this->_config->merchantPath() . $subPath;
  214. $response = $this->_http->put($fullPath, $params);
  215. return $this->_verifyGatewayResponse($response);
  216. }
  217. /**
  218. * sends the delete request to the gateway
  219. *
  220. * @ignore
  221. * @param string $subPath
  222. * @return mixed
  223. */
  224. public function _doDelete($subPath)
  225. {
  226. $fullPath = $this->_config->merchantPath() . $subPath;
  227. $this->_http->delete($fullPath);
  228. return new Result\Successful();
  229. }
  230. /**
  231. * generic method for validating incoming gateway responses
  232. *
  233. * creates a new CreditCard or PayPalAccount object
  234. * and encapsulates it inside a Result\Successful object, or
  235. * encapsulates a Errors object inside a Result\Error
  236. * alternatively, throws an Unexpected exception if the response is invalid.
  237. *
  238. * @ignore
  239. * @param array $response gateway response values
  240. * @return Result\Successful|Result\Error
  241. * @throws Exception\Unexpected
  242. */
  243. private function _verifyGatewayResponse($response)
  244. {
  245. if (isset($response['creditCard'])) {
  246. return new Result\Successful(
  247. CreditCard::factory($response['creditCard']),
  248. 'paymentMethod'
  249. );
  250. } else if (isset($response['paypalAccount'])) {
  251. return new Result\Successful(
  252. PayPalAccount::factory($response['paypalAccount']),
  253. "paymentMethod"
  254. );
  255. } else if (isset($response['coinbaseAccount'])) {
  256. return new Result\Successful(
  257. CoinbaseAccount::factory($response['coinbaseAccount']),
  258. "paymentMethod"
  259. );
  260. } else if (isset($response['applePayCard'])) {
  261. return new Result\Successful(
  262. ApplePayCard::factory($response['applePayCard']),
  263. "paymentMethod"
  264. );
  265. } else if (isset($response['androidPayCard'])) {
  266. return new Result\Successful(
  267. AndroidPayCard::factory($response['androidPayCard']),
  268. "paymentMethod"
  269. );
  270. } else if (isset($response['amexExpressCheckoutCard'])) {
  271. return new Result\Successful(
  272. AmexExpressCheckoutCard::factory($response['amexExpressCheckoutCard']),
  273. "paymentMethod"
  274. );
  275. } else if (isset($response['usBankAccount'])) {
  276. return new Result\Successful(
  277. UsBankAccount::factory($response['usBankAccount']),
  278. "paymentMethod"
  279. );
  280. } else if (isset($response['venmoAccount'])) {
  281. return new Result\Successful(
  282. VenmoAccount::factory($response['venmoAccount']),
  283. "paymentMethod"
  284. );
  285. } else if (isset($response['visaCheckoutCard'])) {
  286. return new Result\Successful(
  287. VisaCheckoutCard::factory($response['visaCheckoutCard']),
  288. "paymentMethod"
  289. );
  290. } else if (isset($response['masterpassCard'])) {
  291. return new Result\Successful(
  292. MasterpassCard::factory($response['masterpassCard']),
  293. "paymentMethod"
  294. );
  295. } else if (isset($response['samsungPayCard'])) {
  296. return new Result\Successful(
  297. MasterpassCard::factory($response['samsungPayCard']),
  298. "paymentMethod"
  299. );
  300. } else if (isset($response['paymentMethodNonce'])) {
  301. return new Result\Successful(
  302. PaymentMethodNonce::factory($response['paymentMethodNonce']),
  303. "paymentMethodNonce"
  304. );
  305. } else if (isset($response['apiErrorResponse'])) {
  306. return new Result\Error($response['apiErrorResponse']);
  307. } else if (is_array($response)) {
  308. return new Result\Successful(
  309. UnknownPaymentMethod::factory($response),
  310. "paymentMethod"
  311. );
  312. } else {
  313. throw new Exception\Unexpected(
  314. 'Expected payment method or apiErrorResponse'
  315. );
  316. }
  317. }
  318. /**
  319. * verifies that a valid payment method identifier is being used
  320. * @ignore
  321. * @param string $identifier
  322. * @param Optional $string $identifierType type of identifier supplied, default 'token'
  323. * @throws InvalidArgumentException
  324. */
  325. private function _validateId($identifier = null, $identifierType = 'token')
  326. {
  327. if (empty($identifier)) {
  328. throw new InvalidArgumentException(
  329. 'expected payment method id to be set'
  330. );
  331. }
  332. if (!preg_match('/^[0-9A-Za-z_-]+$/', $identifier)) {
  333. throw new InvalidArgumentException(
  334. $identifier . ' is an invalid payment method ' . $identifierType . '.'
  335. );
  336. }
  337. }
  338. }
  339. class_alias('Braintree\PaymentMethodGateway', 'Braintree_PaymentMethodGateway');