def test_request_validate_payment_method_id(self):
        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.payment_method_id = '2425cbf1-001f-5000-a001-181a3bf24511'

        # Should pass other validations
        request.validate()
    def test_request_setters(self):
        request = PaymentRequest({
            'amount': {'value': 0.1, 'currency': Currency.RUB},
            'recipient': {
                'account_id': '213',
                'gateway_id': '123'
            },
            'save_payment_method': True,
            'capture': False,
            'payment_method_data': {'type': PaymentMethodType.WEBMONEY},
            'receipt': {'phone': '79990000000', 'email': 'test@email', 'tax_system_code': 1, 'items': [
                {
                    "description": "Product 1",
                    "quantity": 2.0,
                    "amount": {
                        "value": 250.0,
                        "currency": Currency.RUB
                    },
                    "vat_code": 2
                },
                {
                    "description": "Product 2",
                    "quantity": 1.0,
                    "amount": {
                        "value": 100.0,
                        "currency": Currency.RUB
                    },
                    "vat_code": 2
                }
            ]},
            'payment_method_id': '123',
            'payment_token': '99091209012',
            'confirmation': {'type': ConfirmationType.REDIRECT, 'return_url': 'return.url'},
            'client_ip': '192.0.0.0',
            'metadata': {'key': 'value'}
        })

        self.assertIsInstance(request.confirmation, Confirmation)
        self.assertIsInstance(request.amount, Amount)
        self.assertIsInstance(request.receipt, Receipt)
        self.assertIsInstance(request.recipient, Recipient)
        self.assertIsInstance(request.payment_method_data, PaymentData)

        with self.assertRaises(TypeError):
            request.receipt = 'invalid receipt'

        with self.assertRaises(TypeError):
            request.amount = 'invalid amount'

        with self.assertRaises(TypeError):
            request.recipient = 'invalid recipient'

        with self.assertRaises(TypeError):
            request.confirmation = 'invalid confirmation'

        with self.assertRaises(TypeError):
            request.payment_method_data = 'invalid payment_method_data'

        with self.assertRaises(ValueError):
            request.payment_token = ''
    def test_request_validate_expiry(self, modtime):
        modtime.now.return_value = datetime(year=2019, month=3, day=10)

        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.payment_method_data = PaymentDataBankCard(
            card={
                "number": "4111111111111111",
                "expiry_year": "2019",
                "expiry_month": "11",
                "csc": "111"
            })

        # Should pass other validations
        request.validate()

        # Obviously expired
        request.payment_method_data.card.expiry_year = '2018'
        with self.assertRaises(ValueError):
            request.validate()

        # Same month
        request.payment_method_data.card.expiry_year = '2019'
        request.payment_method_data.card.expiry_month = '03'
        request.validate()

        # Just a notch before expiration (same timezone)
        modtime.now.return_value = datetime(year=2019,
                                            month=3,
                                            day=31,
                                            hour=23,
                                            minute=59,
                                            second=59)
        request.payment_method_data.card.expiry_year = '2019'
        request.payment_method_data.card.expiry_month = '03'
        request.validate()

        # Just a notch before expiration (worst timezone case)
        client_tz = timezone(timedelta(hours=-12))
        bank_tz = timezone(timedelta(hours=+14))
        tz_offset = datetime.now(client_tz) - datetime.now(bank_tz)
        tz_offset += timedelta(hours=1)  # DST
        modtime.now.return_value += tz_offset
        request.validate()

        # Couple days after expiration
        modtime.now.return_value = datetime(year=2019, month=4, day=3)
        with self.assertRaises(ValueError):
            request.validate()
    def create(cls, params, client, idempotency_key=None):
        """
        Create payment

        :param params: data passed to API
        :param idempotency_key:
        :return: PaymentResponse
        """
        instance = cls()
        path = cls.base_path

        if not idempotency_key:
            idempotency_key = uuid.uuid4()

        headers = {'Idempotence-Key': str(idempotency_key)}

        if isinstance(params, dict):
            params_object = PaymentRequest(params)
        elif isinstance(params, PaymentRequest):
            params_object = params
        else:
            raise TypeError('Invalid params value type')

        response = client.request(HttpVerb.POST, path, None, headers,
                                  params_object)
        return PaymentResponse(response)
    def test_create_payment_with_object(self):
        self.maxDiff = None
        payment_facade = Payment()
        with patch('yandex_checkout.client.ApiClient.request') as request_mock:
            request_mock.return_value = {
                'amount': {'currency': 'RUB', 'value': 1.0},
                'created_at': '2017-11-30T15:45:31.130Z',
                'id': '21b23b5b-000f-5061-a000-0674e49a8c10',
                'metadata': {'float_value': '123.32', 'key': 'data'},
                'paid': False,
                'payment_method': {'type': 'bank_card'},
                'recipient': {'account_id': '156833', 'gateway_id': '468284'},
                'status': 'waiting_for_capture'
            }
            payment = payment_facade.create(PaymentRequest({
                "amount": {
                    "value": "1.00",
                    "currency": "RUB"
                },
                "receipt": {
                    "items": [
                        {
                            "description": "Item description",
                            "amount": {
                                "value": "1.00",
                                "currency": "RUB"
                            },
                            "quantity": 1,
                            "vat_code": 3
                        }
                    ],
                    "tax_system_id": 2,
                    "email": "test@test"
                },
                "payment_method_data": {
                    "type": "bank_card"
                },
                "confirmation": {
                    "type": "redirect",
                    "return_url": "https://test.test/test"
                },
                "capture": False,
                "save_payment_method": False,
                "metadata": {
                    "key": "data",
                    "float_value": 123.32
                }
            }))

        self.assertIsInstance(payment, PaymentResponse)
        self.assertIsInstance(payment.amount, Amount)
        self.assertIsInstance(payment.payment_method, PaymentDataBankCard)
