PayPalAccountTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace Test\Integration;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Test\Setup;
  5. use Braintree;
  6. class PayPalAccountTest extends Setup
  7. {
  8. public function testFind()
  9. {
  10. $paymentMethodToken = 'PAYPALToken-' . strval(rand());
  11. $customer = Braintree\Customer::createNoValidate();
  12. $http = new HttpClientApi(Braintree\Configuration::$global);
  13. $nonce = $http->nonceForPayPalAccount([
  14. 'paypal_account' => [
  15. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  16. 'token' => $paymentMethodToken
  17. ]
  18. ]);
  19. Braintree\PaymentMethod::create([
  20. 'customerId' => $customer->id,
  21. 'paymentMethodNonce' => $nonce
  22. ]);
  23. $foundPayPalAccount = Braintree\PayPalAccount::find($paymentMethodToken);
  24. $this->assertSame('jane.doe@example.com', $foundPayPalAccount->email);
  25. $this->assertSame($paymentMethodToken, $foundPayPalAccount->token);
  26. $this->assertNotNull($foundPayPalAccount->imageUrl);
  27. }
  28. public function testGatewayFind()
  29. {
  30. $paymentMethodToken = 'PAYPALToken-' . strval(rand());
  31. $customer = Braintree\Customer::createNoValidate();
  32. $http = new HttpClientApi(Braintree\Configuration::$global);
  33. $nonce = $http->nonceForPayPalAccount([
  34. 'paypal_account' => [
  35. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  36. 'token' => $paymentMethodToken
  37. ]
  38. ]);
  39. Braintree\PaymentMethod::create([
  40. 'customerId' => $customer->id,
  41. 'paymentMethodNonce' => $nonce
  42. ]);
  43. $gateway = new Braintree\Gateway([
  44. 'environment' => 'development',
  45. 'merchantId' => 'integration_merchant_id',
  46. 'publicKey' => 'integration_public_key',
  47. 'privateKey' => 'integration_private_key'
  48. ]);
  49. $foundPayPalAccount = $gateway->paypalAccount()->find($paymentMethodToken);
  50. $this->assertSame('jane.doe@example.com', $foundPayPalAccount->email);
  51. $this->assertSame($paymentMethodToken, $foundPayPalAccount->token);
  52. $this->assertNotNull($foundPayPalAccount->imageUrl);
  53. }
  54. public function testFind_doesNotReturnIncorrectPaymentMethodType()
  55. {
  56. $creditCardToken = 'creditCardToken-' . strval(rand());
  57. $customer = Braintree\Customer::createNoValidate();
  58. $result = Braintree\CreditCard::create([
  59. 'customerId' => $customer->id,
  60. 'cardholderName' => 'Cardholder',
  61. 'number' => '5105105105105100',
  62. 'expirationDate' => '05/12',
  63. 'token' => $creditCardToken
  64. ]);
  65. $this->assertTrue($result->success);
  66. $this->setExpectedException('Braintree\Exception\NotFound');
  67. Braintree\PayPalAccount::find($creditCardToken);
  68. }
  69. public function test_PayPalAccountExposesTimestamps()
  70. {
  71. $customer = Braintree\Customer::createNoValidate();
  72. $result = Braintree\PaymentMethod::create([
  73. 'customerId' => $customer->id,
  74. 'paymentMethodNonce' => Braintree\Test\Nonces::$paypalFuturePayment,
  75. ]);
  76. $this->assertTrue($result->success);
  77. $this->assertNotNull($result->paymentMethod->createdAt);
  78. $this->assertNotNull($result->paymentMethod->updatedAt);
  79. }
  80. public function test_PayPalAccountExposesBillingAgreementId()
  81. {
  82. $customer = Braintree\Customer::createNoValidate();
  83. $result = Braintree\PaymentMethod::create([
  84. 'customerId' => $customer->id,
  85. 'paymentMethodNonce' => Braintree\Test\Nonces::$paypalBillingAgreement
  86. ]);
  87. $this->assertTrue($result->success);
  88. $foundPayPalAccount = Braintree\PayPalAccount::find($result->paymentMethod->token);
  89. $this->assertNotNull($foundPayPalAccount->billingAgreementId);
  90. }
  91. public function testFind_throwsIfCannotBeFound()
  92. {
  93. $this->setExpectedException('Braintree\Exception\NotFound');
  94. Braintree\PayPalAccount::find('invalid-token');
  95. }
  96. public function testFind_throwsUsefulErrorMessagesWhenEmpty()
  97. {
  98. $this->setExpectedException('InvalidArgumentException', 'expected paypal account id to be set');
  99. Braintree\PayPalAccount::find('');
  100. }
  101. public function testFind_throwsUsefulErrorMessagesWhenInvalid()
  102. {
  103. $this->setExpectedException('InvalidArgumentException', '@ is an invalid paypal account token');
  104. Braintree\PayPalAccount::find('@');
  105. }
  106. public function testFind_returnsSubscriptionsAssociatedWithAPaypalAccount()
  107. {
  108. $customer = Braintree\Customer::createNoValidate();
  109. $paymentMethodToken = 'paypal-account-' . strval(rand());
  110. $http = new HttpClientApi(Braintree\Configuration::$global);
  111. $nonce = $http->nonceForPayPalAccount([
  112. 'paypal_account' => [
  113. 'consent_code' => 'consent-code',
  114. 'token' => $paymentMethodToken
  115. ]
  116. ]);
  117. $result = Braintree\PaymentMethod::create([
  118. 'paymentMethodNonce' => $nonce,
  119. 'customerId' => $customer->id
  120. ]);
  121. $this->assertTrue($result->success);
  122. $token = $result->paymentMethod->token;
  123. $triallessPlan = SubscriptionHelper::triallessPlan();
  124. $subscription1 = Braintree\Subscription::create([
  125. 'paymentMethodToken' => $token,
  126. 'planId' => $triallessPlan['id']
  127. ])->subscription;
  128. $subscription2 = Braintree\Subscription::create([
  129. 'paymentMethodToken' => $token,
  130. 'planId' => $triallessPlan['id']
  131. ])->subscription;
  132. $paypalAccount = Braintree\PayPalAccount::find($token);
  133. $getIds = function($sub) { return $sub->id; };
  134. $subIds = array_map($getIds, $paypalAccount->subscriptions);
  135. $this->assertTrue(in_array($subscription1->id, $subIds));
  136. $this->assertTrue(in_array($subscription2->id, $subIds));
  137. }
  138. public function testUpdate()
  139. {
  140. $originalToken = 'ORIGINAL_PAYPALToken-' . strval(rand());
  141. $customer = Braintree\Customer::createNoValidate();
  142. $http = new HttpClientApi(Braintree\Configuration::$global);
  143. $nonce = $http->nonceForPayPalAccount([
  144. 'paypal_account' => [
  145. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  146. 'token' => $originalToken
  147. ]
  148. ]);
  149. $createResult = Braintree\PaymentMethod::create([
  150. 'customerId' => $customer->id,
  151. 'paymentMethodNonce' => $nonce
  152. ]);
  153. $this->assertTrue($createResult->success);
  154. $newToken = 'NEW_PAYPALToken-' . strval(rand());
  155. $updateResult = Braintree\PayPalAccount::update($originalToken, [
  156. 'token' => $newToken
  157. ]);
  158. $this->assertTrue($updateResult->success);
  159. $this->assertEquals($newToken, $updateResult->paypalAccount->token);
  160. $this->setExpectedException('Braintree\Exception\NotFound');
  161. Braintree\PayPalAccount::find($originalToken);
  162. }
  163. public function testUpdateAndMakeDefault()
  164. {
  165. $customer = Braintree\Customer::createNoValidate();
  166. $creditCardResult = Braintree\CreditCard::create([
  167. 'customerId' => $customer->id,
  168. 'number' => '5105105105105100',
  169. 'expirationDate' => '05/12'
  170. ]);
  171. $this->assertTrue($creditCardResult->success);
  172. $http = new HttpClientApi(Braintree\Configuration::$global);
  173. $nonce = $http->nonceForPayPalAccount([
  174. 'paypal_account' => [
  175. 'consent_code' => 'PAYPAL_CONSENT_CODE'
  176. ]
  177. ]);
  178. $createResult = Braintree\PaymentMethod::create([
  179. 'customerId' => $customer->id,
  180. 'paymentMethodNonce' => $nonce
  181. ]);
  182. $this->assertTrue($createResult->success);
  183. $updateResult = Braintree\PayPalAccount::update($createResult->paymentMethod->token, [
  184. 'options' => ['makeDefault' => true]
  185. ]);
  186. $this->assertTrue($updateResult->success);
  187. $this->assertTrue($updateResult->paypalAccount->isDefault());
  188. }
  189. public function testUpdate_handleErrors()
  190. {
  191. $customer = Braintree\Customer::createNoValidate();
  192. $firstToken = 'FIRST_PAYPALToken-' . strval(rand());
  193. $http = new HttpClientApi(Braintree\Configuration::$global);
  194. $firstNonce = $http->nonceForPayPalAccount([
  195. 'paypal_account' => [
  196. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  197. 'token' => $firstToken
  198. ]
  199. ]);
  200. $firstPaypalAccount = Braintree\PaymentMethod::create([
  201. 'customerId' => $customer->id,
  202. 'paymentMethodNonce' => $firstNonce
  203. ]);
  204. $this->assertTrue($firstPaypalAccount->success);
  205. $secondToken = 'SECOND_PAYPALToken-' . strval(rand());
  206. $http = new HttpClientApi(Braintree\Configuration::$global);
  207. $secondNonce = $http->nonceForPayPalAccount([
  208. 'paypal_account' => [
  209. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  210. 'token' => $secondToken
  211. ]
  212. ]);
  213. $secondPaypalAccount = Braintree\PaymentMethod::create([
  214. 'customerId' => $customer->id,
  215. 'paymentMethodNonce' => $secondNonce
  216. ]);
  217. $this->assertTrue($secondPaypalAccount->success);
  218. $updateResult = Braintree\PayPalAccount::update($firstToken, [
  219. 'token' => $secondToken
  220. ]);
  221. $this->assertFalse($updateResult->success);
  222. $errors = $updateResult->errors->forKey('paypalAccount')->errors;
  223. $this->assertEquals(Braintree\Error\Codes::PAYPAL_ACCOUNT_TOKEN_IS_IN_USE, $errors[0]->code);
  224. }
  225. public function testDelete()
  226. {
  227. $paymentMethodToken = 'PAYPALToken-' . strval(rand());
  228. $customer = Braintree\Customer::createNoValidate();
  229. $http = new HttpClientApi(Braintree\Configuration::$global);
  230. $nonce = $http->nonceForPayPalAccount([
  231. 'paypal_account' => [
  232. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  233. 'token' => $paymentMethodToken
  234. ]
  235. ]);
  236. Braintree\PaymentMethod::create([
  237. 'customerId' => $customer->id,
  238. 'paymentMethodNonce' => $nonce
  239. ]);
  240. Braintree\PayPalAccount::delete($paymentMethodToken);
  241. $this->setExpectedException('Braintree\Exception\NotFound');
  242. Braintree\PayPalAccount::find($paymentMethodToken);
  243. }
  244. public function testSale_createsASaleUsingGivenToken()
  245. {
  246. $nonce = Braintree\Test\Nonces::$paypalFuturePayment;
  247. $customer = Braintree\Customer::createNoValidate([
  248. 'paymentMethodNonce' => $nonce
  249. ]);
  250. $paypalAccount = $customer->paypalAccounts[0];
  251. $result = Braintree\PayPalAccount::sale($paypalAccount->token, [
  252. 'amount' => '100.00'
  253. ]);
  254. $this->assertTrue($result->success);
  255. $this->assertEquals('100.00', $result->transaction->amount);
  256. $this->assertEquals($customer->id, $result->transaction->customerDetails->id);
  257. $this->assertEquals($paypalAccount->token, $result->transaction->paypalDetails->token);
  258. }
  259. }