1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
|
import time
from random import randint
from tests.test_helper import *
from braintree.test.credit_card_numbers import CreditCardNumbers
from braintree.test.nonces import Nonces
class TestPaymentMethod(unittest.TestCase):
def test_create_with_paypal_future_payments_nonce(self):
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": Nonces.PayPalFuturePayment
})
self.assertTrue(result.is_success)
created_account = result.payment_method
self.assertEquals(created_account.__class__, PayPalAccount)
self.assertEquals(created_account.email, "jane.doe@example.com")
self.assertNotEquals(created_account.image_url, None)
found_account = PaymentMethod.find(result.payment_method.token)
self.assertNotEqual(None, found_account)
self.assertEquals(found_account.token, created_account.token)
def test_create_returns_validation_failures(self):
http = ClientApiHttp.create()
status_code, nonce = http.get_paypal_nonce({
"options": {"validate": False}
})
self.assertEquals(status_code, 202)
result = PaymentMethod.create({
"payment_method_nonce": nonce
})
self.assertFalse(result.is_success)
paypal_error_codes = [
error.code for error in result.errors.for_object("paypal_account").on("base")
]
self.assertTrue(ErrorCodes.PayPalAccount.ConsentCodeOrAccessTokenIsRequired in paypal_error_codes)
customer_error_codes = [
error.code for error in result.errors.for_object("paypal_account").on("customer_id")
]
self.assertTrue(ErrorCodes.PayPalAccount.CustomerIdIsRequiredForVaulting in customer_error_codes)
def test_create_and_make_default(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": "4111111111111111",
"expiration_date": "05/2014",
})
self.assertTrue(credit_card_result.is_success)
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": Nonces.PayPalFuturePayment,
"options": {"make_default": True},
})
self.assertTrue(result.is_success)
self.assertTrue(result.payment_method.default)
def test_create_and_set_token(self):
customer_id = Customer.create().customer.id
token = str(random.randint(1, 1000000))
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": Nonces.PayPalFuturePayment,
"token": token
})
self.assertTrue(result.is_success)
self.assertEquals(token, result.payment_method.token)
def test_create_with_paypal_one_time_nonce_fails(self):
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": Nonces.PayPalOneTimePayment
})
self.assertFalse(result.is_success)
error_code = result.errors.for_object("paypal_account").on("base")[0].code
self.assertEquals(error_code, ErrorCodes.PayPalAccount.CannotVaultOneTimeUsePayPalAccount)
def test_create_with_credit_card_nonce(self):
http = ClientApiHttp.create()
status_code, nonce = http.get_credit_card_nonce({
"number": "4111111111111111",
"expirationMonth": "12",
"expirationYear": "2020",
"options": {"validate": False}
})
self.assertEquals(status_code, 202)
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": nonce
})
self.assertTrue(result.is_success)
created_credit_card = result.payment_method
self.assertEquals(created_credit_card.__class__, CreditCard)
self.assertEquals(created_credit_card.bin, "411111")
found_credit_card = PaymentMethod.find(result.payment_method.token)
self.assertNotEqual(None, found_credit_card)
self.assertEquals(found_credit_card.token, created_credit_card.token)
def test_create_with_sepa_bank_account_nonce(self):
config = Configuration.instantiate()
customer_id = Customer.create().customer.id
token = TestHelper.generate_decoded_client_token({"customer_id": customer_id, "sepa_mandate_type": SEPABankAccount.MandateType.Business})
authorization_fingerprint = json.loads(token)["authorizationFingerprint"]
client_api = ClientApiHttp(config, {
"authorization_fingerprint": authorization_fingerprint,
"shared_customer_identifier": "fake_identifier",
"shared_customer_identifier_type": "testing"
})
nonce = client_api.get_sepa_bank_account_nonce({
"locale": "de-DE",
"bic": "DEUTDEFF",
"iban": "DE89370400440532013000",
"accountHolderName": "Baron Von Holder",
"billingAddress": {"region": "Hesse", "country_name": "Germany"}
})
self.assertNotEquals(nonce, None)
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": nonce
})
self.assertTrue(result.is_success)
self.assertNotEqual(result.payment_method.image_url, None)
found_bank_account = PaymentMethod.find(result.payment_method.token)
self.assertNotEqual(found_bank_account, None)
self.assertEquals(found_bank_account.bic, "DEUTDEFF")
self.assertEquals(found_bank_account.__class__, SEPABankAccount)
def test_create_respects_verify_card_and_verification_merchant_account_id_when_outside_nonce(self):
config = Configuration.instantiate()
customer_id = Customer.create().customer.id
client_token = json.loads(TestHelper.generate_decoded_client_token())
authorization_fingerprint = client_token["authorizationFingerprint"]
http = ClientApiHttp(config, {
"authorization_fingerprint": authorization_fingerprint,
"shared_customer_identifier": "fake_identifier",
"shared_customer_identifier_type": "testing"
})
status_code, nonce = http.get_credit_card_nonce({
"number": "4000111111111115",
"expirationMonth": "11",
"expirationYear": "2099"
})
self.assertTrue(status_code == 201)
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"options": {
"verify_card": "true",
"verification_merchant_account_id": TestHelper.non_default_merchant_account_id
}
})
self.assertFalse(result.is_success)
self.assertTrue(result.credit_card_verification.status == Transaction.Status.ProcessorDeclined)
self.assertTrue(result.credit_card_verification.processor_response_code == "2000")
self.assertTrue(result.credit_card_verification.processor_response_text == "Do Not Honor")
self.assertTrue(result.credit_card_verification.merchant_account_id == TestHelper.non_default_merchant_account_id)
def test_create_respects_fail_one_duplicate_payment_method_when_included_outside_of_the_nonce(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012"
})
self.assertTrue(credit_card_result.is_success)
config = Configuration.instantiate()
customer_id = Customer.create().customer.id
client_token = json.loads(TestHelper.generate_decoded_client_token())
authorization_fingerprint = client_token["authorizationFingerprint"]
http = ClientApiHttp(config, {
"authorization_fingerprint": authorization_fingerprint,
"shared_customer_identifier": "fake_identifier",
"shared_customer_identifier_type": "testing"
})
status_code, nonce = http.get_credit_card_nonce({
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012"
})
self.assertTrue(status_code == 201)
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"options": {
"fail_on_duplicate_payment_method": "true"
}
})
self.assertFalse(result.is_success)
self.assertTrue(result.errors.deep_errors[0].code == "81724")
def test_create_allows_passing_billing_address_id_outside_the_nonce(self):
customer_id = Customer.create().customer.id
http = ClientApiHttp.create()
status_code, nonce = http.get_credit_card_nonce({
"number": "4111111111111111",
"expirationMonth": "12",
"expirationYear": "2020",
"options": {"validate": "false"}
})
self.assertTrue(status_code == 202)
address_result = Address.create({
"customer_id": customer_id,
"first_name": "Bobby",
"last_name": "Tables"
})
self.assertTrue(address_result.is_success)
payment_method_result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"billing_address_id": address_result.address.id
})
self.assertTrue(payment_method_result.is_success)
self.assertTrue(type(payment_method_result.payment_method) is CreditCard)
token = payment_method_result.payment_method.token
found_credit_card = CreditCard.find(token)
self.assertFalse(found_credit_card == None)
self.assertTrue(found_credit_card.billing_address.first_name == "Bobby")
self.assertTrue(found_credit_card.billing_address.last_name == "Tables")
def test_create_allows_passing_billing_address_outside_the_nonce(self):
customer_id = Customer.create().customer.id
http = ClientApiHttp.create()
status_code, nonce = http.get_credit_card_nonce({
"number": "4111111111111111",
"expirationMonth": "12",
"expirationYear": "2020",
"options": {"validate": "false"}
})
self.assertTrue(status_code == 202)
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"billing_address": {
"street_address": "123 Abc Way"
}
})
self.assertTrue(result.is_success)
self.assertTrue(type(result.payment_method) is CreditCard)
token = result.payment_method.token
found_credit_card = CreditCard.find(token)
self.assertFalse(found_credit_card == None)
self.assertTrue(found_credit_card.billing_address.street_address == "123 Abc Way")
def test_create_overrides_the_billing_address_in_the_nonce(self):
customer_id = Customer.create().customer.id
http = ClientApiHttp.create()
status_code, nonce = http.get_credit_card_nonce({
"number": "4111111111111111",
"expirationMonth": "12",
"expirationYear": "2020",
"options": {"validate": "false"},
"billing_address": {
"street_address": "456 Xyz Way"
}
})
self.assertTrue(status_code == 202)
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"billing_address": {
"street_address": "123 Abc Way"
}
})
self.assertTrue(result.is_success)
self.assertTrue(type(result.payment_method) is CreditCard)
token = result.payment_method.token
found_credit_card = CreditCard.find(token)
self.assertFalse(found_credit_card == None)
self.assertTrue(found_credit_card.billing_address.street_address == "123 Abc Way")
def test_create_does_not_override_the_billing_address_for_a_valuted_credit_card(self):
config = Configuration.instantiate()
customer_id = Customer.create().customer.id
client_token = json.loads(TestHelper.generate_decoded_client_token({"customer_id": customer_id}))
authorization_fingerprint = client_token["authorizationFingerprint"]
http = ClientApiHttp(config, {"authorization_fingerprint": authorization_fingerprint})
status_code, nonce = http.get_credit_card_nonce({
"number": "4111111111111111",
"expirationMonth": "12",
"expirationYear": "2020",
"billing_address": {
"street_address": "456 Xyz Way"
}
})
self.assertTrue(status_code == 201)
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"billing_address": {
"street_address": "123 Abc Way"
}
})
self.assertTrue(result.is_success)
self.assertTrue(type(result.payment_method) is CreditCard)
token = result.payment_method.token
found_credit_card = CreditCard.find(token)
self.assertFalse(found_credit_card == None)
self.assertTrue(found_credit_card.billing_address.street_address == "456 Xyz Way")
def test_create_does_not_return_an_error_if_credit_card_options_are_present_for_paypal_nonce(self):
customer_id = Customer.create().customer.id
original_token = "paypal-account-" + str(int(time.time()))
nonce = Nonces.nonce_for_paypal_account({
"consent_code": "consent-code",
"token": original_token
})
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"options": {
"verify_card": "true",
"fail_on_duplicate_payment_method": "true",
"verification_merchant_account_id": "not_a_real_merchant_account_id"
}
})
self.assertTrue(result.is_success)
def test_create_for_paypal_ignores_passed_billing_address_id(self):
nonce = Nonces.nonce_for_paypal_account({
"consent_code": "consent-code"
})
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"billing_address_id": "address_id"
})
self.assertTrue(result.is_success)
self.assertTrue(type(result.payment_method) is PayPalAccount)
self.assertFalse(result.payment_method.image_url == None)
token = result.payment_method.token
found_paypal_account = PayPalAccount.find(token)
self.assertFalse(found_paypal_account == None)
def test_create_for_paypal_ignores_passed_billing_address_params(self):
nonce = Nonces.nonce_for_paypal_account({
"consent_code": "PAYPAL_CONSENT_CODE"
})
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id,
"billing_address": {
"street_address": "123 Abc Way"
}
})
self.assertTrue(result.is_success)
self.assertTrue(type(result.payment_method) is PayPalAccount)
self.assertFalse(result.payment_method.image_url == None)
token = result.payment_method.token
found_paypal_account = PayPalAccount.find(token)
self.assertFalse(found_paypal_account == None)
def test_find_returns_a_paypal_account(self):
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": Nonces.PayPalFuturePayment
})
self.assertTrue(result.is_success)
found_account = PaymentMethod.find(result.payment_method.token)
self.assertNotEqual(None, found_account)
self.assertEquals(found_account.__class__, PayPalAccount)
self.assertEquals(found_account.email, "jane.doe@example.com")
def test_find_returns_a_credit_card(self):
customer = Customer.create().customer
result = CreditCard.create({
"customer_id": customer.id,
"number": "4111111111111111",
"expiration_date": "05/2009",
"cvv": "100",
"cardholder_name": "John Doe"
})
self.assertTrue(result.is_success)
credit_card = result.credit_card
found_credit_card = PaymentMethod.find(result.credit_card.token)
self.assertNotEqual(None, found_credit_card)
self.assertEquals(found_credit_card.__class__, CreditCard)
self.assertEquals(found_credit_card.bin, "411111")
def test_delete_deletes_a_credit_card(self):
customer = Customer.create().customer
result = CreditCard.create({
"customer_id": customer.id,
"number": "4111111111111111",
"expiration_date": "05/2009",
"cvv": "100",
"cardholder_name": "John Doe"
})
self.assertTrue(result.is_success)
delete_result = PaymentMethod.delete(result.credit_card.token)
self.assertTrue(delete_result.is_success)
self.assertRaises(NotFoundError, PaymentMethod.find, result.credit_card.token)
def test_delete_deletes_a_paypal_account(self):
customer_id = Customer.create().customer.id
result = PaymentMethod.create({
"customer_id": customer_id,
"payment_method_nonce": Nonces.PayPalFuturePayment
})
self.assertTrue(result.is_success)
delete_result = PaymentMethod.delete(result.payment_method.token)
self.assertTrue(delete_result.is_success)
self.assertRaises(NotFoundError, PaymentMethod.find, result.payment_method.token)
def test_update_credit_cards_updates_the_credit_card(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"cardholder_name": "Original Holder",
"customer_id": customer_id,
"cvv": "123",
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012"
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"cardholder_name": "New Holder",
"cvv": "456",
"number": CreditCardNumbers.MasterCard,
"expiration_date": "06/2013"
})
self.assertTrue(update_result.is_success)
self.assertTrue(update_result.payment_method.token == credit_card_result.credit_card.token)
updated_credit_card = update_result.payment_method
self.assertTrue(updated_credit_card.bin == CreditCardNumbers.MasterCard[:6])
self.assertTrue(updated_credit_card.last_4 == CreditCardNumbers.MasterCard[-4:])
self.assertTrue(updated_credit_card.expiration_date == "06/2013")
def test_update_creates_a_new_billing_address_by_default(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012",
"billing_address": {
"street_address": "123 Nigeria Ave"
}
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"billing_address": {
"region": "IL"
}
})
self.assertTrue(update_result.is_success)
updated_credit_card = update_result.payment_method
self.assertTrue(updated_credit_card.billing_address.region == "IL")
self.assertTrue(updated_credit_card.billing_address.street_address == None)
self.assertFalse(updated_credit_card.billing_address.id == credit_card_result.credit_card.billing_address.id)
def test_update_updates_the_billing_address_if_option_is_specified(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012",
"billing_address": {
"street_address": "123 Nigeria Ave"
}
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"billing_address": {
"region": "IL",
"options": {
"update_existing": "true"
}
}
})
self.assertTrue(update_result.is_success)
updated_credit_card = update_result.payment_method
self.assertTrue(updated_credit_card.billing_address.region == "IL")
self.assertTrue(updated_credit_card.billing_address.street_address == "123 Nigeria Ave")
self.assertTrue(updated_credit_card.billing_address.id == credit_card_result.credit_card.billing_address.id)
def test_update_updates_the_country_via_codes(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012",
"billing_address": {
"street_address": "123 Nigeria Ave"
}
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"billing_address": {
"country_name": "American Samoa",
"country_code_alpha2": "AS",
"country_code_alpha3": "ASM",
"country_code_numeric": "016",
"options": {
"update_existing": "true"
}
}
})
self.assertTrue(update_result.is_success)
updated_credit_card = update_result.payment_method
self.assertTrue(updated_credit_card.billing_address.country_name == "American Samoa")
self.assertTrue(updated_credit_card.billing_address.country_code_alpha2 == "AS")
self.assertTrue(updated_credit_card.billing_address.country_code_alpha3 == "ASM")
self.assertTrue(updated_credit_card.billing_address.country_code_numeric == "016")
def test_update_can_pass_expiration_month_and_expiration_year(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012"
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"number": CreditCardNumbers.MasterCard,
"expiration_month": "07",
"expiration_year": "2011"
})
self.assertTrue(update_result.is_success)
self.assertTrue(update_result.payment_method.token == credit_card_result.credit_card.token)
updated_credit_card = update_result.payment_method
self.assertTrue(updated_credit_card.expiration_month == "07")
self.assertTrue(updated_credit_card.expiration_year == "2011")
self.assertTrue(updated_credit_card.expiration_date == "07/2011")
def test_update_verifies_the_update_if_options_verify_card_is_true(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"cardholder_name": "Original Holder",
"customer_id": customer_id,
"cvv": "123",
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012"
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"cardholder_name": "New Holder",
"cvv": "456",
"number": CreditCardNumbers.FailsSandboxVerification.MasterCard,
"expiration_date": "06/2013",
"options": {
"verify_card": "true"
}
})
self.assertFalse(update_result.is_success)
self.assertTrue(update_result.credit_card_verification.status == CreditCardVerification.Status.ProcessorDeclined)
self.assertTrue(update_result.credit_card_verification.gateway_rejection_reason == None)
def test_update_can_update_the_billing_address(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"cardholder_name": "Original Holder",
"customer_id": customer_id,
"cvv": "123",
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012",
"billing_address": {
"first_name": "Old First Name",
"last_name": "Old Last Name",
"company": "Old Company",
"street_address": "123 Old St",
"extended_address": "Apt Old",
"locality": "Old City",
"region": "Old State",
"postal_code": "12345",
"country_name": "Canada"
}
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"options": { "verify_card": "false" },
"billing_address": {
"first_name": "New First Name",
"last_name": "New Last Name",
"company": "New Company",
"street_address": "123 New St",
"extended_address": "Apt New",
"locality": "New City",
"region": "New State",
"postal_code": "56789",
"country_name": "United States of America"
}
})
self.assertTrue(update_result.is_success)
address = update_result.payment_method.billing_address
self.assertTrue(address.first_name == "New First Name")
self.assertTrue(address.last_name == "New Last Name")
self.assertTrue(address.company == "New Company")
self.assertTrue(address.street_address == "123 New St")
self.assertTrue(address.extended_address == "Apt New")
self.assertTrue(address.locality == "New City")
self.assertTrue(address.region == "New State")
self.assertTrue(address.postal_code == "56789")
self.assertTrue(address.country_name == "United States of America")
def test_update_returns_an_error_response_if_invalid(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"cardholder_name": "Original Holder",
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2012"
})
update_result = PaymentMethod.update(credit_card_result.credit_card.token, {
"cardholder_name": "New Holder",
"number": "invalid",
"expiration_date": "05/2014",
})
self.assertFalse(update_result.is_success)
self.assertTrue(update_result.errors.for_object("credit_card").on("number")[0].message == "Credit card number must be 12-19 digits.")
def test_update_can_update_the_default(self):
customer_id = Customer.create().customer.id
card1 = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2009"
}).credit_card
card2 = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2009"
}).credit_card
self.assertTrue(card1.default == True)
self.assertTrue(card2.default == False)
update_result = PaymentMethod.update(card2.token, {
"options": { "make_default": True }
})
self.assertTrue(CreditCard.find(card1.token).default == False)
self.assertTrue(CreditCard.find(card2.token).default == True)
def test_update_updates_a_paypal_accounts_token(self):
customer_id = Customer.create().customer.id
original_token = "paypal-account-" + str(int(time.time()))
nonce = Nonces.nonce_for_paypal_account({
"consent_code": "consent-code",
"token": original_token
})
original_result = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id
})
updated_token = "UPDATED_TOKEN-" + str(randint(0,100000000))
updated_result = PaymentMethod.update(
original_token,
{"token": updated_token}
)
updated_paypal_account = PayPalAccount.find(updated_token)
self.assertTrue(updated_paypal_account.email == original_result.payment_method.email)
self.assertRaises(NotFoundError, PaymentMethod.find, original_token)
def test_update_can_make_a_paypal_account_the_default_payment_method(self):
customer_id = Customer.create().customer.id
credit_card_result = CreditCard.create({
"customer_id": customer_id,
"number": CreditCardNumbers.Visa,
"expiration_date": "05/2009",
"options": {"make_default": "true"}
})
self.assertTrue(credit_card_result.is_success)
nonce = Nonces.nonce_for_paypal_account({
"consent_code": "consent-code"
})
original_token = PaymentMethod.create({
"payment_method_nonce": nonce,
"customer_id": customer_id
}).payment_method.token
updated_result = PaymentMethod.update(
original_token,
{"options": {"make_default": "true"}}
)
updated_paypal_account = PayPalAccount.find(original_token)
self.assertTrue(updated_paypal_account.default == True)
def test_update_updates_a_paypal_accounts_token(self):
customer_id = Customer.create().customer.id
first_token = "paypal-account-" + str(randint(0,100000000))
second_token = "paypal-account-" + str(randint(0,100000000))
first_nonce = Nonces.nonce_for_paypal_account({
"consent_code": "consent-code",
"token": first_token
})
first_result = PaymentMethod.create({
"payment_method_nonce": first_nonce,
"customer_id": customer_id
})
second_nonce = Nonces.nonce_for_paypal_account({
"consent_code": "consent-code",
"token": second_token
})
second_result = PaymentMethod.create({
"payment_method_nonce": second_nonce,
"customer_id": customer_id
})
updated_result = PaymentMethod.update(
first_token,
{"token": second_token}
)
self.assertTrue(updated_result.is_success == False)
self.assertTrue(updated_result.errors.deep_errors[0].code == "92906")
class CreditCardForwardingTest(unittest.TestCase):
def setUp(self):
braintree.Configuration.configure(
braintree.Environment.Development,
"forward_payment_method_merchant_id",
"forward_payment_method_public_key",
"forward_payment_method_private_key"
)
def tearDown(self):
braintree.Configuration.configure(
braintree.Environment.Development,
"integration_merchant_id",
"integration_public_key",
"integration_private_key"
)
def test_forward(self):
customer = Customer.create().customer
credit_card_result = CreditCard.create({
"customer_id": customer.id,
"number": "4111111111111111",
"expiration_date": "05/2025"
})
self.assertTrue(credit_card_result.is_success)
source_merchant_card = credit_card_result.credit_card
forward_result = CreditCard.forward(
source_merchant_card.token,
"integration_merchant_id"
)
self.assertTrue(forward_result.is_success)
braintree.Configuration.configure(
braintree.Environment.Development,
"integration_merchant_id",
"integration_public_key",
"integration_private_key"
)
customer = Customer.create().customer
credit_card_result = CreditCard.create({
"customer_id": customer.id,
"payment_method_nonce": forward_result.nonce
})
self.assertTrue(credit_card_result.is_success)
receiving_merchant_card = credit_card_result.credit_card
self.assertEqual(source_merchant_card.bin, receiving_merchant_card.bin)
self.assertEqual(source_merchant_card.last_4, receiving_merchant_card.last_4)
self.assertEqual(source_merchant_card.expiration_month, receiving_merchant_card.expiration_month)
self.assertEqual(source_merchant_card.expiration_year, receiving_merchant_card.expiration_year)
def test_forward_invalid_token_raises_exception(self):
self.assertRaises(NotFoundError, CreditCard.forward, "invalid", "integration_merchant_id")
def test_forward_invalid_receiving_merchant_raises_exception(self):
customer = Customer.create().customer
credit_card_result = CreditCard.create({
"customer_id": customer.id,
"number": "4111111111111111",
"expiration_date": "05/2025"
})
self.assertTrue(credit_card_result.is_success)
source_merchant_card = credit_card_result.credit_card
self.assertRaises(NotFoundError, CreditCard.forward, source_merchant_card.token, "invalid_merchant_id")
|