Skip to content

Commit

Permalink
Merge pull request #336 from Mangopay/feature/paypal-web-payin
Browse files Browse the repository at this point in the history
Feature/paypal web payin
  • Loading branch information
iulian03 authored Sep 18, 2023
2 parents d700b5e + 92a80d6 commit b9ab087
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
9 changes: 8 additions & 1 deletion mangopay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -338,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')
)
31 changes: 29 additions & 2 deletions mangopay/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -692,6 +692,31 @@ 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')
shipping_preference = CharField(api_name='ShippingPreference', choices=constants.SHIPPING_PREFERENCE_CHOICES,
default=None)

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)
Expand Down Expand Up @@ -743,6 +768,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)
Expand All @@ -759,6 +785,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)
Expand Down
22 changes: 22 additions & 0 deletions mangopay/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
66 changes: 56 additions & 10 deletions tests/test_payins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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'),
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1154,6 +1154,52 @@ 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)

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"
pay_in.shipping_preference = "NO_SHIPPING"

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)

0 comments on commit b9ab087

Please sign in to comment.