Skip to content

Commit 2fee567

Browse files
added refund method in Card, Account and MoMo classes
1 parent 9946d79 commit 2fee567

17 files changed

+215
-35
lines changed

README.md

+121
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ This is used to facilitate card transactions.
8989

9090
* ```.verify```
9191

92+
* ```.refund```
93+
9294
* ```.getTypeOfArgsRequired```
9395

9496
* ```.updatePayload```
@@ -318,6 +320,27 @@ except RaveExceptions.CardChargeError as e:
318320
```
319321
#### NB: when charging saved cards, you do not need to call rave.card.Validate()
320322

323+
324+
### ```.refund(flwRef, amount)```
325+
326+
You can call this to refund a full or partial amount of a transaction that was completed successfully. You have to pass the flw reference generated at the point of verifing the transaction. This is the ```flwRef``` in the ```res``` parameter returned any of the calls (```charge``` or ```validate```).
327+
328+
A sample verify call is:
329+
330+
```py
331+
res = rave.Card.refund(data["flwRef"], amount)
332+
```
333+
334+
#### Returns
335+
336+
This call returns a dict with ```status```, ```AmountRefunded```, ```disburse_ref``` and ```disburse_status``` which indicates whether the transaction was completed successfully.
337+
338+
Sample
339+
```py
340+
(True, {'settlement_id': 'NEW', 'id': 36071, 'AccountId': 49278, 'TransactionId': 1664979, 'FlwRef': 'FLW-MOCK-62986ef948d6dd127102cdb58813d216', 'walletId': 50309, 'AmountRefunded': '5000', 'status': 'completed', 'destination': 'payment_source', 'meta': '{"source":"ledgerbalance","uniquereference":"62472228710"}', 'updatedAt': '2021-01-23T01:27:11.969Z', 'createdAt': '2021-01-23T01:27:08.672Z'})
341+
```
342+
343+
321344
### Complete card charge flow
322345

323346
```py
@@ -508,6 +531,24 @@ except RaveExceptions.TransactionVerificationError as e:
508531
print(e.err["flwRef"])
509532
```
510533

534+
### ```.refund(flwRef, amount)```
535+
536+
You can call this to refund a full or partial amount of a transaction that was completed successfully. You have to pass the flw reference generated at the point of verifing the transaction. This is the ```flwRef``` in the ```res``` parameter returned any of the calls (```charge``` or ```validate```).
537+
538+
A sample verify call is:
539+
540+
```py
541+
res = rave.Account.refund(data["flwRef"], amount)
542+
```
543+
544+
#### Returns
545+
546+
This call returns a dict with ```status```, ```AmountRefunded```, ```disburse_ref``` and ```disburse_status``` which indicates whether the transaction was completed successfully.
547+
548+
Sample
549+
```py
550+
(True, {'settlement_id': 'NEW', 'id': 36070, 'AccountId': 49278, 'TransactionId': 1664654, 'FlwRef': '9792859961', 'walletId': 71139, 'AmountRefunded': '1500', 'status': 'pending-momo', 'destination': 'mobilemoney', 'meta': '{"source":"ledgerbalance","disburse_ref":"CC-REFD-31101658361-9792859961","disburse_status":"pending"}', 'updatedAt': '2021-01-23T01:17:43.665Z', 'createdAt': '2021-01-23T01:17:38.327Z'})
551+
```
511552

512553

513554
<br>
@@ -634,6 +675,25 @@ This call returns a dict with ```txRef```, ```flwRef``` and ```transactionComple
634675

635676
If your call could not be completed successfully, a ```TransactionVerificationError``` is raised.
636677

678+
### ```.refund(flwRef, amount)```
679+
680+
You can call this to refund a full or partial amount of a transaction that was completed successfully. You have to pass the flw reference generated at the point of verifing the transaction. This is the ```flwRef``` in the ```res``` parameter returned any of the calls (```charge``` or ```validate```).
681+
682+
A sample verify call is:
683+
684+
```py
685+
res = rave.GhMobile.refund(data["flwRef"], amount)
686+
```
687+
688+
#### Returns
689+
690+
This call returns a dict with ```status```, ```AmountRefunded```, ```disburse_ref``` and ```disburse_status``` which indicates whether the transaction was completed successfully.
691+
692+
Sample
693+
```py
694+
(True, {'settlement_id': 'NEW', 'id': 36070, 'AccountId': 49278, 'TransactionId': 1664654, 'FlwRef': '9792859961', 'walletId': 71139, 'AmountRefunded': '1500', 'status': 'pending-momo', 'destination': 'mobilemoney', 'meta': '{"source":"ledgerbalance","disburse_ref":"CC-REFD-31101658361-9792859961","disburse_status":"pending"}', 'updatedAt': '2021-01-23T01:17:43.665Z', 'createdAt': '2021-01-23T01:17:38.327Z'})
695+
```
696+
637697
<br>
638698

