-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrave_card.py
More file actions
219 lines (192 loc) · 8.74 KB
/
rave_card.py
File metadata and controls
219 lines (192 loc) · 8.74 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
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
from rave_python.rave_exceptions import RaveError, IncompletePaymentDetailsError, CardChargeError, TransactionVerificationError, ServerError
from rave_python.rave_payment import Payment
from rave_python.rave_misc import generateTransactionReference
class Card(Payment):
""" This is the rave object for card transactions. It contains the following public functions:\n
.charge -- This is for making a card charge\n
.validate -- This is called if further action is required i.e. OTP validation\n
.verify -- This checks the status of your transaction\n
"""
def __init__(self, publicKey, secretKey, production, usingEnv):
super(Card, self).__init__(publicKey, secretKey, production, usingEnv)
# returns true if further action is required, false if it isn't
def _handleChargeResponse(self, response, txRef, request=None):
""" This handles charge responses """
res = self._preliminaryResponseChecks(
response, CardChargeError, txRef=txRef)
responseJson = res["json"]
flwRef = res["flwRef"]
# Checking if there is auth url
if responseJson["data"].get("authurl", "N/A") == "N/A":
authUrl = None
else:
authUrl = responseJson["data"]["authurl"]
# If all preliminary checks passed
if not (responseJson["data"].get("chargeResponseCode", None) == "00"):
# Otherwise we return that further action is required, along with
# the response
suggestedAuth = responseJson["data"].get("suggested_auth", None)
return {
"error": False,
"validationRequired": True,
"txRef": txRef,
"flwRef": flwRef,
"suggestedAuth": suggestedAuth,
"authUrl": authUrl}
else:
return {
"error": False,
"status": responseJson["status"],
"validationRequired": False,
"txRef": txRef,
"flwRef": flwRef,
"suggestedAuth": None,
"authUrl": authUrl}
def _handleRefundorVoidResponse(self, response, txRef, request=None):
""" This handles charge responses """
res = self._preliminaryResponseChecks(
response, CardChargeError, txRef=txRef)
responseJson = res["json"]
flwRef = responseJson["data"]["data"]["authorizeId"]
# If all preliminary checks passed
if not (
responseJson["data"]["data"].get(
"responsecode",
None) == "RR"):
# Refund or Void could not be completed
return {
"error": True,
"status": responseJson["status"],
"message": responseJson["message"],
"flwRef": flwRef}
else:
return {
"error": False,
"status": responseJson["status"],
"message": responseJson["message"],
"flwRef": flwRef}
# This can be altered by implementing classes but this is the default behaviour
# Returns True and the data if successful
def _handleVerifyResponse(self, response, txRef):
""" This handles all responses from the verify call.\n
Parameters include:\n
response (dict) -- This is the response Http object returned from the verify call
"""
# Checking if there was a server error during the call (in this case
# html is returned instead of json)
res = self._preliminaryResponseChecks(
response, TransactionVerificationError, txRef=txRef)
responseJson = res["json"]
flwRef = responseJson["data"]["flwref"]
amount = responseJson["data"]["amount"]
chargedamount = responseJson["data"]["chargedamount"]
cardToken = responseJson["data"]["card"]["card_tokens"][0]["embedtoken"]
vbvmessage = responseJson["data"]["vbvmessage"]
chargemessage = responseJson["data"]["chargemessage"]
chargecode = responseJson["data"]["chargecode"]
currency = responseJson["data"]["currency"]
paymenttype = responseJson["data"]["paymenttype"]
custname = responseJson["data"]["custname"]
custemail = responseJson["data"]["custemail"]
custphone = responseJson["data"]["custphone"]
meta = responseJson["data"]["meta"]
# Check if the call returned something other than a 200
if not response.ok:
errMsg = responseJson["data"].get(
"message", "Your call failed with no response")
raise TransactionVerificationError(
{"error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": errMsg})
# if the chargecode is not 00
elif not (responseJson["data"].get("chargecode", None) == "00"):
return {
"error": False,
"transactionComplete": False,
"txRef": txRef,
"flwRef": flwRef,
"amount": amount,
"chargedamount": chargedamount,
"cardToken": cardToken,
"vbvmessage": vbvmessage,
"chargemessage": chargemessage,
"chargecode": chargecode,
"currency": currency,
"paymenttype": paymenttype,
"custname": custname,
"custemail": custemail,
"custphone": custphone,
"meta": meta}
else:
return {
"error": False,
"transactionComplete": True,
"txRef": txRef,
"flwRef": flwRef,
"amount": amount,
"chargedamount": chargedamount,
"cardToken": cardToken,
"vbvmessage": vbvmessage,
"chargemessage": chargemessage,
"chargecode": chargecode,
"currency": currency,
"paymenttype": paymenttype,
"custname": custname,
"custemail": custemail,
"custphone": custphone,
"meta": meta}
# Charge card function
def charge(self, cardDetails, hasFailed=False, chargeWithToken=False):
""" This is called to initiate the charge process.\n
Parameters include:\n
cardDetails (dict) -- This is a dictionary comprising payload parameters.\n
hasFailed (bool) -- This indicates whether the request had previously failed for timeout handling
"""
# setting the endpoint
feature_name = "Initiate-Card-charge"
if not chargeWithToken:
endpoint = self._baseUrl + self._endpointMap["card"]["charge"]
requiredParameters = [
"cardno",
"cvv",
"expirymonth",
"expiryyear",
"amount",
"email"]
# optionalParameters = ["phonenumber", "firstname", "lastname"]
else:
if "charge_type" in cardDetails and cardDetails["charge_type"] == 'preauth':
endpoint = self._baseUrl + \
self._endpointMap["preauth"]["charge"]
else:
endpoint = self._baseUrl + \
self._endpointMap["card"]["chargeSavedCard"]
requiredParameters = [
"currency",
"token",
"country",
"amount",
"email",
"txRef"]
# optionalParameters = ["firstname", "lastname"]
# add token to requiredParameters
# requiredParameters.append("token")
if not ("txRef" in cardDetails):
cardDetails.update({"txRef": generateTransactionReference()})
return super(
Card,
self).charge(
feature_name,
cardDetails,
requiredParameters,
endpoint)
def validate(self, flwRef, otp):
feature_name = "Validate-Card-charge"
endpoint = self._baseUrl + self._endpointMap["card"]["validate"]
return super(Card, self).validate(feature_name, flwRef, otp, endpoint)
def verify(self, txRef):
feature_name = "Verify-Card-charge"
endpoint = self._baseUrl + self._endpointMap["card"]["verify"]
return super(Card, self).verify(feature_name, txRef, endpoint)
def refund(self, flwRef, amount):
feature_name = "Card-refund"
endpoint = self._baseUrl + self._endpointMap["card"]["refund"]
return super(Card, self).refund(feature_name, flwRef, amount)