Skip to content

Commit

Permalink
Added Klarna mop
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviana Ghita committed Sep 12, 2023
1 parent ddfcb70 commit 7aab710
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
3 changes: 2 additions & 1 deletion mangopay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@
("MBWAY", "mbway", "Mbway"),
("MULTIBANCO", "multibanco", "Multibanco"),
("SATISPAY", "satispay", "Satispay"),
("BLIK", "blik", "Blik")
("BLIK", "blik", "Blik"),
("KLARNA","klarna","Klarna")
)

CARD_STATUS_CHOICES = Choices(
Expand Down
39 changes: 35 additions & 4 deletions mangopay/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,11 @@ def cast(cls, result):
("BANK_WIRE", "EXTERNAL_INSTRUCTION"): BankWirePayInExternalInstruction,
("APPLEPAY", "DIRECT"): ApplepayPayIn,
("GOOGLEPAY", "DIRECT"): GooglepayPayIn,
("MBWAY", "DIRECT"): MbwayPayIn,
("MULTIBANCO", "DIRECT"): MultibancoPayIn,
("SATISPAY", "DIRECT"): SatispayPayIn,
("BLIK", "DIRECT"): BlikPayIn,
("MBWAY", "WEB"): MbwayPayIn,
("MULTIBANCO", "WEB"): MultibancoPayIn,
("SATISPAY", "WEB"): SatispayPayIn,
("BLIK", "WEB"): BlikPayIn,
("KLARNA", "WEB"): KlarnaPayIn
}

return types.get((payment_type, execution_type), cls)
Expand Down Expand Up @@ -815,6 +816,36 @@ class Meta:
}


class KlarnaPayIn(PayIn):
author = ForeignKeyField(User, api_name='AuthorId', required=True)
debited_funds = MoneyField(api_name='DebitedFunds', required=True)
fees = MoneyField(api_name='Fees', required=True)
return_url = CharField(api_name='ReturnURL', required=True)
line_items = ListField(api_name='LineItems', required=True)
country = CharField(api_name='Country', required=True)
phone = CharField(api_name='Phone', required=True)
email = EmailField(api_name='Email', required=True)
additional_data = CharField(api_name='AdditionalData', required=True)
billing = BillingField(api_name='Billing', required=True)
merchant_order_id = CharField(api_name='MerchantOrderId', required=True)
culture = CharField(api_name='Culture', required=True)
shipping = ShippingField(api_name='Shipping')
creation_date = DateTimeField(api_name='CreationDate')
credited_funds = MoneyField(api_name='CreditedFunds')
redirect_url = CharField(api_name='RedirectURL')
payment_method = CharField(api_name='PaymentMethod')


class Meta:
verbose_name = 'klarna_payin'
verbose_name_plural = 'klarna_payins'
url = {
InsertQuery.identifier: '/payins/payment-methods/klarna',
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
}
69 changes: 67 additions & 2 deletions tests/test_payins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from mangopay.resources import DirectDebitDirectPayIn, Mandate, ApplepayPayIn, GooglepayPayIn, \
RecurringPayInRegistration, \
RecurringPayInCIT, PayInRefund, RecurringPayInMIT, CardPreAuthorizedDepositPayIn, MbwayPayIn, MultibancoPayIn, SatispayPayIn, \
BlikPayIn
BlikPayIn, KlarnaPayIn
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 @@ -1268,4 +1268,69 @@ def test_PayIns_BlikWeb_Create(self):
self.assertEqual("REGULAR", result.nature)
self.assertEqual("WEB", result.execution_type)
self.assertEqual("BLIK", result.payment_type)
self.assertEqual("PAYIN", result.type)


def test_PayIns_KlarnaWeb_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 = KlarnaPayIn()
pay_in.author = user
pay_in.debited_funds = Money()
pay_in.debited_funds.amount = 1000
pay_in.debited_funds.currency = "EUR"
pay_in.fees = Money()
pay_in.fees.amount = 100
pay_in.fees.currency = "EUR"

pay_in.return_url = "http://www.my-site.com/returnURL"

line_item = LineItem()
line_item.name = "test"
line_item.quantity = 1
line_item.unit_amount = 1000
line_item.tax_amount = 0
pay_in.line_items = [line_item]

pay_in.country = 'FR'
pay_in.culture = 'FR'
pay_in.phone = "33#607080900"
pay_in.email= "mango@mangopay.com"
pay_in.additional_data = "{}"

address = Address()
address.address_line_1 = "Big Street"
address.address_line_2 = "no 2 ap 6"
address.country = "FR"
address.city = "Lyon"
address.postal_code = "68400"
address.region = "Ile de France"
pay_in.billing = Billing(first_name="John", last_name="Doe", address=address)
pay_in.shipping = Shipping(first_name="John", last_name="Doe", address=address)

pay_in.merchant_order_id = "afd48-879d-48fg"

pay_in.statement_descriptor = "test"
pay_in.tag = "test tag"

pay_in.credited_wallet = credited_wallet

result = KlarnaPayIn(**pay_in.save())
fetched = KlarnaPayIn().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("KLARNA", result.payment_type)
self.assertEqual("PAYIN", result.type)

0 comments on commit 7aab710

Please sign in to comment.