639699
### Complete GhMobile charge flow
@@ -743,6 +803,26 @@ This call returns a dict with ```txRef```, ```flwRef``` and ```transactionComple
743803

744804
If your call could not be completed successfully, a ```TransactionVerificationError``` is raised.
745805

806+
807+
### ```.refund(flwRef, amount)```
808+
809+
You can call this to refund a full or partial amount of a transaction that was completed successfully. You have to pass the flw reference generated at the point of verifing the transaction. This is the ```flwRef``` in the ```res``` parameter returned any of the calls (```charge``` or ```validate```).
810+
811+
A sample verify call is:
812+
813+
```py
814+
res = rave.Mpesa.refund(data["flwRef"], amount)
815+
```
816+
817+
#### Returns
818+
819+
This call returns a dict with ```status```, ```AmountRefunded```, ```disburse_ref``` and ```disburse_status``` which indicates whether the transaction was completed successfully.
820+
821+
Sample
822+
```py
823+
(True, {'settlement_id': 'NEW', 'id': 36070, 'AccountId': 49278, 'TransactionId': 1664654, 'FlwRef': '9792859961', 'walletId': 71139, 'AmountRefunded': '1500', 'status': 'pending-momo', 'destination': 'mobilemoney', 'meta': '{"source":"ledgerbalance","disburse_ref":"CC-REFD-31101658361-9792859961","disburse_status":"pending"}', 'updatedAt': '2021-01-23T01:17:43.665Z', 'createdAt': '2021-01-23T01:17:38.327Z'})
824+
```
825+
746826
<br>
747827

748828
### Complete Mpesa charge flow
@@ -853,6 +933,26 @@ This call returns a dict with ```txRef```, ```flwRef``` and ```transactionComple
853933

854934
If your call could not be completed successfully, a ```TransactionVerificationError``` is raised.
855935

936+
937+
### ```.refund(flwRef, amount)```
938+
939+
You can call this to refund a full or partial amount of a transaction that was completed successfully. You have to pass the flw reference generated at the point of verifing the transaction. This is the ```flwRef``` in the ```res``` parameter returned any of the calls (```charge``` or ```validate```).
940+
941+
A sample verify call is:
942+
943+
```py
944+
res = rave.UGMobile.refund(data["flwRef"], amount)
945+
```
946+
947+
#### Returns
948+
949+
This call returns a dict with ```status```, ```AmountRefunded```, ```disburse_ref``` and ```disburse_status``` which indicates whether the transaction was completed successfully.
950+
951+
Sample
952+
```py
953+
(True, {'settlement_id': 'NEW', 'id': 36070, 'AccountId': 49278, 'TransactionId': 1664654, 'FlwRef': '9792859961', 'walletId': 71139, 'AmountRefunded': '1500', 'status': 'pending-momo', 'destination': 'mobilemoney', 'meta': '{"source":"ledgerbalance","disburse_ref":"CC-REFD-31101658361-9792859961","disburse_status":"pending"}', 'updatedAt': '2021-01-23T01:17:43.665Z', 'createdAt': '2021-01-23T01:17:38.327Z'})
954+
```
955+
856956
<br>
857957

