From ef7b15f173748c67b807d9162628363cee1ca466 Mon Sep 17 00:00:00 2001 From: jim-junior Date: Sun, 25 Feb 2024 19:18:35 +0300 Subject: [PATCH 1/2] Add cord and mobile money chard --- mobile_money/flutterwave/__init__.py | 0 mobile_money/flutterwave/charge.py | 174 +++++++++++++++++++++++++++ mobile_money/flutterwave/transfer.py | 2 + 3 files changed, 176 insertions(+) create mode 100644 mobile_money/flutterwave/__init__.py create mode 100644 mobile_money/flutterwave/charge.py create mode 100644 mobile_money/flutterwave/transfer.py diff --git a/mobile_money/flutterwave/__init__.py b/mobile_money/flutterwave/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mobile_money/flutterwave/charge.py b/mobile_money/flutterwave/charge.py new file mode 100644 index 0000000..12a60be --- /dev/null +++ b/mobile_money/flutterwave/charge.py @@ -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 diff --git a/mobile_money/flutterwave/transfer.py b/mobile_money/flutterwave/transfer.py new file mode 100644 index 0000000..0e0a4ff --- /dev/null +++ b/mobile_money/flutterwave/transfer.py @@ -0,0 +1,2 @@ +import httpx +from httpx import Response From b1d40cee7e11cb9ef6ff3910b2d45a543f875731 Mon Sep 17 00:00:00 2001 From: jim-junior Date: Sun, 25 Feb 2024 19:22:06 +0300 Subject: [PATCH 2/2] add setup --- setup.cfg | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index f785bdf..073b69a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 @@ -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 \ No newline at end of file + httpx >= 0.21.0 + xmltodict >= 0.13.0 + pycryptodome >= 3.20.1 \ No newline at end of file