1+ from rave_python .rave_payment import Payment
2+ from rave_python .rave_exceptions import AccountChargeError
3+ from rave_python .rave_misc import generateTransactionReference
4+
5+ class Enaira (Payment ):
6+ """ This is the rave object for eNaira wallet transactions. It contains the following public functions:\n
7+ .charge -- This is for charging the eNaira wallet\n
8+ .verify -- This checks the status of your transaction\n
9+ .refunds -- This initiates the refund for the transaction\n
10+ """
11+
12+ def _handleChargeResponse (self , response , txRef , request = None ):
13+ """ This handles account charge responses """
14+ # This checks if we can parse the json successfully
15+ res = self ._preliminaryResponseChecks (
16+ response , AccountChargeError , txRef = txRef )
17+
18+ response_json = res ['json' ]
19+ # change - added data before flwRef
20+ response_data = response_json ['data' ]
21+ flw_ref = response_data ['flwRef' ]
22+
23+ data = {
24+ 'error' : False ,
25+ 'validationRequired' : True ,
26+ 'txRef' : txRef ,
27+ 'flwref' : flw_ref
28+ }
29+
30+ if 'qr_image' in response_data :
31+ data .update ({
32+ 'validateInstructions' : "Please scan the qr image in your eNaira app." ,
33+ 'image' : response_data ['qr_image' ]
34+ })
35+ else :
36+ data .update ({
37+ 'validateInstructions' : response_data ['validate_instructions' ],
38+ 'image' : None
39+ })
40+
41+ # If all preliminary checks are passed
42+ return data
43+
44+ # Charge account function
45+ def charge (self , accountDetails , hasFailed = False ):
46+ """ This is the direct account charge call.\n
47+ Parameters include:\n
48+ accountDetails (dict) -- These are the parameters passed to the function for processing\n
49+ hasFailed (boolean) -- This is a flag to determine if the attempt had previously failed due to a timeout\n
50+ """
51+
52+ # setting the endpoint
53+ endpoint = self ._baseUrl + self ._endpointMap ['account' ]['charge' ]
54+ feature_name = "eNaira Payments"
55+
56+ # It is faster to just update rather than check if it is already
57+ # present
58+
59+ if accountDetails .get ("is_token" ) == True :
60+ accountDetails .update ({'payment_type' : 'enaira' , 'is_token' : True , 'country' : 'NG' })
61+ else :
62+ accountDetails .update ({'payment_type' : 'enaira' , 'is_qr' : True , 'country' : 'NG' })
63+
64+ # Generate transaction reference if txRef doesn't exist
65+ accountDetails .setdefault ('txRef' , generateTransactionReference ())
66+
67+ # Checking for required account components
68+ requiredParameters = [
69+ 'amount' ,
70+ 'email' ,
71+ 'firstname' ,
72+ 'lastname'
73+ ]
74+
75+ return super (Enaira , self ).charge (feature_name , accountDetails , requiredParameters , endpoint )
76+
77+ def validate (self , flwRef , otp ):
78+ endpoint = self ._baseUrl + self ._endpointMap ['account' ]['validate' ]
79+ feature_name = "Account-charge-validate"
80+ return super ().validate (feature_name , flwRef , endpoint )
81+
82+ def verify (self , txRef ):
83+ endpoint = self ._baseUrl + self ._endpointMap ['account' ]['verify' ]
84+ feature_name = "Verify eNaira"
85+ return super (Enaira , self ).verify (feature_name , txRef , endpoint )
86+
87+ def refund (self , flwRef , amount ):
88+ feature_name = "Refund eNaira"
89+ endpoint = self ._baseUrl + self ._endpointMap ["account" ]["refund" ]
90+ return super (Enaira , self ).refund (feature_name , flwRef , amount )
91+
0 commit comments