Ejemplo n.º 6
0
    def create(cls, params, idempotency_key=None, user=None, order=None):

        instance = cls()
        path = cls.base_path

        if not idempotency_key:
            idempotency_key = uuid.uuid4()

        instance.uuid = idempotency_key

        headers = {'Idempotence-Key': str(idempotency_key)}

        if isinstance(params, dict):
            params_object = PaymentRequest(params)
        elif isinstance(params, PaymentRequest):
            params_object = params
        else:
            raise TypeError('Invalid params value type')

        response = instance.client.request(HttpVerb.POST, path, None, headers,
                                           params_object)

        instance.y_id = response['id']
        instance.status = response['status']
        instance.paid = response['paid']
        instance.amount = response['amount']['value']
        instance.confirmation_url = response['confirmation'][
            'confirmation_url']
        instance.created_at = response['created_at']
        instance.description = response['description']
        instance.metadata = response['metadata']
        instance.refundable = response['refundable']

        instance.user = user
        instance.order = order

        instance.full_clean()
        instance.save()

        return instance
 def __init__(self):
     self.__request = PaymentRequest()
    def test_request_cast(self):
        self.maxDiff = None
        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.description = 'Test description'
        request.recipient = Recipient({
            'account_id': '213',
            'gateway_id': '123'
        })
        request.save_payment_method = True
        request.capture = False
        request.payment_method_data = PaymentDataWebmoney()
        request.receipt = Receipt({
            'phone':
            '79990000000',
            'email':
            'test@email',
            'tax_system_code':
            1,
            'items': [{
                "description": "Product 1",
                "quantity": 2.0,
                "amount": {
                    "value": 250.0,
                    "currency": Currency.RUB
                },
                "vat_code": 2
            }, {
                "description": "Product 2",
                "quantity": 1.0,
                "amount": {
                    "value": 100.0,
                    "currency": Currency.RUB
                },
                "vat_code": 2
            }]
        })
        request.payment_method_id = '123'
        request.payment_token = '99091209012'
        request.confirmation = ConfirmationRedirect(
            {'return_url': 'return.url'})
        request.client_ip = '192.0.0.0'
        request.metadata = {'key': 'value'}

        self.assertEqual(
            {
                'amount': {
                    'value': 0.1,
                    'currency': Currency.RUB
                },
                'recipient': {
                    'account_id': '213',
                    'gateway_id': '123'
                },
                'description': 'Test description',
                'save_payment_method': True,
                'capture': False,
                'payment_method_data': {
                    'type': PaymentMethodType.WEBMONEY
                },
                'receipt': {
                    'phone':
                    '79990000000',
                    'email':
                    'test@email',
                    'tax_system_code':
                    1,
                    'items': [{
                        "description": "Product 1",
                        "quantity": 2.0,
                        "amount": {
                            "value": 250.0,
                            "currency": Currency.RUB
                        },
                        "vat_code": 2
                    }, {
                        "description": "Product 2",
                        "quantity": 1.0,
                        "amount": {
                            "value": 100.0,
                            "currency": Currency.RUB
                        },
                        "vat_code": 2
                    }]
                },
                'payment_method_id': '123',
                'payment_token': '99091209012',
                'confirmation': {
                    'type': ConfirmationType.REDIRECT,
                    'return_url': 'return.url'
                },
                'client_ip': '192.0.0.0',
                'metadata': {
                    'key': 'value'
                }
            }, dict(request))
    def test_request_validate(self):
        request = PaymentRequest()

        with self.assertRaises(ValueError):
            request.validate()

        request.amount = Amount({'value': 0.0, 'currency': Currency.RUB})

        with self.assertRaises(ValueError):
            request.validate()

        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.receipt = {
            'phone':
            '79990000000',
            'items': [{
                "description": "Product 1",
                "quantity": 2.0,
                "amount": {
                    "value": 250.0,
                    "currency": Currency.RUB
                },
            }, {
                "description": "Product 2",
                "quantity": 1.0,
                "amount": {
                    "value": 100.0,
                    "currency": Currency.RUB
                },
            }]
        }
        with self.assertRaises(ValueError):
            request.validate()

        request.receipt = {
            'tax_system_code':
            1,
            'items': [{
                "description": "Product 1",
                "quantity": 2.0,
                "amount": {
                    "value": 250.0,
                    "currency": Currency.RUB
                },
                "vat_code": 2
            }, {
                "description": "Product 2",
                "quantity": 1.0,
                "amount": {
                    "value": 100.0,
                    "currency": Currency.RUB
                },
                "vat_code": 2
            }]
        }
        with self.assertRaises(ValueError):
            request.validate()

        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.payment_token = '123'
        request.payment_method_id = '123'
        with self.assertRaises(ValueError):
            request.validate()

        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.payment_token = '123'
        request.payment_method_data = PaymentDataWebmoney()
        with self.assertRaises(ValueError):
            request.validate()

        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.payment_method_id = '123'
        request.payment_method_data = PaymentDataWebmoney()
        with self.assertRaises(ValueError):
            request.validate()
