Exemplo n.º 1
0
 def setUp(self):
     super().setUp()
     self.account = get_account()
     self.account.save()
     self.payment = get_payment(account=self.account,
                                state=PaymentState.READY_TO_PROCESS)
     self.payment.save()
Exemplo n.º 2
0
    def test_create_gw_connection_error(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()

        with patch('django_pain.card_payment_handlers.csob.CsobClient'
                   ) as gateway_client_mock:
            gateway_client_mock.side_effect = PaymentHandlerConnectionError()
            response = self.client.post(
                '/api/private/bankpayment/',
                data={
                    'amount':
                    '1000',
                    'variable_symbol':
                    '130',
                    'processor':
                    'donations',
                    'card_handler':
                    'csob',
                    'return_url':
                    'https://donations.nic.cz/return/',
                    'return_method':
                    'POST',
                    'language':
                    'cs',
                    'cart':
                    '[{"name":"Dar","amount":1000,"description":"Longer description","quantity":1}]',
                })

        self.assertEqual(response.status_code, 503)
Exemplo n.º 3
0
    def test_get_state(self):
        account = get_account(account_number='123456', currency='CZK')
        payment = get_payment(identifier='1', account=account, counter_account_name='Account one',
                              state=PaymentState.PROCESSED)

        serializer = BankPaymentSerializer()
        self.assertEqual(serializer.get_state(payment), ExternalPaymentState.PAID)
Exemplo n.º 4
0
    def test_retrieve_exists(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()
        payment = get_payment(identifier='1',
                              account=account,
                              counter_account_number='',
                              payment_type=PaymentType.CARD_PAYMENT,
                              state=PaymentState.INITIALIZED,
                              card_handler='csob')
        payment.save()

        result_mock = Mock()
        result_mock.payload = {
            'paymentStatus': CSOB.PAYMENT_STATUS_CANCELLED,
            'resultCode': CSOB.RETURN_CODE_OK
        }

        card_payment_hadler = get_card_payment_handler_instance(
            payment.card_handler)
        with patch.object(card_payment_hadler,
                          '_client') as gateway_client_mock:
            gateway_client_mock.payment_status.return_value = result_mock
            response = self.client.get('/api/private/bankpayment/{}/'.format(
                payment.uuid))

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['state'], ExternalPaymentState.CANCELED)
Exemplo n.º 5
0
    def test_init_payment_ok(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()

        handler = CSOBCardPaymentHandler('csob')
        with patch.object(handler, '_client') as gateway_client_mock:
            gateway_client_mock.gateway_return.return_value = OrderedDict([
                ('payId', 'unique_id_123'),
                ('resultCode', CSOB.RETURN_CODE_OK), ('resultMessage', 'OK'),
                ('paymentStatus', CSOB.PAYMENT_STATUS_INIT),
                ('dttime', datetime.datetime(2020, 6, 10, 16, 47, 30))
            ])
            gateway_client_mock.get_payment_process_url.return_value = sentinel.url
            payment, redirect_url = handler.init_payment(
                100, '123', 'donations', 'https://example.com', 'POST', [
                    CartItem('Gift for FRED', 1, 1000000,
                             'Gift for the best FRED')
                ], 'CZ')
        self.assertEqual(redirect_url, sentinel.url)
        self.assertEqual(payment.identifier, 'unique_id_123')
        self.assertEqual(payment.state, PaymentState.INITIALIZED)
        self.assertEqual(payment.card_payment_state,
                         CSOB.PAYMENT_STATUSES[CSOB.PAYMENT_STATUS_INIT])
        self.assertEqual(payment.amount, Money(100, 'CZK'))
        self.assertEqual(payment.processor, 'donations')
        self.assertEqual(payment.card_handler, 'csob')
Exemplo n.º 6
0
    def test_create(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()

        with patch('django_pain.card_payment_handlers.csob.CsobClient'
                   ) as gateway_client_mock:
            gateway_client_mock.return_value.gateway_return.return_value = OrderedDict(
                [('payId', 'unique_id_123'), ('resultCode', 0),
                 ('resultMessage', 'OK'),
                 ('paymentStatus', CSOB.PAYMENT_STATUS_INIT),
                 ('dttime', datetime.datetime(2020, 6, 10, 16, 47, 30))])
            gateway_client_mock.return_value.get_payment_process_url.return_value = 'https://example.com'

            response = self.client.post(
                '/api/private/bankpayment/',
                data={
                    'amount':
                    '1000',
                    'variable_symbol':
                    '130',
                    'processor':
                    'donations',
                    'card_handler':
                    'csob',
                    'return_url':
                    'https://donations.nic.cz/return/',
                    'return_method':
                    'POST',
                    'language':
                    'cs',
                    'cart':
                    '[{"name":"Dar","amount":1000,"description":"Longer description","quantity":1}]',
                })

        self.assertEqual(response.status_code, 201)
Exemplo n.º 7
0
 def setUp(self):
     self.admin = User.objects.create_superuser('admin',
                                                '*****@*****.**',
                                                'password')
     self.account = get_account(account_number='123456/0300',
                                currency='EUR')
     self.account.save()
Exemplo n.º 8
0
    def setUp(self):
        super().setUp()
        self.admin = User.objects.create_superuser('admin',
                                                   '*****@*****.**',
                                                   'password')
        self.request_factory = RequestFactory()

        self.account = get_account(account_name='My Account')
        self.account.save()
        self.imported_payment = get_payment(
            identifier='My Payment 1',
            account=self.account,
            state=PaymentState.READY_TO_PROCESS,
            variable_symbol='VAR1',
            transaction_date=date(2019, 1, 1),
        )
        self.imported_payment.save()
        self.processed_payment = get_payment(
            identifier='My Payment 2',
            account=self.account,
            state=PaymentState.PROCESSED,
            variable_symbol='VAR2',
            processor='dummy',
            processing_error=PaymentProcessingError.DUPLICITY,
        )
        self.processed_payment.save()
        self.invoice = get_invoice(number='INV111222')
        self.invoice.save()
        self.invoice.payments.add(self.processed_payment)
        self.payment_client = get_client(handle='HANDLE',
                                         payment=self.processed_payment)
        self.payment_client.save()
Exemplo n.º 9
0
 def test_account_admin_locking(self):
     instance = get_account(account_number='123456/0300', currency='EUR')
     viewname = 'admin:django_pain_bankaccount_changelist'
     find_in_response = '123456/0300'
     model_class = BankAccount
     self._test_admin_locking(instance, viewname, find_in_response,
                              model_class)
Exemplo n.º 10
0
 def setUpTestData(cls):
     account1 = get_account(account_number='123456', currency='CZK')
     account1.save()
     account2 = get_account(account_number='654321', currency='CZK')
     account2.save()
     get_payment(identifier='1', account=account1, counter_account_name='Account one',
                 state=PaymentState.READY_TO_PROCESS).save()
     get_payment(identifier='2', account=account2, counter_account_name='Account two',
                 state=PaymentState.READY_TO_PROCESS).save()
     get_payment(identifier='3', account=account1, counter_account_name='Account three',
                 state=PaymentState.PROCESSED).save()
     get_payment(identifier='4', account=account2, counter_account_name='Account four',
                 description='I am your father!', state=PaymentState.PROCESSED).save()
     get_payment(identifier='5', account=account1, counter_account_name='Account five',
                 state=PaymentState.DEFERRED).save()
     get_payment(identifier='6', account=account1, counter_account_name='Account six',
                 description='May the force be with you', state=PaymentState.DEFERRED).save()
     get_payment(identifier='7', account=account2, counter_account_name='Account seven',
                 state=PaymentState.DEFERRED).save()
Exemplo n.º 11
0
 def test_payment_admin_locking(self):
     account = get_account(account_name='My Account')
     account.save()
     instance = get_payment(
         identifier='My Payment 1',
         account=account,
         state=PaymentState.READY_TO_PROCESS,
         variable_symbol='VAR1',
         transaction_date=date(2019, 1, 1),
     )
     viewname = 'admin:django_pain_bankpayment_changelist'
     find_in_response = 'VAR1'
     model_class = BankPayment
     self._test_admin_locking(instance, viewname, find_in_response,
                              model_class)
Exemplo n.º 12
0
    def test_update_payment_state_connection_error(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()
        payment = get_payment(identifier='1',
                              account=account,
                              counter_account_number='',
                              payment_type=PaymentType.CARD_PAYMENT,
                              state=PaymentState.INITIALIZED,
                              card_handler='csob')
        payment.save()

        handler = CSOBCardPaymentHandler('csob')
        with patch.object(handler, '_client') as gateway_client_mock:
            gateway_client_mock.payment_status.side_effect = requests.ConnectionError(
            )

            self.assertRaises(PaymentHandlerConnectionError,
                              handler.update_payments_state, payment)
Exemplo n.º 13
0
    def test_init_payment_not_ok(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()

        handler = CSOBCardPaymentHandler('csob')
        with patch.object(handler, '_client') as gateway_client_mock:
            gateway_client_mock.gateway_return.return_value = OrderedDict([
                ('payId', 'unique_id_123'),
                ('resultCode', CSOB.RETURN_CODE_MERCHANT_BLOCKED),
                ('resultMessage', 'Merchant blocked'),
                ('dttime', datetime.datetime(2020, 6, 10, 16, 47, 30))
            ])
            self.assertRaisesRegex(PaymentHandlerError, 'resultCode != OK',
                                   handler.init_payment, 100, '123',
                                   'donations', 'https://example.com', 'POST',
                                   [
                                       CartItem('Gift for FRED', 1, 1000000,
                                                'Gift for the best FRED')
                                   ], 'CZ')
Exemplo n.º 14
0
    def test_retrieve_gateway_connection_error(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()
        payment = get_payment(identifier='1',
                              account=account,
                              counter_account_number='',
                              payment_type=PaymentType.CARD_PAYMENT,
                              state=PaymentState.READY_TO_PROCESS,
                              card_handler='csob')
        payment.save()

        card_payment_hadler = get_card_payment_handler_instance(
            payment.card_handler)
        with patch.object(card_payment_hadler,
                          '_client') as gateway_client_mock:
            gateway_client_mock.payment_status.side_effect = PaymentHandlerConnectionError(
            )
            response = self.client.get('/api/private/bankpayment/{}/'.format(
                payment.uuid))

        self.assertEqual(response.status_code, 503)
Exemplo n.º 15
0
    def test_update_payment_state_not_ok(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()
        payment = get_payment(identifier='1',
                              account=account,
                              counter_account_number='',
                              payment_type=PaymentType.CARD_PAYMENT,
                              state=PaymentState.INITIALIZED,
                              card_handler='csob')
        payment.save()

        result_mock = Mock()
        result_mock.payload = {'resultCode': CSOB.RETURN_CODE_MERCHANT_BLOCKED}

        handler = CSOBCardPaymentHandler('csob')
        with patch.object(handler, '_client') as gateway_client_mock:
            gateway_client_mock.payment_status.return_value = result_mock
            self.assertRaisesRegex(PaymentHandlerError,
                                   'payment_status resultCode != OK',
                                   handler.update_payments_state, payment)
        self.assertEqual(payment.state, PaymentState.INITIALIZED)
Exemplo n.º 16
0
    def test_update_payment_state_no_update_not_initialized(self):
        account = get_account(account_number='123456', currency='CZK')
        account.save()
        payment = get_payment(identifier='1',
                              account=account,
                              counter_account_number='',
                              payment_type=PaymentType.CARD_PAYMENT,
                              state=PaymentState.PROCESSED,
                              card_handler='csob')
        payment.save()

        result_mock = Mock()
        result_mock.payload = {
            'paymentStatus': CSOB.PAYMENT_STATUS_CANCELLED,
            'resultCode': CSOB.RETURN_CODE_OK
        }

        handler = CSOBCardPaymentHandler('csob')
        with patch.object(handler, '_client') as gateway_client_mock:
            gateway_client_mock.payment_status.return_value = result_mock

            handler.update_payments_state(payment)

        self.assertEqual(payment.state, PaymentState.PROCESSED)