This repository has been archived by the owner on Jan 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
api.py
205 lines (168 loc) · 7.08 KB
/
api.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import logging
from django.conf import settings
from ..utils import SlumberWrapper
from .errors import ERROR_STRINGS
log = logging.getLogger('w.solitude')
client = None
class SellerNotConfigured(Exception):
"""The seller has not yet been configued for the payment."""
class SolitudeAPI(SlumberWrapper):
"""A solitude API client.
:param url: URL of the solitude endpoint.
"""
errors = ERROR_STRINGS
def _buyer_from_response(self, res):
buyer = {}
if res.get('errors'):
return res
elif res.get('objects'):
buyer['id'] = res['objects'][0]['resource_pk']
buyer['pin'] = res['objects'][0]['pin']
buyer['uuid'] = res['objects'][0]['uuid']
elif res.get('resource_pk'):
buyer['id'] = res['resource_pk']
buyer['pin'] = res['pin']
buyer['uuid'] = res['uuid']
return buyer
def buyer_has_pin(self, uuid):
"""Returns True if the existing buyer has a PIN.
:param uuid: String to identify the buyer by.
:rtype: boolean
"""
res = self.safe_run(self.slumber.generic.buyer.get, **{'uuid': uuid})
if res['meta']['total_count'] == 0:
return False
else:
return res['objects'][0]['pin']
def create_buyer(self, uuid, pin=None):
"""Creates a buyer with an optional PIN in solitude.
:param uuid: String to identify the buyer by.
:param pin: Optional PIN that will be hashed.
:rtype: dictionary
"""
res = self.safe_run(self.slumber.generic.buyer.post, {'uuid': uuid,
'pin': pin})
return self._buyer_from_response(res)
def change_pin(self, buyer_id, pin):
"""Changes a buyer's PIN in solitude.
:param buyer_id integer: ID of the buyer you'd like to change the PIN
for.
:param pin: PIN to replace the buyer's pin with.
:rtype: dictionary
"""
res = self.safe_run(self.slumber.generic.buyer(id=buyer_id).patch,
{'pin': pin})
# Empty string is a good thing from tastypie for a PATCH.
if 'errors' in res:
return res
return {}
def get_buyer(self, uuid):
"""Retrieves a buyer by the their uuid.
:param uuid: String to identify the buyer by.
:rtype: dictionary
"""
res = self.safe_run(self.slumber.generic.buyer.get, uuid=uuid)
return self._buyer_from_response(res)
def get_secret(self, uuid):
"""Retrieves a seller secret by their uuid.
:param uuid: Sellers uuid.
:rtype: dictionary
"""
res = self.parse_res(self.safe_run(self.slumber.generic.product.get,
seller__active=True,
seller__uuid=uuid))
if len(res['objects']) != 1:
raise ValueError('Not exactly one result found.')
return res['objects'][0]['secret']
def confirm_pin(self, uuid, pin):
"""Confirms the buyer's pin, marking it at confirmed in solitude
:param uuid: String to identify the buyer by.
:param pin: PIN to confirm
:rtype: boolean
"""
res = self.safe_run(self.slumber.generic.confirm_pin.post,
{'uuid': uuid, 'pin': pin})
return res.get('confirmed', False)
def verify_pin(self, uuid, pin):
"""Checks the buyer's PIN against what is stored in solitude.
:param uuid: String to identify the buyer by.
:param pin: PIN to check
:rtype: boolean
"""
res = self.safe_run(self.slumber.generic.verify_pin.post,
{'uuid': uuid, 'pin': pin})
return res.get('valid', False)
def configure_product_for_billing(self, webpay_trans_id,
seller_uuid,
product_id, product_name,
redirect_url_onsuccess,
redirect_url_onerror,
prices):
"""
Get the billing configuration ID for a Bango transaction.
"""
res = self.slumber.generic.seller.get(uuid=seller_uuid)
if res['meta']['total_count'] == 0:
raise SellerNotConfigured('Seller with uuid %s does not exist'
% seller_uuid)
seller_id = res['objects'][0]['resource_pk']
log.info('transaction %s: seller: %s' % (webpay_trans_id,
seller_id))
res = self.slumber.bango.product.get(
seller_product__seller=seller_id,
seller_product__external_id=product_id
)
if res['meta']['total_count'] == 0:
bango_product_uri = self.create_product(product_id,
# TODO: look at why we need currency and price for the
# premium call. This might be a Bango issue.
product_name, 1, 'EUR', res['objects'][0])
else:
bango_product_uri = res['objects'][0]['resource_uri']
log.info('transaction %s: bango product: %s'
% (webpay_trans_id, bango_product_uri))
res = self.slumber.bango.billing.post({
'pageTitle': product_name,
'prices': prices,
'seller_product_bango': bango_product_uri,
'redirect_url_onsuccess': redirect_url_onsuccess,
'redirect_url_onerror': redirect_url_onerror,
})
bill_id = res['billingConfigurationId']
log.info('transaction %s: billing config ID: %s'
% (webpay_trans_id, bill_id))
return bill_id
def create_product(self, external_id, product_name, currency, amount,
seller):
"""
Creates a product and a Bango ID on the fly in solitude.
"""
if not seller['bango']:
raise ValueError('No bango account set up for %s' %
seller['resource_pk'])
product = self.slumber.generic.product.post({
'external_id': external_id,
'seller': seller['bango']['seller']
})
bango = self.slumber.bango.product.post({
'seller_bango': seller['bango']['resource_uri'],
'seller_product': product['resource_uri'],
'name': product_name,
'categoryId': 1,
'secret': 'n' # This is likely going to be removed.
})
self.slumber.bango.premium.post({
'price': amount,
'currencyIso': currency,
'seller_product_bango': bango['resource_uri']
})
self.slumber.bango.rating.post({
'rating': 'UNIVERSAL',
'ratingScheme': 'GLOBAL',
'seller_product_bango': bango['resource_uri']
})
return bango['resource_uri']
if getattr(settings, 'SOLITUDE_URL', False):
client = SolitudeAPI(settings.SOLITUDE_URL)
else:
client = SolitudeAPI('http://example.com')