MerchantTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 MerchantTest extends Setup
  8. {
  9. public function testCreateMerchant()
  10. {
  11. $gateway = new Braintree\Gateway([
  12. 'clientId' => 'client_id$development$integration_client_id',
  13. 'clientSecret' => 'client_secret$development$integration_client_secret',
  14. ]);
  15. $result = $gateway->merchant()->create([
  16. 'email' => 'name@email.com',
  17. 'countryCodeAlpha3' => 'USA',
  18. 'paymentMethods' => ['credit_card', 'paypal'],
  19. ]);
  20. $this->assertEquals(true, $result->success);
  21. $merchant = $result->merchant;
  22. $this->assertNotNull($merchant->id);
  23. $credentials = $result->credentials;
  24. $this->assertNotNull($credentials->accessToken);
  25. }
  26. /**
  27. * @expectedException Braintree\Exception\Configuration
  28. * @expectedExceptionMessage clientId needs to be passed to Braintree\Gateway
  29. */
  30. public function testAssertsHasCredentials()
  31. {
  32. $gateway = new Braintree\Gateway([
  33. 'clientSecret' => 'client_secret$development$integration_client_secret',
  34. ]);
  35. $gateway->merchant()->create([
  36. 'email' => 'name@email.com',
  37. 'countryCodeAlpha3' => 'USA',
  38. ]);
  39. }
  40. public function testBadPaymentMethods()
  41. {
  42. $gateway = new Braintree\Gateway([
  43. 'clientId' => 'client_id$development$integration_client_id',
  44. 'clientSecret' => 'client_secret$development$integration_client_secret',
  45. ]);
  46. $result = $gateway->merchant()->create([
  47. 'email' => 'name@email.com',
  48. 'countryCodeAlpha3' => 'USA',
  49. 'paymentMethods' => ['fake_money'],
  50. ]);
  51. $this->assertEquals(false, $result->success);
  52. $errors = $result->errors->forKey('merchant')->onAttribute('paymentMethods');
  53. $this->assertEquals(Braintree\Error\Codes::MERCHANT_ACCOUNT_PAYMENT_METHODS_ARE_INVALID, $errors[0]->code);
  54. }
  55. public function testCreateUSMerchantThatAcceptsMultipleCurrencies()
  56. {
  57. $gateway = new Braintree\Gateway([
  58. 'clientId' => 'client_id$development$signup_client_id',
  59. 'clientSecret' => 'client_secret$development$signup_client_secret',
  60. ]);
  61. $result = $gateway->merchant()->create([
  62. 'email' => 'name@email.com',
  63. 'countryCodeAlpha3' => 'USA',
  64. 'paymentMethods' => ['credit_card', 'paypal'],
  65. 'currencies' => ['GBP', 'USD']
  66. ]);
  67. $this->assertEquals(true, $result->success);
  68. $merchant = $result->merchant;
  69. $this->assertNotNull($merchant->id);
  70. $credentials = $result->credentials;
  71. $this->assertNotNull($credentials->accessToken);
  72. $merchantAccounts = $merchant->merchantAccounts;
  73. $this->assertEquals(2, count($merchantAccounts));
  74. $usdMerchantAccount = $this->getMerchantAccountForCurrency($merchantAccounts, 'USD');
  75. $this->assertNotNull($usdMerchantAccount);
  76. $this->assertEquals(true, $usdMerchantAccount->default);
  77. $this->assertEquals('USD', $usdMerchantAccount->currencyIsoCode);
  78. $gbpMerchantAccount = $this->getMerchantAccountForCurrency($merchantAccounts, 'GBP');
  79. $this->assertNotNull($gbpMerchantAccount);
  80. $this->assertEquals(false, $gbpMerchantAccount->default);
  81. $this->assertEquals('GBP', $gbpMerchantAccount->currencyIsoCode);
  82. }
  83. public function testCreateEUMerchantThatAcceptsMultipleCurrencies()
  84. {
  85. $gateway = new Braintree\Gateway([
  86. 'clientId' => 'client_id$development$signup_client_id',
  87. 'clientSecret' => 'client_secret$development$signup_client_secret',
  88. ]);
  89. $result = $gateway->merchant()->create([
  90. 'email' => 'name@email.com',
  91. 'countryCodeAlpha3' => 'GBR',
  92. 'paymentMethods' => ['credit_card', 'paypal'],
  93. 'currencies' => ['GBP', 'USD']
  94. ]);
  95. $this->assertEquals(true, $result->success);
  96. $merchant = $result->merchant;
  97. $this->assertNotNull($merchant->id);
  98. $credentials = $result->credentials;
  99. $this->assertNotNull($credentials->accessToken);
  100. $merchantAccounts = $merchant->merchantAccounts;
  101. $this->assertEquals(2, count($merchantAccounts));
  102. $usdMerchantAccount = $this->getMerchantAccountForCurrency($merchantAccounts, 'USD');
  103. $this->assertNotNull($usdMerchantAccount);
  104. $this->assertEquals(false, $usdMerchantAccount->default);
  105. $this->assertEquals('USD', $usdMerchantAccount->currencyIsoCode);
  106. $gbpMerchantAccount = $this->getMerchantAccountForCurrency($merchantAccounts, 'GBP');
  107. $this->assertNotNull($gbpMerchantAccount);
  108. $this->assertEquals(true, $gbpMerchantAccount->default);
  109. $this->assertEquals('GBP', $gbpMerchantAccount->currencyIsoCode);
  110. }
  111. public function testCreatePaypalOnlyMerchantThatAcceptsMultipleCurrencies()
  112. {
  113. $gateway = new Braintree\Gateway([
  114. 'clientId' => 'client_id$development$signup_client_id',
  115. 'clientSecret' => 'client_secret$development$signup_client_secret',
  116. ]);
  117. $result = $gateway->merchant()->create([
  118. 'email' => 'name@email.com',
  119. 'countryCodeAlpha3' => 'USA',
  120. 'paymentMethods' => ['paypal'],
  121. 'currencies' => ['GBP', 'USD'],
  122. 'paypalAccount' => [
  123. 'clientId' => 'fake_client_id',
  124. 'clientSecret' => 'fake_client_secret',
  125. ]
  126. ]);
  127. $this->assertEquals(true, $result->success);
  128. $merchant = $result->merchant;
  129. $this->assertNotNull($merchant->id);
  130. $credentials = $result->credentials;
  131. $this->assertNotNull($credentials->accessToken);
  132. $merchantAccounts = $merchant->merchantAccounts;
  133. $this->assertEquals(2, count($merchantAccounts));
  134. $usdMerchantAccount = $this->getMerchantAccountForCurrency($merchantAccounts, 'USD');
  135. $this->assertNotNull($usdMerchantAccount);
  136. $this->assertEquals(true, $usdMerchantAccount->default);
  137. $this->assertEquals('USD', $usdMerchantAccount->currencyIsoCode);
  138. $gbpMerchantAccount = $this->getMerchantAccountForCurrency($merchantAccounts, 'GBP');
  139. $this->assertNotNull($gbpMerchantAccount);
  140. $this->assertEquals(false, $gbpMerchantAccount->default);
  141. $this->assertEquals('GBP', $gbpMerchantAccount->currencyIsoCode);
  142. }
  143. private function getMerchantAccountForCurrency($merchantAccounts, $currency)
  144. {
  145. foreach($merchantAccounts as $merchantAccount) {
  146. if($merchantAccount->id == $currency) {
  147. return $merchantAccount;
  148. }
  149. }
  150. return null;
  151. }
  152. public function testCreatePaypalOnlyMerchantWithNoCurrenciesProvided()
  153. {
  154. $gateway = new Braintree\Gateway([
  155. 'clientId' => 'client_id$development$signup_client_id',
  156. 'clientSecret' => 'client_secret$development$signup_client_secret',
  157. ]);
  158. $result = $gateway->merchant()->create([
  159. 'email' => 'name@email.com',
  160. 'countryCodeAlpha3' => 'JPN',
  161. 'paymentMethods' => ['paypal'],
  162. 'paypalAccount' => [
  163. 'clientId' => 'fake_client_id',
  164. 'clientSecret' => 'fake_client_secret',
  165. ]
  166. ]);
  167. $this->assertEquals(true, $result->success);
  168. $merchant = $result->merchant;
  169. $this->assertNotNull($merchant->id);
  170. $credentials = $result->credentials;
  171. $this->assertNotNull($credentials->accessToken);
  172. $merchantAccounts = $merchant->merchantAccounts;
  173. $this->assertEquals(1, count($merchantAccounts));
  174. $jpyMerchantAccount = $merchantAccounts[0];
  175. $this->assertEquals(true, $jpyMerchantAccount->default);
  176. $this->assertEquals('JPY', $jpyMerchantAccount->currencyIsoCode);
  177. }
  178. public function testCreatePaypalOnlyMerchantWithUnsupportedCountryCodeProvided()
  179. {
  180. $gateway = new Braintree\Gateway([
  181. 'clientId' => 'client_id$development$signup_client_id',
  182. 'clientSecret' => 'client_secret$development$signup_client_secret',
  183. ]);
  184. $result = $gateway->merchant()->create([
  185. 'email' => 'name@email.com',
  186. 'countryCodeAlpha3' => 'YEM',
  187. 'paymentMethods' => ['paypal'],
  188. 'paypalAccount' => [
  189. 'clientId' => 'fake_client_id',
  190. 'clientSecret' => 'fake_client_secret',
  191. ]
  192. ]);
  193. $this->assertEquals(true, $result->success);
  194. $merchant = $result->merchant;
  195. $this->assertNotNull($merchant->id);
  196. $credentials = $result->credentials;
  197. $this->assertNotNull($credentials->accessToken);
  198. $merchantAccounts = $merchant->merchantAccounts;
  199. $this->assertEquals(1, count($merchantAccounts));
  200. $usdMerchantAccount = $merchantAccounts[0];
  201. $this->assertEquals(true, $usdMerchantAccount->default);
  202. $this->assertEquals('USD', $usdMerchantAccount->currencyIsoCode);
  203. }
  204. public function testInvalidCurrencyForMultiCurrency()
  205. {
  206. $gateway = new Braintree\Gateway([
  207. 'clientId' => 'client_id$development$signup_client_id',
  208. 'clientSecret' => 'client_secret$development$signup_client_secret',
  209. ]);
  210. $result = $gateway->merchant()->create([
  211. 'email' => 'name@email.com',
  212. 'countryCodeAlpha3' => 'USA',
  213. 'paymentMethods' => ['paypal'],
  214. 'currencies' => ['FAKE', 'USD'],
  215. 'paypalAccount' => [
  216. 'clientId' => 'fake_client_id',
  217. 'clientSecret' => 'fake_client_secret',
  218. ]
  219. ]);
  220. $this->assertEquals(false, $result->success);
  221. $errors = $result->errors->forKey('merchant')->onAttribute('currencies');
  222. $this->assertEquals(Braintree\Error\Codes::MERCHANT_CURRENCIES_ARE_INVALID, $errors[0]->code);
  223. }
  224. }