-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
480 lines (435 loc) · 16.7 KB
/
model.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
class BankAccount(object):
"""
account_name: str: Name used to open bank account.
bsb: str: bank account BSB
account_number: str: bank account number
"""
def __init__(self, account_name, bsb, account_number):
self.account_name = account_name
self.bsb = bsb
self.account_number = account_number
def to_dict(self):
return {
"accountName": self.account_name,
"bsb": self.bsb,
"accountNumber": self.account_number,
}
class PayWayCard(object):
def __init__(
self,
card_number=None,
cvn=None,
card_holder_name=None,
expiry_date_month=None,
expiry_date_year=None,
):
self.card_number = card_number
self.cvn = cvn
self.card_holder_name = card_holder_name
self.expiry_date_month = expiry_date_month
self.expiry_date_year = expiry_date_year
def to_dict(self):
return {
"cardNumber": self.card_number,
"cvn": self.cvn,
"cardholderName": self.card_holder_name,
"expiryDateMonth": self.expiry_date_month,
"expiryDateYear": self.expiry_date_year, # Should be YY
}
@staticmethod
def from_dict(payway_card):
card = PayWayCard()
if payway_card.get("maskedCardNumber"):
card.card_number = payway_card.get("maskedCardNumber")
else:
card.card_number = payway_card.get("cardNumber")
card.cvn = payway_card.get("cvn")
card.card_holder_name = payway_card.get("cardholderName")
card.expiry_date_month = payway_card.get("expiryDateMonth")
card.expiry_date_year = payway_card.get("expiryDateYear")
return card
class PayWayCustomer(object):
def __init__(
self,
custom_id=None,
customer_name=None,
email_address=None,
send_email_receipts=None,
phone_number=None,
street=None,
street2=None,
city_name=None,
state=None,
postal_code=None,
token=None,
customer_number=None,
payment_setup=None,
notes=None,
custom_field_1=None,
custom_field_2=None,
custom_field_3=None,
custom_field_4=None,
):
self.custom_id = custom_id
self.customer_name = customer_name
self.email_address = email_address
self.send_email_receipts = send_email_receipts
self.phone_number = phone_number
self.street = street
self.street2 = street2
self.city_name = city_name
self.state = state
self.postal_code = postal_code
self.token = token
self.customer_number = customer_number
self.payment_setup = payment_setup
self.notes = notes
self.customField1 = custom_field_1
self.customField2 = custom_field_2
self.customField3 = custom_field_3
self.customField4 = custom_field_4
def to_dict(self):
customer = {
"customerName": self.customer_name,
"emailAddress": self.email_address,
"sendEmailReceipts": "true" if self.send_email_receipts else "false",
"phoneNumber": self.phone_number,
"street1": self.street,
"street2": self.street2,
"cityName": self.city_name,
"state": self.state,
"postalCode": self.postal_code,
"notes": self.notes,
"customField1": self.customField1,
"customField2": self.customField2,
"customField3": self.customField3,
"customField4": self.customField4,
}
if self.token:
customer.update({"singleUseTokenId": self.token})
return customer
@staticmethod
def from_dict(response):
"""
Parse PayWay Customer response data
:param response: dict PayWay response dictionary
:return:
"""
customer = PayWayCustomer()
contact = response.get("contact")
customer.customer_name = contact.get("customerName")
customer.email_address = contact.get("emailAddress")
customer.send_email_receipts = contact.get("sendEmailReceipts")
customer.phone_number = contact.get("phoneNumber")
address = contact.get("address")
customer.street = address.get("street1")
customer.street2 = address.get("street2")
customer.city_name = address.get("cityName")
customer.state = address.get("state")
customer.postal_code = address.get("postalCode")
customer.customer_number = response.get("customerNumber")
if response.get("paymentSetup") is not None:
customer.payment_setup = PaymentSetup().from_dict(
response.get("paymentSetup")
)
if response.get("customFields") is not None:
custom_fields = response.get("customFields")
for k, v in custom_fields.items():
setattr(customer, k, v)
if response.get("notes") is not None:
customer.notes = response["notes"]
return customer
class PaymentError(object):
field_name = None
message = None
field_value = None
@staticmethod
def from_dict(payway_response):
"""
Returns a list of errors from PayWay
:param: payway_response: dict PayWay response dictionary
"""
errors = payway_response.get("data")
payment_errors = []
for error in errors:
payway_error = PaymentError()
payway_error.field_name = error.get("fieldName")
payway_error.message = error.get("message")
payway_error.field_value = error.get("fieldValue")
payment_errors.append(payway_error)
return payment_errors
def to_message(self):
return "Field: {} Message: {} Field Value: {}".format(
self.field_name,
self.message,
self.field_value,
)
@staticmethod
def list_to_message(payway_errors):
"""
Convert list to readable string
:param payway_errors:
:return:
"""
message = ""
for error in payway_errors:
message += error.to_message()
if len(payway_errors) > 1:
message += " | "
return message
class ServerError(object):
error_number = None
trace_code = None
@staticmethod
def from_dict(response):
"""
:param: response: dict PayWay response dictionary
"""
payway_error = ServerError()
payway_error.error_number = response.get("errorNumber")
payway_error.trace_code = response.get("traceCode")
return payway_error
def to_message(self):
return "Error number: {} Trace code: {}".format(
self.error_number, self.trace_code
)
class PayWayPayment(object):
"""
customer_number: Customer to which this payment belongs.
transaction_type: payment, refund, preAuth, capture or accountVerification
amount: Amount before any surcharge added. Negative for a refund.
currency: aud
order_number: A reference number for this transaction, generated by you.
ip_address: IP address your customer used to connect and process the transaction (if applicable).
parent_transaction_id: The transactionId of the pre-authorisation
token: A token issued by PayWay which holds credit card details. See single use tokens.
merchant_id: This merchant will be used for processing.
"""
def __init__(
self,
transaction_type,
customer_number=None,
amount=None,
currency=None,
order_number=None,
ip_address=None,
parent_transaction_id=None,
token=None,
merchant_id=None,
):
self.transaction_type = transaction_type
self.customer_number = customer_number
self.amount = amount
self.currency = currency
self.order_number = order_number
self.ip_address = ip_address
self.parent_transaction_id = parent_transaction_id
self.token = token
self.merchant_id = merchant_id
def to_dict(self):
payment = {
"customerNumber": self.customer_number,
"transactionType": self.transaction_type, # default to "payment"
"principalAmount": self.amount,
"currency": self.currency,
"orderNumber": self.order_number, # Max 20 ascii chars
"customerIpAddress": self.ip_address,
}
if self.parent_transaction_id:
payment["parentTransactionId"] = self.parent_transaction_id
# fields for pre-authorisation
if self.token:
payment["singleUseTokenId"] = self.token
if self.merchant_id:
payment["merchantId"] = self.merchant_id
return payment
class PayWayTransaction(object):
transaction_id = None
receipt_number = None
status = None
response_code = None
response_text = None
transaction_type = None
customer_number = None
customer_name = None
customer_email = None
bpay_ref = None
order_number = None
currency = None
principal_amount = None
surcharge_amount = None
payment_amount = None
payment_method = None
declined_date = None
card = None
merchant = None
virtual_account = None
australia_post = None
bpay = None
your_bank_account = None
customer_paypal_account = None
your_paypal_account = None
transaction_date_time = None
user = None
settlement_date = None
parent_transaction = None
ip_address = None
fraud_result = None
ip_country = None
card_country = None
custom_fields = None
is_voidable = None
is_refundable = None
def to_dict(self):
return {
"transactionId": self.transaction_id,
"receiptNumber": self.receipt_number,
"status": self.status,
"responseCode": self.response_code,
"responseText": self.response_text,
"transactionType": self.transaction_type,
"customerNumber": self.customer_number,
"customerName": self.customer_name,
"customerEmail": self.customer_email,
"currency": self.currency,
"principalAmount": self.principal_amount,
"surchargeAmount": self.surcharge_amount,
"paymentAmount": self.payment_amount,
"paymentMethod": self.payment_method,
"creditCard": self.card.to_dict(),
"merchant": self.merchant.to_dict(),
"virtualAccount": self.virtual_account,
"bpaustraliaPostay": self.australia_post,
"bpay": self.bpay,
"yourBankAccount": self.your_bank_account,
"customerPayPalAccount": self.customer_paypal_account,
"yourPayPalAccount": self.your_paypal_account,
"transactionDateTime": self.transaction_date_time,
"user": self.user,
"settlementDate": self.settlement_date,
"declinedDate": self.declined_date,
"parentTransaction": self.parent_transaction,
"customerIpAddress": self.ip_address,
"fraudResult": self.fraud_result,
"customerIpCountry": self.ip_country,
"cardCountry": self.card_country,
"customFields": self.custom_fields,
"isVoidable": self.is_voidable,
"isRefundable": self.is_refundable,
}
@staticmethod
def from_dict(response):
"""
:param: response: dict PayWay response dictionary
"""
transaction = PayWayTransaction()
transaction.transaction_id = response.get("transactionId")
transaction.receipt_number = response.get("receiptNumber")
transaction.status = response.get("status")
transaction.response_code = response.get("responseCode")
transaction.response_text = response.get("responseText")
transaction.transaction_type = response.get("transactionType")
transaction.customer_number = response.get("customerNumber")
transaction.customer_name = response.get("customerName")
transaction.customer_email = response.get("customerEmail")
transaction.currency = response.get("currency")
transaction.principal_amount = response.get("principalAmount")
transaction.surcharge_amount = response.get("surchargeAmount")
transaction.payment_amount = response.get("paymentAmount")
transaction.payment_method = response.get("paymentMethod")
if response.get("creditCard") is not None:
transaction.card = PayWayCard.from_dict(response.get("creditCard"))
if response.get("merchant") is not None:
transaction.merchant = Merchant.from_dict(response.get("merchant"))
transaction.virtual_account = response.get("virtualAccount")
transaction.australia_post = response.get("bpaustraliaPostay")
transaction.bpay = response.get("bpay")
transaction.your_bank_account = response.get("yourBankAccount")
transaction.customer_paypal_account = response.get("customerPayPalAccount")
transaction.your_paypal_account = response.get("yourPayPalAccount")
transaction.transaction_date_time = response.get("transactionDateTime")
transaction.user = response.get("user")
transaction.settlement_date = response.get("settlementDate")
transaction.declined_date = response.get("declinedDate")
transaction.parent_transaction = response.get("parentTransaction")
transaction.ip_address = response.get("customerIpAddress")
transaction.fraud_result = response.get("fraudResult")
transaction.ip_country = response.get("customerIpCountry")
transaction.card_country = response.get("cardCountry")
transaction.custom_fields = response.get("customFields")
transaction.is_voidable = response.get("isVoidable")
transaction.is_refundable = response.get("isRefundable")
return transaction
class Merchant(object):
"""
merchantId Issued by us to uniquely identify a merchant facility
merchantName
settlementBsb The BSB of your settlement bank account
settlementAccountNumber The account number of your settlement bank account
surchargeBsb If surcharges are settled separately, the BSB for your surcharge settlement account
surchargeAccountNumber If surcharges are settled separately, the account number for your surcharge settlement
account
"""
merchant_id = None
merchant_name = None
settlement_bsb = None
settlement_account_number = None
surcharge_bsb = None
surcharge_account_number = None
def to_dict(self):
return {
"merchantId": self.merchant_id,
"merchantName": self.merchant_name,
"settlementBsb": self.settlement_bsb,
"settlementAccountNumber": self.settlement_account_number,
"surchargeBsb": self.surcharge_bsb,
"surchargeAccountNumber": self.surcharge_account_number,
}
@staticmethod
def from_dict(payway_obj):
"""
:param: payway_obj: dict PayWay response dictionary
"""
merchant = Merchant()
merchant.merchant_id = payway_obj.get("merchantId")
merchant.merchant_name = payway_obj.get("merchantName")
merchant.settlement_bsb = payway_obj.get("settlementBsb")
merchant.settlement_account_number = payway_obj.get("settlementAccountNumber")
merchant.surcharge_bsb = payway_obj.get("surchargeBsb")
merchant.surcharge_account_number = payway_obj.get("surchargeAccountNumber")
return merchant
class PaymentSetup(object):
payment_method = None
stopped = None
credit_card = None
merchant = None
@staticmethod
def from_dict(response):
"""
:param: response: dict PayWay response dictionary
"""
ps = PaymentSetup()
ps.payment_method = response.get("paymentMethod")
ps.stopped = response.get("stopped")
if response.get("creditCard") is not None:
ps.credit_card = PayWayCard().from_dict(response.get("creditCard"))
if response.get("merchant") is not None:
ps.merchant = Merchant().from_dict(response.get("merchant"))
return ps
class TokenResponse(object):
token = None
payment_method = None
card = None
bank_account = None
@staticmethod
def from_dict(response):
"""
:param: response: dict PayWay response dictionary
"""
tr = TokenResponse()
tr.token = response.get("singleUseTokenId")
tr.payment_method = response.get("paymentMethod")
if response.get("creditCard") is not None:
card = PayWayCard().from_dict(response["creditCard"])
tr.card = card
return tr