ClientTokenTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Test\Integration;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Test;
  5. use Test\Setup;
  6. use Braintree;
  7. class ClientTokenTest extends Setup
  8. {
  9. public function test_ClientTokenAuthorizesRequest()
  10. {
  11. $clientToken = Test\Helper::decodedClientToken();
  12. $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
  13. $http = new HttpClientApi(Braintree\Configuration::$global);
  14. $response = $http->get_cards([
  15. "authorization_fingerprint" => $authorizationFingerprint,
  16. "shared_customer_identifier" => "fake_identifier",
  17. "shared_customer_identifier_type" => "testing",
  18. ]);
  19. $this->assertEquals(200, $response["status"]);
  20. }
  21. public function test_VersionOptionSupported()
  22. {
  23. $clientToken = Braintree\ClientToken::generate(["version" => 1]);
  24. $version = json_decode($clientToken)->version;
  25. $this->assertEquals(1, $version);
  26. }
  27. public function test_VersionDefaultsToTwo()
  28. {
  29. $encodedClientToken = Braintree\ClientToken::generate();
  30. $clientToken = base64_decode($encodedClientToken);
  31. $version = json_decode($clientToken)->version;
  32. $this->assertEquals(2, $version);
  33. }
  34. public function testGateway_VersionDefaultsToTwo()
  35. {
  36. $gateway = new Braintree\Gateway([
  37. 'environment' => 'development',
  38. 'merchantId' => 'integration_merchant_id',
  39. 'publicKey' => 'integration_public_key',
  40. 'privateKey' => 'integration_private_key',
  41. ]);
  42. $encodedClientToken = $gateway->clientToken()->generate();
  43. $clientToken = base64_decode($encodedClientToken);
  44. $version = json_decode($clientToken)->version;
  45. $this->assertEquals(2, $version);
  46. }
  47. public function test_GatewayRespectsVerifyCard()
  48. {
  49. $result = Braintree\Customer::create();
  50. $this->assertTrue($result->success);
  51. $customerId = $result->customer->id;
  52. $clientToken = Test\Helper::decodedClientToken([
  53. "customerId" => $customerId,
  54. "options" => [
  55. "verifyCard" => true
  56. ]
  57. ]);
  58. $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
  59. $http = new HttpClientApi(Braintree\Configuration::$global);
  60. $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
  61. "credit_card" => [
  62. "number" => "4000111111111115",
  63. "expirationDate" => "11/2099"
  64. ],
  65. "authorization_fingerprint" => $authorizationFingerprint,
  66. "shared_customer_identifier" => "fake_identifier",
  67. "shared_customer_identifier_type" => "testing"
  68. ]));
  69. $this->assertEquals(422, $response["status"]);
  70. }
  71. public function test_GatewayRespectsFailOnDuplicatePaymentMethod()
  72. {
  73. $result = Braintree\Customer::create();
  74. $this->assertTrue($result->success);
  75. $customerId = $result->customer->id;
  76. $clientToken = Test\Helper::decodedClientToken([
  77. "customerId" => $customerId,
  78. ]);
  79. $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
  80. $http = new HttpClientApi(Braintree\Configuration::$global);
  81. $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
  82. "credit_card" => [
  83. "number" => "4242424242424242",
  84. "expirationDate" => "11/2099"
  85. ],
  86. "authorization_fingerprint" => $authorizationFingerprint,
  87. "shared_customer_identifier" => "fake_identifier",
  88. "shared_customer_identifier_type" => "testing"
  89. ]));
  90. $this->assertEquals(201, $response["status"]);
  91. $clientToken = Test\Helper::decodedClientToken([
  92. "customerId" => $customerId,
  93. "options" => [
  94. "failOnDuplicatePaymentMethod" => true
  95. ]
  96. ]);
  97. $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
  98. $http = new HttpClientApi(Braintree\Configuration::$global);
  99. $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
  100. "credit_card" => [
  101. "number" => "4242424242424242",
  102. "expirationDate" => "11/2099"
  103. ],
  104. "authorization_fingerprint" => $authorizationFingerprint,
  105. "shared_customer_identifier" => "fake_identifier",
  106. "shared_customer_identifier_type" => "testing"
  107. ]));
  108. $this->assertEquals(422, $response["status"]);
  109. }
  110. public function test_GatewayRespectsMakeDefault()
  111. {
  112. $result = Braintree\Customer::create();
  113. $this->assertTrue($result->success);
  114. $customerId = $result->customer->id;
  115. $result = Braintree\CreditCard::create([
  116. 'customerId' => $customerId,
  117. 'number' => '4111111111111111',
  118. 'expirationDate' => '11/2099'
  119. ]);
  120. $this->assertTrue($result->success);
  121. $clientToken = Test\Helper::decodedClientToken([
  122. "customerId" => $customerId,
  123. "options" => [
  124. "makeDefault" => true
  125. ]
  126. ]);
  127. $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
  128. $http = new HttpClientApi(Braintree\Configuration::$global);
  129. $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
  130. "credit_card" => [
  131. "number" => "4242424242424242",
  132. "expirationDate" => "11/2099"
  133. ],
  134. "authorization_fingerprint" => $authorizationFingerprint,
  135. "shared_customer_identifier" => "fake_identifier",
  136. "shared_customer_identifier_type" => "testing"
  137. ]));
  138. $this->assertEquals(201, $response["status"]);
  139. $customer = Braintree\Customer::find($customerId);
  140. $this->assertEquals(2, count($customer->creditCards));
  141. foreach ($customer->creditCards as $creditCard) {
  142. if ($creditCard->last4 == "4242") {
  143. $this->assertTrue($creditCard->default);
  144. }
  145. }
  146. }
  147. public function test_ClientTokenAcceptsMerchantAccountId()
  148. {
  149. $expectedMerchantAccountId = Test\Helper::nonDefaultMerchantAccountId();
  150. $clientToken = Test\Helper::decodedClientToken([
  151. 'merchantAccountId' => $expectedMerchantAccountId
  152. ]);
  153. $merchantAccountId = json_decode($clientToken)->merchantAccountId;
  154. $this->assertEquals($expectedMerchantAccountId, $merchantAccountId);
  155. }
  156. public function test_GenerateRaisesExceptionOnGateway422()
  157. {
  158. $this->setExpectedException('InvalidArgumentException', 'customer_id');
  159. Braintree\ClientToken::generate([
  160. "customerId" => "not_a_customer"
  161. ]);
  162. }
  163. public function test_ClientTokenRejectsSepaParams()
  164. {
  165. $this->setExpectedException('InvalidArgumentException', 'sepaMandateType');
  166. Braintree\ClientToken::generate([
  167. "sepaMandateType" => "Business"
  168. ]);
  169. }
  170. }