CreditCardVerificationGateway.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Braintree;
  3. class CreditCardVerificationGateway
  4. {
  5. private $_gateway;
  6. private $_config;
  7. private $_http;
  8. public function __construct($gateway)
  9. {
  10. $this->_gateway = $gateway;
  11. $this->_config = $gateway->config;
  12. $this->_config->assertHasAccessTokenOrKeys();
  13. $this->_http = new Http($gateway->config);
  14. }
  15. public function create($attributes)
  16. {
  17. $response = $this->_http->post($this->_config->merchantPath() . "/verifications", ['verification' => $attributes]);
  18. return $this->_verifyGatewayResponse($response);
  19. }
  20. private function _verifyGatewayResponse($response)
  21. {
  22. if(isset($response['verification'])){
  23. return new Result\Successful(
  24. CreditCardVerification::factory($response['verification'])
  25. );
  26. } else if (isset($response['apiErrorResponse'])) {
  27. return new Result\Error($response['apiErrorResponse']);
  28. } else {
  29. throw new Exception\Unexpected(
  30. "Expected transaction or apiErrorResponse"
  31. );
  32. }
  33. }
  34. public function fetch($query, $ids)
  35. {
  36. $criteria = [];
  37. foreach ($query as $term) {
  38. $criteria[$term->name] = $term->toparam();
  39. }
  40. $criteria["ids"] = CreditCardVerificationSearch::ids()->in($ids)->toparam();
  41. $path = $this->_config->merchantPath() . '/verifications/advanced_search';
  42. $response = $this->_http->post($path, ['search' => $criteria]);
  43. return Util::extractattributeasarray(
  44. $response['creditCardVerifications'],
  45. 'verification'
  46. );
  47. }
  48. public function search($query)
  49. {
  50. $criteria = [];
  51. foreach ($query as $term) {
  52. $criteria[$term->name] = $term->toparam();
  53. }
  54. $path = $this->_config->merchantPath() . '/verifications/advanced_search_ids';
  55. $response = $this->_http->post($path, ['search' => $criteria]);
  56. $pager = [
  57. 'object' => $this,
  58. 'method' => 'fetch',
  59. 'methodArgs' => [$query]
  60. ];
  61. return new ResourceCollection($response, $pager);
  62. }
  63. }
  64. class_alias('Braintree\CreditCardVerificationGateway', 'Braintree_CreditCardVerificationGateway');