def setUp(self): self.api = TransactionAPI('123', '456') self.success = lambda *args, **kwargs: SUCCESS.reset() or SUCCESS self.error = lambda *args, **kwargs: ERROR.reset() or ERROR self.year = date.today().year + 10 self.credit_card = CreditCard('4111111111111111', self.year, 1, '911') self.address = Address('45 Rose Ave', 'Venice', 'CA', '90291')
def test_create_saved_payment(self): service = self.api.client.service.CreateCustomerPaymentProfile service.return_value = SUCCESS year = date.today().year + 10 credit_card = CreditCard('4111111111111111', year, 1, '911', 'Jeff', 'Schenck') address = Address('45 Rose Ave', 'Venice', 'CA', '90291') # Without profile id should return object payment_profile = self.api.create_saved_payment(credit_card, address) self.assertEqual(service.call_args, None) self.assertEqual(payment_profile.__class__.__name__, 'customerPaymentProfileType') self.assertEqual(payment_profile.payment.__class__.__name__, 'paymentType') self.assertEqual(payment_profile.payment.creditCard.__class__.__name__, 'creditCardType') self.assertEqual(payment_profile.payment.creditCard.cardNumber, '4111111111111111') self.assertEqual(payment_profile.payment.creditCard.expirationDate, '{0}-01'.format(year)) self.assertEqual(payment_profile.payment.creditCard.cardCode, '911') self.assertEqual(payment_profile.billTo.firstName, 'Jeff') self.assertEqual(payment_profile.billTo.lastName, 'Schenck') self.assertEqual(payment_profile.billTo.address, '45 Rose Ave') self.assertEqual(payment_profile.billTo.city, 'Venice') self.assertEqual(payment_profile.billTo.state, 'CA') self.assertEqual(payment_profile.billTo.zip, '90291') self.assertEqual(payment_profile.billTo.country, 'US') # With profile id should make call to API payment_profile_id = self.api.create_saved_payment(credit_card, profile_id='1') self.assertEqual(payment_profile_id, '123458') self.assertEqual(service.call_args[0][1], '1') payment_profile = service.call_args[0][2] self.assertEqual(payment_profile._kind, 'CustomerPaymentProfileType') self.assertEqual(payment_profile.payment._kind, 'PaymentType') self.assertEqual(payment_profile.payment.creditCard._kind, 'CreditCardType') self.assertEqual(payment_profile.payment.creditCard.cardNumber, '4111111111111111') self.assertEqual(payment_profile.payment.creditCard.expirationDate, '{0}-01'.format(year)) self.assertEqual(payment_profile.payment.creditCard.cardCode, '911') self.assertEqual(payment_profile.billTo.firstName, 'Jeff') self.assertEqual(payment_profile.billTo.lastName, 'Schenck') self.assertNotEqual(payment_profile.billTo.address, '45 Rose Ave') self.assertNotEqual(payment_profile.billTo.city, 'Venice') self.assertNotEqual(payment_profile.billTo.state, 'CA') self.assertNotEqual(payment_profile.billTo.zip, '90291') self.assertNotEqual(payment_profile.billTo.country, 'US')
def test_credit_card_validation(self): # Expiration in the past fails expired = date.today() - timedelta(days=31) self.assertRaises(AuthorizeInvalidError, CreditCard, '4111111111111111', expired.year, expired.month, '911') # CVV in wrong format fails self.assertRaises(AuthorizeInvalidError, CreditCard, '4111111111111111', self.YEAR, 1, 'incorrect') # Invalid credit card number fails self.assertRaises(AuthorizeInvalidError, CreditCard, '4111111111111112', self.YEAR, 1, '911') # Test standard test credit card numbers that should validate for card_type, card_number in TEST_CARD_NUMBERS: CreditCard(card_number, self.YEAR, 1, '911')
def get_card(card_num, cvn, exp_month, exp_year, fname, lname, street, city, state, zipcode, authorize_id, authorize_key, country='USA'): client = get_client(authorize_id, authorize_key) address = Address(street, city, state, zipcode, country) card = CreditCard(card_num, exp_year, exp_month, cvn, fname, lname) return AuthorizeCreditCard(client, card, address=address)
def update_saved_payment(self, profile_id, payment_id, **kwargs): payment_profile = self.client.factory.create( 'CustomerPaymentProfileExType') customer_type_enum = self.client.factory.create('CustomerTypeEnum') payment_profile.customerType = customer_type_enum.individual payment_simple_type = self.client.factory.create('PaymentType') card_simple_type = self.client.factory.create('CreditCardSimpleType') number = kwargs['number'] # Authorize.net uses this constant to indicate that we want to keep # the existing expiration date. date = 'XXXX' card_simple_type.cardNumber = number if kwargs['exp_month'] and kwargs['exp_year']: exp = CreditCard.exp_time(kwargs['exp_month'], kwargs['exp_year']) if exp <= datetime.now(): raise AuthorizeInvalidError('This credit card has expired.') card_simple_type.expirationDate =\ '{0}-{1:0>2}'.format(kwargs['exp_year'], kwargs['exp_month']) else: card_simple_type.expirationDate = date payment_simple_type.creditCard = card_simple_type payment_profile.payment = payment_simple_type payment_profile.payment.creditCard = card_simple_type payment_profile.customerPaymentProfileId = payment_id if kwargs['first_name']: payment_profile.billTo.firstName = kwargs['first_name'] if kwargs['last_name']: payment_profile.billTo.lastName = kwargs['last_name'] payment_profile = self._address_to_profile( kwargs['address'], payment_profile) self._make_call( 'UpdateCustomerPaymentProfile', profile_id, payment_profile, 'none') if not kwargs['email']: return profile = self.client.factory.create('CustomerProfileExType') profile.email = kwargs['email'] profile.customerProfileId = profile_id self._make_call('UpdateCustomerProfile', profile)
def test_credit_card_safe_number(self): credit_card = CreditCard('4111111111111111', self.YEAR, 1, '911') self.assertEqual(credit_card.safe_number, '************1111')
def test_credit_card_expiration(self): credit_card = CreditCard('4111111111111111', self.YEAR, 1, '911') self.assertEqual(credit_card.expiration, datetime(self.YEAR, 1, 31, 23, 59, 59))
def test_credit_card_type_detection(self): for card_type, card_number in TEST_CARD_NUMBERS: credit_card = CreditCard(card_number, self.YEAR, 1, '911') self.assertEqual(credit_card.card_type, card_type)
def test_basic_credit_card(self): credit_card = CreditCard('4111-1111-1111-1111', self.YEAR, 1, '911') repr(credit_card)
def test_create_subscription(self): service = self.api.client.service.ARBCreateSubscription service.return_value = SUCCESS year = date.today().year + 10 credit_card = CreditCard('4111111111111111', year, 1, '911', 'Jeff', 'Schenck') nameless_credit_card = CreditCard('4111111111111111', year, 1, '911') start = date.today() + timedelta(days=7) # Test missing credit card name self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, nameless_credit_card, 10, start, months=1, occurrences=10) # Test both or neither of days and months arguments self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, occurrences=10) self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, days=30, months=1, occurrences=10) # Test validation of months and of days arguments self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, days=1, occurrences=10) self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, days=400, occurrences=10) self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, months=0, occurrences=10) self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, months=13, occurrences=10) # Test start date in the past past_start = date.today() - timedelta(days=1) self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, past_start, months=1, occurrences=10) # Test providing only one of trial_amount and trial_occurrences self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, months=1, occurrences=10, trial_amount=5) self.assertRaises(AuthorizeInvalidError, self.api.create_subscription, credit_card, 10, start, months=1, occurrences=10, trial_occurrences=3) # Test basic successful subscription subscription_id = self.api.create_subscription(credit_card, 10, start, months=1, occurrences=10) self.assertEqual(subscription_id, '123') subscription = service.call_args[0][1] self.assertEqual(subscription._kind, 'ARBSubscriptionType') self.assertEqual(subscription.amount, '10.00') self.assertEqual(subscription.payment._kind, 'PaymentType') self.assertEqual(subscription.payment.creditCard._kind, 'CreditCardType') self.assertEqual(subscription.payment.creditCard.cardNumber, '4111111111111111') self.assertEqual(subscription.payment.creditCard.expirationDate, '{0}-01'.format(year)) self.assertEqual(subscription.payment.creditCard.cardCode, '911') self.assertEqual(subscription.billTo.firstName, 'Jeff') self.assertEqual(subscription.billTo.lastName, 'Schenck') self.assertEqual(subscription.paymentSchedule.interval.length, 1) self.assertEqual(subscription.paymentSchedule.startDate, start.strftime('%Y-%m-%d')) self.assertEqual(subscription.paymentSchedule.totalOccurrences, 10) # Test with days interval self.api.create_subscription(credit_card, 10, start, days=14, occurrences=10) subscription = service.call_args[0][1] self.assertEqual(subscription.paymentSchedule.interval.length, 14) # Test with infinite occurrences self.api.create_subscription(credit_card, 10, start, months=1) subscription = service.call_args[0][1] self.assertEqual(subscription.paymentSchedule.totalOccurrences, 9999) # Test with trial period self.api.create_subscription(credit_card, 10, start, months=1, occurrences=10, trial_amount=5, trial_occurrences=3) subscription = service.call_args[0][1] self.assertEqual(subscription.paymentSchedule.trialOccurrences, 3) self.assertEqual(subscription.trialAmount, '5.00')