858958
### Complete UGMobile charge flow
@@ -963,6 +1063,27 @@ This call returns a dict with ```txRef```, ```flwRef``` and ```transactionComple
9631063

9641064
If your call could not be completed successfully, a ```TransactionVerificationError``` is raised.
9651065

1066+
1067+
### ```.refund(flwRef, amount)```
1068+
1069+
You can call this to refund a full or partial amount of a transaction that was completed successfully. You have to pass the flw reference generated at the point of verifing the transaction. This is the ```flwRef``` in the ```res``` parameter returned any of the calls (```charge``` or ```validate```).
1070+
1071+
A sample verify call is:
1072+
1073+
```py
1074+
res = rave.ZBMobile.refund(data["flwRef"], amount)
1075+
```
1076+
1077+
#### Returns
1078+
1079+
This call returns a dict with ```status```, ```AmountRefunded```, ```disburse_ref``` and ```disburse_status``` which indicates whether the transaction was completed successfully.
1080+
1081+
Sample
1082+
```py
1083+
(True, {'settlement_id': 'NEW', 'id': 36070, 'AccountId': 49278, 'TransactionId': 1664654, 'FlwRef': '9792859961', 'walletId': 71139, 'AmountRefunded': '1500', 'status': 'pending-momo', 'destination': 'mobilemoney', 'meta': '{"source":"ledgerbalance","disburse_ref":"CC-REFD-31101658361-9792859961","disburse_status":"pending"}', 'updatedAt': '2021-01-23T01:17:43.665Z', 'createdAt': '2021-01-23T01:17:38.327Z'})
1084+
```
1085+
1086+
9661087
<br>
9671088

9681089
### Complete ZBMobile charge flow

rave_python/rave_account.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _handleChargeResponse(self, response, txRef, request=None):
3636

3737
# Charge account function
3838
def charge(self, accountDetails, hasFailed=False):
39-
""" This is the ghMobile charge call.\n
39+
""" This is the direct account charge call.\n
4040
Parameters include:\n
4141
accountDetails (dict) -- These are the parameters passed to the function for processing\n
4242
hasFailed (boolean) -- This is a flag to determine if the attempt had previously failed due to a timeout\n
@@ -65,4 +65,9 @@ def validate(self, flwRef, otp):
6565
def verify(self, txRef):
6666
endpoint = self._baseUrl + self._endpointMap['account']['verify']
6767
feature_name = "Account-charge-verify"
68-
return super().verify(feature_name, txRef, endpoint)
68+
return super().verify(feature_name, txRef, endpoint)
69+
70+
def refund(self, flwRef, amount):
71+
feature_name = "Account-charge-refund"
72+
endpoint = self._baseUrl + self._endpointMap["account"]["refund"]
73+
return super().refund(feature_name, flwRef, amount)

rave_python/rave_base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, publicKey=None, secretKey=None, production=False, usingEnv=Tr
2121
"validate": "flwv3-pug/getpaidx/api/validatecharge",
2222
"verify": "flwv3-pug/getpaidx/api/v2/verify",
2323
"chargeSavedCard": "flwv3-pug/getpaidx/api/tokenized/charge",
24+
"refund": "gpx/merchant/transactions/refund"
2425
},
2526
"ebills": {
2627
"create": "flwv3-pug/getpaidx/api/ebills/generateorder/",
@@ -34,7 +35,8 @@ def __init__(self, publicKey=None, secretKey=None, production=False, usingEnv=Tr
3435
"account": {
3536
"charge": "flwv3-pug/getpaidx/api/charge",
3637
"validate": "flwv3-pug/getpaidx/api/validate",
37-
"verify": "flwv3-pug/getpaidx/api/v2/verify"
38+
"verify": "flwv3-pug/getpaidx/api/v2/verify",
39+
"refund": "gpx/merchant/transactions/refund"
3840
},
3941
"payment_plan": {
4042
"create": "v2/gpx/paymentplans/create",

rave_python/rave_bills.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def create(self, details):
5757
tracking_payload = {
5858
"publicKey": self._getPublicKey(),
5959
"language": "Python v2",
60-
"version": "1.2.5",
60+
"version": "1.2.9",
6161
"title": "Create-Bills-error",
6262
"message": responseTime
6363
}
@@ -68,7 +68,7 @@ def create(self, details):
6868
tracking_payload = {
6969
"publicKey": self._getPublicKey(),
7070
"language": "Python v2",
71-
"version": "1.2.5",
71+
"version": "1.2.9",
7272
"title": "Create-Bills",
7373
"message": responseTime
7474
}

rave_python/rave_card.py

+5
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ def verify(self, txRef):
124124
feature_name = "Verify-Card-charge"
125125
endpoint = self._baseUrl + self._endpointMap["card"]["verify"]
126126
return super(Card, self).verify(feature_name, txRef, endpoint)
127+
128+
def refund(self, flwRef, amount):
129+
feature_name = "Card-refund"
130+
endpoint = self._baseUrl + self._endpointMap["card"]["refund"]
131+
return super(Card, self).refund(feature_name, flwRef, amount)

rave_python/rave_francophone.py

+8
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,24 @@ def charge(self, accountDetails, hasFailed=False):
5151

5252
feature_name = "Initiate-Francophone-mobile-money-charge"
5353
endpoint = self._baseUrl + self._endpointMap["account"]["charge"]
54+
5455
# It is faster to add boilerplate than to check if each one is present
5556
accountDetails.update({"payment_type": "mobilemoneyfrancophone", "is_mobile_money_franco":"1"})
5657

5758
# If transaction reference is not set
5859
if not ("txRef" in accountDetails):
5960
accountDetails.update({"txRef": generateTransactionReference()})
61+
6062
# If order reference is not set
6163
if not ("orderRef" in accountDetails):
6264
accountDetails.update({"orderRef": generateTransactionReference()})
65+
6366
# Checking for required account components
6467
# requiredParameters = ["amount", "email", "phonenumber", "IP", "redirect_url"]
6568
requiredParameters = ["amount"]
6669
return super(Francophone, self).charge(feature_name, accountDetails, requiredParameters, endpoint)
70+
71+
def refund(self, flwRef, amount):
72+
feature_name = "Francophone-charge-refund"
73+
endpoint = self._baseUrl + self._endpointMap["refund"]
74+
return super(Francophone, self).refund(feature_name, flwRef, amount)

rave_python/rave_ghmobile.py

+7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def charge(self, accountDetails, hasFailed=False):
2222

2323
# It is faster to add boilerplate than to check if each one is present
2424
accountDetails.update({"payment_type": "mobilemoneygh", "country":"GH", "is_mobile_money_gh":"1", "currency":"GHS"})
25+
2526
# If transaction reference is not set
2627
if not ("txRef" in accountDetails):
2728
accountDetails.update({"txRef": generateTransactionReference()})
29+
2830
# If order reference is not set
2931
if not ("orderRef" in accountDetails):
3032
accountDetails.update({"orderRef": generateTransactionReference()})
@@ -33,3 +35,8 @@ def charge(self, accountDetails, hasFailed=False):
3335
requiredParameters = ["amount", "email", "phonenumber", "network", "IP", "redirect_url"]
3436
return super(GhMobile, self).charge(feature_name, accountDetails, requiredParameters, endpoint)
3537

38+
def refund(self, flwRef, amount):
39+
feature_name = "Ghana-mobile-money-charge-refund"
40+
endpoint = self._baseUrl + self._endpointMap["refund"]
41+
return super(GhMobile, self).refund(feature_name, flwRef, amount)
42+

rave_python/rave_mpesa.py

+8
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ def charge(self, accountDetails, hasFailed=False):
2020
# Setting the endpoint
2121
feature_name = "Initiate-Mpesa-charge"
2222
endpoint = self._baseUrl + self._endpointMap["account"]["charge"]
23+
2324
# Adding boilerplate mpesa requirements
2425
accountDetails.update({"payment_type": "mpesa", "country":"KE", "is_mpesa":"1", "is_mpesa_lipa":"1", "currency":"KES"})
26+
2527
# If transaction reference is not set
2628
if not ("txRef" in accountDetails):
2729
accountDetails.update({"txRef": generateTransactionReference()})
30+
2831
# If order reference is not set
2932
if not ("orderRef" in accountDetails):
3033
accountDetails.update({"orderRef": generateTransactionReference()})
@@ -38,3 +41,8 @@ def verify(self, txRef):
3841
feature_name = "Verify-Mpesa_charge"
3942
endpoint = self._baseUrl + self._endpointMap["account"]["verify"]
4043
return super(Mpesa, self).verify(feature_name, txRef, endpoint)
44+
45+
def refund(self, flwRef, amount):
46+
feature_name = "Mpesa-charge-refund"
47+
endpoint = self._baseUrl + self._endpointMap["account"]["refund"]
48+
return super(Mpesa, self).refund(feature_name, flwRef, amount)

0 commit comments

Comments
 (0)