ClientTokenGateway.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. class ClientTokenGateway
  5. {
  6. /**
  7. *
  8. * @var Gateway
  9. */
  10. private $_gateway;
  11. /**
  12. *
  13. * @var Configuration
  14. */
  15. private $_config;
  16. /**
  17. *
  18. * @var Http
  19. */
  20. private $_http;
  21. /**
  22. *
  23. * @param Gateway $gateway
  24. */
  25. public function __construct($gateway)
  26. {
  27. $this->_gateway = $gateway;
  28. $this->_config = $gateway->config;
  29. $this->_config->assertHasAccessTokenOrKeys();
  30. $this->_http = new Http($gateway->config);
  31. }
  32. public function generate($params=[])
  33. {
  34. if (!array_key_exists("version", $params)) {
  35. $params["version"] = ClientToken::DEFAULT_VERSION;
  36. }
  37. $this->conditionallyVerifyKeys($params);
  38. $generateParams = ["client_token" => $params];
  39. return $this->_doGenerate('/client_token', $generateParams);
  40. }
  41. /**
  42. * sends the generate request to the gateway
  43. *
  44. * @ignore
  45. * @param var $url
  46. * @param array $params
  47. * @return string
  48. */
  49. public function _doGenerate($subPath, $params)
  50. {
  51. $fullPath = $this->_config->merchantPath() . $subPath;
  52. $response = $this->_http->post($fullPath, $params);
  53. return $this->_verifyGatewayResponse($response);
  54. }
  55. /**
  56. *
  57. * @param array $params
  58. * @throws InvalidArgumentException
  59. */
  60. public function conditionallyVerifyKeys($params)
  61. {
  62. if (array_key_exists("customerId", $params)) {
  63. Util::verifyKeys($this->generateWithCustomerIdSignature(), $params);
  64. } else {
  65. Util::verifyKeys($this->generateWithoutCustomerIdSignature(), $params);
  66. }
  67. }
  68. /**
  69. *
  70. * @return mixed[]
  71. */
  72. public function generateWithCustomerIdSignature()
  73. {
  74. return [
  75. "version", "customerId", "proxyMerchantId",
  76. ["options" => ["makeDefault", "verifyCard", "failOnDuplicatePaymentMethod"]],
  77. "merchantAccountId"];
  78. }
  79. /**
  80. *
  81. * @return string[]
  82. */
  83. public function generateWithoutCustomerIdSignature()
  84. {
  85. return ["version", "proxyMerchantId", "merchantAccountId"];
  86. }
  87. /**
  88. * generic method for validating incoming gateway responses
  89. *
  90. * If the request is successful, returns a client token string.
  91. * Otherwise, throws an InvalidArgumentException with the error
  92. * response from the Gateway or an HTTP status code exception.
  93. *
  94. * @ignore
  95. * @param array $response gateway response values
  96. * @return string client token
  97. * @throws InvalidArgumentException | HTTP status code exception
  98. */
  99. private function _verifyGatewayResponse($response)
  100. {
  101. if (isset($response['clientToken'])) {
  102. return $response['clientToken']['value'];
  103. } elseif (isset($response['apiErrorResponse'])) {
  104. throw new InvalidArgumentException(
  105. $response['apiErrorResponse']['message']
  106. );
  107. } else {
  108. throw new Exception\Unexpected(
  109. "Expected clientToken or apiErrorResponse"
  110. );
  111. }
  112. }
  113. }
  114. class_alias('Braintree\ClientTokenGateway', 'Braintree_ClientTokenGateway');