Skip to content

Commit

Permalink
Merge pull request #1 from open-ug/flutterwave
Browse files Browse the repository at this point in the history
Add Flutterwave
  • Loading branch information
jim-junior authored Feb 25, 2024
2 parents b4cbf4a + b1d40ce commit cd55197
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 3 deletions.
Empty file.
174 changes: 174 additions & 0 deletions mobile_money/flutterwave/charge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import httpx
import json
import base64
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad


CHARGE_CARD_ENDPOINT = "https://api.flutterwave.com/v3/charges?type=card"
MOBILE_MONEY_ENDPOINT = "https://api.flutterwave.com/v3/charges?type=mobile_money_"
MPESA_ENDPOINT = "https://api.flutterwave.com/v3/charges?type=mpesa"
TOKENISED_TRANSACTIONS = "https://api.flutterwave.com/v3/tokenized-charges"


# Function that charges a card using flutterwave api


def charge_card(
card_number,
cvv,
expiry_month,
expiry_year,
transaction_id,
amount,
email,
currency="USD",
FLUTTERWAVE_SECRET=None,
ENCRYPTION_KEY=None,
):
"""
Charge a card
:param card_number: Card number
:param cvv: Card CVV
:param expiry_month: Expiry month
:param expiry_year: Expiry Year
:param transaction_id: Transaction ID
:param amount: Amount
:param email: Email
:param currency: Currency Default USD
"""

payload = {
"amount": amount,
"tx_ref": transaction_id,
"currency": currency,
"card_number": card_number,
"cvv": cvv,
"expiry_month": expiry_month,
"expiry_year": expiry_year,
"email": email,
}

headers = {
"Authorization": "Bearer " + FLUTTERWAVE_SECRET,
"Content-Type": "application/json",
}

# Encrypt the payload using 3DES algorithm
payload = json.dumps(payload)
payload = payload.encode("utf-8")
encryption_key = ENCRYPTION_KEY.encode("utf-8")
cipher = DES3.new(encryption_key, DES3.MODE_ECB)
payload = cipher.encrypt(pad(payload, DES3.block_size))
payload = base64.b64encode(payload)
payload = payload.decode("utf-8")

# Send the request to the api
response = httpx.post(CHARGE_CARD_ENDPOINT, headers=headers, data={
"client": payload
})

return response


def charge_mobile_money(
phone_number, transaction_id, order_id, amount, email, country="uganda", currency="UGX", network="MTN",
FLUTTERWAVE_SECRET=None
):
"""
Charge Mobile Money
"""
endpoint = MOBILE_MONEY_ENDPOINT + country

if phone_number.startswith("+"):
# change +2567 to 07
phone_number = phone_number[4:]
phone_number = "0" + phone_number

payload = {
"amount": amount,
"tx_ref": transaction_id,
"currency": currency,
"email": email,
"phone_number": phone_number,
"order_id": order_id,
}

if country == "ghana":
payload["network"] = network

headers = {
"Authorization": "Bearer " + FLUTTERWAVE_SECRET,
"Content-Type": "application/json",
}

response = httpx.post(endpoint, headers=headers, data=json.dumps(payload))
return response


def charge_mpesa(phone_number, transaction_id, order_id, amount, email, country="kenya",
FLUTTERWAVE_SECRET=None):
"""
Charge Mpesa
"""
currency = "KES"
endpoint = MPESA_ENDPOINT + country
if country == "kenya":
currency = "KES"
elif country == "tanzania":
currency = "TZS"

payload = {
"amount": amount,
"tx_ref": transaction_id,
"currency": currency,
"email": email,
"phone_number": phone_number,
"order_id": order_id,
}

headers = {
"Authorization": "Bearer " + FLUTTERWAVE_SECRET,
"Content-Type": "application/json",
}

response = httpx.post(endpoint, headers=headers, data=json.dumps(payload))
return response


def verify_transaction(transaction_id,
flutterwave_secret=None):
"""
Verify a transaction
"""
endpoint = f"https://api.flutterwave.com/v3/transactions/{transaction_id}/verify"
headers = {
"Authorization": "Bearer " + flutterwave_secret,
"Content-Type": "application/json",
}

response = httpx.get(endpoint, headers=headers)
return response


def charge_card_token(card_token, transaction_id, amount, email, currency="USD",
FLUTTERWAVE_SECRET=None):
"""
Charge a card using a token
"""
payload = {
"amount": amount,
"tx_ref": transaction_id,
"currency": currency,
"email": email,
"token": card_token,
}

headers = {
"Authorization": "Bearer " + FLUTTERWAVE_SECRET,
"Content-Type": "application/json",
}

response = httpx.post(TOKENISED_TRANSACTIONS,
headers=headers, data=json.dumps(payload))
return response
2 changes: 2 additions & 0 deletions mobile_money/flutterwave/transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import httpx
from httpx import Response
7 changes: 4 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ugmobilemoney
version = 0.0.2
version = 0.0.3
description = Carry out Collections and Disbursements using MTN and Airtel Mobile Money in Uganda and Africa
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -23,5 +23,6 @@ include_package_data = true
packages = find:
python_requires = >=3.6
install_requires =
httpx >= 0.21.0 # Replace "X.Y" as appropriate
xmltodict >= 0.13.0 # Replace "X.Y" as appropriate
httpx >= 0.21.0
xmltodict >= 0.13.0
pycryptodome >= 3.20.1

0 comments on commit cd55197

Please sign in to comment.