From 513481457b16be9e0be5ee570446f3f3c3e09aee Mon Sep 17 00:00:00 2001 From: Iulian Masar Date: Tue, 5 Sep 2023 09:57:02 +0300 Subject: [PATCH 1/4] implemented paypal web payin --- mangopay/constants.py | 3 ++- mangopay/resources.py | 25 ++++++++++++++++++++++ mangopay/utils.py | 22 +++++++++++++++++++ tests/test_payins.py | 49 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/mangopay/constants.py b/mangopay/constants.py index 43010d5..f19194b 100644 --- a/mangopay/constants.py +++ b/mangopay/constants.py @@ -287,7 +287,8 @@ ("BANK_WIRE", "bank_wire", "Bank Wire"), ("APPLEPAY", "applepay", "Applepay"), ("GOOGLEPAY", "googlepay", "Googlepay"), - ("MBWAY", "mbway", "Mbway") + ("MBWAY", "mbway", "Mbway"), + ("PAYPAL", "paypal", "PayPal") ) CARD_STATUS_CHOICES = Choices( diff --git a/mangopay/resources.py b/mangopay/resources.py index 0478d5d..f651ccd 100644 --- a/mangopay/resources.py +++ b/mangopay/resources.py @@ -692,6 +692,29 @@ class Meta: } +@python_2_unicode_compatible +class PayPalWebPayIn(PayIn): + creation_date = DateTimeField(api_name='CreationDate') + author = ForeignKeyField(User, api_name='AuthorId', required=True) + debited_funds = MoneyField(api_name='DebitedFunds', required=True) + fees = MoneyField(api_name='Fees', required=True) + credited_wallet = ForeignKeyField(Wallet, api_name='CreditedWalletId', required=True) + return_url = CharField(api_name='ReturnURL', required=True) + redirect_url = CharField(api_name='RedirectURL') + statement_descriptor = CharField(api_name='StatementDescriptor') + shipping = ShippingField(api_name='Shipping') + line_items = ListField(api_name='LineItems', required=True) + culture = CharField(api_name='Culture') + + class Meta: + verbose_name = 'payin' + verbose_name_plural = 'payins' + url = { + InsertQuery.identifier: '/payins/payment-methods/paypal', + SelectQuery.identifier: '/payins' + } + + @python_2_unicode_compatible class PayconiqPayIn(PayIn): author = ForeignKeyField(User, api_name='AuthorId', required=True) @@ -743,6 +766,7 @@ class Meta: InsertQuery.identifier: '/payins/googlepay/direct' } + class MbwayPayIn(PayIn): creation_date = DateTimeField(api_name='CreationDate') author = ForeignKeyField(User, api_name='AuthorId', required=True) @@ -759,6 +783,7 @@ class Meta: SelectQuery.identifier: '/payins' } + class CardWebPayIn(PayIn): author = ForeignKeyField(User, api_name='AuthorId', required=True) credited_wallet = ForeignKeyField(Wallet, api_name='CreditedWalletId', required=True) diff --git a/mangopay/utils.py b/mangopay/utils.py index 40d4415..62a8ee4 100644 --- a/mangopay/utils.py +++ b/mangopay/utils.py @@ -923,3 +923,25 @@ def to_api_json(self): "PayinCaptureId": self.payin_capture_id, "PayinComplementId": self.payin_complement_id } + + +class LineItem(object): + def __init__(self, name=None, quantity=None, unit_amount=None, tax_amount=None, description=None): + self.name = name + self.quantity = quantity + self.unit_amount = unit_amount + self.tax_amount = tax_amount + self.description = description + + def __str__(self): + return 'LineItem: %s %s %s %s %s' % \ + (self.name, self.quantity, self.unit_amount, self.tax_amount, self.description) + + def to_api_json(self): + return { + "Name": self.name, + "Quantity": self.quantity, + "UnitAmount": self.unit_amount, + "TaxAmount": self.tax_amount, + "Description": self.description + } diff --git a/tests/test_payins.py b/tests/test_payins.py index d1deeb7..37c6ec2 100644 --- a/tests/test_payins.py +++ b/tests/test_payins.py @@ -9,9 +9,9 @@ from mangopay.resources import DirectDebitDirectPayIn, Mandate, ApplepayPayIn, GooglepayPayIn, \ RecurringPayInRegistration, \ - RecurringPayInCIT, PayInRefund, RecurringPayInMIT, CardPreAuthorizedDepositPayIn, MbwayPayIn + RecurringPayInCIT, PayInRefund, RecurringPayInMIT, CardPreAuthorizedDepositPayIn, MbwayPayIn, PayPalWebPayIn from mangopay.utils import (Money, ShippingAddress, Shipping, Billing, Address, SecurityInfo, ApplepayPaymentData, - GooglepayPaymentData, DebitedBankAccount, BrowserInfo) + GooglepayPaymentData, DebitedBankAccount, BrowserInfo, LineItem) from tests import settings from tests.resources import (Wallet, PayIn, DirectPayIn, BankWirePayIn, BankWirePayInExternalInstruction, PayPalPayIn, @@ -1157,3 +1157,48 @@ def test_PayIns_MbwayDirect_Create(self): self.assertEqual("DIRECT", result.execution_type) self.assertEqual("MBWAY", result.payment_type) self.assertEqual("PAYIN", result.type) + + def test_PayIns_PayPalWeb_Create(self): + user = BaseTestLive.get_john(True) + + # create wallet + credited_wallet = Wallet() + credited_wallet.owners = (user,) + credited_wallet.currency = 'EUR' + credited_wallet.description = 'WALLET IN EUR' + credited_wallet = Wallet(**credited_wallet.save()) + + pay_in = PayPalWebPayIn() + pay_in.author = user + pay_in.credited_wallet = credited_wallet + pay_in.fees = Money() + pay_in.fees.amount = 100 + pay_in.fees.currency = "EUR" + pay_in.debited_funds = Money() + pay_in.debited_funds.amount = 1000 + pay_in.debited_funds.currency = "EUR" + pay_in.return_url = "http://mangopay.com" + + line_item = LineItem() + line_item.name = "test" + line_item.quantity = 1 + line_item.unit_amount = 1000 + line_item.tax_amount = 0 + line_item.description = "test" + pay_in.line_items = [line_item] + + pay_in.statement_descriptor = "test" + pay_in.tag = "test tag" + + result = PayPalWebPayIn(**pay_in.save()) + fetched = PayPalWebPayIn().get(result.id) + + self.assertIsNotNone(result) + self.assertIsNotNone(fetched) + self.assertEqual(result.id, fetched.id) + + self.assertEqual("CREATED", result.status) + self.assertEqual("REGULAR", result.nature) + self.assertEqual("WEB", result.execution_type) + self.assertEqual("PAYPAL", result.payment_type) + self.assertEqual("PAYIN", result.type) From ddd5e9fd2bcf67eb26b48a911d9f80e49cf19b69 Mon Sep 17 00:00:00 2001 From: Iulian Masar Date: Tue, 5 Sep 2023 09:59:21 +0300 Subject: [PATCH 2/4] updated mbway tests --- tests/test_payins.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_payins.py b/tests/test_payins.py index 37c6ec2..057d1a0 100644 --- a/tests/test_payins.py +++ b/tests/test_payins.py @@ -696,7 +696,7 @@ def test_create_direct_debit_via_web_interface_payin(self): self.assertEqual(card_payin.payment_type, 'DIRECT_DEBIT') @responses.activate - def test_create_mbway_direct_payins(self): + def test_create_mbway_web_payins(self): self.mock_natural_user() self.mock_user_wallet() @@ -728,14 +728,14 @@ def test_create_mbway_direct_payins(self): "Nature": "REGULAR", "CreditedWalletId": "102150481", "PaymentType": "MBWAY", - "ExecutionType": "DIRECT", + "ExecutionType": "WEB", "StatementDescriptor": "My descriptor", "PhoneNumber": "351#269458236" }, 'status': 200 }) - mbway_direct_params = { + mbway_web_params = { "author": self.natural_user, "debited_funds": Money(amount=500, currency='EUR'), "fees": Money(amount=0, currency='EUR'), @@ -744,16 +744,16 @@ def test_create_mbway_direct_payins(self): "phone": "351#269458236", "tag": "MB WAY Tag" } - mbway_payin = MbwayPayIn(**mbway_direct_params) + mbway_payin = MbwayPayIn(**mbway_web_params) self.assertIsNone(mbway_payin.get_pk()) mbway_payin.save() self.assertIsInstance(mbway_payin, MbwayPayIn) self.assertEqual(mbway_payin.status, 'CREATED') self.assertEqual(mbway_payin.payment_type, 'MBWAY') - self.assertEqual(mbway_payin.execution_type, 'DIRECT') + self.assertEqual(mbway_payin.execution_type, 'WEB') - for key, value in mbway_direct_params.items(): + for key, value in mbway_web_params.items(): self.assertEqual(getattr(mbway_payin, key), value) self.assertIsNotNone(mbway_payin.get_pk()) @@ -1123,7 +1123,7 @@ def test_card_preauthorized_deposit_payin(self): self.assertEqual("PREAUTHORIZED", pay_in.payment_type) self.assertEqual("PAYIN", pay_in.type) - def test_PayIns_MbwayDirect_Create(self): + def test_PayIns_MbwayWeb_Create(self): user = BaseTestLive.get_john(True) # create wallet @@ -1154,7 +1154,7 @@ def test_PayIns_MbwayDirect_Create(self): self.assertEqual("CREATED", result.status) self.assertEqual("REGULAR", result.nature) - self.assertEqual("DIRECT", result.execution_type) + self.assertEqual("WEB", result.execution_type) self.assertEqual("MBWAY", result.payment_type) self.assertEqual("PAYIN", result.type) From 98661f54d2f6fc0d6d811d50cbb5ccf355b6cd2b Mon Sep 17 00:00:00 2001 From: Iulian Masar Date: Tue, 5 Sep 2023 10:46:31 +0300 Subject: [PATCH 3/4] updated payment type --- mangopay/resources.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mangopay/resources.py b/mangopay/resources.py index f651ccd..5bec556 100644 --- a/mangopay/resources.py +++ b/mangopay/resources.py @@ -475,9 +475,9 @@ def cast(cls, result): ("PREAUTHORIZED", "DIRECT"): PreAuthorizedPayIn, ("BANK_WIRE", "DIRECT"): BankWirePayIn, ("BANK_WIRE", "EXTERNAL_INSTRUCTION"): BankWirePayInExternalInstruction, - ("APPLEPAY", "DIRECT"): ApplepayPayIn, + ("APPLEPAY", "WEB"): ApplepayPayIn, ("GOOGLEPAY", "DIRECT"): GooglepayPayIn, - ("MBWAY", "DIRECT"): MbwayPayIn + ("MBWAY", "WEB"): MbwayPayIn } return types.get((payment_type, execution_type), cls) From 92a80d623a5c5b5fd44cd4b167d6f4296ce23640 Mon Sep 17 00:00:00 2001 From: Silviana Ghita Date: Tue, 12 Sep 2023 10:57:23 +0300 Subject: [PATCH 4/4] Added ShippingPreference property to PayPalWeb --- mangopay/constants.py | 6 ++++++ mangopay/resources.py | 2 ++ tests/test_payins.py | 1 + 3 files changed, 9 insertions(+) diff --git a/mangopay/constants.py b/mangopay/constants.py index f19194b..4f1308e 100644 --- a/mangopay/constants.py +++ b/mangopay/constants.py @@ -339,4 +339,10 @@ ('CREATED', 'created', 'Created'), ('SUCCEEDED', 'succeeded', 'Succeeded'), ('FAILED', 'failed', 'Failed') +) + +SHIPPING_PREFERENCE_CHOICES = Choices( + ('SET_PROVIDED_ADDRESS', 'set_provided_address', 'Set Provided Address'), + ('GET_FROM_FILE', 'get_from_file', 'Get From File'), + ('NO_SHIPPING', 'no_shipping', 'No Shipping') ) \ No newline at end of file diff --git a/mangopay/resources.py b/mangopay/resources.py index 5bec556..08f1e97 100644 --- a/mangopay/resources.py +++ b/mangopay/resources.py @@ -705,6 +705,8 @@ class PayPalWebPayIn(PayIn): shipping = ShippingField(api_name='Shipping') line_items = ListField(api_name='LineItems', required=True) culture = CharField(api_name='Culture') + shipping_preference = CharField(api_name='ShippingPreference', choices=constants.SHIPPING_PREFERENCE_CHOICES, + default=None) class Meta: verbose_name = 'payin' diff --git a/tests/test_payins.py b/tests/test_payins.py index 057d1a0..e21f34f 100644 --- a/tests/test_payins.py +++ b/tests/test_payins.py @@ -1178,6 +1178,7 @@ def test_PayIns_PayPalWeb_Create(self): pay_in.debited_funds.amount = 1000 pay_in.debited_funds.currency = "EUR" pay_in.return_url = "http://mangopay.com" + pay_in.shipping_preference = "NO_SHIPPING" line_item = LineItem() line_item.name = "test"