CreditCardGateway.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. /**
  5. * Braintree CreditCardGateway module
  6. * Creates and manages Braintree CreditCards
  7. *
  8. * <b>== More information ==</b>
  9. *
  10. * For more detailed information on CreditCards, see {@link https://developers.braintreepayments.com/reference/response/credit-card/php https://developers.braintreepayments.com/reference/response/credit-card/php}<br />
  11. * For more detailed information on CreditCard verifications, see {@link https://developers.braintreepayments.com/reference/response/credit-card-verification/php https://developers.braintreepayments.com/reference/response/credit-card-verification/php}
  12. *
  13. * @package Braintree
  14. * @category Resources
  15. */
  16. class CreditCardGateway
  17. {
  18. private $_gateway;
  19. private $_config;
  20. private $_http;
  21. public function __construct($gateway)
  22. {
  23. $this->_gateway = $gateway;
  24. $this->_config = $gateway->config;
  25. $this->_config->assertHasAccessTokenOrKeys();
  26. $this->_http = new Http($gateway->config);
  27. }
  28. public function create($attribs)
  29. {
  30. Util::verifyKeys(self::createSignature(), $attribs);
  31. return $this->_doCreate('/payment_methods', ['credit_card' => $attribs]);
  32. }
  33. /**
  34. * attempts the create operation assuming all data will validate
  35. * returns a CreditCard object instead of a Result
  36. *
  37. * @access public
  38. * @param array $attribs
  39. * @return CreditCard
  40. * @throws Exception\ValidationError
  41. */
  42. public function createNoValidate($attribs)
  43. {
  44. $result = $this->create($attribs);
  45. return Util::returnObjectOrThrowException(__CLASS__, $result);
  46. }
  47. /**
  48. * create a customer from a TransparentRedirect operation
  49. *
  50. * @deprecated since version 2.3.0
  51. * @access public
  52. * @param array $attribs
  53. * @return Result\Successful|Result\Error
  54. */
  55. public function createFromTransparentRedirect($queryString)
  56. {
  57. trigger_error("DEPRECATED: Please use TransparentRedirectRequest::confirm", E_USER_NOTICE);
  58. $params = TransparentRedirect::parseAndValidateQueryString(
  59. $queryString
  60. );
  61. return $this->_doCreate(
  62. '/payment_methods/all/confirm_transparent_redirect_request',
  63. ['id' => $params['id']]
  64. );
  65. }
  66. /**
  67. *
  68. * @deprecated since version 2.3.0
  69. * @access public
  70. * @param none
  71. * @return string
  72. */
  73. public function createCreditCardUrl()
  74. {
  75. trigger_error("DEPRECATED: Please use TransparentRedirectRequest::url", E_USER_NOTICE);
  76. return $this->_config->baseUrl() . $this->_config->merchantPath().
  77. '/payment_methods/all/create_via_transparent_redirect_request';
  78. }
  79. /**
  80. * returns a ResourceCollection of expired credit cards
  81. * @return ResourceCollection
  82. */
  83. public function expired()
  84. {
  85. $path = $this->_config->merchantPath() . '/payment_methods/all/expired_ids';
  86. $response = $this->_http->post($path);
  87. $pager = [
  88. 'object' => $this,
  89. 'method' => 'fetchExpired',
  90. 'methodArgs' => []
  91. ];
  92. return new ResourceCollection($response, $pager);
  93. }
  94. public function fetchExpired($ids)
  95. {
  96. $path = $this->_config->merchantPath() . "/payment_methods/all/expired";
  97. $response = $this->_http->post($path, ['search' => ['ids' => $ids]]);
  98. return Util::extractattributeasarray(
  99. $response['paymentMethods'],
  100. 'creditCard'
  101. );
  102. }
  103. /**
  104. * returns a ResourceCollection of credit cards expiring between start/end
  105. *
  106. * @return ResourceCollection
  107. */
  108. public function expiringBetween($startDate, $endDate)
  109. {
  110. $queryPath = $this->_config->merchantPath() . '/payment_methods/all/expiring_ids?start=' . date('mY', $startDate) . '&end=' . date('mY', $endDate);
  111. $response = $this->_http->post($queryPath);
  112. $pager = [
  113. 'object' => $this,
  114. 'method' => 'fetchExpiring',
  115. 'methodArgs' => [$startDate, $endDate]
  116. ];
  117. return new ResourceCollection($response, $pager);
  118. }
  119. public function fetchExpiring($startDate, $endDate, $ids)
  120. {
  121. $queryPath = $this->_config->merchantPath() . '/payment_methods/all/expiring?start=' . date('mY', $startDate) . '&end=' . date('mY', $endDate);
  122. $response = $this->_http->post($queryPath, ['search' => ['ids' => $ids]]);
  123. return Util::extractAttributeAsArray(
  124. $response['paymentMethods'],
  125. 'creditCard'
  126. );
  127. }
  128. /**
  129. * find a creditcard by token
  130. *
  131. * @access public
  132. * @param string $token credit card unique id
  133. * @return CreditCard
  134. * @throws Exception\NotFound
  135. */
  136. public function find($token)
  137. {
  138. $this->_validateId($token);
  139. try {
  140. $path = $this->_config->merchantPath() . '/payment_methods/credit_card/' . $token;
  141. $response = $this->_http->get($path);
  142. return CreditCard::factory($response['creditCard']);
  143. } catch (Exception\NotFound $e) {
  144. throw new Exception\NotFound(
  145. 'credit card with token ' . $token . ' not found'
  146. );
  147. }
  148. }
  149. /**
  150. * Convert a payment method nonce to a credit card
  151. *
  152. * @access public
  153. * @param string $nonce payment method nonce
  154. * @return CreditCard
  155. * @throws Exception\NotFound
  156. */
  157. public function fromNonce($nonce)
  158. {
  159. $this->_validateId($nonce, "nonce");
  160. try {
  161. $path = $this->_config->merchantPath() . '/payment_methods/from_nonce/' . $nonce;
  162. $response = $this->_http->get($path);
  163. return CreditCard::factory($response['creditCard']);
  164. } catch (Exception\NotFound $e) {
  165. throw new Exception\NotFound(
  166. 'credit card with nonce ' . $nonce . ' locked, consumed or not found'
  167. );
  168. }
  169. }
  170. /**
  171. * create a credit on the card for the passed transaction
  172. *
  173. * @access public
  174. * @param array $attribs
  175. * @return Result\Successful|Result\Error
  176. */
  177. public function credit($token, $transactionAttribs)
  178. {
  179. $this->_validateId($token);
  180. return Transaction::credit(
  181. array_merge(
  182. $transactionAttribs,
  183. ['paymentMethodToken' => $token]
  184. )
  185. );
  186. }
  187. /**
  188. * create a credit on this card, assuming validations will pass
  189. *
  190. * returns a Transaction object on success
  191. *
  192. * @access public
  193. * @param array $attribs
  194. * @return Transaction
  195. * @throws Exception\ValidationError
  196. */
  197. public function creditNoValidate($token, $transactionAttribs)
  198. {
  199. $result = $this->credit($token, $transactionAttribs);
  200. return Util::returnObjectOrThrowException('Braintree\Transaction', $result);
  201. }
  202. /**
  203. * create a new sale for the current card
  204. *
  205. * @param string $token
  206. * @param array $transactionAttribs
  207. * @return Result\Successful|Result\Error
  208. * @see Transaction::sale()
  209. */
  210. public function sale($token, $transactionAttribs)
  211. {
  212. $this->_validateId($token);
  213. return Transaction::sale(
  214. array_merge(
  215. $transactionAttribs,
  216. ['paymentMethodToken' => $token]
  217. )
  218. );
  219. }
  220. /**
  221. * create a new sale using this card, assuming validations will pass
  222. *
  223. * returns a Transaction object on success
  224. *
  225. * @access public
  226. * @param array $transactionAttribs
  227. * @param string $token
  228. * @return Transaction
  229. * @throws Exception\ValidationsFailed
  230. * @see Transaction::sale()
  231. */
  232. public function saleNoValidate($token, $transactionAttribs)
  233. {
  234. $result = $this->sale($token, $transactionAttribs);
  235. return Util::returnObjectOrThrowException('Braintree\Transaction', $result);
  236. }
  237. /**
  238. * updates the creditcard record
  239. *
  240. * if calling this method in context, $token
  241. * is the 2nd attribute. $token is not sent in object context.
  242. *
  243. * @access public
  244. * @param array $attributes
  245. * @param string $token (optional)
  246. * @return Result\Successful|Result\Error
  247. */
  248. public function update($token, $attributes)
  249. {
  250. Util::verifyKeys(self::updateSignature(), $attributes);
  251. $this->_validateId($token);
  252. return $this->_doUpdate('put', '/payment_methods/credit_card/' . $token, ['creditCard' => $attributes]);
  253. }
  254. /**
  255. * update a creditcard record, assuming validations will pass
  256. *
  257. * if calling this method in context, $token
  258. * is the 2nd attribute. $token is not sent in object context.
  259. * returns a CreditCard object on success
  260. *
  261. * @access public
  262. * @param array $attributes
  263. * @param string $token
  264. * @return CreditCard
  265. * @throws Exception\ValidationsFailed
  266. */
  267. public function updateNoValidate($token, $attributes)
  268. {
  269. $result = $this->update($token, $attributes);
  270. return Util::returnObjectOrThrowException(__CLASS__, $result);
  271. }
  272. /**
  273. *
  274. * @access public
  275. * @param none
  276. * @return string
  277. */
  278. public function updateCreditCardUrl()
  279. {
  280. trigger_error("DEPRECATED: Please use TransparentRedirectRequest::url", E_USER_NOTICE);
  281. return $this->_config->baseUrl() . $this->_config->merchantPath() .
  282. '/payment_methods/all/update_via_transparent_redirect_request';
  283. }
  284. /**
  285. * update a customer from a TransparentRedirect operation
  286. *
  287. * @deprecated since version 2.3.0
  288. * @access public
  289. * @param array $attribs
  290. * @return object
  291. */
  292. public function updateFromTransparentRedirect($queryString)
  293. {
  294. trigger_error("DEPRECATED: Please use TransparentRedirectRequest::confirm", E_USER_NOTICE);
  295. $params = TransparentRedirect::parseAndValidateQueryString(
  296. $queryString
  297. );
  298. return $this->_doUpdate(
  299. 'post',
  300. '/payment_methods/all/confirm_transparent_redirect_request',
  301. ['id' => $params['id']]
  302. );
  303. }
  304. public function delete($token)
  305. {
  306. $this->_validateId($token);
  307. $path = $this->_config->merchantPath() . '/payment_methods/credit_card/' . $token;
  308. $this->_http->delete($path);
  309. return new Result\Successful();
  310. }
  311. private static function baseOptions()
  312. {
  313. return ['makeDefault', 'verificationMerchantAccountId', 'verifyCard', 'verificationAmount', 'venmoSdkSession'];
  314. }
  315. private static function baseSignature($options)
  316. {
  317. return [
  318. 'billingAddressId', 'cardholderName', 'cvv', 'number', 'deviceSessionId',
  319. 'expirationDate', 'expirationMonth', 'expirationYear', 'token', 'venmoSdkPaymentMethodCode',
  320. 'deviceData', 'fraudMerchantId', 'paymentMethodNonce',
  321. ['options' => $options],
  322. [
  323. 'billingAddress' => self::billingAddressSignature()
  324. ],
  325. ];
  326. }
  327. public static function billingAddressSignature()
  328. {
  329. return [
  330. 'firstName',
  331. 'lastName',
  332. 'company',
  333. 'countryCodeAlpha2',
  334. 'countryCodeAlpha3',
  335. 'countryCodeNumeric',
  336. 'countryName',
  337. 'extendedAddress',
  338. 'locality',
  339. 'region',
  340. 'postalCode',
  341. 'streetAddress'
  342. ];
  343. }
  344. public static function createSignature()
  345. {
  346. $options = self::baseOptions();
  347. $options[] = "failOnDuplicatePaymentMethod";
  348. $signature = self::baseSignature($options);
  349. $signature[] = 'customerId';
  350. return $signature;
  351. }
  352. public static function updateSignature()
  353. {
  354. $options = self::baseOptions();
  355. $options[] = "failOnDuplicatePaymentMethod";
  356. $signature = self::baseSignature($options);
  357. $updateExistingBillingSignature = [
  358. [
  359. 'options' => [
  360. 'updateExisting'
  361. ]
  362. ]
  363. ];
  364. foreach($signature AS $key => $value) {
  365. if(is_array($value) and array_key_exists('billingAddress', $value)) {
  366. $signature[$key]['billingAddress'] = array_merge_recursive($value['billingAddress'], $updateExistingBillingSignature);
  367. }
  368. }
  369. return $signature;
  370. }
  371. /**
  372. * sends the create request to the gateway
  373. *
  374. * @ignore
  375. * @param string $subPath
  376. * @param array $params
  377. * @return mixed
  378. */
  379. public function _doCreate($subPath, $params)
  380. {
  381. $fullPath = $this->_config->merchantPath() . $subPath;
  382. $response = $this->_http->post($fullPath, $params);
  383. return $this->_verifyGatewayResponse($response);
  384. }
  385. /**
  386. * verifies that a valid credit card identifier is being used
  387. * @ignore
  388. * @param string $identifier
  389. * @param Optional $string $identifierType type of identifier supplied, default "token"
  390. * @throws InvalidArgumentException
  391. */
  392. private function _validateId($identifier = null, $identifierType = "token")
  393. {
  394. if (empty($identifier)) {
  395. throw new InvalidArgumentException(
  396. 'expected credit card id to be set'
  397. );
  398. }
  399. if (!preg_match('/^[0-9A-Za-z_-]+$/', $identifier)) {
  400. throw new InvalidArgumentException(
  401. $identifier . ' is an invalid credit card ' . $identifierType . '.'
  402. );
  403. }
  404. }
  405. /**
  406. * sends the update request to the gateway
  407. *
  408. * @ignore
  409. * @param string $url
  410. * @param array $params
  411. * @return mixed
  412. */
  413. private function _doUpdate($httpVerb, $subPath, $params)
  414. {
  415. $fullPath = $this->_config->merchantPath() . $subPath;
  416. $response = $this->_http->$httpVerb($fullPath, $params);
  417. return $this->_verifyGatewayResponse($response);
  418. }
  419. /**
  420. * generic method for validating incoming gateway responses
  421. *
  422. * creates a new CreditCard object and encapsulates
  423. * it inside a Result\Successful object, or
  424. * encapsulates a Errors object inside a Result\Error
  425. * alternatively, throws an Unexpected exception if the response is invalid
  426. *
  427. * @ignore
  428. * @param array $response gateway response values
  429. * @return Result\Successful|Result\Error
  430. * @throws Exception\Unexpected
  431. */
  432. private function _verifyGatewayResponse($response)
  433. {
  434. if (isset($response['creditCard'])) {
  435. // return a populated instance of Address
  436. return new Result\Successful(
  437. CreditCard::factory($response['creditCard'])
  438. );
  439. } elseif (isset($response['apiErrorResponse'])) {
  440. return new Result\Error($response['apiErrorResponse']);
  441. } else {
  442. throw new Exception\Unexpected(
  443. "Expected address or apiErrorResponse"
  444. );
  445. }
  446. }
  447. }
  448. class_alias('Braintree\CreditCardGateway', 'Braintree_CreditCardGateway');