Ejemplo n.º 10
0
    def test_request_cast(self):
        self.maxDiff = None
        request = PaymentRequest()
        request.amount = Amount({'value': 0.1, 'currency': Currency.RUB})
        request.description = 'Test description'
        request.recipient = Recipient({
            'account_id': '213',
            'gateway_id': '123'
        })
        request.save_payment_method = True
        request.capture = False
        request.payment_method_data = PaymentDataWebmoney()
        request.receipt = Receipt({
            'phone':
            '79990000000',
            'email':
            '*****@*****.**',
            'tax_system_code':
            1,
            'items': [{
                "description": "Product 1",
                "quantity": 2.0,
                "amount": {
                    "value": 250.0,
                    "currency": Currency.RUB
                },
                "vat_code": 2
            }, {
                "description": "Product 2",
                "quantity": 1.0,
                "amount": {
                    "value": 100.0,
                    "currency": Currency.RUB
                },
                "vat_code": 2
            }]
        })
        request.airline = Airline({
            "booking_reference":
            "IIIKRV",
            "passengers": [{
                "first_name": "SERGEI",
                "last_name": "IVANOV"
            }],
            "legs": [{
                "departure_airport": "LED",
                "destination_airport": "AMS",
                "departure_date": "2018-06-20"
            }]
        })
        request.payment_method_id = '123'
        request.payment_token = '99091209012'
        request.confirmation = ConfirmationRedirect({
            'locale': 'ru_RU',
            'return_url': 'return.url'
        })
        request.client_ip = '192.0.0.0'
        request.metadata = {'key': 'value'}
        request.transfers.append(
            Transfer({
                'account_id': '79990000000',
                "amount": {
                    "value": 100.01,
                    "currency": Currency.RUB
                }
            }))

        self.assertEqual(
            {
                'amount': {
                    'value': 0.1,
                    'currency': Currency.RUB
                },
                'recipient': {
                    'account_id': '213',
                    'gateway_id': '123'
                },
                'description':
                'Test description',
                'save_payment_method':
                True,
                'capture':
                False,
                'payment_method_data': {
                    'type': PaymentMethodType.WEBMONEY
                },
                'receipt': {
                    'customer': {
                        'email': '*****@*****.**',
                        'phone': '79990000000'
                    },
                    'phone':
                    '79990000000',
                    'email':
                    '*****@*****.**',
                    'tax_system_code':
                    1,
                    'items': [{
                        "description": "Product 1",
                        "quantity": 2.0,
                        "amount": {
                            "value": 250.0,
                            "currency": Currency.RUB
                        },
                        "vat_code": 2
                    }, {
                        "description": "Product 2",
                        "quantity": 1.0,
                        "amount": {
                            "value": 100.0,
                            "currency": Currency.RUB
                        },
                        "vat_code": 2
                    }]
                },
                'payment_method_id':
                '123',
                'payment_token':
                '99091209012',
                'confirmation': {
                    'type': ConfirmationType.REDIRECT,
                    'locale': 'ru_RU',
                    'return_url': 'return.url'
                },
                'client_ip':
                '192.0.0.0',
                'metadata': {
                    'key': 'value'
                },
                "airline": {
                    "booking_reference":
                    "IIIKRV",
                    "passengers": [{
                        "first_name": "SERGEI",
                        "last_name": "IVANOV"
                    }],
                    "legs": [{
                        "departure_airport": "LED",
                        "destination_airport": "AMS",
                        "departure_date": "2018-06-20"
                    }]
                },
                'transfers': [{
                    'account_id': '79990000000',
                    "amount": {
                        "value": 100.01,
                        "currency": Currency.RUB
                    }
                }]
            }, dict(request))
