HttpClientApi.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Test\Integration;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Braintree;
  5. use Test;
  6. class HttpClientApi extends Braintree\Http
  7. {
  8. protected function _doRequest($httpVerb, $path, $requestBody = null, $file = null)
  9. {
  10. return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . "/merchants/" . $this->_config->getMerchantId() . $path, $requestBody, $file);
  11. }
  12. public function get($path)
  13. {
  14. return $this->_doRequest('GET', $path);
  15. }
  16. public function post($path, $body = null)
  17. {
  18. return $this->_doRequest('POST', $path, $body);
  19. }
  20. public function _doUrlRequest($httpVerb, $url, $requestBody = null, $file = null)
  21. {
  22. $curl = curl_init();
  23. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  24. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpVerb);
  25. curl_setopt($curl, CURLOPT_URL, $url);
  26. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  27. 'Content-Type: application/json',
  28. 'X-ApiVersion: ' . Braintree\Configuration::API_VERSION,
  29. ]);
  30. curl_setopt($curl, CURLOPT_USERPWD, $this->_config->publicKey() . ':' . $this->_config->privateKey());
  31. if(!empty($requestBody)) {
  32. curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
  33. }
  34. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  35. $response = curl_exec($curl);
  36. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  37. curl_close($curl);
  38. return ['status' => $httpStatus, 'body' => $response];
  39. }
  40. public function get_configuration($options) {
  41. $encoded_fingerprint = urlencode($options["authorization_fingerprint"]);
  42. $url = "/client_api/v1/configuration?";
  43. $url .= "authorizationFingerprint=" . $encoded_fingerprint;
  44. $url .= "&configVersion=3";
  45. $response = $this->get($url);
  46. if ($response["status"] == 200) {
  47. return json_decode($response["body"]);
  48. } else {
  49. throw new Braintree\Exception(print_r($response, true));
  50. }
  51. }
  52. public function get_cards($options) {
  53. $encoded_fingerprint = urlencode($options["authorization_fingerprint"]);
  54. $url = "/client_api/v1/payment_methods.json?";
  55. $url .= "authorizationFingerprint=" . $encoded_fingerprint;
  56. $url .= "&sharedCustomerIdentifier=" . $options["shared_customer_identifier"];
  57. $url .= "&sharedCustomerIdentifierType=" . $options["shared_customer_identifier_type"];
  58. return $this->get($url);
  59. }
  60. public function nonce_for_new_card($options) {
  61. $clientTokenOptions = [];
  62. if (array_key_exists("customerId", $options)) {
  63. $clientTokenOptions["customerId"] = $options["customerId"];
  64. unset($options["customerId"]);
  65. }
  66. $clientToken = json_decode(Test\Helper::decodedClientToken($clientTokenOptions));
  67. $options["authorization_fingerprint"] = $clientToken->authorizationFingerprint;
  68. $options["shared_customer_identifier"] = "fake_identifier_" . rand();
  69. $options["shared_customer_identifier_type"] = "testing";
  70. $response = $this->post('/client_api/v1/payment_methods/credit_cards.json', json_encode($options));
  71. if ($response["status"] == 201 || $response["status"] == 202) {
  72. $body = json_decode($response["body"]);
  73. return $body->creditCards[0]->nonce;
  74. } else {
  75. throw new Braintree\Exception(print_r($response, true));
  76. }
  77. }
  78. public function nonceForPayPalAccount($options) {
  79. $clientToken = json_decode(Test\Helper::decodedClientToken());
  80. $options["authorization_fingerprint"] = $clientToken->authorizationFingerprint;
  81. $response = $this->post('/client_api/v1/payment_methods/paypal_accounts.json', json_encode($options));
  82. if ($response["status"] == 201 || $response["status"] == 202) {
  83. $body = json_decode($response["body"], true);
  84. return $body["paypalAccounts"][0]["nonce"];
  85. } else {
  86. throw new Braintree\Exception(print_r($response, true));
  87. }
  88. }
  89. }