CustomerTest.php 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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 CustomerTest extends Setup
  8. {
  9. public function testAll_smokeTest()
  10. {
  11. $all = Braintree\Customer::all();
  12. $this->assertTrue($all->maximumCount() > 0);
  13. }
  14. public function testAllWithManyResults()
  15. {
  16. $collection = Braintree\Customer::all();
  17. $this->assertTrue($collection->maximumCount() > 1);
  18. $customer = $collection->firstItem();
  19. $this->assertTrue(strlen($customer->id) > 0);
  20. $this->assertTrue($customer instanceof Braintree\Customer);
  21. }
  22. public function testCreate()
  23. {
  24. $result = Braintree\Customer::create([
  25. 'firstName' => 'Mike',
  26. 'lastName' => 'Jones',
  27. 'company' => 'Jones Co.',
  28. 'email' => 'mike.jones@example.com',
  29. 'phone' => '419.555.1234',
  30. 'fax' => '419.555.1235',
  31. 'website' => 'http://example.com'
  32. ]);
  33. $this->assertEquals(true, $result->success);
  34. $customer = $result->customer;
  35. $this->assertEquals('Mike', $customer->firstName);
  36. $this->assertEquals('Jones', $customer->lastName);
  37. $this->assertEquals('Jones Co.', $customer->company);
  38. $this->assertEquals('mike.jones@example.com', $customer->email);
  39. $this->assertEquals('419.555.1234', $customer->phone);
  40. $this->assertEquals('419.555.1235', $customer->fax);
  41. $this->assertEquals('http://example.com', $customer->website);
  42. $this->assertNotNull($customer->merchantId);
  43. }
  44. public function testCreateWithIdOfZero()
  45. {
  46. $result = Braintree\Customer::create([
  47. 'id' => '0'
  48. ]);
  49. $this->assertEquals(true, $result->success);
  50. $this->assertEquals($result->customer->id, '0');
  51. $customer = Braintree\Customer::find('0');
  52. $this->assertEquals('0', $customer->id);
  53. Braintree\Customer::delete('0');
  54. }
  55. public function testGatewayCreate()
  56. {
  57. $gateway = new Braintree\Gateway([
  58. 'environment' => 'development',
  59. 'merchantId' => 'integration_merchant_id',
  60. 'publicKey' => 'integration_public_key',
  61. 'privateKey' => 'integration_private_key'
  62. ]);
  63. $result = $gateway->customer()->create([
  64. 'firstName' => 'Mike',
  65. 'lastName' => 'Jones',
  66. ]);
  67. $this->assertEquals(true, $result->success);
  68. $customer = $result->customer;
  69. $this->assertEquals('Mike', $customer->firstName);
  70. $this->assertEquals('Jones', $customer->lastName);
  71. $this->assertNotNull($customer->merchantId);
  72. }
  73. public function testCreateWithAccessToken()
  74. {
  75. $credentials = Test\Braintree\OAuthTestHelper::createCredentials([
  76. 'clientId' => 'client_id$development$integration_client_id',
  77. 'clientSecret' => 'client_secret$development$integration_client_secret',
  78. 'merchantId' => 'integration_merchant_id',
  79. ]);
  80. $gateway = new Braintree\Gateway([
  81. 'accessToken' => $credentials->accessToken,
  82. ]);
  83. $result = $gateway->customer()->create([
  84. 'firstName' => 'Mike',
  85. 'lastName' => 'Jones',
  86. ]);
  87. $this->assertEquals(true, $result->success);
  88. $customer = $result->customer;
  89. $this->assertEquals('Mike', $customer->firstName);
  90. $this->assertEquals('Jones', $customer->lastName);
  91. $this->assertNotNull($customer->merchantId);
  92. }
  93. public function testCreateCustomerWithCardUsingNonce()
  94. {
  95. $http = new HttpClientApi(Braintree\Configuration::$global);
  96. $nonce = $http->nonce_for_new_card([
  97. "creditCard" => [
  98. "number" => "4111111111111111",
  99. "expirationMonth" => "11",
  100. "expirationYear" => "2099"
  101. ],
  102. "share" => true
  103. ]);
  104. $result = Braintree\Customer::create([
  105. 'creditCard' => [
  106. 'paymentMethodNonce' => $nonce
  107. ]
  108. ]);
  109. $this->assertTrue($result->success);
  110. $this->assertSame("411111", $result->customer->creditCards[0]->bin);
  111. $this->assertSame("1111", $result->customer->creditCards[0]->last4);
  112. }
  113. public function testCreateCustomerWithApplePayCard()
  114. {
  115. $nonce = Braintree\Test\Nonces::$applePayVisa;
  116. $result = Braintree\Customer::create([
  117. 'paymentMethodNonce' => $nonce
  118. ]);
  119. $this->assertTrue($result->success);
  120. $customer = $result->customer;
  121. $this->assertNotNull($customer->applePayCards[0]);
  122. $this->assertNotNull($customer->paymentMethods[0]);
  123. }
  124. public function testCreateCustomerWithAndroidPayProxyCard()
  125. {
  126. $nonce = Braintree\Test\Nonces::$androidPayDiscover;
  127. $result = Braintree\Customer::create([
  128. 'paymentMethodNonce' => $nonce
  129. ]);
  130. $this->assertTrue($result->success);
  131. $customer = $result->customer;
  132. $this->assertNotNull($customer->androidPayCards[0]);
  133. $this->assertNotNull($customer->paymentMethods[0]);
  134. $androidPayCard = $customer->androidPayCards[0];
  135. $this->assertTrue($androidPayCard instanceof Braintree\AndroidPayCard);
  136. $this->assertNotNull($androidPayCard->token);
  137. $this->assertNotNull($androidPayCard->expirationYear);
  138. }
  139. public function testCreateCustomerWithAndroidPayNetworkToken()
  140. {
  141. $nonce = Braintree\Test\Nonces::$androidPayMasterCard;
  142. $result = Braintree\Customer::create([
  143. 'paymentMethodNonce' => $nonce
  144. ]);
  145. $this->assertTrue($result->success);
  146. $customer = $result->customer;
  147. $this->assertNotNull($customer->androidPayCards[0]);
  148. $this->assertNotNull($customer->paymentMethods[0]);
  149. $androidPayCard = $customer->androidPayCards[0];
  150. $this->assertTrue($androidPayCard instanceof Braintree\AndroidPayCard);
  151. $this->assertNotNull($androidPayCard->token);
  152. $this->assertNotNull($androidPayCard->expirationYear);
  153. }
  154. public function testCreateCustomerWithAmexExpressCheckoutCard()
  155. {
  156. $nonce = Braintree\Test\Nonces::$amexExpressCheckout;
  157. $result = Braintree\Customer::create([
  158. 'paymentMethodNonce' => $nonce
  159. ]);
  160. $this->assertTrue($result->success);
  161. $customer = $result->customer;
  162. $this->assertNotNull($customer->amexExpressCheckoutCards[0]);
  163. $this->assertNotNull($customer->paymentMethods[0]);
  164. $amexExpressCheckoutCard = $customer->amexExpressCheckoutCards[0];
  165. $this->assertTrue($amexExpressCheckoutCard instanceof Braintree\AmexExpressCheckoutCard);
  166. $this->assertNotNull($amexExpressCheckoutCard->token);
  167. $this->assertNotNull($amexExpressCheckoutCard->expirationYear);
  168. }
  169. public function testCreateCustomerWithVenmoAccount()
  170. {
  171. $nonce = Braintree\Test\Nonces::$venmoAccount;
  172. $result = Braintree\Customer::create(array(
  173. 'paymentMethodNonce' => $nonce
  174. ));
  175. $this->assertTrue($result->success);
  176. $customer = $result->customer;
  177. $this->assertNotNull($customer->venmoAccounts[0]);
  178. $this->assertNotNull($customer->paymentMethods[0]);
  179. $venmoAccount = $customer->venmoAccounts[0];
  180. $this->assertTrue($venmoAccount instanceof Braintree\VenmoAccount);
  181. $this->assertNotNull($venmoAccount->token);
  182. $this->assertNotNull($venmoAccount->username);
  183. $this->assertNotNull($venmoAccount->venmoUserId);
  184. }
  185. public function testCannotCreateCustomerWithCoinbase()
  186. {
  187. $nonce = Braintree\Test\Nonces::$coinbase;
  188. $result = Braintree\Customer::create([
  189. 'paymentMethodNonce' => $nonce
  190. ]);
  191. $this->assertFalse($result->success);
  192. $this->assertEquals(Braintree\Error\Codes::PAYMENT_METHOD_NO_LONGER_SUPPORTED, $result->errors->forKey('coinbaseAccount')->onAttribute('base')[0]->code);
  193. }
  194. public function testCreateCustomerWithUsBankAccount()
  195. {
  196. $nonce = Test\Helper::generateValidUsBankAccountNonce();
  197. $result = Braintree\Customer::create(array(
  198. 'paymentMethodNonce' => $nonce,
  199. 'creditCard' => [
  200. 'options' => [
  201. 'verificationMerchantAccountId' => Test\Helper::usBankMerchantAccount()
  202. ]
  203. ]
  204. ));
  205. $this->assertTrue($result->success);
  206. $customer = $result->customer;
  207. $this->assertNotNull($customer->usBankAccounts[0]);
  208. $this->assertNotNull($customer->paymentMethods[0]);
  209. $usBankAccount = $customer->usBankAccounts[0];
  210. $this->assertTrue($usBankAccount instanceof Braintree\UsBankAccount);
  211. $this->assertNotNull($usBankAccount->token);
  212. $this->assertEquals('Dan Schulman', $usBankAccount->accountHolderName);
  213. $this->assertEquals('021000021', $usBankAccount->routingNumber);
  214. $this->assertEquals('1234', $usBankAccount->last4);
  215. $this->assertEquals('checking', $usBankAccount->accountType);
  216. $this->assertRegexp('/CHASE/', $usBankAccount->bankName);
  217. }
  218. public function testCreate_withUnicode()
  219. {
  220. $result = Braintree\Customer::create([
  221. 'firstName' => "Здравствуйте",
  222. 'lastName' => 'Jones',
  223. 'company' => 'Jones Co.',
  224. 'email' => 'mike.jones@example.com',
  225. 'phone' => '419.555.1234',
  226. 'fax' => '419.555.1235',
  227. 'website' => 'http://example.com'
  228. ]);
  229. $this->assertEquals(true, $result->success);
  230. $customer = $result->customer;
  231. $this->assertEquals("Здравствуйте", $customer->firstName);
  232. $this->assertEquals('Jones', $customer->lastName);
  233. $this->assertEquals('Jones Co.', $customer->company);
  234. $this->assertEquals('mike.jones@example.com', $customer->email);
  235. $this->assertEquals('419.555.1234', $customer->phone);
  236. $this->assertEquals('419.555.1235', $customer->fax);
  237. $this->assertEquals('http://example.com', $customer->website);
  238. $this->assertNotNull($customer->merchantId);
  239. }
  240. public function testCreate_withCountry()
  241. {
  242. $result = Braintree\Customer::create([
  243. 'firstName' => 'Bat',
  244. 'lastName' => 'Manderson',
  245. 'creditCard' => [
  246. 'number' => '5105105105105100',
  247. 'expirationDate' => '05/12',
  248. 'billingAddress' => [
  249. 'countryName' => 'Gabon',
  250. 'countryCodeAlpha2' => 'GA',
  251. 'countryCodeAlpha3' => 'GAB',
  252. 'countryCodeNumeric' => '266'
  253. ]
  254. ]
  255. ]);
  256. $this->assertEquals(true, $result->success);
  257. $customer = $result->customer;
  258. $this->assertEquals('Gabon', $customer->creditCards[0]->billingAddress->countryName);
  259. $this->assertEquals('GA', $customer->creditCards[0]->billingAddress->countryCodeAlpha2);
  260. $this->assertEquals('GAB', $customer->creditCards[0]->billingAddress->countryCodeAlpha3);
  261. $this->assertEquals('266', $customer->creditCards[0]->billingAddress->countryCodeNumeric);
  262. $this->assertEquals(1, preg_match('/\A\w{32}\z/', $customer->creditCards[0]->uniqueNumberIdentifier));
  263. }
  264. public function testCreate_withVenmoSdkSession()
  265. {
  266. $result = Braintree\Customer::create([
  267. 'firstName' => 'Bat',
  268. 'lastName' => 'Manderson',
  269. 'creditCard' => [
  270. 'number' => '5105105105105100',
  271. 'expirationDate' => '05/12',
  272. 'options' => [
  273. 'venmoSdkSession' => Braintree\Test\VenmoSdk::getTestSession()
  274. ]
  275. ]
  276. ]);
  277. $this->assertEquals(true, $result->success);
  278. $customer = $result->customer;
  279. $this->assertEquals(false, $customer->creditCards[0]->venmoSdk);
  280. }
  281. public function testCreate_withVenmoSdkPaymentMethodCode()
  282. {
  283. $result = Braintree\Customer::create([
  284. 'firstName' => 'Bat',
  285. 'lastName' => 'Manderson',
  286. 'creditCard' => [
  287. 'venmoSdkPaymentMethodCode' => Braintree\Test\VenmoSdk::$visaPaymentMethodCode
  288. ],
  289. ]);
  290. $this->assertEquals(true, $result->success);
  291. $customer = $result->customer;
  292. $this->assertEquals("411111", $customer->creditCards[0]->bin);
  293. }
  294. public function testCreate_blankCustomer()
  295. {
  296. $result = Braintree\Customer::create();
  297. $this->assertEquals(true, $result->success);
  298. $this->assertNotNull($result->customer->id);
  299. $result = Braintree\Customer::create([]);
  300. $this->assertEquals(true, $result->success);
  301. $this->assertNotNull($result->customer->id);
  302. }
  303. public function testCreate_withSpecialChars()
  304. {
  305. $result = Braintree\Customer::create(['firstName' => '<>&"\'']);
  306. $this->assertEquals(true, $result->success);
  307. $this->assertEquals('<>&"\'', $result->customer->firstName);
  308. }
  309. public function testCreate_withCustomFields()
  310. {
  311. $result = Braintree\Customer::create([
  312. 'firstName' => 'Mike',
  313. 'customFields' => [
  314. 'store_me' => 'some custom value'
  315. ]
  316. ]);
  317. $this->assertEquals(true, $result->success);
  318. $customFields = $result->customer->customFields;
  319. $this->assertEquals('some custom value', $customFields['store_me']);
  320. }
  321. public function testCreate_withFraudParams()
  322. {
  323. $result = Braintree\Customer::create([
  324. 'firstName' => 'Mike',
  325. 'creditCard' => [
  326. 'number' => '5105105105105100',
  327. 'expirationDate' => '05/12',
  328. 'cvv' => '123',
  329. 'cardholderName' => 'Mike Jones',
  330. 'deviceSessionId' => 'abc123',
  331. 'fraudMerchantId' => '456'
  332. ]
  333. ]);
  334. $this->assertEquals(true, $result->success);
  335. }
  336. public function testCreate_withRiskData()
  337. {
  338. $result = Braintree\Customer::create([
  339. 'firstName' => 'Mike',
  340. 'creditCard' => [
  341. 'number' => '5105105105105100',
  342. 'expirationDate' => '05/12',
  343. 'cvv' => '123',
  344. 'cardholderName' => 'Mike Jones',
  345. ],
  346. 'riskData' => [
  347. 'customer_browser' => 'IE5',
  348. 'customer_ip' => '192.168.0.1'
  349. ]
  350. ]);
  351. $this->assertEquals(true, $result->success);
  352. }
  353. public function testCreate_withCreditCard()
  354. {
  355. $result = Braintree\Customer::create([
  356. 'firstName' => 'Mike',
  357. 'lastName' => 'Jones',
  358. 'company' => 'Jones Co.',
  359. 'email' => 'mike.jones@example.com',
  360. 'phone' => '419.555.1234',
  361. 'fax' => '419.555.1235',
  362. 'website' => 'http://example.com',
  363. 'creditCard' => [
  364. 'number' => '5105105105105100',
  365. 'expirationDate' => '05/12',
  366. 'cvv' => '123',
  367. 'cardholderName' => 'Mike Jones'
  368. ]
  369. ]);
  370. $this->assertEquals(true, $result->success);
  371. $customer = $result->customer;
  372. $this->assertEquals('Mike', $customer->firstName);
  373. $this->assertEquals('Jones', $customer->lastName);
  374. $this->assertEquals('Jones Co.', $customer->company);
  375. $this->assertEquals('mike.jones@example.com', $customer->email);
  376. $this->assertEquals('419.555.1234', $customer->phone);
  377. $this->assertEquals('419.555.1235', $customer->fax);
  378. $this->assertEquals('http://example.com', $customer->website);
  379. $creditCard = $customer->creditCards[0];
  380. $this->assertEquals('510510', $creditCard->bin);
  381. $this->assertEquals('5100', $creditCard->last4);
  382. $this->assertEquals('Mike Jones', $creditCard->cardholderName);
  383. $this->assertEquals('05/2012', $creditCard->expirationDate);
  384. $this->assertEquals('05', $creditCard->expirationMonth);
  385. $this->assertEquals('2012', $creditCard->expirationYear);
  386. }
  387. public function testCreate_withDuplicateCardCheck()
  388. {
  389. $customer = Braintree\Customer::createNoValidate();
  390. $attributes = [
  391. 'firstName' => 'Mike',
  392. 'lastName' => 'Jones',
  393. 'company' => 'Jones Co.',
  394. 'email' => 'mike.jones@example.com',
  395. 'phone' => '419.555.1234',
  396. 'fax' => '419.555.1235',
  397. 'website' => 'http://example.com',
  398. 'creditCard' => [
  399. 'number' => '5105105105105100',
  400. 'expirationDate' => '05/12',
  401. 'cvv' => '123',
  402. 'cardholderName' => 'Mike Jones',
  403. 'options' => [
  404. 'failOnDuplicatePaymentMethod' => true
  405. ]
  406. ]
  407. ];
  408. Braintree\Customer::create($attributes);
  409. $result = Braintree\Customer::create($attributes);
  410. $this->assertFalse($result->success);
  411. $errors = $result->errors->forKey('customer')->forKey('creditCard')->onAttribute('number');
  412. $this->assertEquals(Braintree\Error\Codes::CREDIT_CARD_DUPLICATE_CARD_EXISTS, $errors[0]->code);
  413. $this->assertEquals(1, preg_match('/Duplicate card exists in the vault\./', $result->message));
  414. }
  415. public function testCreate_withCreditCardAndSpecificVerificationMerchantAccount()
  416. {
  417. $result = Braintree\Customer::create([
  418. 'firstName' => 'Mike',
  419. 'lastName' => 'Jones',
  420. 'company' => 'Jones Co.',
  421. 'email' => 'mike.jones@example.com',
  422. 'phone' => '419.555.1234',
  423. 'fax' => '419.555.1235',
  424. 'website' => 'http://example.com',
  425. 'creditCard' => [
  426. 'number' => '5105105105105100',
  427. 'expirationDate' => '05/12',
  428. 'cvv' => '123',
  429. 'cardholderName' => 'Mike Jones',
  430. 'options' => [
  431. 'verificationMerchantAccountId' => Test\Helper::nonDefaultMerchantAccountId(),
  432. 'verifyCard' => true
  433. ]
  434. ]
  435. ]);
  436. Test\Helper::assertPrintable($result);
  437. $this->assertFalse($result->success);
  438. $this->assertEquals(Braintree\Result\CreditCardVerification::PROCESSOR_DECLINED, $result->creditCardVerification->status);
  439. $this->assertEquals('2000', $result->creditCardVerification->processorResponseCode);
  440. $this->assertEquals('Do Not Honor', $result->creditCardVerification->processorResponseText);
  441. $this->assertEquals('M', $result->creditCardVerification->cvvResponseCode);
  442. $this->assertEquals(null, $result->creditCardVerification->avsErrorResponseCode);
  443. $this->assertEquals('I', $result->creditCardVerification->avsPostalCodeResponseCode);
  444. $this->assertEquals('I', $result->creditCardVerification->avsStreetAddressResponseCode);
  445. }
  446. public function testCreate_withCreditCardAndVerificationAmount()
  447. {
  448. $result = Braintree\Customer::create([
  449. 'firstName' => 'Mike',
  450. 'lastName' => 'Jones',
  451. 'company' => 'Jones Co.',
  452. 'email' => 'mike.jones@example.com',
  453. 'phone' => '419.555.1234',
  454. 'fax' => '419.555.1235',
  455. 'website' => 'http://example.com',
  456. 'creditCard' => [
  457. 'number' => '5555555555554444',
  458. 'expirationDate' => '05/12',
  459. 'cvv' => '123',
  460. 'cardholderName' => 'Mike Jones',
  461. 'options' => [
  462. 'verifyCard' => true,
  463. 'verificationAmount' => '2.00'
  464. ]
  465. ]
  466. ]);
  467. $this->assertTrue($result->success);
  468. }
  469. public function testCreate_withCreditCardAndBillingAddress()
  470. {
  471. $result = Braintree\Customer::create([
  472. 'firstName' => 'Mike',
  473. 'lastName' => 'Jones',
  474. 'company' => 'Jones Co.',
  475. 'email' => 'mike.jones@example.com',
  476. 'phone' => '419.555.1234',
  477. 'fax' => '419.555.1235',
  478. 'website' => 'http://example.com',
  479. 'creditCard' => [
  480. 'number' => '5105105105105100',
  481. 'expirationDate' => '05/12',
  482. 'cvv' => '123',
  483. 'cardholderName' => 'Mike Jones',
  484. 'billingAddress' => [
  485. 'firstName' => 'Drew',
  486. 'lastName' => 'Smith',
  487. 'company' => 'Smith Co.',
  488. 'streetAddress' => '1 E Main St',
  489. 'extendedAddress' => 'Suite 101',
  490. 'locality' => 'Chicago',
  491. 'region' => 'IL',
  492. 'postalCode' => '60622',
  493. 'countryName' => 'United States of America'
  494. ]
  495. ]
  496. ]);
  497. Test\Helper::assertPrintable($result);
  498. $this->assertEquals(true, $result->success);
  499. $customer = $result->customer;
  500. $this->assertEquals('Mike', $customer->firstName);
  501. $this->assertEquals('Jones', $customer->lastName);
  502. $this->assertEquals('Jones Co.', $customer->company);
  503. $this->assertEquals('mike.jones@example.com', $customer->email);
  504. $this->assertEquals('419.555.1234', $customer->phone);
  505. $this->assertEquals('419.555.1235', $customer->fax);
  506. $this->assertEquals('http://example.com', $customer->website);
  507. $creditCard = $customer->creditCards[0];
  508. $this->assertEquals('510510', $creditCard->bin);
  509. $this->assertEquals('5100', $creditCard->last4);
  510. $this->assertEquals('Mike Jones', $creditCard->cardholderName);
  511. $this->assertEquals('05/2012', $creditCard->expirationDate);
  512. $this->assertEquals('05', $creditCard->expirationMonth);
  513. $this->assertEquals('2012', $creditCard->expirationYear);
  514. $address = $customer->addresses[0];
  515. $this->assertEquals($address, $creditCard->billingAddress);
  516. $this->assertEquals('Drew', $address->firstName);
  517. $this->assertEquals('Smith', $address->lastName);
  518. $this->assertEquals('Smith Co.', $address->company);
  519. $this->assertEquals('1 E Main St', $address->streetAddress);
  520. $this->assertEquals('Suite 101', $address->extendedAddress);
  521. $this->assertEquals('Chicago', $address->locality);
  522. $this->assertEquals('IL', $address->region);
  523. $this->assertEquals('60622', $address->postalCode);
  524. $this->assertEquals('United States of America', $address->countryName);
  525. }
  526. public function testCreate_withValidationErrors()
  527. {
  528. $result = Braintree\Customer::create([
  529. 'email' => 'invalid',
  530. 'creditCard' => [
  531. 'number' => 'invalid',
  532. 'billingAddress' => [
  533. 'streetAddress' => str_repeat('x', 256)
  534. ]
  535. ]
  536. ]);
  537. Test\Helper::assertPrintable($result);
  538. $this->assertEquals(false, $result->success);
  539. $errors = $result->errors->forKey('customer')->onAttribute('email');
  540. $this->assertEquals(Braintree\Error\Codes::CUSTOMER_EMAIL_IS_INVALID, $errors[0]->code);
  541. $errors = $result->errors->forKey('customer')->forKey('creditCard')->onAttribute('number');
  542. $this->assertEquals(Braintree\Error\Codes::CREDIT_CARD_NUMBER_INVALID_LENGTH, $errors[0]->code);
  543. $errors = $result->errors->forKey('customer')->forKey('creditCard')->forKey('billingAddress')->onAttribute('streetAddress');
  544. $this->assertEquals(Braintree\Error\Codes::ADDRESS_STREET_ADDRESS_IS_TOO_LONG, $errors[0]->code);
  545. }
  546. public function testCreate_countryValidations_inconsistency()
  547. {
  548. $result = Braintree\Customer::create([
  549. 'creditCard' => [
  550. 'billingAddress' => [
  551. 'countryName' => 'Georgia',
  552. 'countryCodeAlpha2' => 'TF'
  553. ]
  554. ]
  555. ]);
  556. $this->assertEquals(false, $result->success);
  557. $errors = $result->errors->forKey('customer')->forKey('creditCard')->forKey('billingAddress')->onAttribute('base');
  558. $this->assertEquals(Braintree\Error\Codes::ADDRESS_INCONSISTENT_COUNTRY, $errors[0]->code);
  559. }
  560. public function testCreateNoValidate_returnsCustomer()
  561. {
  562. $customer = Braintree\Customer::createNoValidate([
  563. 'firstName' => 'Paul',
  564. 'lastName' => 'Martin'
  565. ]);
  566. $this->assertEquals('Paul', $customer->firstName);
  567. $this->assertEquals('Martin', $customer->lastName);
  568. }
  569. public function testCreateNoValidate_throwsIfInvalid()
  570. {
  571. $this->setExpectedException('Braintree\Exception\ValidationsFailed');
  572. $customer = Braintree\Customer::createNoValidate(['email' => 'invalid']);
  573. }
  574. public function testCreate_worksWithFuturePayPalNonce()
  575. {
  576. $nonce = Braintree\Test\Nonces::$paypalFuturePayment;
  577. $result = Braintree\Customer::create([
  578. 'paymentMethodNonce' => $nonce
  579. ]);
  580. $this->assertTrue($result->success);
  581. }
  582. public function testCreate_worksWithOrderPaymentPayPalNonce()
  583. {
  584. $http = new HttpClientApi(Braintree\Configuration::$global);
  585. $nonce = $http->nonceForPayPalAccount([
  586. 'paypal_account' => [
  587. 'intent' => 'order',
  588. 'payment_token' => 'paypal-payment-token',
  589. 'payer_id' => 'paypal-payer-id',
  590. ]
  591. ]);
  592. $result = Braintree\Customer::create([
  593. 'paymentMethodNonce' => $nonce
  594. ]);
  595. $this->assertTrue($result->success);
  596. }
  597. public function testCreate_worksWithOrderPaymentPayPalNonceWithPayeeEmailSnakeCase()
  598. {
  599. $http = new HttpClientApi(Braintree\Configuration::$global);
  600. $nonce = $http->nonceForPayPalAccount([
  601. 'paypal_account' => [
  602. 'intent' => 'order',
  603. 'payment_token' => 'paypal-payment-token',
  604. 'payer_id' => 'paypal-payer-id',
  605. ]
  606. ]);
  607. $result = Braintree\Customer::create([
  608. 'paymentMethodNonce' => $nonce,
  609. 'options' => [
  610. 'paypal' => [
  611. 'payee_email' => 'payee@example.com',
  612. 'order_id' => 'merchant-order-id',
  613. 'custom_field' => 'custom merchant field',
  614. 'description' => 'merchant description',
  615. 'amount' => '1.23',
  616. ],
  617. ],
  618. ]);
  619. $this->assertTrue($result->success);
  620. }
  621. public function testCreate_worksWithOrderPaymentPayPalNonceWithPayeeEmailCamelCase()
  622. {
  623. $http = new HttpClientApi(Braintree\Configuration::$global);
  624. $nonce = $http->nonceForPayPalAccount([
  625. 'paypal_account' => [
  626. 'intent' => 'order',
  627. 'payment_token' => 'paypal-payment-token',
  628. 'payer_id' => 'paypal-payer-id',
  629. ]
  630. ]);
  631. $result = Braintree\Customer::create([
  632. 'paymentMethodNonce' => $nonce,
  633. 'options' => [
  634. 'paypal' => [
  635. 'payeeEmail' => 'payee@example.com',
  636. 'orderId' => 'merchant-order-id',
  637. 'customField' => 'custom merchant field',
  638. 'description' => 'merchant description',
  639. 'amount' => '1.23',
  640. 'shipping' => [
  641. 'firstName' => 'Andrew',
  642. 'lastName' => 'Mason',
  643. 'company' => 'Braintree',
  644. 'streetAddress' => '456 W Main St',
  645. 'extendedAddress' => 'Apt 2F',
  646. 'locality' => 'Bartlett',
  647. 'region' => 'IL',
  648. 'postalCode' => '60103',
  649. 'countryName' => 'United States of America',
  650. 'countryCodeAlpha2' => 'US',
  651. 'countryCodeAlpha3' => 'USA',
  652. 'countryCodeNumeric' => '840'
  653. ],
  654. ],
  655. ],
  656. ]);
  657. $this->assertTrue($result->success);
  658. }
  659. public function testCreate_doesNotWorkWithOnetimePayPalNonce()
  660. {
  661. $nonce = Braintree\Test\Nonces::$paypalOneTimePayment;
  662. $result = Braintree\Customer::create([
  663. 'paymentMethodNonce' => $nonce
  664. ]);
  665. $this->assertFalse($result->success);
  666. $errors = $result->errors->forKey('customer')->forKey('paypalAccount')->errors;
  667. $this->assertEquals(Braintree\Error\Codes::PAYPAL_ACCOUNT_CANNOT_VAULT_ONE_TIME_USE_PAYPAL_ACCOUNT, $errors[0]->code);
  668. }
  669. public function testDelete_deletesTheCustomer()
  670. {
  671. $result = Braintree\Customer::create([]);
  672. $this->assertEquals(true, $result->success);
  673. Braintree\Customer::find($result->customer->id);
  674. Braintree\Customer::delete($result->customer->id);
  675. $this->setExpectedException('Braintree\Exception\NotFound');
  676. Braintree\Customer::find($result->customer->id);
  677. }
  678. public function testFind()
  679. {
  680. $result = Braintree\Customer::create([
  681. 'firstName' => 'Mike',
  682. 'lastName' => 'Jones',
  683. 'company' => 'Jones Co.',
  684. 'email' => 'mike.jones@example.com',
  685. 'phone' => '419.555.1234',
  686. 'fax' => '419.555.1235',
  687. 'website' => 'http://example.com'
  688. ]);
  689. $this->assertEquals(true, $result->success);
  690. $customer = Braintree\Customer::find($result->customer->id);
  691. $this->assertEquals('Mike', $customer->firstName);
  692. $this->assertEquals('Jones', $customer->lastName);
  693. $this->assertEquals('Jones Co.', $customer->company);
  694. $this->assertEquals('mike.jones@example.com', $customer->email);
  695. $this->assertEquals('419.555.1234', $customer->phone);
  696. $this->assertEquals('419.555.1235', $customer->fax);
  697. $this->assertEquals('http://example.com', $customer->website);
  698. }
  699. public function test_findCustomerWithAllFilterableAssociationsFilteredOut()
  700. {
  701. $result = Braintree\Customer::create([
  702. 'firstName' => 'Mike',
  703. 'lastName' => 'Jones',
  704. 'company' => 'Jones Co.',
  705. 'email' => 'mike.jones@example.com',
  706. 'phone' => '419.555.1234',
  707. 'fax' => '419.555.1235',
  708. 'website' => 'http://example.com',
  709. 'customFields' => [
  710. 'storeMe' => 'custom value'
  711. ]
  712. ]);
  713. $creditCard = Braintree\CreditCard::create([
  714. 'customerId' => $result->customer->id,
  715. 'number' => '5105105105105100',
  716. 'expirationDate' => '05/12',
  717. 'billingAddress' => [
  718. 'firstName' => 'Drew',
  719. 'lastName' => 'Smith',
  720. 'company' => 'Smith Co.',
  721. 'streetAddress' => '1 E Main St',
  722. 'locality' => 'Chicago',
  723. 'region' => 'IL',
  724. 'postalCode' => '60622',
  725. 'countryName' => 'United States of America'
  726. ]
  727. ]);
  728. $id = strval(rand());
  729. $subscriptions = Braintree\Subscription::create([
  730. 'id' => $id,
  731. 'paymentMethodToken' => $creditCard->creditCard->token,
  732. 'planId' => 'integration_trialless_plan',
  733. 'price' => '1.00'
  734. ]);
  735. $customer = Braintree\Customer::find($result->customer->id, "customernoassociations");
  736. $this->assertEquals(0, count($customer->creditCards));
  737. $this->assertEquals(0, count($customer->paymentMethods));
  738. $this->assertEquals(0, count($customer->addresses));
  739. $this->assertEquals(0, count($customer->customFields));
  740. }
  741. public function test_findCustomerWithNestedFilterableAssociationsFilteredOut()
  742. {
  743. $result = Braintree\Customer::create([
  744. 'firstName' => 'Mike',
  745. 'lastName' => 'Jones',
  746. 'company' => 'Jones Co.',
  747. 'email' => 'mike.jones@example.com',
  748. 'phone' => '419.555.1234',
  749. 'fax' => '419.555.1235',
  750. 'website' => 'http://example.com',
  751. 'customFields' => [
  752. 'storeMe' => 'custom value'
  753. ]
  754. ]);
  755. $creditCard = Braintree\CreditCard::create([
  756. 'customerId' => $result->customer->id,
  757. 'number' => '5105105105105100',
  758. 'expirationDate' => '05/12',
  759. 'billingAddress' => [
  760. 'firstName' => 'Drew',
  761. 'lastName' => 'Smith',
  762. 'company' => 'Smith Co.',
  763. 'streetAddress' => '1 E Main St',
  764. 'locality' => 'Chicago',
  765. 'region' => 'IL',
  766. 'postalCode' => '60622',
  767. 'countryName' => 'United States of America'
  768. ]
  769. ]);
  770. $id = strval(rand());
  771. $subscriptions = Braintree\Subscription::create([
  772. 'id' => $id,
  773. 'paymentMethodToken' => $creditCard->creditCard->token,
  774. 'planId' => 'integration_trialless_plan',
  775. 'price' => '1.00'
  776. ]);
  777. $customer = Braintree\Customer::find($result->customer->id, "customertoplevelassociations");
  778. $this->assertEquals(1, count($customer->creditCards));
  779. $this->assertEquals(0, count($customer->creditCards[0]->subscriptions));
  780. $this->assertEquals(1, count($customer->paymentMethods));
  781. $this->assertEquals(0, count($customer->paymentMethods[0]->subscriptions));
  782. $this->assertEquals(1, count($customer->addresses));
  783. $this->assertEquals(1, count($customer->customFields));
  784. }
  785. public function test_findUsBankAccountGivenPaymentMethodToken()
  786. {
  787. $nonce = Test\Helper::generateValidUsBankAccountNonce();
  788. $result = Braintree\Customer::create(array(
  789. 'paymentMethodNonce' => $nonce,
  790. 'creditCard' => [
  791. 'options' => [
  792. 'verificationMerchantAccountId' => Test\Helper::usBankMerchantAccount()
  793. ]
  794. ]
  795. ));
  796. $this->assertTrue($result->success);
  797. $customer = Braintree\Customer::find($result->customer->id);
  798. $this->assertNotNull($customer->usBankAccounts[0]);
  799. $this->assertNotNull($customer->paymentMethods[0]);
  800. $usBankAccount = $customer->usBankAccounts[0];
  801. $this->assertTrue($usBankAccount instanceof Braintree\UsBankAccount);
  802. $this->assertNotNull($usBankAccount->token);
  803. $this->assertEquals('Dan Schulman', $usBankAccount->accountHolderName);
  804. $this->assertEquals('021000021', $usBankAccount->routingNumber);
  805. $this->assertEquals('1234', $usBankAccount->last4);
  806. $this->assertEquals('checking', $usBankAccount->accountType);
  807. $this->assertRegexp('/CHASE/', $usBankAccount->bankName);
  808. }
  809. public function testFind_throwsExceptionIfNotFound()
  810. {
  811. $this->setExpectedException('Braintree\Exception\NotFound');
  812. Braintree\Customer::find("does-not-exist");
  813. }
  814. public function testUpdate()
  815. {
  816. $result = Braintree\Customer::create([
  817. 'firstName' => 'Old First',
  818. 'lastName' => 'Old Last',
  819. 'company' => 'Old Company',
  820. 'email' => 'old.email@example.com',
  821. 'phone' => 'old phone',
  822. 'fax' => 'old fax',
  823. 'website' => 'http://old.example.com'
  824. ]);
  825. $this->assertEquals(true, $result->success);
  826. $customer = $result->customer;
  827. $updateResult = Braintree\Customer::update($customer->id, [
  828. 'firstName' => 'New First',
  829. 'lastName' => 'New Last',
  830. 'company' => 'New Company',
  831. 'email' => 'new.email@example.com',
  832. 'phone' => 'new phone',
  833. 'fax' => 'new fax',
  834. 'website' => 'http://new.example.com'
  835. ]);
  836. $this->assertEquals(true, $updateResult->success);
  837. $this->assertEquals('New First', $updateResult->customer->firstName);
  838. $this->assertEquals('New Last', $updateResult->customer->lastName);
  839. $this->assertEquals('New Company', $updateResult->customer->company);
  840. $this->assertEquals('new.email@example.com', $updateResult->customer->email);
  841. $this->assertEquals('new phone', $updateResult->customer->phone);
  842. $this->assertEquals('new fax', $updateResult->customer->fax);
  843. $this->assertEquals('http://new.example.com', $updateResult->customer->website);
  844. }
  845. public function testUpdate_withCountry()
  846. {
  847. $customer = Braintree\Customer::create([
  848. 'firstName' => 'Bat',
  849. 'lastName' => 'Manderson',
  850. 'creditCard' => [
  851. 'number' => '5105105105105100',
  852. 'expirationDate' => '05/12',
  853. 'billingAddress' => [
  854. 'countryName' => 'United States of America',
  855. 'countryCodeAlpha2' => 'US',
  856. 'countryCodeAlpha3' => 'USA',
  857. 'countryCodeNumeric' => '840'
  858. ]
  859. ]
  860. ])->customer;
  861. $result = Braintree\Customer::update($customer->id, [
  862. 'firstName' => 'Bat',
  863. 'lastName' => 'Manderson',
  864. 'creditCard' => [
  865. 'options' => [
  866. 'updateExistingToken' => $customer->creditCards[0]->token
  867. ],
  868. 'billingAddress' => [
  869. 'countryName' => 'Gabon',
  870. 'countryCodeAlpha2' => 'GA',
  871. 'countryCodeAlpha3' => 'GAB',
  872. 'countryCodeNumeric' => '266',
  873. 'options' => [
  874. 'updateExisting' => true
  875. ]
  876. ]
  877. ]
  878. ]);
  879. $this->assertEquals(true, $result->success);
  880. $updatedCustomer = $result->customer;
  881. $this->assertEquals('Gabon', $updatedCustomer->creditCards[0]->billingAddress->countryName);
  882. $this->assertEquals('GA', $updatedCustomer->creditCards[0]->billingAddress->countryCodeAlpha2);
  883. $this->assertEquals('GAB', $updatedCustomer->creditCards[0]->billingAddress->countryCodeAlpha3);
  884. $this->assertEquals('266', $updatedCustomer->creditCards[0]->billingAddress->countryCodeNumeric);
  885. }
  886. public function testUpdate_withUpdatingExistingCreditCard()
  887. {
  888. $create_result = Braintree\Customer::create([
  889. 'firstName' => 'Old First',
  890. 'lastName' => 'Old Last',
  891. 'website' => 'http://old.example.com',
  892. 'creditCard' => [
  893. 'number' => '5105105105105100',
  894. 'expirationDate' => '05/12',
  895. 'cardholderName' => 'Old Cardholder'
  896. ]
  897. ]);
  898. $this->assertEquals(true, $create_result->success);
  899. $customer = $create_result->customer;
  900. $creditCard = $customer->creditCards[0];
  901. $result = Braintree\Customer::update($customer->id, [
  902. 'firstName' => 'New First',
  903. 'lastName' => 'New Last',
  904. 'creditCard' => [
  905. 'number' => '4111111111111111',
  906. 'expirationDate' => '11/14',
  907. 'cardholderName' => 'New Cardholder',
  908. 'options' => [
  909. 'updateExistingToken' => $creditCard->token
  910. ]
  911. ]
  912. ]);
  913. $this->assertEquals(true, $result->success);
  914. $this->assertEquals('New First', $result->customer->firstName);
  915. $this->assertEquals('New Last', $result->customer->lastName);
  916. $this->assertEquals(1, sizeof($result->customer->creditCards));
  917. $creditCard = $result->customer->creditCards[0];
  918. $this->assertEquals('411111', $creditCard->bin);
  919. $this->assertEquals('11/2014', $creditCard->expirationDate);
  920. $this->assertEquals('New Cardholder', $creditCard->cardholderName);
  921. }
  922. public function testUpdate_failOnDuplicatePaymentMethod()
  923. {
  924. $create_result = Braintree\Customer::create([
  925. 'creditCard' => [
  926. 'number' => '4111111111111111',
  927. 'expirationDate' => '11/14',
  928. ]
  929. ]);
  930. $this->assertEquals(true, $create_result->success);
  931. $result = Braintree\Customer::update($create_result->customer->id, [
  932. 'creditCard' => [
  933. 'number' => '4111111111111111',
  934. 'expirationDate' => '11/14',
  935. 'options' => [
  936. 'failOnDuplicatePaymentMethod' => true
  937. ]
  938. ]
  939. ]);
  940. $this->assertFalse($result->success);
  941. $errors = $result->errors->forKey('customer')->forKey('creditCard')->onAttribute('number');
  942. $this->assertEquals(Braintree\Error\Codes::CREDIT_CARD_DUPLICATE_CARD_EXISTS, $errors[0]->code);
  943. $this->assertEquals(1, preg_match('/Duplicate card exists in the vault\./', $result->message));
  944. }
  945. public function testUpdate_forBillingAddressAndExistingCreditCardAndCustomerDetailsTogether()
  946. {
  947. $create_result = Braintree\Customer::create([
  948. 'firstName' => 'Old First',
  949. 'lastName' => 'Old Last',
  950. 'creditCard' => [
  951. 'number' => '5105105105105100',
  952. 'expirationDate' => '05/12',
  953. 'cvv' => '123',
  954. 'cardholderName' => 'Old Cardholder',
  955. 'billingAddress' => [
  956. 'firstName' => 'Drew',
  957. 'lastName' => 'Smith'
  958. ]
  959. ]
  960. ]);
  961. $this->assertEquals(true, $create_result->success);
  962. $customer = $create_result->customer;
  963. $creditCard = $customer->creditCards[0];
  964. $result = Braintree\Customer::update($customer->id, [
  965. 'firstName' => 'New Customer First',
  966. 'lastName' => 'New Customer Last',
  967. 'creditCard' => [
  968. 'number' => '4111111111111111',
  969. 'expirationDate' => '11/14',
  970. 'options' => [
  971. 'updateExistingToken' => $creditCard->token
  972. ],
  973. 'billingAddress' => [
  974. 'firstName' => 'New Billing First',
  975. 'lastName' => 'New Billing Last',
  976. 'options' => [
  977. 'updateExisting' => true
  978. ]
  979. ]
  980. ]
  981. ]);
  982. $this->assertEquals(true, $result->success);
  983. $this->assertEquals('New Customer First', $result->customer->firstName);
  984. $this->assertEquals('New Customer Last', $result->customer->lastName);
  985. $this->assertEquals(1, sizeof($result->customer->creditCards));
  986. $this->assertEquals(1, sizeof($result->customer->addresses));
  987. $creditCard = $result->customer->creditCards[0];
  988. $this->assertEquals('411111', $creditCard->bin);
  989. $this->assertEquals('11/2014', $creditCard->expirationDate);
  990. $billingAddress = $creditCard->billingAddress;
  991. $this->assertEquals('New Billing First', $billingAddress->firstName);
  992. $this->assertEquals('New Billing Last', $billingAddress->lastName);
  993. }
  994. public function testUpdate_withNewCreditCardAndExistingBillingAddress()
  995. {
  996. $customer = Braintree\Customer::create()->customer;
  997. $address = Braintree\Address::create([
  998. 'customerId' => $customer->id,
  999. 'firstName' => 'Dan'
  1000. ])->address;
  1001. $result = Braintree\Customer::update($customer->id, [
  1002. 'creditCard' => [
  1003. 'number' => '4111111111111111',
  1004. 'expirationDate' => '11/14',
  1005. 'billingAddressId' => $address->id
  1006. ]
  1007. ]);
  1008. $billingAddress = $result->customer->creditCards[0]->billingAddress;
  1009. $this->assertEquals($address->id, $billingAddress->id);
  1010. $this->assertEquals('Dan', $billingAddress->firstName);
  1011. }
  1012. public function testUpdate_withNewCreditCardAndVerificationAmount()
  1013. {
  1014. $customer = Braintree\Customer::create()->customer;
  1015. $result = Braintree\Customer::update($customer->id, [
  1016. 'creditCard' => [
  1017. 'number' => '4111111111111111',
  1018. 'expirationDate' => '11/14',
  1019. 'options' => [
  1020. 'verifyCard' => true,
  1021. 'verificationAmount' => '2.00'
  1022. ]
  1023. ]
  1024. ]);
  1025. $this->assertTrue($result->success);
  1026. }
  1027. public function testUpdate_worksWithFuturePayPalNonce()
  1028. {
  1029. $customerResult = Braintree\Customer::create([
  1030. 'creditCard' => [
  1031. 'number' => '5105105105105100',
  1032. 'expirationDate' => '05/12',
  1033. 'options' => [
  1034. 'makeDefault' => true
  1035. ]
  1036. ]
  1037. ]);
  1038. $paypalAccountToken = 'PAYPALToken-' . strval(rand());
  1039. $http = new HttpClientApi(Braintree\Configuration::$global);
  1040. $nonce = $http->nonceForPayPalAccount([
  1041. 'paypal_account' => [
  1042. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  1043. 'token' => $paypalAccountToken,
  1044. 'options' => [
  1045. 'makeDefault' => true
  1046. ]
  1047. ]
  1048. ]);
  1049. $result = Braintree\Customer::update($customerResult->customer->id, [
  1050. 'paymentMethodNonce' => $nonce
  1051. ]);
  1052. $this->assertTrue($result->success);
  1053. $this->assertEquals($result->customer->defaultPaymentMethod()->token, $paypalAccountToken);
  1054. }
  1055. public function testUpdate_worksWithOrderPaymentPayPalNonce()
  1056. {
  1057. $customerResult = Braintree\Customer::create([
  1058. 'creditCard' => [
  1059. 'number' => '5105105105105100',
  1060. 'expirationDate' => '05/12',
  1061. 'options' => [
  1062. 'makeDefault' => true
  1063. ]
  1064. ]
  1065. ]);
  1066. $paypalAccountToken = 'PAYPALToken-' . strval(rand());
  1067. $http = new HttpClientApi(Braintree\Configuration::$global);
  1068. $nonce = $http->nonceForPayPalAccount([
  1069. 'paypal_account' => [
  1070. 'intent' => 'order',
  1071. 'payment_token' => 'paypal-payment-token',
  1072. 'payer_id' => 'paypal-payer-id',
  1073. 'token' => $paypalAccountToken,
  1074. 'options' => [
  1075. 'makeDefault' => true
  1076. ]
  1077. ]
  1078. ]);
  1079. $result = Braintree\Customer::update($customerResult->customer->id, [
  1080. 'paymentMethodNonce' => $nonce
  1081. ]);
  1082. $this->assertTrue($result->success);
  1083. $this->assertEquals($result->customer->defaultPaymentMethod()->token, $paypalAccountToken);
  1084. }
  1085. public function testUpdate_worksWithOrderPaymentPayPalNonceWithPayeeEmailSnakeCase()
  1086. {
  1087. $customerResult = Braintree\Customer::create([
  1088. 'creditCard' => [
  1089. 'number' => '5105105105105100',
  1090. 'expirationDate' => '05/12',
  1091. 'options' => [
  1092. 'makeDefault' => true
  1093. ]
  1094. ]
  1095. ]);
  1096. $paypalAccountToken = 'PAYPALToken-' . strval(rand());
  1097. $http = new HttpClientApi(Braintree\Configuration::$global);
  1098. $nonce = $http->nonceForPayPalAccount([
  1099. 'paypal_account' => [
  1100. 'intent' => 'order',
  1101. 'payment_token' => 'paypal-payment-token',
  1102. 'payer_id' => 'paypal-payer-id',
  1103. 'token' => $paypalAccountToken,
  1104. 'options' => [
  1105. 'makeDefault' => true
  1106. ]
  1107. ]
  1108. ]);
  1109. $result = Braintree\Customer::update($customerResult->customer->id, [
  1110. 'paymentMethodNonce' => $nonce,
  1111. 'options' => [
  1112. 'paypal' => [
  1113. 'payee_email' => 'payee@example.com',
  1114. ],
  1115. ],
  1116. ]);
  1117. $this->assertTrue($result->success);
  1118. $this->assertEquals($result->customer->defaultPaymentMethod()->token, $paypalAccountToken);
  1119. }
  1120. public function testUpdate_worksWithOrderPaymentPayPalNonceWithPayeeEmailCamelCase()
  1121. {
  1122. $customerResult = Braintree\Customer::create([
  1123. 'creditCard' => [
  1124. 'number' => '5105105105105100',
  1125. 'expirationDate' => '05/12',
  1126. 'options' => [
  1127. 'makeDefault' => true
  1128. ]
  1129. ]
  1130. ]);
  1131. $paypalAccountToken = 'PAYPALToken-' . strval(rand());
  1132. $http = new HttpClientApi(Braintree\Configuration::$global);
  1133. $nonce = $http->nonceForPayPalAccount([
  1134. 'paypal_account' => [
  1135. 'intent' => 'order',
  1136. 'payment_token' => 'paypal-payment-token',
  1137. 'payer_id' => 'paypal-payer-id',
  1138. 'token' => $paypalAccountToken,
  1139. 'options' => [
  1140. 'makeDefault' => true
  1141. ]
  1142. ]
  1143. ]);
  1144. $result = Braintree\Customer::update($customerResult->customer->id, [
  1145. 'paymentMethodNonce' => $nonce,
  1146. 'options' => [
  1147. 'paypal' => [
  1148. 'payeeEmail' => 'payee@example.com',
  1149. 'shipping' => [
  1150. 'firstName' => 'Andrew',
  1151. 'lastName' => 'Mason',
  1152. 'company' => 'Braintree',
  1153. 'streetAddress' => '456 W Main St',
  1154. 'extendedAddress' => 'Apt 2F',
  1155. 'locality' => 'Bartlett',
  1156. 'region' => 'IL',
  1157. 'postalCode' => '60103',
  1158. 'countryName' => 'United States of America',
  1159. 'countryCodeAlpha2' => 'US',
  1160. 'countryCodeAlpha3' => 'USA',
  1161. 'countryCodeNumeric' => '840'
  1162. ],
  1163. ],
  1164. ],
  1165. ]);
  1166. $this->assertTrue($result->success);
  1167. $this->assertEquals($result->customer->defaultPaymentMethod()->token, $paypalAccountToken);
  1168. }
  1169. public function testUpdateDefaultPaymentMethod()
  1170. {
  1171. $result = Braintree\Customer::create([
  1172. 'firstName' => 'Old First',
  1173. 'lastName' => 'Old Last',
  1174. ]);
  1175. $this->assertEquals(true, $result->success);
  1176. $customer = $result->customer;
  1177. $token1 = 'TOKEN-' . strval(rand());
  1178. $result = Braintree\PaymentMethod::create([
  1179. 'customerId' => $customer->id,
  1180. 'paymentMethodNonce' => Braintree\Test\Nonces::$transactableVisa,
  1181. 'token' => $token1
  1182. ]);
  1183. $updateResult = Braintree\Customer::update($customer->id, [
  1184. 'defaultPaymentMethodToken' => $token1
  1185. ]);
  1186. $this->assertEquals(true, $updateResult->success);
  1187. $this->assertEquals($updateResult->customer->defaultPaymentMethod()->token, $token1);
  1188. $token2 = 'TOKEN-' . strval(rand());
  1189. $result = Braintree\PaymentMethod::create([
  1190. 'customerId' => $customer->id,
  1191. 'paymentMethodNonce' => Braintree\Test\Nonces::$transactableMasterCard,
  1192. 'token' => $token2
  1193. ]);
  1194. $updateResult = Braintree\Customer::update($customer->id, [
  1195. 'defaultPaymentMethodToken' => $token2
  1196. ]);
  1197. $this->assertEquals(true, $updateResult->success);
  1198. $this->assertEquals($updateResult->customer->defaultPaymentMethod()->token, $token2);
  1199. }
  1200. public function testUpdateDefaultPaymentMethodFromOptions()
  1201. {
  1202. $result = Braintree\Customer::create([
  1203. 'firstName' => 'Old First',
  1204. 'lastName' => 'Old Last',
  1205. ]);
  1206. $this->assertEquals(true, $result->success);
  1207. $customer = $result->customer;
  1208. $token1 = 'TOKEN-' . strval(rand());
  1209. $result = Braintree\PaymentMethod::create([
  1210. 'customerId' => $customer->id,
  1211. 'paymentMethodNonce' => Braintree\Test\Nonces::$transactableVisa,
  1212. 'token' => $token1
  1213. ]);
  1214. $updateResult = Braintree\Customer::update($customer->id, [
  1215. 'creditCard' => [
  1216. 'options' => [
  1217. 'updateExistingToken' => $token1,
  1218. 'makeDefault' => true
  1219. ]
  1220. ]
  1221. ]);
  1222. $this->assertEquals(true, $updateResult->success);
  1223. $this->assertEquals($updateResult->customer->defaultPaymentMethod()->token, $token1);
  1224. $token2 = 'TOKEN-' . strval(rand());
  1225. $result = Braintree\PaymentMethod::create([
  1226. 'customerId' => $customer->id,
  1227. 'paymentMethodNonce' => Braintree\Test\Nonces::$transactableMasterCard,
  1228. 'token' => $token2
  1229. ]);
  1230. $updateResult = Braintree\Customer::update($customer->id, [
  1231. 'creditCard' => [
  1232. 'options' => [
  1233. 'updateExistingToken' => $token2,
  1234. 'makeDefault' => true
  1235. ]
  1236. ]
  1237. ]);
  1238. $this->assertEquals(true, $updateResult->success);
  1239. $this->assertEquals($updateResult->customer->defaultPaymentMethod()->token, $token2);
  1240. }
  1241. public function testUpdate_doesNotWorkWithOnetimePayPalNonce()
  1242. {
  1243. $customerResult = Braintree\Customer::create([
  1244. 'creditCard' => [
  1245. 'number' => '5105105105105100',
  1246. 'expirationDate' => '05/12',
  1247. 'options' => [
  1248. 'makeDefault' => true
  1249. ]
  1250. ]
  1251. ]);
  1252. $paypalAccountToken = 'PAYPALToken-' . strval(rand());
  1253. $http = new HttpClientApi(Braintree\Configuration::$global);
  1254. $nonce = $http->nonceForPayPalAccount([
  1255. 'paypal_account' => [
  1256. 'access_token' => 'PAYPAL_ACCESS_TOKEN',
  1257. 'token' => $paypalAccountToken,
  1258. 'options' => [
  1259. 'makeDefault' => true
  1260. ]
  1261. ]
  1262. ]);
  1263. $result = Braintree\Customer::update($customerResult->customer->id, [
  1264. 'paymentMethodNonce' => $nonce
  1265. ]);
  1266. $this->assertFalse($result->success);
  1267. $errors = $result->errors->forKey('customer')->forKey('paypalAccount')->errors;
  1268. $this->assertEquals(Braintree\Error\Codes::PAYPAL_ACCOUNT_CANNOT_VAULT_ONE_TIME_USE_PAYPAL_ACCOUNT, $errors[0]->code);
  1269. }
  1270. public function testUpdateNoValidate()
  1271. {
  1272. $result = Braintree\Customer::create([
  1273. 'firstName' => 'Old First',
  1274. 'lastName' => 'Old Last',
  1275. 'company' => 'Old Company',
  1276. 'email' => 'old.email@example.com',
  1277. 'phone' => 'old phone',
  1278. 'fax' => 'old fax',
  1279. 'website' => 'http://old.example.com'
  1280. ]);
  1281. $this->assertEquals(true, $result->success);
  1282. $customer = $result->customer;
  1283. $updated = Braintree\Customer::updateNoValidate($customer->id, [
  1284. 'firstName' => 'New First',
  1285. 'lastName' => 'New Last',
  1286. 'company' => 'New Company',
  1287. 'email' => 'new.email@example.com',
  1288. 'phone' => 'new phone',
  1289. 'fax' => 'new fax',
  1290. 'website' => 'http://new.example.com'
  1291. ]);
  1292. $this->assertEquals('New First', $updated->firstName);
  1293. $this->assertEquals('New Last', $updated->lastName);
  1294. $this->assertEquals('New Company', $updated->company);
  1295. $this->assertEquals('new.email@example.com', $updated->email);
  1296. $this->assertEquals('new phone', $updated->phone);
  1297. $this->assertEquals('new fax', $updated->fax);
  1298. $this->assertEquals('http://new.example.com', $updated->website);
  1299. }
  1300. public function testCreateFromTransparentRedirect()
  1301. {
  1302. Test\Helper::suppressDeprecationWarnings();
  1303. $queryString = $this->createCustomerViaTr(
  1304. [
  1305. 'customer' => [
  1306. 'first_name' => 'Joe',
  1307. 'last_name' => 'Martin',
  1308. 'credit_card' => [
  1309. 'number' => '5105105105105100',
  1310. 'expiration_date' => '05/12'
  1311. ]
  1312. ]
  1313. ],
  1314. [
  1315. ]
  1316. );
  1317. $result = Braintree\Customer::createFromTransparentRedirect($queryString);
  1318. $this->assertTrue($result->success);
  1319. $this->assertEquals('Joe', $result->customer->firstName);
  1320. $this->assertEquals('Martin', $result->customer->lastName);
  1321. $creditCard = $result->customer->creditCards[0];
  1322. $this->assertEquals('510510', $creditCard->bin);
  1323. $this->assertEquals('5100', $creditCard->last4);
  1324. $this->assertEquals('05/2012', $creditCard->expirationDate);
  1325. }
  1326. public function testCreateFromTransparentRedirect_withParamsInTrData()
  1327. {
  1328. Test\Helper::suppressDeprecationWarnings();
  1329. $queryString = $this->createCustomerViaTr(
  1330. [
  1331. ],
  1332. [
  1333. 'customer' => [
  1334. 'firstName' => 'Joe',
  1335. 'lastName' => 'Martin',
  1336. 'creditCard' => [
  1337. 'number' => '5105105105105100',
  1338. 'expirationDate' => '05/12'
  1339. ]
  1340. ]
  1341. ]
  1342. );
  1343. $result = Braintree\Customer::createFromTransparentRedirect($queryString);
  1344. $this->assertTrue($result->success);
  1345. $this->assertEquals('Joe', $result->customer->firstName);
  1346. $this->assertEquals('Martin', $result->customer->lastName);
  1347. $creditCard = $result->customer->creditCards[0];
  1348. $this->assertEquals('510510', $creditCard->bin);
  1349. $this->assertEquals('5100', $creditCard->last4);
  1350. $this->assertEquals('05/2012', $creditCard->expirationDate);
  1351. }
  1352. public function testCreateFromTransparentRedirect_withValidationErrors()
  1353. {
  1354. Test\Helper::suppressDeprecationWarnings();
  1355. $queryString = $this->createCustomerViaTr(
  1356. [
  1357. 'customer' => [
  1358. 'first_name' => str_repeat('x', 256),
  1359. 'credit_card' => [
  1360. 'number' => 'invalid',
  1361. 'expiration_date' => ''
  1362. ]
  1363. ]
  1364. ],
  1365. [
  1366. ]
  1367. );
  1368. $result = Braintree\Customer::createFromTransparentRedirect($queryString);
  1369. $this->assertFalse($result->success);
  1370. $errors = $result->errors->forKey('customer')->onAttribute('firstName');
  1371. $this->assertEquals(Braintree\Error\Codes::CUSTOMER_FIRST_NAME_IS_TOO_LONG, $errors[0]->code);
  1372. $errors = $result->errors->forKey('customer')->forKey('creditCard')->onAttribute('number');
  1373. $this->assertEquals(Braintree\Error\Codes::CREDIT_CARD_NUMBER_INVALID_LENGTH, $errors[0]->code);
  1374. $errors = $result->errors->forKey('customer')->forKey('creditCard')->onAttribute('expirationDate');
  1375. $this->assertEquals(Braintree\Error\Codes::CREDIT_CARD_EXPIRATION_DATE_IS_REQUIRED, $errors[0]->code);
  1376. }
  1377. public function testCreateWithInvalidUTF8Bytes()
  1378. {
  1379. $result = Braintree\Customer::create([
  1380. 'firstName' => "Jos\xe8 Maria"
  1381. ]);
  1382. $this->assertEquals(true, $result->success);
  1383. $customer = $result->customer;
  1384. $this->assertEquals("Jos\xc3\xa8 Maria", $customer->firstName);
  1385. }
  1386. public function testCreateWithValidUTF8Bytes()
  1387. {
  1388. $result = Braintree\Customer::create([
  1389. 'firstName' => "Jos\303\251"
  1390. ]);
  1391. $this->assertEquals(true, $result->success);
  1392. $customer = $result->customer;
  1393. $this->assertEquals("Jos\303\251", $customer->firstName);
  1394. }
  1395. public function testUpdateFromTransparentRedirect()
  1396. {
  1397. Test\Helper::suppressDeprecationWarnings();
  1398. $customer = Braintree\Customer::createNoValidate();
  1399. $queryString = $this->updateCustomerViaTr(
  1400. [
  1401. 'customer' => [
  1402. 'first_name' => 'Joe',
  1403. 'last_name' => 'Martin',
  1404. 'email' => 'joe.martin@example.com'
  1405. ]
  1406. ],
  1407. [
  1408. 'customerId' => $customer->id
  1409. ]
  1410. );
  1411. $result = Braintree\Customer::updateFromTransparentRedirect($queryString);
  1412. $this->assertTrue($result->success);
  1413. $this->assertEquals('Joe', $result->customer->firstName);
  1414. $this->assertEquals('Martin', $result->customer->lastName);
  1415. $this->assertEquals('joe.martin@example.com', $result->customer->email);
  1416. }
  1417. public function testUpdateFromTransparentRedirect_withParamsInTrData()
  1418. {
  1419. Test\Helper::suppressDeprecationWarnings();
  1420. $customer = Braintree\Customer::createNoValidate();
  1421. $queryString = $this->updateCustomerViaTr(
  1422. [
  1423. ],
  1424. [
  1425. 'customerId' => $customer->id,
  1426. 'customer' => [
  1427. 'firstName' => 'Joe',
  1428. 'lastName' => 'Martin',
  1429. 'email' => 'joe.martin@example.com'
  1430. ]
  1431. ]
  1432. );
  1433. $result = Braintree\Customer::updateFromTransparentRedirect($queryString);
  1434. $this->assertTrue($result->success);
  1435. $this->assertEquals('Joe', $result->customer->firstName);
  1436. $this->assertEquals('Martin', $result->customer->lastName);
  1437. $this->assertEquals('joe.martin@example.com', $result->customer->email);
  1438. }
  1439. public function testUpdateFromTransparentRedirect_withValidationErrors()
  1440. {
  1441. Test\Helper::suppressDeprecationWarnings();
  1442. $customer = Braintree\Customer::createNoValidate();
  1443. $queryString = $this->updateCustomerViaTr(
  1444. [
  1445. 'customer' => [
  1446. 'first_name' => str_repeat('x', 256),
  1447. ]
  1448. ],
  1449. [
  1450. 'customerId' => $customer->id
  1451. ]
  1452. );
  1453. $result = Braintree\Customer::updateFromTransparentRedirect($queryString);
  1454. $this->assertFalse($result->success);
  1455. $errors = $result->errors->forKey('customer')->onAttribute('firstName');
  1456. $this->assertEquals(Braintree\Error\Codes::CUSTOMER_FIRST_NAME_IS_TOO_LONG, $errors[0]->code);
  1457. }
  1458. public function testUpdateFromTransparentRedirect_withUpdateExisting()
  1459. {
  1460. Test\Helper::suppressDeprecationWarnings();
  1461. $customer = Braintree\Customer::create([
  1462. 'firstName' => 'Mike',
  1463. 'lastName' => 'Jones',
  1464. 'creditCard' => [
  1465. 'number' => '5105105105105100',
  1466. 'expirationDate' => '05/12',
  1467. 'cardholderName' => 'Mike Jones',
  1468. 'billingAddress' => [
  1469. 'firstName' => 'Drew',
  1470. 'lastName' => 'Smith'
  1471. ]
  1472. ]
  1473. ])->customer;
  1474. $queryString = $this->updateCustomerViaTr(
  1475. [],
  1476. [
  1477. 'customerId' => $customer->id,
  1478. 'customer' => [
  1479. 'firstName' => 'New First',
  1480. 'lastName' => 'New Last',
  1481. 'creditCard' => [
  1482. 'number' => '4111111111111111',
  1483. 'expirationDate' => '05/13',
  1484. 'cardholderName' => 'New Cardholder',
  1485. 'options' => [
  1486. 'updateExistingToken' => $customer->creditCards[0]->token
  1487. ],
  1488. 'billingAddress' => [
  1489. 'firstName' => 'New First Billing',
  1490. 'lastName' => 'New Last Billing',
  1491. 'options' => [
  1492. 'updateExisting' => true
  1493. ]
  1494. ]
  1495. ]
  1496. ]
  1497. ]
  1498. );
  1499. $result = Braintree\Customer::updateFromTransparentRedirect($queryString);
  1500. $this->assertTrue($result->success);
  1501. $this->assertEquals(true, $result->success);
  1502. $customer = $result->customer;
  1503. $this->assertEquals('New First', $customer->firstName);
  1504. $this->assertEquals('New Last', $customer->lastName);
  1505. $this->assertEquals(1, sizeof($result->customer->creditCards));
  1506. $creditCard = $customer->creditCards[0];
  1507. $this->assertEquals('411111', $creditCard->bin);
  1508. $this->assertEquals('1111', $creditCard->last4);
  1509. $this->assertEquals('New Cardholder', $creditCard->cardholderName);
  1510. $this->assertEquals('05/2013', $creditCard->expirationDate);
  1511. $this->assertEquals(1, sizeof($result->customer->addresses));
  1512. $address = $customer->addresses[0];
  1513. $this->assertEquals($address, $creditCard->billingAddress);
  1514. $this->assertEquals('New First Billing', $address->firstName);
  1515. $this->assertEquals('New Last Billing', $address->lastName);
  1516. }
  1517. public function testSale_createsASaleUsingGivenToken()
  1518. {
  1519. $customer = Braintree\Customer::createNoValidate([
  1520. 'creditCard' => [
  1521. 'number' => '5105105105105100',
  1522. 'expirationDate' => '05/12'
  1523. ]
  1524. ]);
  1525. $creditCard = $customer->creditCards[0];
  1526. $result = Braintree\Customer::sale($customer->id, [
  1527. 'amount' => '100.00'
  1528. ]);
  1529. $this->assertTrue($result->success);
  1530. $this->assertEquals('100.00', $result->transaction->amount);
  1531. $this->assertEquals($customer->id, $result->transaction->customerDetails->id);
  1532. $this->assertEquals($creditCard->token, $result->transaction->creditCardDetails->token);
  1533. }
  1534. public function testSaleNoValidate_createsASaleUsingGivenToken()
  1535. {
  1536. $customer = Braintree\Customer::createNoValidate([
  1537. 'creditCard' => [
  1538. 'number' => '5105105105105100',
  1539. 'expirationDate' => '05/12'
  1540. ]
  1541. ]);
  1542. $creditCard = $customer->creditCards[0];
  1543. $transaction = Braintree\Customer::saleNoValidate($customer->id, [
  1544. 'amount' => '100.00'
  1545. ]);
  1546. $this->assertEquals('100.00', $transaction->amount);
  1547. $this->assertEquals($customer->id, $transaction->customerDetails->id);
  1548. $this->assertEquals($creditCard->token, $transaction->creditCardDetails->token);
  1549. }
  1550. public function testSaleNoValidate_throwsIfInvalid()
  1551. {
  1552. $customer = Braintree\Customer::createNoValidate([
  1553. 'creditCard' => [
  1554. 'number' => '5105105105105100',
  1555. 'expirationDate' => '05/12'
  1556. ]
  1557. ]);
  1558. $creditCard = $customer->creditCards[0];
  1559. $this->setExpectedException('Braintree\Exception\ValidationsFailed');
  1560. Braintree\Customer::saleNoValidate($customer->id, [
  1561. 'amount' => 'invalid'
  1562. ]);
  1563. }
  1564. public function testCredit_createsACreditUsingGivenCustomerId()
  1565. {
  1566. $customer = Braintree\Customer::createNoValidate([
  1567. 'creditCard' => [
  1568. 'number' => '5105105105105100',
  1569. 'expirationDate' => '05/12'
  1570. ]
  1571. ]);
  1572. $creditCard = $customer->creditCards[0];
  1573. $result = Braintree\Customer::credit($customer->id, [
  1574. 'amount' => '100.00'
  1575. ]);
  1576. $this->assertTrue($result->success);
  1577. $this->assertEquals('100.00', $result->transaction->amount);
  1578. $this->assertEquals(Braintree\Transaction::CREDIT, $result->transaction->type);
  1579. $this->assertEquals($customer->id, $result->transaction->customerDetails->id);
  1580. $this->assertEquals($creditCard->token, $result->transaction->creditCardDetails->token);
  1581. }
  1582. public function testCreditNoValidate_createsACreditUsingGivenId()
  1583. {
  1584. $customer = Braintree\Customer::createNoValidate([
  1585. 'creditCard' => [
  1586. 'number' => '5105105105105100',
  1587. 'expirationDate' => '05/12'
  1588. ]
  1589. ]);
  1590. $creditCard = $customer->creditCards[0];
  1591. $transaction = Braintree\Customer::creditNoValidate($customer->id, [
  1592. 'amount' => '100.00'
  1593. ]);
  1594. $this->assertEquals('100.00', $transaction->amount);
  1595. $this->assertEquals(Braintree\Transaction::CREDIT, $transaction->type);
  1596. $this->assertEquals($customer->id, $transaction->customerDetails->id);
  1597. $this->assertEquals($creditCard->token, $transaction->creditCardDetails->token);
  1598. }
  1599. public function testCreditNoValidate_throwsIfInvalid()
  1600. {
  1601. $customer = Braintree\Customer::createNoValidate([
  1602. 'creditCard' => [
  1603. 'number' => '5105105105105100',
  1604. 'expirationDate' => '05/12'
  1605. ]
  1606. ]);
  1607. $creditCard = $customer->creditCards[0];
  1608. $this->setExpectedException('Braintree\Exception\ValidationsFailed');
  1609. Braintree\Customer::creditNoValidate($customer->id, [
  1610. 'amount' => 'invalid'
  1611. ]);
  1612. }
  1613. public function createCustomerViaTr($regularParams, $trParams)
  1614. {
  1615. Test\Helper::suppressDeprecationWarnings();
  1616. $trData = Braintree\TransparentRedirect::createCustomerData(
  1617. array_merge($trParams, ["redirectUrl" => "http://www.example.com"])
  1618. );
  1619. return Test\Helper::submitTrRequest(
  1620. Braintree\Customer::createCustomerUrl(),
  1621. $regularParams,
  1622. $trData
  1623. );
  1624. }
  1625. public function updateCustomerViaTr($regularParams, $trParams)
  1626. {
  1627. Test\Helper::suppressDeprecationWarnings();
  1628. $trData = Braintree\TransparentRedirect::updateCustomerData(
  1629. array_merge($trParams, ["redirectUrl" => "http://www.example.com"])
  1630. );
  1631. return Test\Helper::submitTrRequest(
  1632. Braintree\Customer::updateCustomerUrl(),
  1633. $regularParams,
  1634. $trData
  1635. );
  1636. }
  1637. }