Ejemplo n.º 11
0
    def test_request_setters(self):
        request = PaymentRequest({
            'amount': {
                'value': 0.1,
                'currency': Currency.RUB
            },
            'recipient': {
                'account_id': '213',
                'gateway_id': '123'
            },
            'save_payment_method': True,
            'capture': False,
            'payment_method_data': {
                'type': PaymentMethodType.WEBMONEY
            },
            'receipt': {
                'phone':
                '79990000000',
                'email':
                '*****@*****.**',
                'tax_system_code':
                1,
                'items': [{
                    "description": "Product 1",
                    "quantity": 2.0,
                    "amount": {
                        "value": 250.0,
                        "currency": Currency.RUB
                    },
                    "vat_code": 2
                }, {
                    "description": "Product 2",
                    "quantity": 1.0,
                    "amount": {
                        "value": 100.0,
                        "currency": Currency.RUB
                    },
                    "vat_code": 2
                }]
            },
            'payment_method_id': '123',
            'payment_token': '99091209012',
            'confirmation': {
                'type': ConfirmationType.REDIRECT,
                'return_url': 'return.url'
            },
            'client_ip': '192.0.0.0',
            "airline": {
                "booking_reference":
                "IIIKRV",
                "passengers": [{
                    "first_name": "SERGEI",
                    "last_name": "IVANOV"
                }],
                "legs": [{
                    "departure_airport": "LED",
                    "destination_airport": "AMS",
                    "departure_date": "2018-06-20"
                }]
            },
            'metadata': {
                'key': 'value'
            }
        })

        self.assertIsInstance(request.confirmation, Confirmation)
        self.assertIsInstance(request.amount, Amount)
        self.assertIsInstance(request.receipt, Receipt)
        self.assertIsInstance(request.recipient, Recipient)
        self.assertIsInstance(request.payment_method_data, PaymentData)
        self.assertIsInstance(request.airline, Airline)

        with self.assertRaises(TypeError):
            request.receipt = 'invalid receipt'

        with self.assertRaises(TypeError):
            request.amount = 'invalid amount'

        with self.assertRaises(TypeError):
            request.recipient = 'invalid recipient'

        with self.assertRaises(TypeError):
            request.confirmation = 'invalid confirmation'

        with self.assertRaises(TypeError):
            request.payment_method_data = 'invalid payment_method_data'

        with self.assertRaises(TypeError):
            request.payment_token = ''

        with self.assertRaises(ValueError):
            request.description = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ' \
                                  'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

        with self.assertRaises(TypeError):
            request.airline = 'Invalid airline'

        with self.assertRaises(TypeError):
            request.transfers = 'Invalid airline'