def create_payment_method(self): payment_method = PaymentMethod.find_by_token(self.payment_token) if payment_method is None: payment_method = PaymentMethod.create(self.customer, self.details) return payment_method
def test_find_by_payment_mehod_id_and_processor_name_retrieves_the_expected_payment_processor_payment_information( self, database): name = 'stripetest' information = {'payment_token': 'sometoken'} customer = Customer.create(first_name='Test', last_name='Test', company='Really Cool LTD', email='*****@*****.**', phone='+1111111111', fax='+12222222222', website='https://www.reallycool.test') payment_method = PaymentMethod.create( customer=customer, details=self.details, ) payment_processor_payment_information = PaymentProcessorPaymentInformation.create( name=name, payment_method=payment_method, information=information) assert PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name( uuid.uuid4(), name) is None assert PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name( payment_method.id, name) is not None assert PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name( payment_method.id, name) == payment_processor_payment_information
def test_creates_payment_method_when_all_details_are_valid( self, testapp, database, payment_processors): client = testapp.test_client() customer = Customer.create( **{ 'first_name': 'Test', 'last_name': 'Test', 'company': 'Really Cool LTD', 'email': '*****@*****.**', 'phone': '+1111111111' }) customer_information = {'customer_id': 'someid'} payment_processor_customer_information = PaymentProcessorCustomerInformation.create( name='braintree', customer=customer, information=customer_information) params = { 'cardholder_name': 'Test Coool', 'number': '4005519200000004', 'cvv': '111', 'expiration_date': '12/99' } headers = { 'Authorization': f'Bearer {customer.token}', } response = client.post('/payment_methods', json=params, headers=headers) assert response.status_code == HTTPStatus.CREATED assert PaymentMethod.find_by_token(response.json['token']) is not None
def test_creates_customer_with_all_expected_attributes(self, database): name = 'braintreetest' information = { 'payment_token': 'sometoken', 'nonce_token': 'someothertoken' } customer = Customer.create(first_name='Test', last_name='Test', company='Really Cool LTD', email='*****@*****.**', phone='+1111111111', fax='+12222222222', website='https://www.reallycool.test') payment_method = PaymentMethod.create( customer=customer, details=self.details, ) payment_processor_payment_information = PaymentProcessorPaymentInformation.create( name=name, payment_method=payment_method, information=information) assert PaymentProcessorPaymentInformation.query.filter_by( name=name).first().id == payment_processor_payment_information.id assert payment_processor_payment_information.id is not None assert payment_processor_payment_information.name == name assert payment_processor_payment_information.information == information assert payment_processor_payment_information.payment_method_id == payment_method.id
def test_converts_payment_method_to_dict(self, database): customer = Customer.create(first_name='Test', last_name='Coool', company='Really Cool LTD', email='*****@*****.**', phone='+1111111111', fax='+12222222222', website='https://www.reallycool.test') details = { 'card_holder_name': 'Test Coool', 'number': '1111' * 4, 'cvv': '111', 'expiration_date': '12/99' } payment_method = PaymentMethod.create( customer=customer, details=details, ) expected_dict = { 'token': payment_method.token, 'updated_at': int(payment_method.created_at.timestamp()), 'created_at': int(payment_method.created_at.timestamp()) } assert payment_method.as_dict() == expected_dict
def test_creates_payment_method_with_all_attributes_correct( self, database): customer = Customer.create(first_name='Test', last_name='Coool', company='Really Cool LTD', email='*****@*****.**', phone='+1111111111', fax='+12222222222', website='https://www.reallycool.test') details = { 'card_holder_name': 'Test Coool', 'number': '1111' * 4, 'cvv': '111', 'expiration_date': '12/99' } payment_method = PaymentMethod.create( customer=customer, details=details, ) assert PaymentMethod.query.filter_by( customer_id=customer.id).first().id == payment_method.id assert payment_method.id is not None assert PaymentMethod.query.filter_by( customer_id=customer.id).first().token == payment_method.token assert payment_method.token is not None assert payment_method.customer_id == customer.id assert payment_method.details == details
def test_find_by_id_retrieves_the_expected_payment_method(self, database): customer = Customer.create(first_name='Test', last_name='Coool2', company='Really Cool LTD', email='*****@*****.**', phone='+1111111111', fax='+12222222222', website='https://www.reallycool.test') details = { 'card_holder_name': 'Test Coool', 'number': '1111' * 4, 'cvv': '111', 'expiration_date': '12/99' } payment_method = PaymentMethod.create(customer=customer, details=details) assert PaymentMethod.find_by_id(uuid.uuid4()) is None assert PaymentMethod.find_by_id(payment_method.id) is not None assert PaymentMethod.find_by_id(payment_method.id) == payment_method
def test_does_not_create_payment_method_with_invalid_customer( self, database): customer = Customer(id=uuid.uuid4(), first_name='None', last_name='Existing', company='Really Cool LTD', email='*****@*****.**', phone='+1111111111', fax='+12222222222', website='https://www.reallycool.test') details = { 'card_holder_name': 'Test Coool', 'number': '1111' * 4, 'cvv': '111', 'expiration_date': '12/99' } with pytest.raises(InvalidCustomer): payment_method = PaymentMethod.create(customer=customer, details=details) assert PaymentMethod.query.filter_by( customer_id=customer.id).first() is None
def _find_payment_method(self, token: str): self.payment_method = PaymentMethod.find_by_token(token) if self.payment_method is None: raise InvalidPaymentMethod