/wp-content/plugins/woocommerce-gateway-paypal-powered-by-braintree/braintree_sdk/tests/integration/HttpClientApi.php

https://gitlab.com/hunt9310/ras · PHP · 116 lines · 97 code · 19 blank · 0 comment · 18 complexity · 6f76f301d78ae0b01e080e25c3c9d666 MD5 · raw file

  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)
  9. {
  10. return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . "/merchants/" . $this->_config->getMerchantId() . $path, $requestBody);
  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)
  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_cards($options) {
  41. $encoded_fingerprint = urlencode($options["authorization_fingerprint"]);
  42. $url = "/client_api/v1/payment_methods.json?";
  43. $url .= "authorizationFingerprint=" . $encoded_fingerprint;
  44. $url .= "&sharedCustomerIdentifier=" . $options["shared_customer_identifier"];
  45. $url .= "&sharedCustomerIdentifierType=" . $options["shared_customer_identifier_type"];
  46. return $this->get($url);
  47. }
  48. public function nonce_for_new_card($options) {
  49. $clientTokenOptions = [];
  50. if (array_key_exists("customerId", $options)) {
  51. $clientTokenOptions["customerId"] = $options["customerId"];
  52. unset($options["customerId"]);
  53. }
  54. $clientToken = json_decode(Test\Helper::decodedClientToken($clientTokenOptions));
  55. $options["authorization_fingerprint"] = $clientToken->authorizationFingerprint;
  56. $options["shared_customer_identifier"] = "fake_identifier_" . rand();
  57. $options["shared_customer_identifier_type"] = "testing";
  58. $response = $this->post('/client_api/v1/payment_methods/credit_cards.json', json_encode($options));
  59. if ($response["status"] == 201 || $response["status"] == 202) {
  60. $body = json_decode($response["body"]);
  61. return $body->creditCards[0]->nonce;
  62. } else {
  63. throw new Exception(var_dump($response));
  64. }
  65. }
  66. public function nonceForNewEuropeanBankAccount($options) {
  67. $clientTokenOptions = [
  68. 'sepaMandateType' => 'business',
  69. 'sepaMandateAcceptanceLocation' => 'Rostock, Germany'
  70. ];
  71. if (array_key_exists("customerId", $options)) {
  72. $clientTokenOptions["customerId"] = $options["customerId"];
  73. unset($options["customerId"]);
  74. }
  75. $gateway = new Braintree\Gateway($this->_config);
  76. $clientToken = json_decode(base64_decode($gateway->clientToken()->generate($clientTokenOptions)));
  77. $options["authorization_fingerprint"] = $clientToken->authorizationFingerprint;
  78. $response = $this->post('/client_api/v1/sepa_mandates/', json_encode($options));
  79. if ($response["status"] == 201 || $response["status"] == 202) {
  80. $body = json_decode($response["body"]);
  81. return $body->europeBankAccounts[0]->nonce;
  82. } else {
  83. throw new Exception(var_dump($response));
  84. }
  85. }
  86. public function nonceForPayPalAccount($options) {
  87. $clientToken = json_decode(Test\Helper::decodedClientToken());
  88. $options["authorization_fingerprint"] = $clientToken->authorizationFingerprint;
  89. $response = $this->post('/client_api/v1/payment_methods/paypal_accounts.json', json_encode($options));
  90. if ($response["status"] == 201 || $response["status"] == 202) {
  91. $body = json_decode($response["body"], true);
  92. return $body["paypalAccounts"][0]["nonce"];
  93. } else {
  94. throw new Exception(var_dump($response));
  95. }
  96. }
  97. }