/www/braintree-php-3.5.0/tests/integration/HttpClientApi.php

https://gitlab.com/spejs/CrimeWatch · PHP · 111 lines · 93 code · 18 blank · 0 comment · 18 complexity · fb73bf1435feaaa0e54533ff786526f4 MD5 · raw file

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