PaymentMethodTest.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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 PaymentMethodTest extends Setup
  8. {
  9. public function testCreate_fromVaultedCreditCardNonce()
  10. {
  11. $customer = Braintree\Customer::createNoValidate();
  12. $http = new HttpClientApi(Braintree\Configuration::$global);
  13. $nonce = $http->nonce_for_new_card([
  14. 'credit_card' => [
  15. 'number' => '4111111111111111',
  16. 'expirationMonth' => '11',
  17. 'expirationYear' => '2099'
  18. ],
  19. 'share' => true
  20. ]);
  21. $result = Braintree\PaymentMethod::create([
  22. 'customerId' => $customer->id,
  23. 'paymentMethodNonce' => $nonce
  24. ]);
  25. $this->assertSame('411111', $result->paymentMethod->bin);
  26. $this->assertSame('1111', $result->paymentMethod->last4);
  27. $this->assertNotNull($result->paymentMethod->token);
  28. $this->assertNotNull($result->paymentMethod->imageUrl);
  29. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  30. }
  31. public function testGatewayCreate_fromVaultedCreditCardNonce()
  32. {
  33. $customer = Braintree\Customer::createNoValidate();
  34. $http = new HttpClientApi(Braintree\Configuration::$global);
  35. $nonce = $http->nonce_for_new_card([
  36. 'credit_card' => [
  37. 'number' => '4111111111111111',
  38. 'expirationMonth' => '11',
  39. 'expirationYear' => '2099'
  40. ],
  41. 'share' => true
  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. $result = $gateway->paymentMethod()->create([
  50. 'customerId' => $customer->id,
  51. 'paymentMethodNonce' => $nonce
  52. ]);
  53. $this->assertSame('411111', $result->paymentMethod->bin);
  54. $this->assertSame('1111', $result->paymentMethod->last4);
  55. $this->assertNotNull($result->paymentMethod->token);
  56. $this->assertNotNull($result->paymentMethod->imageUrl);
  57. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  58. }
  59. public function testCreate_fromFakeApplePayNonce()
  60. {
  61. $customer = Braintree\Customer::createNoValidate();
  62. $result = Braintree\PaymentMethod::create([
  63. 'customerId' => $customer->id,
  64. 'paymentMethodNonce' => Braintree\Test\Nonces::$applePayVisa,
  65. ]);
  66. $this->assertTrue($result->success);
  67. $applePayCard = $result->paymentMethod;
  68. $this->assertNotNull($applePayCard->token);
  69. $this->assertNotNull($applePayCard->bin);
  70. $this->assertSame(Braintree\ApplePayCard::VISA, $applePayCard->cardType);
  71. $this->assertContains("Visa ", $applePayCard->paymentInstrumentName);
  72. $this->assertContains("Visa ", $applePayCard->sourceDescription);
  73. $this->assertTrue($applePayCard->default);
  74. $this->assertContains('apple_pay', $applePayCard->imageUrl);
  75. $this->assertTrue(intval($applePayCard->expirationMonth) > 0);
  76. $this->assertTrue(intval($applePayCard->expirationYear) > 0);
  77. $this->assertSame($customer->id, $applePayCard->customerId);
  78. }
  79. public function testCreate_fromFakeAndroidPayProxyCardNonce()
  80. {
  81. $customer = Braintree\Customer::createNoValidate();
  82. $result = Braintree\PaymentMethod::create([
  83. 'customerId' => $customer->id,
  84. 'paymentMethodNonce' => Braintree\Test\Nonces::$androidPayDiscover
  85. ]);
  86. $this->assertTrue($result->success);
  87. $androidPayCard = $result->paymentMethod;
  88. $this->assertNotNull($androidPayCard->token);
  89. $this->assertSame(Braintree\CreditCard::DISCOVER, $androidPayCard->virtualCardType);
  90. $this->assertSame(Braintree\CreditCard::DISCOVER, $androidPayCard->cardType);
  91. $this->assertSame("1117", $androidPayCard->virtualCardLast4);
  92. $this->assertSame("1117", $androidPayCard->last4);
  93. $this->assertSame(Braintree\CreditCard::DISCOVER, $androidPayCard->sourceCardType);
  94. $this->assertSame("1111", $androidPayCard->sourceCardLast4);
  95. $this->assertSame("Discover 1111", $androidPayCard->sourceDescription);
  96. $this->assertTrue($androidPayCard->default);
  97. $this->assertContains('android_pay', $androidPayCard->imageUrl);
  98. $this->assertTrue(intval($androidPayCard->expirationMonth) > 0);
  99. $this->assertTrue(intval($androidPayCard->expirationYear) > 0);
  100. $this->assertSame($customer->id, $androidPayCard->customerId);
  101. }
  102. public function testCreate_fromFakeAndroidPayNetworkTokenNonce()
  103. {
  104. $customer = Braintree\Customer::createNoValidate();
  105. $result = Braintree\PaymentMethod::create([
  106. 'customerId' => $customer->id,
  107. 'paymentMethodNonce' => Braintree\Test\Nonces::$androidPayMasterCard
  108. ]);
  109. $this->assertTrue($result->success);
  110. $androidPayCard = $result->paymentMethod;
  111. $this->assertNotNull($androidPayCard->token);
  112. $this->assertSame(Braintree\CreditCard::MASTER_CARD, $androidPayCard->virtualCardType);
  113. $this->assertSame(Braintree\CreditCard::MASTER_CARD, $androidPayCard->cardType);
  114. $this->assertSame("4444", $androidPayCard->virtualCardLast4);
  115. $this->assertSame("4444", $androidPayCard->last4);
  116. $this->assertSame(Braintree\CreditCard::MASTER_CARD, $androidPayCard->sourceCardType);
  117. $this->assertSame("4444", $androidPayCard->sourceCardLast4);
  118. $this->assertSame("MasterCard 4444", $androidPayCard->sourceDescription);
  119. $this->assertTrue($androidPayCard->default);
  120. $this->assertContains('android_pay', $androidPayCard->imageUrl);
  121. $this->assertTrue(intval($androidPayCard->expirationMonth) > 0);
  122. $this->assertTrue(intval($androidPayCard->expirationYear) > 0);
  123. $this->assertSame($customer->id, $androidPayCard->customerId);
  124. }
  125. public function testCreate_fromFakeAmexExpressCheckoutCardNonce()
  126. {
  127. $customer = Braintree\Customer::createNoValidate();
  128. $result = Braintree\PaymentMethod::create([
  129. 'customerId' => $customer->id,
  130. 'paymentMethodNonce' => Braintree\Test\Nonces::$amexExpressCheckout
  131. ]);
  132. $this->assertTrue($result->success);
  133. $amexExpressCheckoutCard = $result->paymentMethod;
  134. $this->assertInstanceOf('Braintree\AmexExpressCheckoutCard', $amexExpressCheckoutCard);
  135. $this->assertNotNull($amexExpressCheckoutCard->token);
  136. $this->assertSame(Braintree\CreditCard::AMEX, $amexExpressCheckoutCard->cardType);
  137. $this->assertSame("341111", $amexExpressCheckoutCard->bin);
  138. $this->assertSame("12/21", $amexExpressCheckoutCard->cardMemberExpiryDate);
  139. $this->assertSame("0005", $amexExpressCheckoutCard->cardMemberNumber);
  140. $this->assertSame("American Express", $amexExpressCheckoutCard->cardType);
  141. $this->assertNotNull($amexExpressCheckoutCard->sourceDescription);
  142. $this->assertContains(".png", $amexExpressCheckoutCard->imageUrl);
  143. $this->assertTrue(intval($amexExpressCheckoutCard->expirationMonth) > 0);
  144. $this->assertTrue(intval($amexExpressCheckoutCard->expirationYear) > 0);
  145. $this->assertTrue($amexExpressCheckoutCard->default);
  146. $this->assertSame($customer->id, $amexExpressCheckoutCard->customerId);
  147. $this->assertEquals([], $amexExpressCheckoutCard->subscriptions);
  148. }
  149. public function testCreate_fromFakeVenmoAccountNonce()
  150. {
  151. $customer = Braintree\Customer::createNoValidate();
  152. $result = Braintree\PaymentMethod::create(array(
  153. 'customerId' => $customer->id,
  154. 'paymentMethodNonce' => Braintree\Test\Nonces::$venmoAccount
  155. ));
  156. $this->assertTrue($result->success);
  157. $venmoAccount = $result->paymentMethod;
  158. $this->assertInstanceOf('Braintree\VenmoAccount', $venmoAccount);
  159. $this->assertNotNull($venmoAccount->token);
  160. $this->assertNotNull($venmoAccount->sourceDescription);
  161. $this->assertContains(".png", $venmoAccount->imageUrl);
  162. $this->assertTrue($venmoAccount->default);
  163. $this->assertSame($customer->id, $venmoAccount->customerId);
  164. $this->assertEquals(array(), $venmoAccount->subscriptions);
  165. $this->assertSame("venmojoe", $venmoAccount->username);
  166. $this->assertSame("Venmo-Joe-1", $venmoAccount->venmoUserId);
  167. }
  168. public function testCreate_fromFakeEuropeBankAccountNonce()
  169. {
  170. $customer = Braintree\Customer::createNoValidate();
  171. $this->setExpectedException('Braintree\Exception\ServerError');
  172. $result = Braintree\PaymentMethod::create(array(
  173. 'customerId' => $customer->id,
  174. 'paymentMethodNonce' => Braintree\Test\Nonces::$europe
  175. ));
  176. }
  177. public function testCreate_fromUnvalidatedCreditCardNonce()
  178. {
  179. $customer = Braintree\Customer::createNoValidate();
  180. $http = new HttpClientApi(Braintree\Configuration::$global);
  181. $nonce = $http->nonce_for_new_card([
  182. 'credit_card' => [
  183. 'number' => '4111111111111111',
  184. 'expirationMonth' => '11',
  185. 'expirationYear' => '2099',
  186. 'options' => [
  187. 'validate' => false
  188. ]
  189. ]
  190. ]);
  191. $result = Braintree\PaymentMethod::create([
  192. 'customerId' => $customer->id,
  193. 'paymentMethodNonce' => $nonce
  194. ]);
  195. $this->assertSame('411111', $result->paymentMethod->bin);
  196. $this->assertSame('1111', $result->paymentMethod->last4);
  197. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  198. $this->assertNotNull($result->paymentMethod->token);
  199. }
  200. public function testCreate_fromUnvalidatedFuturePaypalAccountNonce()
  201. {
  202. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  203. $customer = Braintree\Customer::createNoValidate();
  204. $http = new HttpClientApi(Braintree\Configuration::$global);
  205. $nonce = $http->nonceForPayPalAccount([
  206. 'paypal_account' => [
  207. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  208. 'token' => $paymentMethodToken
  209. ]
  210. ]);
  211. $result = Braintree\PaymentMethod::create([
  212. 'customerId' => $customer->id,
  213. 'paymentMethodNonce' => $nonce
  214. ]);
  215. $this->assertSame('jane.doe@example.com', $result->paymentMethod->email);
  216. $this->assertSame($paymentMethodToken, $result->paymentMethod->token);
  217. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  218. }
  219. public function testCreate_fromOrderPaymentPaypalAccountNonce()
  220. {
  221. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  222. $customer = Braintree\Customer::createNoValidate();
  223. $http = new HttpClientApi(Braintree\Configuration::$global);
  224. $nonce = $http->nonceForPayPalAccount([
  225. 'paypal_account' => [
  226. 'intent' => 'order',
  227. 'payment_token' => 'paypal-payment-token',
  228. 'payer_id' => 'paypal-payer-id',
  229. 'token' => $paymentMethodToken,
  230. ]
  231. ]);
  232. $result = Braintree\PaymentMethod::create([
  233. 'customerId' => $customer->id,
  234. 'paymentMethodNonce' => $nonce
  235. ]);
  236. $this->assertSame('bt_buyer_us@paypal.com', $result->paymentMethod->email);
  237. $this->assertSame($paymentMethodToken, $result->paymentMethod->token);
  238. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  239. $this->assertNotNull($result->paymentMethod->payerId);
  240. }
  241. public function testCreate_fromOrderPaymentPaypalAccountNonceWithPayPalOptionsSnakeCase()
  242. {
  243. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  244. $customer = Braintree\Customer::createNoValidate();
  245. $http = new HttpClientApi(Braintree\Configuration::$global);
  246. $nonce = $http->nonceForPayPalAccount([
  247. 'paypal_account' => [
  248. 'intent' => 'order',
  249. 'payment_token' => 'paypal-payment-token',
  250. 'payer_id' => 'paypal-payer-id',
  251. 'token' => $paymentMethodToken,
  252. ]
  253. ]);
  254. $result = Braintree\PaymentMethod::create([
  255. 'customerId' => $customer->id,
  256. 'paymentMethodNonce' => $nonce,
  257. 'options' => [
  258. 'paypal' => [
  259. 'payee_email' => 'payee@example.com',
  260. 'order_id' => 'merchant-order-id',
  261. 'custom_field' => 'custom merchant field',
  262. 'description' => 'merchant description',
  263. 'amount' => '1.23',
  264. ]
  265. ],
  266. ]);
  267. $this->assertSame('bt_buyer_us@paypal.com', $result->paymentMethod->email);
  268. $this->assertSame($paymentMethodToken, $result->paymentMethod->token);
  269. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  270. $this->assertNotNull($result->paymentMethod->payerId);
  271. }
  272. public function testCreate_fromOrderPaymentPaypalAccountNonceWithPayPalOptionsCamelCase()
  273. {
  274. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  275. $customer = Braintree\Customer::createNoValidate();
  276. $http = new HttpClientApi(Braintree\Configuration::$global);
  277. $nonce = $http->nonceForPayPalAccount([
  278. 'paypal_account' => [
  279. 'intent' => 'order',
  280. 'payment_token' => 'paypal-payment-token',
  281. 'payer_id' => 'paypal-payer-id',
  282. 'token' => $paymentMethodToken,
  283. ]
  284. ]);
  285. $result = Braintree\PaymentMethod::create([
  286. 'customerId' => $customer->id,
  287. 'paymentMethodNonce' => $nonce,
  288. 'options' => [
  289. 'paypal' => [
  290. 'payeeEmail' => 'payee@example.com',
  291. 'orderId' => 'merchant-order-id',
  292. 'customField' => 'custom merchant field',
  293. 'description' => 'merchant description',
  294. 'amount' => '1.23',
  295. 'shipping' => [
  296. 'firstName' => 'Andrew',
  297. 'lastName' => 'Mason',
  298. 'company' => 'Braintree',
  299. 'streetAddress' => '456 W Main St',
  300. 'extendedAddress' => 'Apt 2F',
  301. 'locality' => 'Bartlett',
  302. 'region' => 'IL',
  303. 'postalCode' => '60103',
  304. 'countryName' => 'United States of America',
  305. 'countryCodeAlpha2' => 'US',
  306. 'countryCodeAlpha3' => 'USA',
  307. 'countryCodeNumeric' => '840'
  308. ],
  309. ],
  310. ],
  311. ]);
  312. $this->assertSame('bt_buyer_us@paypal.com', $result->paymentMethod->email);
  313. $this->assertSame($paymentMethodToken, $result->paymentMethod->token);
  314. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  315. $this->assertNotNull($result->paymentMethod->payerId);
  316. }
  317. public function testCreate_fromPayPalRefreshToken()
  318. {
  319. $customer = Braintree\Customer::createNoValidate();
  320. $http = new HttpClientApi(Braintree\Configuration::$global);
  321. $result = Braintree\PaymentMethod::create([
  322. 'customerId' => $customer->id,
  323. 'paypalRefreshToken' => 'PAYPAL_REFRESH_TOKEN',
  324. ]);
  325. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  326. $this->assertSame("B_FAKE_ID", $result->paymentMethod->billingAgreementId);
  327. $this->assertNotNull($result->paymentMethod->payerId);
  328. }
  329. public function testCreate_fromPayPalRefreshTokenWithoutUpgrade()
  330. {
  331. $customer = Braintree\Customer::createNoValidate();
  332. $http = new HttpClientApi(Braintree\Configuration::$global);
  333. $result = Braintree\PaymentMethod::create([
  334. 'customerId' => $customer->id,
  335. 'paypalRefreshToken' => 'PAYPAL_REFRESH_TOKEN',
  336. 'paypalVaultWithoutUpgrade' => true,
  337. ]);
  338. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  339. $this->assertNull($result->paymentMethod->billingAgreementId);
  340. }
  341. public function testCreate_fromAbstractPaymentMethodNonce()
  342. {
  343. $customer = Braintree\Customer::createNoValidate();
  344. $result = Braintree\PaymentMethod::create([
  345. 'customerId' => $customer->id,
  346. 'paymentMethodNonce' => Braintree\Test\Nonces::$abstractTransactable,
  347. ]);
  348. $this->assertTrue($result->success);
  349. $this->assertNotNull($result->paymentMethod->token);
  350. $this->assertSame($customer->id, $result->paymentMethod->customerId);
  351. }
  352. public function testCreate_doesNotWorkForUnvalidatedOnetimePaypalAccountNonce()
  353. {
  354. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  355. $customer = Braintree\Customer::createNoValidate();
  356. $http = new HttpClientApi(Braintree\Configuration::$global);
  357. $nonce = $http->nonceForPayPalAccount([
  358. 'paypal_account' => [
  359. 'access_token' => 'PAYPAL_ACCESS_TOKEN',
  360. 'token' => $paymentMethodToken
  361. ]
  362. ]);
  363. $result = Braintree\PaymentMethod::create([
  364. 'customerId' => $customer->id,
  365. 'paymentMethodNonce' => $nonce
  366. ]);
  367. $this->assertFalse($result->success);
  368. $errors = $result->errors->forKey('paypalAccount')->errors;
  369. $this->assertEquals(Braintree\Error\Codes::PAYPAL_ACCOUNT_CANNOT_VAULT_ONE_TIME_USE_PAYPAL_ACCOUNT, $errors[0]->code);
  370. }
  371. public function testCreate_handlesValidationErrorsForPayPalAccounts()
  372. {
  373. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  374. $customer = Braintree\Customer::createNoValidate();
  375. $http = new HttpClientApi(Braintree\Configuration::$global);
  376. $nonce = $http->nonceForPayPalAccount([
  377. 'paypal_account' => [
  378. 'token' => $paymentMethodToken
  379. ]
  380. ]);
  381. $result = Braintree\PaymentMethod::create([
  382. 'customerId' => $customer->id,
  383. 'paymentMethodNonce' => $nonce
  384. ]);
  385. $this->assertFalse($result->success);
  386. $errors = $result->errors->forKey('paypalAccount')->errors;
  387. $this->assertEquals(Braintree\Error\Codes::PAYPAL_ACCOUNT_CANNOT_VAULT_ONE_TIME_USE_PAYPAL_ACCOUNT, $errors[0]->code);
  388. $this->assertEquals(Braintree\Error\Codes::PAYPAL_ACCOUNT_CONSENT_CODE_OR_ACCESS_TOKEN_IS_REQUIRED, $errors[1]->code);
  389. }
  390. public function testCreate_allowsPassingDefaultOptionWithNonce()
  391. {
  392. $customer = Braintree\Customer::createNoValidate();
  393. $card1 = Braintree\CreditCard::create([
  394. 'customerId' => $customer->id,
  395. 'cardholderName' => 'Cardholder',
  396. 'number' => '5105105105105100',
  397. 'expirationDate' => '05/12'
  398. ])->creditCard;
  399. $this->assertTrue($card1->isDefault());
  400. $http = new HttpClientApi(Braintree\Configuration::$global);
  401. $nonce = $http->nonce_for_new_card([
  402. 'credit_card' => [
  403. 'number' => '4111111111111111',
  404. 'expirationMonth' => '11',
  405. 'expirationYear' => '2099',
  406. 'options' => [
  407. 'validate' => false
  408. ]
  409. ]
  410. ]);
  411. $result = Braintree\PaymentMethod::create([
  412. 'customerId' => $customer->id,
  413. 'paymentMethodNonce' => $nonce,
  414. 'options' => [
  415. 'makeDefault' => true
  416. ]
  417. ]);
  418. $card2 = $result->paymentMethod;
  419. $card1 = Braintree\CreditCard::find($card1->token);
  420. $this->assertFalse($card1->isDefault());
  421. $this->assertTrue($card2->isDefault());
  422. }
  423. public function testCreate_overridesNonceToken()
  424. {
  425. $customer = Braintree\Customer::createNoValidate();
  426. $firstToken = 'FIRST_TOKEN-' . strval(rand());
  427. $secondToken = 'SECOND_TOKEN-' . strval(rand());
  428. $http = new HttpClientApi(Braintree\Configuration::$global);
  429. $nonce = $http->nonce_for_new_card([
  430. 'credit_card' => [
  431. 'token' => $firstToken,
  432. 'number' => '4111111111111111',
  433. 'expirationMonth' => '11',
  434. 'expirationYear' => '2099',
  435. 'options' => [
  436. 'validate' => false
  437. ]
  438. ]
  439. ]);
  440. $result = Braintree\PaymentMethod::create([
  441. 'customerId' => $customer->id,
  442. 'paymentMethodNonce' => $nonce,
  443. 'token' => $secondToken
  444. ]);
  445. $card = $result->paymentMethod;
  446. $this->assertEquals($secondToken, $card->token);
  447. }
  448. public function testCreateWithVerificationAmount()
  449. {
  450. $http = new HttpClientApi(Braintree\Configuration::$global);
  451. $nonce = $http->nonce_for_new_card([
  452. 'credit_card' => [
  453. 'number' => '4000111111111115',
  454. 'expirationMonth' => '11',
  455. 'expirationYear' => '2099',
  456. ]
  457. ]);
  458. $customer = Braintree\Customer::createNoValidate();
  459. $result = Braintree\PaymentMethod::create([
  460. 'paymentMethodNonce' => $nonce,
  461. 'customerId' => $customer->id,
  462. 'options' => [
  463. 'verifyCard' => 'true',
  464. 'verificationAmount' => '5.00',
  465. ]
  466. ]);
  467. $this->assertFalse($result->success);
  468. $this->assertEquals(Braintree\Result\CreditCardVerification::PROCESSOR_DECLINED, $result->creditCardVerification->status);
  469. }
  470. public function testCreate_respectsVerifyCardAndVerificationMerchantAccountIdWhenIncludedOutsideOfTheNonce()
  471. {
  472. $http = new HttpClientApi(Braintree\Configuration::$global);
  473. $nonce = $http->nonce_for_new_card([
  474. 'credit_card' => [
  475. 'number' => '4000111111111115',
  476. 'expirationMonth' => '11',
  477. 'expirationYear' => '2099',
  478. ]
  479. ]);
  480. $customer = Braintree\Customer::createNoValidate();
  481. $result = Braintree\PaymentMethod::create([
  482. 'paymentMethodNonce' => $nonce,
  483. 'customerId' => $customer->id,
  484. 'options' => [
  485. 'verifyCard' => 'true',
  486. 'verificationMerchantAccountId' => Test\Helper::nonDefaultMerchantAccountId(),
  487. ]
  488. ]);
  489. $this->assertFalse($result->success);
  490. $this->assertEquals(Braintree\Result\CreditCardVerification::PROCESSOR_DECLINED, $result->creditCardVerification->status);
  491. $this->assertEquals('2000', $result->creditCardVerification->processorResponseCode);
  492. $this->assertEquals('Do Not Honor', $result->creditCardVerification->processorResponseText);
  493. $this->assertEquals(Test\Helper::nonDefaultMerchantAccountId(), $result->creditCardVerification->merchantAccountId);
  494. }
  495. public function testCreate_respectsFailOnDuplicatePaymentMethodWhenIncludedOutsideTheNonce()
  496. {
  497. $customer = Braintree\Customer::createNoValidate();
  498. $result = Braintree\CreditCard::create([
  499. 'customerId' => $customer->id,
  500. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  501. 'expirationDate' => "05/2012"
  502. ]);
  503. $this->assertTrue($result->success);
  504. $http = new HttpClientApi(Braintree\Configuration::$global);
  505. $nonce = $http->nonce_for_new_card([
  506. 'credit_card' => [
  507. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  508. 'expirationDate' => "05/2012"
  509. ]
  510. ]);
  511. $updateResult = Braintree\PaymentMethod::create([
  512. 'paymentMethodNonce' => $nonce,
  513. 'customerId' => $customer->id,
  514. 'options' => [
  515. 'failOnDuplicatePaymentMethod' => 'true',
  516. ]
  517. ]);
  518. $this->assertFalse($updateResult->success);
  519. $resultErrors = $updateResult->errors->deepAll();
  520. $this->assertEquals("81724", $resultErrors[0]->code);
  521. }
  522. public function testCreate_allowsPassingABillingAddressOutsideOfTheNonce()
  523. {
  524. $customer = Braintree\Customer::createNoValidate();
  525. $http = new HttpClientApi(Braintree\Configuration::$global);
  526. $nonce = $http->nonce_for_new_card([
  527. 'credit_card' => [
  528. 'number' => '4111111111111111',
  529. 'expirationMonth' => '12',
  530. 'expirationYear' => '2020',
  531. 'options' => [
  532. 'validate' => false
  533. ]
  534. ]
  535. ]);
  536. $result = Braintree\PaymentMethod::create([
  537. 'paymentMethodNonce' => $nonce,
  538. 'customerId' => $customer->id,
  539. 'billingAddress' => [
  540. 'streetAddress' => '123 Abc Way'
  541. ]
  542. ]);
  543. $this->assertTrue($result->success);
  544. $this->assertTrue(is_a($result->paymentMethod, 'Braintree\CreditCard'));
  545. $token = $result->paymentMethod->token;
  546. $foundCreditCard = Braintree\CreditCard::find($token);
  547. $this->assertTrue(NULL != $foundCreditCard);
  548. $this->assertEquals('123 Abc Way', $foundCreditCard->billingAddress->streetAddress);
  549. }
  550. public function testCreate_overridesTheBillingAddressInTheNonce()
  551. {
  552. $customer = Braintree\Customer::createNoValidate();
  553. $http = new HttpClientApi(Braintree\Configuration::$global);
  554. $nonce = $http->nonce_for_new_card([
  555. 'credit_card' => [
  556. 'number' => '4111111111111111',
  557. 'expirationMonth' => '12',
  558. 'expirationYear' => '2020',
  559. 'options' => [
  560. 'validate' => false
  561. ],
  562. 'billingAddress' => [
  563. 'streetAddress' => '456 Xyz Way'
  564. ]
  565. ]
  566. ]);
  567. $result = Braintree\PaymentMethod::create([
  568. 'paymentMethodNonce' => $nonce,
  569. 'customerId' => $customer->id,
  570. 'billingAddress' => [
  571. 'streetAddress' => '123 Abc Way'
  572. ]
  573. ]);
  574. $this->assertTrue($result->success);
  575. $this->assertTrue(is_a($result->paymentMethod, 'Braintree\CreditCard'));
  576. $token = $result->paymentMethod->token;
  577. $foundCreditCard = Braintree\CreditCard::find($token);
  578. $this->assertTrue(NULL != $foundCreditCard);
  579. $this->assertEquals('123 Abc Way', $foundCreditCard->billingAddress->streetAddress);
  580. }
  581. public function testCreate_doesNotOverrideTheBillingAddressForAVaultedCreditCard()
  582. {
  583. $customer = Braintree\Customer::createNoValidate();
  584. $http = new HttpClientApi(Braintree\Configuration::$global);
  585. $nonce = $http->nonce_for_new_card([
  586. 'customerId' => $customer->id,
  587. 'credit_card' => [
  588. 'number' => '4111111111111111',
  589. 'expirationMonth' => '12',
  590. 'expirationYear' => '2020',
  591. 'billingAddress' => [
  592. 'streetAddress' => '456 Xyz Way'
  593. ]
  594. ]
  595. ]);
  596. $result = Braintree\PaymentMethod::create([
  597. 'paymentMethodNonce' => $nonce,
  598. 'customerId' => $customer->id,
  599. 'billingAddress' => [
  600. 'streetAddress' => '123 Abc Way'
  601. ]
  602. ]);
  603. $this->assertTrue($result->success);
  604. $this->assertTrue(is_a($result->paymentMethod, 'Braintree\CreditCard'));
  605. $token = $result->paymentMethod->token;
  606. $foundCreditCard = Braintree\CreditCard::find($token);
  607. $this->assertTrue(NULL != $foundCreditCard);
  608. $this->assertEquals('456 Xyz Way', $foundCreditCard->billingAddress->streetAddress);
  609. }
  610. public function testCreate_allowsPassingABillingAddressIdOutsideOfTheNonce()
  611. {
  612. $customer = Braintree\Customer::createNoValidate();
  613. $http = new HttpClientApi(Braintree\Configuration::$global);
  614. $nonce = $http->nonce_for_new_card([
  615. 'credit_card' => [
  616. 'number' => '4111111111111111',
  617. 'expirationMonth' => '12',
  618. 'expirationYear' => '2020',
  619. 'options' => [
  620. 'validate' => false
  621. ]
  622. ]
  623. ]);
  624. $address = Braintree\Address::create([
  625. 'customerId' => $customer->id,
  626. 'firstName' => 'Bobby',
  627. 'lastName' => 'Tables'
  628. ])->address;
  629. $result = Braintree\PaymentMethod::create([
  630. 'paymentMethodNonce' => $nonce,
  631. 'customerId' => $customer->id,
  632. 'billingAddressId' => $address->id
  633. ]);
  634. $this->assertTrue($result->success);
  635. $this->assertTrue(is_a($result->paymentMethod, 'Braintree\CreditCard'));
  636. $token = $result->paymentMethod->token;
  637. $foundCreditCard = Braintree\CreditCard::find($token);
  638. $this->assertTrue(NULL != $foundCreditCard);
  639. $this->assertEquals('Bobby', $foundCreditCard->billingAddress->firstName);
  640. $this->assertEquals('Tables', $foundCreditCard->billingAddress->lastName);
  641. }
  642. public function testCreate_doesNotReturnAnErrorIfCreditCardOptionsArePresentForAPaypalNonce()
  643. {
  644. $customer = Braintree\Customer::createNoValidate();
  645. $originalToken = 'paypal-account-' . strval(rand());
  646. $http = new HttpClientApi(Braintree\Configuration::$global);
  647. $nonce = $http->nonceForPaypalAccount([
  648. 'paypalAccount' => [
  649. 'consentCode' => 'consent-code',
  650. 'token' => $originalToken
  651. ]
  652. ]);
  653. $result = Braintree\PaymentMethod::create([
  654. 'paymentMethodNonce' => $nonce,
  655. 'customerId' => $customer->id,
  656. 'options' => [
  657. 'verifyCard' => 'true',
  658. 'failOnDuplicatePaymentMethod' => 'true',
  659. 'verificationMerchantAccountId' => 'Not a Real Merchant Account Id'
  660. ]
  661. ]);
  662. $this->assertTrue($result->success);
  663. }
  664. public function testCreate_ignoresPassedBillingAddressParamsForPaypalAccount()
  665. {
  666. $http = new HttpClientApi(Braintree\Configuration::$global);
  667. $nonce = $http->nonceForPaypalAccount([
  668. 'paypalAccount' => [
  669. 'consentCode' => 'PAYPAL_CONSENT_CODE',
  670. ]
  671. ]);
  672. $customer = Braintree\Customer::createNoValidate();
  673. $result = Braintree\PaymentMethod::create([
  674. 'paymentMethodNonce' => $nonce,
  675. 'customerId' => $customer->id,
  676. 'billingAddress' => [
  677. 'streetAddress' => '123 Abc Way'
  678. ]
  679. ]);
  680. $this->assertTrue($result->success);
  681. $this->assertTrue(is_a($result->paymentMethod, 'Braintree\PaypalAccount'));
  682. $token = $result->paymentMethod->token;
  683. $foundPaypalAccount = Braintree\PaypalAccount::find($token);
  684. $this->assertTrue(NULL != $foundPaypalAccount);
  685. }
  686. public function testCreate_ignoresPassedBillingAddressIdForPaypalAccount()
  687. {
  688. $http = new HttpClientApi(Braintree\Configuration::$global);
  689. $nonce = $http->nonceForPaypalAccount([
  690. 'paypalAccount' => [
  691. 'consentCode' => 'PAYPAL_CONSENT_CODE',
  692. ]
  693. ]);
  694. $customer = Braintree\Customer::createNoValidate();
  695. $result = Braintree\PaymentMethod::create([
  696. 'paymentMethodNonce' => $nonce,
  697. 'customerId' => $customer->id,
  698. 'billingAddressId' => 'address_id'
  699. ]);
  700. $this->assertTrue($result->success);
  701. $this->assertTrue(is_a($result->paymentMethod, 'Braintree\PaypalAccount'));
  702. $token = $result->paymentMethod->token;
  703. $foundPaypalAccount = Braintree\PaypalAccount::find($token);
  704. $this->assertTrue(NULL != $foundPaypalAccount);
  705. }
  706. public function testCreate_acceptsNumberAndOtherCreditCardParameters()
  707. {
  708. $customer = Braintree\Customer::createNoValidate();
  709. $result = Braintree\PaymentMethod::create([
  710. 'customerId' => $customer->id,
  711. 'paymentMethodNonce' => Braintree\Test\Nonces::$transactable,
  712. 'cardholderName' => 'Jane Doe',
  713. 'cvv' => '123',
  714. 'expirationMonth' => '10',
  715. 'expirationYear' => '24',
  716. 'number' => '4242424242424242'
  717. ]);
  718. $this->assertTrue($result->success);
  719. $this->assertTrue('Jane Doe' == $result->paymentMethod->cardholderName);
  720. $this->assertTrue('10' == $result->paymentMethod->expirationMonth);
  721. $this->assertTrue('2024' == $result->paymentMethod->expirationYear);
  722. $this->assertTrue('424242' == $result->paymentMethod->bin);
  723. $this->assertTrue('4242' == $result->paymentMethod->last4);
  724. }
  725. public function testFind_returnsCreditCards()
  726. {
  727. $paymentMethodToken = 'CREDIT_CARD_TOKEN-' . strval(rand());
  728. $customer = Braintree\Customer::createNoValidate();
  729. $creditCardResult = Braintree\CreditCard::create([
  730. 'customerId' => $customer->id,
  731. 'number' => '5105105105105100',
  732. 'expirationDate' => '05/2011',
  733. 'token' => $paymentMethodToken
  734. ]);
  735. $this->assertTrue($creditCardResult->success);
  736. $foundCreditCard = Braintree\PaymentMethod::find($creditCardResult->creditCard->token);
  737. $this->assertEquals($paymentMethodToken, $foundCreditCard->token);
  738. $this->assertEquals('510510', $foundCreditCard->bin);
  739. $this->assertEquals('5100', $foundCreditCard->last4);
  740. $this->assertEquals('05/2011', $foundCreditCard->expirationDate);
  741. }
  742. public function testFind_returnsCreditCardsWithSubscriptions()
  743. {
  744. $customer = Braintree\Customer::createNoValidate();
  745. $creditCardResult = Braintree\CreditCard::create([
  746. 'customerId' => $customer->id,
  747. 'number' => '5105105105105100',
  748. 'expirationDate' => '05/2011',
  749. ]);
  750. $this->assertTrue($creditCardResult->success);
  751. $subscriptionId = strval(rand());
  752. Braintree\Subscription::create([
  753. 'id' => $subscriptionId,
  754. 'paymentMethodToken' => $creditCardResult->creditCard->token,
  755. 'planId' => 'integration_trialless_plan',
  756. 'price' => '1.00'
  757. ]);
  758. $foundCreditCard = Braintree\PaymentMethod::find($creditCardResult->creditCard->token);
  759. $this->assertEquals($subscriptionId, $foundCreditCard->subscriptions[0]->id);
  760. $this->assertEquals('integration_trialless_plan', $foundCreditCard->subscriptions[0]->planId);
  761. $this->assertEquals('1.00', $foundCreditCard->subscriptions[0]->price);
  762. }
  763. public function testFind_returnsPayPalAccounts()
  764. {
  765. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  766. $customer = Braintree\Customer::createNoValidate();
  767. $http = new HttpClientApi(Braintree\Configuration::$global);
  768. $nonce = $http->nonceForPayPalAccount([
  769. 'paypal_account' => [
  770. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  771. 'token' => $paymentMethodToken
  772. ]
  773. ]);
  774. Braintree\PaymentMethod::create([
  775. 'customerId' => $customer->id,
  776. 'paymentMethodNonce' => $nonce
  777. ]);
  778. $foundPayPalAccount = Braintree\PaymentMethod::find($paymentMethodToken);
  779. $this->assertSame('jane.doe@example.com', $foundPayPalAccount->email);
  780. $this->assertSame($paymentMethodToken, $foundPayPalAccount->token);
  781. }
  782. public function testFind_returnsApplePayCards()
  783. {
  784. $paymentMethodToken = 'APPLE_PAY-' . strval(rand());
  785. $customer = Braintree\Customer::createNoValidate();
  786. $nonce = Braintree\Test\Nonces::$applePayVisa;
  787. Braintree\PaymentMethod::create([
  788. 'customerId' => $customer->id,
  789. 'paymentMethodNonce' => $nonce,
  790. 'token' => $paymentMethodToken
  791. ]);
  792. $foundApplePayCard = Braintree\PaymentMethod::find($paymentMethodToken);
  793. $this->assertSame($paymentMethodToken, $foundApplePayCard->token);
  794. $this->assertInstanceOf('Braintree\ApplePayCard', $foundApplePayCard);
  795. $this->assertTrue(intval($foundApplePayCard->expirationMonth) > 0);
  796. $this->assertTrue(intval($foundApplePayCard->expirationYear) > 0);
  797. }
  798. public function testFind_returnsAndroidPayCards()
  799. {
  800. $paymentMethodToken = 'ANDROID-PAY-' . strval(rand());
  801. $customer = Braintree\Customer::createNoValidate();
  802. $nonce = Braintree\Test\Nonces::$androidPay;
  803. Braintree\PaymentMethod::create([
  804. 'customerId' => $customer->id,
  805. 'paymentMethodNonce' => $nonce,
  806. 'token' => $paymentMethodToken
  807. ]);
  808. $foundAndroidPayCard = Braintree\PaymentMethod::find($paymentMethodToken);
  809. $this->assertSame($paymentMethodToken, $foundAndroidPayCard->token);
  810. $this->assertInstanceOf('Braintree\AndroidPayCard', $foundAndroidPayCard);
  811. $this->assertSame(Braintree\CreditCard::DISCOVER, $foundAndroidPayCard->virtualCardType);
  812. $this->assertSame("1117", $foundAndroidPayCard->virtualCardLast4);
  813. $this->assertSame(Braintree\CreditCard::DISCOVER, $foundAndroidPayCard->sourceCardType);
  814. $this->assertSame("1111", $foundAndroidPayCard->sourceCardLast4);
  815. $this->assertSame($customer->id, $foundAndroidPayCard->customerId);
  816. $this->assertTrue($foundAndroidPayCard->default);
  817. $this->assertContains('android_pay', $foundAndroidPayCard->imageUrl);
  818. $this->assertTrue(intval($foundAndroidPayCard->expirationMonth) > 0);
  819. $this->assertTrue(intval($foundAndroidPayCard->expirationYear) > 0);
  820. }
  821. public function testFind_returnsAbstractPaymentMethods()
  822. {
  823. $paymentMethodToken = 'ABSTRACT-' . strval(rand());
  824. $customer = Braintree\Customer::createNoValidate();
  825. $nonce = Braintree\Test\Nonces::$abstractTransactable;
  826. Braintree\PaymentMethod::create([
  827. 'customerId' => $customer->id,
  828. 'paymentMethodNonce' => $nonce,
  829. 'token' => $paymentMethodToken
  830. ]);
  831. $foundPaymentMethod = Braintree\PaymentMethod::find($paymentMethodToken);
  832. $this->assertSame($paymentMethodToken, $foundPaymentMethod-> token);
  833. }
  834. public function testFind_throwsIfCannotBeFound()
  835. {
  836. $this->setExpectedException('Braintree\Exception\NotFound');
  837. Braintree\PaymentMethod::find('NON_EXISTENT_TOKEN');
  838. }
  839. public function testUpdate_updatesTheCreditCard()
  840. {
  841. $customer = Braintree\Customer::createNoValidate();
  842. $creditCardResult = Braintree\CreditCard::create([
  843. 'cardholderName' => 'Original Holder',
  844. 'customerId' => $customer->id,
  845. 'cvv' => '123',
  846. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  847. 'expirationDate' => "05/2012"
  848. ]);
  849. $this->assertTrue($creditCardResult->success);
  850. $creditCard = $creditCardResult->creditCard;
  851. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  852. 'cardholderName' => 'New Holder',
  853. 'cvv' => '456',
  854. 'number' => Braintree\Test\CreditCardNumbers::$masterCard,
  855. 'expirationDate' => "06/2013"
  856. ]);
  857. $this->assertTrue($updateResult->success);
  858. $this->assertSame($updateResult->paymentMethod->token, $creditCard->token);
  859. $updatedCreditCard = $updateResult->paymentMethod;
  860. $this->assertSame("New Holder", $updatedCreditCard->cardholderName);
  861. $this->assertSame(substr(Braintree\Test\CreditCardNumbers::$masterCard, 0, 6), $updatedCreditCard->bin);
  862. $this->assertSame(substr(Braintree\Test\CreditCardNumbers::$masterCard, -4), $updatedCreditCard->last4);
  863. $this->assertSame("06/2013", $updatedCreditCard->expirationDate);
  864. }
  865. public function testUpdate_createsANewBillingAddressByDefault()
  866. {
  867. $customer = Braintree\Customer::createNoValidate();
  868. $creditCardResult = Braintree\CreditCard::create([
  869. 'customerId' => $customer->id,
  870. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  871. 'expirationDate' => "05/2012",
  872. 'billingAddress' => [
  873. 'streetAddress' => '123 Nigeria Ave'
  874. ]
  875. ]);
  876. $this->assertTrue($creditCardResult->success);
  877. $creditCard = $creditCardResult->creditCard;
  878. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  879. 'billingAddress' => [
  880. 'region' => 'IL'
  881. ]
  882. ]);
  883. $this->assertTrue($updateResult->success);
  884. $updatedCreditCard = $updateResult->paymentMethod;
  885. $this->assertSame("IL", $updatedCreditCard->billingAddress->region);
  886. $this->assertSame(NULL, $updatedCreditCard->billingAddress->streetAddress);
  887. $this->assertFalse($creditCard->billingAddress->id == $updatedCreditCard->billingAddress->id);
  888. }
  889. public function testUpdate_updatesTheBillingAddressIfOptionIsSpecified()
  890. {
  891. $customer = Braintree\Customer::createNoValidate();
  892. $creditCardResult = Braintree\CreditCard::create([
  893. 'customerId' => $customer->id,
  894. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  895. 'expirationDate' => "05/2012",
  896. 'billingAddress' => [
  897. 'streetAddress' => '123 Nigeria Ave'
  898. ]
  899. ]);
  900. $this->assertTrue($creditCardResult->success);
  901. $creditCard = $creditCardResult->creditCard;
  902. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  903. 'billingAddress' => [
  904. 'region' => 'IL',
  905. 'options' => [
  906. 'updateExisting' => 'true'
  907. ]
  908. ],
  909. ]);
  910. $this->assertTrue($updateResult->success);
  911. $updatedCreditCard = $updateResult->paymentMethod;
  912. $this->assertSame("IL", $updatedCreditCard->billingAddress->region);
  913. $this->assertSame("123 Nigeria Ave", $updatedCreditCard->billingAddress->streetAddress);
  914. $this->assertTrue($creditCard->billingAddress->id == $updatedCreditCard->billingAddress->id);
  915. }
  916. public function testUpdate_updatesTheCountryViaCodes()
  917. {
  918. $customer = Braintree\Customer::createNoValidate();
  919. $creditCardResult = Braintree\CreditCard::create([
  920. 'customerId' => $customer->id,
  921. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  922. 'expirationDate' => "05/2012",
  923. 'billingAddress' => [
  924. 'streetAddress' => '123 Nigeria Ave'
  925. ]
  926. ]);
  927. $this->assertTrue($creditCardResult->success);
  928. $creditCard = $creditCardResult->creditCard;
  929. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  930. 'billingAddress' => [
  931. 'countryName' => 'American Samoa',
  932. 'countryCodeAlpha2' => 'AS',
  933. 'countryCodeAlpha3' => 'ASM',
  934. 'countryCodeNumeric' => '016',
  935. 'options' => [
  936. 'updateExisting' => 'true'
  937. ]
  938. ],
  939. ]);
  940. $this->assertTrue($updateResult->success);
  941. $updatedCreditCard = $updateResult->paymentMethod;
  942. $this->assertSame("American Samoa", $updatedCreditCard->billingAddress->countryName);
  943. $this->assertSame("AS", $updatedCreditCard->billingAddress->countryCodeAlpha2);
  944. $this->assertSame("ASM", $updatedCreditCard->billingAddress->countryCodeAlpha3);
  945. $this->assertSame("016", $updatedCreditCard->billingAddress->countryCodeNumeric);
  946. }
  947. public function testUpdate_canPassExpirationMonthAndExpirationYear()
  948. {
  949. $customer = Braintree\Customer::createNoValidate();
  950. $creditCardResult = Braintree\CreditCard::create([
  951. 'customerId' => $customer->id,
  952. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  953. 'expirationDate' => "05/2012"
  954. ]);
  955. $this->assertTrue($creditCardResult->success);
  956. $creditCard = $creditCardResult->creditCard;
  957. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  958. 'number' => Braintree\Test\CreditCardNumbers::$masterCard,
  959. 'expirationMonth' => "07",
  960. 'expirationYear' => "2011"
  961. ]);
  962. $this->assertTrue($updateResult->success);
  963. $this->assertSame($updateResult->paymentMethod->token, $creditCard->token);
  964. $updatedCreditCard = $updateResult->paymentMethod;
  965. $this->assertSame("07", $updatedCreditCard->expirationMonth);
  966. $this->assertSame("07", $updatedCreditCard->expirationMonth);
  967. $this->assertSame("07/2011", $updatedCreditCard->expirationDate);
  968. }
  969. public function testUpdate_verifiesTheUpdateIfOptionsVerifyCardIsTrue()
  970. {
  971. $customer = Braintree\Customer::createNoValidate();
  972. $creditCardResult = Braintree\CreditCard::create([
  973. 'cardholderName' => 'Original Holder',
  974. 'customerId' => $customer->id,
  975. 'cvv' => '123',
  976. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  977. 'expirationDate' => "05/2012"
  978. ]);
  979. $this->assertTrue($creditCardResult->success);
  980. $creditCard = $creditCardResult->creditCard;
  981. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  982. 'cardholderName' => 'New Holder',
  983. 'cvv' => '456',
  984. 'number' => Braintree\Test\CreditCardNumbers::$failsSandboxVerification['MasterCard'],
  985. 'expirationDate' => "06/2013",
  986. 'options' => [
  987. 'verifyCard' => 'true'
  988. ]
  989. ]);
  990. $this->assertFalse($updateResult->success);
  991. $this->assertEquals(Braintree\Result\CreditCardVerification::PROCESSOR_DECLINED, $updateResult->creditCardVerification->status);
  992. $this->assertEquals(NULL, $updateResult->creditCardVerification->gatewayRejectionReason);
  993. }
  994. public function testUpdate_canPassCustomVerificationAmount()
  995. {
  996. $customer = Braintree\Customer::createNoValidate();
  997. $creditCardResult = Braintree\CreditCard::create([
  998. 'cardholderName' => 'Card Holder',
  999. 'customerId' => $customer->id,
  1000. 'cvv' => '123',
  1001. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  1002. 'expirationDate' => "05/2020"
  1003. ]);
  1004. $this->assertTrue($creditCardResult->success);
  1005. $creditCard = $creditCardResult->creditCard;
  1006. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  1007. 'paymentMethodNonce' => Braintree\Test\Nonces::$processorDeclinedMasterCard,
  1008. 'options' => [
  1009. 'verifyCard' => 'true',
  1010. 'verificationAmount' => '2.34'
  1011. ]
  1012. ]);
  1013. $this->assertFalse($updateResult->success);
  1014. $this->assertEquals(Braintree\Result\CreditCardVerification::PROCESSOR_DECLINED, $updateResult->creditCardVerification->status);
  1015. $this->assertEquals(NULL, $updateResult->creditCardVerification->gatewayRejectionReason);
  1016. }
  1017. public function testUpdate_canUpdateTheBillingAddress()
  1018. {
  1019. $customer = Braintree\Customer::createNoValidate();
  1020. $creditCardResult = Braintree\CreditCard::create([
  1021. 'cardholderName' => 'Original Holder',
  1022. 'customerId' => $customer->id,
  1023. 'cvv' => '123',
  1024. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  1025. 'expirationDate' => '05/2012',
  1026. 'billingAddress' => [
  1027. 'firstName' => 'Old First Name',
  1028. 'lastName' => 'Old Last Name',
  1029. 'company' => 'Old Company',
  1030. 'streetAddress' => '123 Old St',
  1031. 'extendedAddress' => 'Apt Old',
  1032. 'locality' => 'Old City',
  1033. 'region' => 'Old State',
  1034. 'postalCode' => '12345',
  1035. 'countryName' => 'Canada'
  1036. ]
  1037. ]);
  1038. $this->assertTrue($creditCardResult->success);
  1039. $creditCard = $creditCardResult->creditCard;
  1040. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  1041. 'billingAddress' => [
  1042. 'firstName' => 'New First Name',
  1043. 'lastName' => 'New Last Name',
  1044. 'company' => 'New Company',
  1045. 'streetAddress' => '123 New St',
  1046. 'extendedAddress' => 'Apt New',
  1047. 'locality' => 'New City',
  1048. 'region' => 'New State',
  1049. 'postalCode' => '56789',
  1050. 'countryName' => 'United States of America'
  1051. ]
  1052. ]);
  1053. $this->assertTrue($updateResult->success);
  1054. $address = $updateResult->paymentMethod->billingAddress;
  1055. $this->assertSame('New First Name', $address->firstName);
  1056. $this->assertSame('New Last Name', $address->lastName);
  1057. $this->assertSame('New Company', $address->company);
  1058. $this->assertSame('123 New St', $address->streetAddress);
  1059. $this->assertSame('Apt New', $address->extendedAddress);
  1060. $this->assertSame('New City', $address->locality);
  1061. $this->assertSame('New State', $address->region);
  1062. $this->assertSame('56789', $address->postalCode);
  1063. $this->assertSame('United States of America', $address->countryName);
  1064. }
  1065. public function testUpdate_returnsAnErrorIfInvalid()
  1066. {
  1067. $customer = Braintree\Customer::createNoValidate();
  1068. $creditCardResult = Braintree\CreditCard::create([
  1069. 'cardholderName' => 'Original Holder',
  1070. 'customerId' => $customer->id,
  1071. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  1072. 'expirationDate' => "05/2012"
  1073. ]);
  1074. $this->assertTrue($creditCardResult->success);
  1075. $creditCard = $creditCardResult->creditCard;
  1076. $updateResult = Braintree\PaymentMethod::update($creditCard->token, [
  1077. 'cardholderName' => 'New Holder',
  1078. 'number' => 'invalid',
  1079. 'expirationDate' => "05/2014",
  1080. ]);
  1081. $this->assertFalse($updateResult->success);
  1082. $numberErrors = $updateResult->errors->forKey('creditCard')->onAttribute('number');
  1083. $this->assertEquals("Credit card number must be 12-19 digits.", $numberErrors[0]->message);
  1084. }
  1085. public function testUpdate_canUpdateTheDefault()
  1086. {
  1087. $customer = Braintree\Customer::createNoValidate();
  1088. $creditCardResult1 = Braintree\CreditCard::create([
  1089. 'customerId' => $customer->id,
  1090. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  1091. 'expirationDate' => "05/2009"
  1092. ]);
  1093. $this->assertTrue($creditCardResult1->success);
  1094. $creditCard1 = $creditCardResult1->creditCard;
  1095. $creditCardResult2 = Braintree\CreditCard::create([
  1096. 'customerId' => $customer->id,
  1097. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  1098. 'expirationDate' => "05/2009"
  1099. ]);
  1100. $this->assertTrue($creditCardResult2->success);
  1101. $creditCard2 = $creditCardResult2->creditCard;
  1102. $this->assertTrue($creditCard1->default);
  1103. $this->assertFalse($creditCard2->default);
  1104. $updateResult = Braintree\PaymentMethod::update($creditCard2->token, [
  1105. 'options' => [
  1106. 'makeDefault' => 'true'
  1107. ]
  1108. ]);
  1109. $this->assertTrue($updateResult->success);
  1110. $this->assertFalse(Braintree\PaymentMethod::find($creditCard1->token)->default);
  1111. $this->assertTrue(Braintree\PaymentMethod::find($creditCard2->token)->default);
  1112. }
  1113. public function testUpdate_updatesAPaypalAccountsToken()
  1114. {
  1115. $customer = Braintree\Customer::createNoValidate();
  1116. $originalToken = 'paypal-account-' . strval(rand());
  1117. $customer = Braintree\Customer::createNoValidate();
  1118. $http = new HttpClientApi(Braintree\Configuration::$global);
  1119. $nonce = $http->nonceForPayPalAccount([
  1120. 'paypal_account' => [
  1121. 'consent_code' => 'consent-code',
  1122. 'token' => $originalToken
  1123. ]
  1124. ]);
  1125. $originalResult = Braintree\PaymentMethod::create([
  1126. 'paymentMethodNonce' => $nonce,
  1127. 'customerId' => $customer->id
  1128. ]);
  1129. $this->assertTrue($originalResult->success);
  1130. $originalPaypalAccount = $originalResult->paymentMethod;
  1131. $updatedToken = 'UPDATED_TOKEN-' . strval(rand());
  1132. $updateResult = Braintree\PaymentMethod::update($originalPaypalAccount->token, [
  1133. 'token' => $updatedToken
  1134. ]);
  1135. $this->assertTrue($updateResult->success);
  1136. $updatedPaypalAccount = Braintree\PaymentMethod::find($updatedToken);
  1137. $this->assertEquals($originalPaypalAccount->email, $updatedPaypalAccount->email);
  1138. $this->setExpectedException('Braintree\Exception\NotFound', 'payment method with token ' . $originalToken . ' not found');
  1139. Braintree\PaymentMethod::find($originalToken);
  1140. }
  1141. public function testUpdate_canMakeAPaypalAccountTheDefaultPaymentMethod()
  1142. {
  1143. $customer = Braintree\Customer::createNoValidate();
  1144. $creditCardResult = Braintree\CreditCard::create([
  1145. 'customerId' => $customer->id,
  1146. 'number' => Braintree\Test\CreditCardNumbers::$visa,
  1147. 'expirationDate' => '05/2009',
  1148. 'options' => [
  1149. 'makeDefault' => 'true'
  1150. ]
  1151. ]);
  1152. $this->assertTrue($creditCardResult->success);
  1153. $creditCard = $creditCardResult->creditCard;
  1154. $http = new HttpClientApi(Braintree\Configuration::$global);
  1155. $nonce = $http->nonceForPayPalAccount([
  1156. 'paypal_account' => [
  1157. 'consent_code' => 'consent-code',
  1158. ]
  1159. ]);
  1160. $originalToken = Braintree\PaymentMethod::create([
  1161. 'paymentMethodNonce' => $nonce,
  1162. 'customerId' => $customer->id
  1163. ])->paymentMethod->token;
  1164. $updateResult = Braintree\PaymentMethod::update($originalToken, [
  1165. 'options' => [
  1166. 'makeDefault' => 'true'
  1167. ]
  1168. ]);
  1169. $this->assertTrue($updateResult->success);
  1170. $updatedPaypalAccount = Braintree\PaymentMethod::find($originalToken);
  1171. $this->assertTrue($updatedPaypalAccount->default);
  1172. }
  1173. public function testUpdate_returnsAnErrorIfATokenForAccountIsUsedToAttemptAnUpdate()
  1174. {
  1175. $customer = Braintree\Customer::createNoValidate();
  1176. $firstToken = 'paypal-account-' . strval(rand());
  1177. $secondToken = 'paypal-account-' . strval(rand());
  1178. $http = new HttpClientApi(Braintree\Configuration::$global);
  1179. $firstNonce = $http->nonceForPayPalAccount([
  1180. 'paypal_account' => [
  1181. 'consent_code' => 'consent-code',
  1182. 'token' => $firstToken
  1183. ]
  1184. ]);
  1185. $firstResult = Braintree\PaymentMethod::create([
  1186. 'paymentMethodNonce' => $firstNonce,
  1187. 'customerId' => $customer->id
  1188. ]);
  1189. $this->assertTrue($firstResult->success);
  1190. $firstPaypalAccount = $firstResult->paymentMethod;
  1191. $http = new HttpClientApi(Braintree\Configuration::$global);
  1192. $secondNonce = $http->nonceForPayPalAccount([
  1193. 'paypal_account' => [
  1194. 'consent_code' => 'consent-code',
  1195. 'token' => $secondToken
  1196. ]
  1197. ]);
  1198. $secondResult = Braintree\PaymentMethod::create([
  1199. 'paymentMethodNonce' => $secondNonce,
  1200. 'customerId' => $customer->id
  1201. ]);
  1202. $this->assertTrue($secondResult->success);
  1203. $secondPaypalAccount = $firstResult->paymentMethod;
  1204. $updateResult = Braintree\PaymentMethod::update($firstToken, [
  1205. 'token' => $secondToken
  1206. ]);
  1207. $this->assertFalse($updateResult->success);
  1208. $resultErrors = $updateResult->errors->deepAll();
  1209. $this->assertEquals("92906", $resultErrors[0]->code);
  1210. }
  1211. public function testDelete_worksWithCreditCards()
  1212. {
  1213. $paymentMethodToken = 'CREDIT_CARD_TOKEN-' . strval(rand());
  1214. $customer = Braintree\Customer::createNoValidate();
  1215. $creditCardResult = Braintree\CreditCard::create([
  1216. 'customerId' => $customer->id,
  1217. 'number' => '5105105105105100',
  1218. 'expirationDate' => '05/2011',
  1219. 'token' => $paymentMethodToken
  1220. ]);
  1221. $this->assertTrue($creditCardResult->success);
  1222. Braintree\PaymentMethod::delete($paymentMethodToken);
  1223. $this->setExpectedException('Braintree\Exception\NotFound');
  1224. Braintree\PaymentMethod::find($paymentMethodToken);
  1225. self::integrationMerchantConfig();
  1226. }
  1227. public function testDelete_worksWithPayPalAccounts()
  1228. {
  1229. $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand());
  1230. $customer = Braintree\Customer::createNoValidate();
  1231. $http = new HttpClientApi(Braintree\Configuration::$global);
  1232. $nonce = $http->nonceForPayPalAccount([
  1233. 'paypal_account' => [
  1234. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  1235. 'token' => $paymentMethodToken
  1236. ]
  1237. ]);
  1238. $paypalAccountResult = Braintree\PaymentMethod::create([
  1239. 'customerId' => $customer->id,
  1240. 'paymentMethodNonce' => $nonce
  1241. ]);
  1242. $this->assertTrue($paypalAccountResult->success);
  1243. Braintree\PaymentMethod::delete($paymentMethodToken, ['revokeAllGrants' => false]);
  1244. $this->setExpectedException('Braintree\Exception\NotFound');
  1245. Braintree\PaymentMethod::find($paymentMethodToken);
  1246. }
  1247. public function testGrant_returnsASingleUseNonce()
  1248. {
  1249. $partnerMerchantGateway = new Braintree\Gateway([
  1250. 'environment' => 'development',
  1251. 'merchantId' => 'integration_merchant_public_id',
  1252. 'publicKey' => 'oauth_app_partner_user_public_key',
  1253. 'privateKey' => 'oauth_app_partner_user_private_key'
  1254. ]);
  1255. $customer = $partnerMerchantGateway->customer()->create([
  1256. 'firstName' => 'Joe',
  1257. 'lastName' => 'Brown'
  1258. ])->customer;
  1259. $creditCard = $partnerMerchantGateway->creditCard()->create([
  1260. 'customerId' => $customer->id,
  1261. 'cardholderName' => 'Adam Davis',
  1262. 'number' => '4111111111111111',
  1263. 'expirationDate' => '05/2009'
  1264. ])->creditCard;
  1265. $oauthAppGateway = new Braintree\Gateway([
  1266. 'clientId' => 'client_id$development$integration_client_id',
  1267. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1268. ]);
  1269. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1270. 'merchant_public_id' => 'integration_merchant_id',
  1271. 'scope' => 'grant_payment_method'
  1272. ]);
  1273. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1274. 'code' => $code,
  1275. ]);
  1276. $grantingGateway = new Braintree\Gateway([
  1277. 'accessToken' => $credentials->accessToken
  1278. ]);
  1279. $grantResult = $grantingGateway->paymentMethod()->grant($creditCard->token);
  1280. $this->assertTrue($grantResult->success);
  1281. $result = Braintree\Transaction::sale([
  1282. 'amount' => '100.00',
  1283. 'paymentMethodNonce' => $grantResult->paymentMethodNonce->nonce
  1284. ]);
  1285. $this->assertTrue($result->success);
  1286. $secondResult = Braintree\Transaction::sale([
  1287. 'amount' => '100.00',
  1288. 'paymentMethodNonce' => $grantResult->paymentMethodNonce->nonce
  1289. ]);
  1290. $this->assertFalse($secondResult->success);
  1291. }
  1292. public function testGrant_returnsANonceThatIsNotVaultable()
  1293. {
  1294. $partnerMerchantGateway = new Braintree\Gateway([
  1295. 'environment' => 'development',
  1296. 'merchantId' => 'integration_merchant_public_id',
  1297. 'publicKey' => 'oauth_app_partner_user_public_key',
  1298. 'privateKey' => 'oauth_app_partner_user_private_key'
  1299. ]);
  1300. $customer = $partnerMerchantGateway->customer()->create([
  1301. 'firstName' => 'Joe',
  1302. 'lastName' => 'Brown'
  1303. ])->customer;
  1304. $creditCard = $partnerMerchantGateway->creditCard()->create([
  1305. 'customerId' => $customer->id,
  1306. 'cardholderName' => 'Adam Davis',
  1307. 'number' => '4111111111111111',
  1308. 'expirationDate' => '05/2009'
  1309. ])->creditCard;
  1310. $oauthAppGateway = new Braintree\Gateway([
  1311. 'clientId' => 'client_id$development$integration_client_id',
  1312. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1313. ]);
  1314. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1315. 'merchant_public_id' => 'integration_merchant_id',
  1316. 'scope' => 'grant_payment_method'
  1317. ]);
  1318. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1319. 'code' => $code,
  1320. ]);
  1321. $grantingGateway = new Braintree\Gateway([
  1322. 'accessToken' => $credentials->accessToken
  1323. ]);
  1324. $grantResult = $grantingGateway->paymentMethod()->grant($creditCard->token, ['allow_vaulting' => false]);
  1325. $customer = $partnerMerchantGateway->customer()->create([
  1326. 'firstName' => 'Bob',
  1327. 'lastName' => 'Rob'
  1328. ])->customer;
  1329. $result = Braintree\PaymentMethod::create([
  1330. 'customerId' => $customer->id,
  1331. 'paymentMethodNonce' => $grantResult->paymentMethodNonce->nonce
  1332. ]);
  1333. $this->assertFalse($result->success);
  1334. }
  1335. public function testGrant_returnsANonceThatIsVaultableSnakeCase()
  1336. {
  1337. $partnerMerchantGateway = new Braintree\Gateway([
  1338. 'environment' => 'development',
  1339. 'merchantId' => 'integration_merchant_public_id',
  1340. 'publicKey' => 'oauth_app_partner_user_public_key',
  1341. 'privateKey' => 'oauth_app_partner_user_private_key'
  1342. ]);
  1343. $customer = $partnerMerchantGateway->customer()->create([
  1344. 'firstName' => 'Joe',
  1345. 'lastName' => 'Brown'
  1346. ])->customer;
  1347. $creditCard = $partnerMerchantGateway->creditCard()->create([
  1348. 'customerId' => $customer->id,
  1349. 'cardholderName' => 'Adam Davis',
  1350. 'number' => '4111111111111111',
  1351. 'expirationDate' => '05/2009'
  1352. ])->creditCard;
  1353. $oauthAppGateway = new Braintree\Gateway([
  1354. 'clientId' => 'client_id$development$integration_client_id',
  1355. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1356. ]);
  1357. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1358. 'merchant_public_id' => 'integration_merchant_id',
  1359. 'scope' => 'grant_payment_method'
  1360. ]);
  1361. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1362. 'code' => $code,
  1363. ]);
  1364. $grantingGateway = new Braintree\Gateway([
  1365. 'accessToken' => $credentials->accessToken
  1366. ]);
  1367. $grantResult = $grantingGateway->paymentMethod()->grant($creditCard->token, ['allow_vaulting' => true]);
  1368. $customer = Braintree\Customer::create([
  1369. 'firstName' => 'Bob',
  1370. 'lastName' => 'Rob'
  1371. ])->customer;
  1372. $result = Braintree\PaymentMethod::create([
  1373. 'customerId' => $customer->id,
  1374. 'paymentMethodNonce' => $grantResult->paymentMethodNonce->nonce
  1375. ]);
  1376. $this->assertTrue($result->success);
  1377. }
  1378. public function testGrant_returnsANonceThatIsVaultableCamelCase()
  1379. {
  1380. $partnerMerchantGateway = new Braintree\Gateway([
  1381. 'environment' => 'development',
  1382. 'merchantId' => 'integration_merchant_public_id',
  1383. 'publicKey' => 'oauth_app_partner_user_public_key',
  1384. 'privateKey' => 'oauth_app_partner_user_private_key'
  1385. ]);
  1386. $customer = $partnerMerchantGateway->customer()->create([
  1387. 'firstName' => 'Joe',
  1388. 'lastName' => 'Brown'
  1389. ])->customer;
  1390. $creditCard = $partnerMerchantGateway->creditCard()->create([
  1391. 'customerId' => $customer->id,
  1392. 'cardholderName' => 'Adam Davis',
  1393. 'number' => '4111111111111111',
  1394. 'expirationDate' => '05/2009'
  1395. ])->creditCard;
  1396. $oauthAppGateway = new Braintree\Gateway([
  1397. 'clientId' => 'client_id$development$integration_client_id',
  1398. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1399. ]);
  1400. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1401. 'merchant_public_id' => 'integration_merchant_id',
  1402. 'scope' => 'grant_payment_method'
  1403. ]);
  1404. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1405. 'code' => $code,
  1406. ]);
  1407. $grantingGateway = new Braintree\Gateway([
  1408. 'accessToken' => $credentials->accessToken
  1409. ]);
  1410. $grantResult = $grantingGateway->paymentMethod()->grant($creditCard->token, ['allowVaulting' => true, 'includeBillingPostalCode' => true]);
  1411. $customer = Braintree\Customer::create([
  1412. 'firstName' => 'Bob',
  1413. 'lastName' => 'Rob'
  1414. ])->customer;
  1415. $result = Braintree\PaymentMethod::create([
  1416. 'customerId' => $customer->id,
  1417. 'paymentMethodNonce' => $grantResult->paymentMethodNonce->nonce
  1418. ]);
  1419. $this->assertTrue($result->success);
  1420. }
  1421. public function testGrant_raisesAnErrorIfTokenIsNotFound()
  1422. {
  1423. $oauthAppGateway = new Braintree\Gateway([
  1424. 'clientId' => 'client_id$development$integration_client_id',
  1425. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1426. ]);
  1427. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1428. 'merchant_public_id' => 'integration_merchant_id',
  1429. 'scope' => 'grant_payment_method'
  1430. ]);
  1431. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1432. 'code' => $code,
  1433. ]);
  1434. $grantingGateway = new Braintree\Gateway([
  1435. 'accessToken' => $credentials->accessToken
  1436. ]);
  1437. $this->setExpectedException('Braintree\Exception\NotFound');
  1438. $grantResult = $grantingGateway->paymentMethod()->grant("not_a_real_token", false);
  1439. }
  1440. public function testRevoke_rendersANonceUnusable()
  1441. {
  1442. $partnerMerchantGateway = new Braintree\Gateway([
  1443. 'environment' => 'development',
  1444. 'merchantId' => 'integration_merchant_public_id',
  1445. 'publicKey' => 'oauth_app_partner_user_public_key',
  1446. 'privateKey' => 'oauth_app_partner_user_private_key'
  1447. ]);
  1448. $customer = $partnerMerchantGateway->customer()->create([
  1449. 'firstName' => 'Joe',
  1450. 'lastName' => 'Brown'
  1451. ])->customer;
  1452. $creditCard = $partnerMerchantGateway->creditCard()->create([
  1453. 'customerId' => $customer->id,
  1454. 'cardholderName' => 'Adam Davis',
  1455. 'number' => '4111111111111111',
  1456. 'expirationDate' => '05/2009'
  1457. ])->creditCard;
  1458. $oauthAppGateway = new Braintree\Gateway([
  1459. 'clientId' => 'client_id$development$integration_client_id',
  1460. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1461. ]);
  1462. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1463. 'merchant_public_id' => 'integration_merchant_id',
  1464. 'scope' => 'grant_payment_method'
  1465. ]);
  1466. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1467. 'code' => $code,
  1468. ]);
  1469. $grantingGateway = new Braintree\Gateway([
  1470. 'accessToken' => $credentials->accessToken
  1471. ]);
  1472. $grantResult = $grantingGateway->paymentMethod()->grant($creditCard->token, false);
  1473. $revokeResult = $grantingGateway->paymentMethod()->revoke($creditCard->token);
  1474. $this->assertTrue($revokeResult->success);
  1475. $result = Braintree\Transaction::sale([
  1476. 'amount' => '100.00',
  1477. 'paymentMethodNonce' => $grantResult->paymentMethodNonce->nonce
  1478. ]);
  1479. $this->assertFalse($result->success);
  1480. }
  1481. public function testRevoke_raisesAnErrorIfTokenIsNotFound()
  1482. {
  1483. $oauthAppGateway = new Braintree\Gateway([
  1484. 'clientId' => 'client_id$development$integration_client_id',
  1485. 'clientSecret' => 'client_secret$development$integration_client_secret'
  1486. ]);
  1487. $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, [
  1488. 'merchant_public_id' => 'integration_merchant_id',
  1489. 'scope' => 'grant_payment_method'
  1490. ]);
  1491. $credentials = $oauthAppGateway->oauth()->createTokenFromCode([
  1492. 'code' => $code,
  1493. ]);
  1494. $grantingGateway = new Braintree\Gateway([
  1495. 'accessToken' => $credentials->accessToken
  1496. ]);
  1497. $this->setExpectedException('Braintree\Exception\NotFound');
  1498. $grantResult = $grantingGateway->paymentMethod()->revoke("not_a_real_token");
  1499. }
  1500. }