123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace Test\Integration;
- require_once dirname(__DIR__) . '/Setup.php';
- use Test;
- use Test\Setup;
- use Braintree;
- class ClientTokenTest extends Setup
- {
- public function test_ClientTokenAuthorizesRequest()
- {
- $clientToken = Test\Helper::decodedClientToken();
- $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
- $http = new HttpClientApi(Braintree\Configuration::$global);
- $response = $http->get_cards([
- "authorization_fingerprint" => $authorizationFingerprint,
- "shared_customer_identifier" => "fake_identifier",
- "shared_customer_identifier_type" => "testing",
- ]);
- $this->assertEquals(200, $response["status"]);
- }
- public function test_VersionOptionSupported()
- {
- $clientToken = Braintree\ClientToken::generate(["version" => 1]);
- $version = json_decode($clientToken)->version;
- $this->assertEquals(1, $version);
- }
- public function test_VersionDefaultsToTwo()
- {
- $encodedClientToken = Braintree\ClientToken::generate();
- $clientToken = base64_decode($encodedClientToken);
- $version = json_decode($clientToken)->version;
- $this->assertEquals(2, $version);
- }
- public function testGateway_VersionDefaultsToTwo()
- {
- $gateway = new Braintree\Gateway([
- 'environment' => 'development',
- 'merchantId' => 'integration_merchant_id',
- 'publicKey' => 'integration_public_key',
- 'privateKey' => 'integration_private_key',
- ]);
- $encodedClientToken = $gateway->clientToken()->generate();
- $clientToken = base64_decode($encodedClientToken);
- $version = json_decode($clientToken)->version;
- $this->assertEquals(2, $version);
- }
- public function test_GatewayRespectsVerifyCard()
- {
- $result = Braintree\Customer::create();
- $this->assertTrue($result->success);
- $customerId = $result->customer->id;
- $clientToken = Test\Helper::decodedClientToken([
- "customerId" => $customerId,
- "options" => [
- "verifyCard" => true
- ]
- ]);
- $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
- $http = new HttpClientApi(Braintree\Configuration::$global);
- $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
- "credit_card" => [
- "number" => "4000111111111115",
- "expirationDate" => "11/2099"
- ],
- "authorization_fingerprint" => $authorizationFingerprint,
- "shared_customer_identifier" => "fake_identifier",
- "shared_customer_identifier_type" => "testing"
- ]));
- $this->assertEquals(422, $response["status"]);
- }
- public function test_GatewayRespectsFailOnDuplicatePaymentMethod()
- {
- $result = Braintree\Customer::create();
- $this->assertTrue($result->success);
- $customerId = $result->customer->id;
- $clientToken = Test\Helper::decodedClientToken([
- "customerId" => $customerId,
- ]);
- $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
- $http = new HttpClientApi(Braintree\Configuration::$global);
- $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
- "credit_card" => [
- "number" => "4242424242424242",
- "expirationDate" => "11/2099"
- ],
- "authorization_fingerprint" => $authorizationFingerprint,
- "shared_customer_identifier" => "fake_identifier",
- "shared_customer_identifier_type" => "testing"
- ]));
- $this->assertEquals(201, $response["status"]);
- $clientToken = Test\Helper::decodedClientToken([
- "customerId" => $customerId,
- "options" => [
- "failOnDuplicatePaymentMethod" => true
- ]
- ]);
- $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
- $http = new HttpClientApi(Braintree\Configuration::$global);
- $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
- "credit_card" => [
- "number" => "4242424242424242",
- "expirationDate" => "11/2099"
- ],
- "authorization_fingerprint" => $authorizationFingerprint,
- "shared_customer_identifier" => "fake_identifier",
- "shared_customer_identifier_type" => "testing"
- ]));
- $this->assertEquals(422, $response["status"]);
- }
- public function test_GatewayRespectsMakeDefault()
- {
- $result = Braintree\Customer::create();
- $this->assertTrue($result->success);
- $customerId = $result->customer->id;
- $result = Braintree\CreditCard::create([
- 'customerId' => $customerId,
- 'number' => '4111111111111111',
- 'expirationDate' => '11/2099'
- ]);
- $this->assertTrue($result->success);
- $clientToken = Test\Helper::decodedClientToken([
- "customerId" => $customerId,
- "options" => [
- "makeDefault" => true
- ]
- ]);
- $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
- $http = new HttpClientApi(Braintree\Configuration::$global);
- $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode([
- "credit_card" => [
- "number" => "4242424242424242",
- "expirationDate" => "11/2099"
- ],
- "authorization_fingerprint" => $authorizationFingerprint,
- "shared_customer_identifier" => "fake_identifier",
- "shared_customer_identifier_type" => "testing"
- ]));
- $this->assertEquals(201, $response["status"]);
- $customer = Braintree\Customer::find($customerId);
- $this->assertEquals(2, count($customer->creditCards));
- foreach ($customer->creditCards as $creditCard) {
- if ($creditCard->last4 == "4242") {
- $this->assertTrue($creditCard->default);
- }
- }
- }
- public function test_ClientTokenAcceptsMerchantAccountId()
- {
- $expectedMerchantAccountId = Test\Helper::nonDefaultMerchantAccountId();
- $clientToken = Test\Helper::decodedClientToken([
- 'merchantAccountId' => $expectedMerchantAccountId
- ]);
- $merchantAccountId = json_decode($clientToken)->merchantAccountId;
- $this->assertEquals($expectedMerchantAccountId, $merchantAccountId);
- }
- public function test_GenerateRaisesExceptionOnGateway422()
- {
- $this->setExpectedException('InvalidArgumentException', 'customer_id');
- Braintree\ClientToken::generate([
- "customerId" => "not_a_customer"
- ]);
- }
- public function test_ClientTokenRejectsSepaParams()
- {
- $this->setExpectedException('InvalidArgumentException', 'sepaMandateType');
- Braintree\ClientToken::generate([
- "sepaMandateType" => "Business"
- ]);
- }
- }
|