-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathgateway.py
More file actions
58 lines (46 loc) · 1.95 KB
/
gateway.py
File metadata and controls
58 lines (46 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from django.utils.translation import ugettext_lazy as _
from oscar_accounts import codes, core, exceptions, facade
from oscar_accounts.compact_oscar import get_model, UnableToTakePayment
Account = get_model('oscar_accounts', 'Account')
Transfer = get_model('oscar_accounts', 'Transfer')
def user_accounts(user):
"""
Return accounts available to the passed user
"""
return Account.active.filter(primary_user=user)
def redeem(order_number, user, allocations):
"""
Settle payment for the passed set of account allocations
Will raise UnableToTakePayment if any of the transfers is invalid
"""
# First, we need to check if the allocations are still valid. The accounts
# may have changed status since the allocations were written to the
# session.
transfers = []
destination = core.redemptions_account()
for code, amount in allocations.items():
try:
account = Account.active.get(code=code)
except Account.DoesNotExist:
raise UnableToTakePayment(
_("No active account found with code %s") % code)
# We verify each transaction
try:
Transfer.objects.verify_transfer(
account, destination, amount, user)
except exceptions.AccountException as e:
raise UnableToTakePayment(str(e))
transfers.append((account, destination, amount))
# All transfers verified, now redeem
for account, destination, amount in transfers:
facade.transfer(account, destination, amount,
user=user, merchant_reference=order_number,
description="Redeemed to pay for order %s" % order_number)
def create_giftcard(order_number, user, amount):
source = core.paid_source_account()
code = codes.generate()
destination = Account.objects.create(
code=code
)
facade.transfer(source, destination, amount, user,
"Create new account")