Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit feb5b59

Browse files
Merge pull request #8 from WilliamOtieno/develop
Develop
2 parents c6d070d + a0cece0 commit feb5b59

File tree

7 files changed

+188
-4
lines changed

7 files changed

+188
-4
lines changed

python_flutterwave/charge/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
from .card import *
33
from .mobile import *
44
from .validation import *
5-

python_flutterwave/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
token = os.environ.get("FW_SECRET_KEY")
77

8+
89
def require_token(func):
910
def wrapper(*args, **kwargs):
1011
if token == "" or token is None:

python_flutterwave/exceptions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ class FlutterwaveAPIException(Exception):
33

44

55
class TokenException(FlutterwaveAPIException):
6-
76
def __init__(self, token, message):
87
super().__init__(f"Token Error: {message}")
98
self.token = token
10-

python_flutterwave/objects.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class RetryStrategy:
6+
"""
7+
This object defines retries for failed tokenization attempts.
8+
:param retry_interval: int
9+
:param retry_amount_variable: int
10+
:param retry_attempt_variable: int
11+
:param last_retry_attempt: int = 10
12+
"""
13+
14+
retry_interval: int
15+
retry_amount_variable: int
16+
retry_attempt_variable: int
17+
last_retry_attempt: int = 10
18+
19+
20+
@dataclass
21+
class ChargeData:
22+
"""
23+
Object containing your charge data
24+
:param currency: str
25+
:param token: str
26+
:param country: str
27+
:param amount: int
28+
:param email: str
29+
:param tx_ref: str
30+
"""
31+
32+
currency: str
33+
token: str
34+
country: str
35+
amount: int
36+
email: str
37+
tx_ref: str
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .tokenized_charge import *
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import os
2+
import requests
3+
import json
4+
from dataclasses import asdict
5+
from python_flutterwave.decorators import handle_api_exceptions
6+
from python_flutterwave.objects import ChargeData, RetryStrategy
7+
8+
auth_token = os.environ.get("FW_SECRET_KEY")
9+
10+
11+
@handle_api_exceptions
12+
def initiate_tokenized_charge(
13+
amount: int, email: str, tx_ref: str, currency: str, token: str
14+
) -> dict:
15+
"""
16+
This endpoint helps you tokenize a customer's card
17+
:param token: str
18+
:param tx_ref: str
19+
:param amount: int
20+
:param email: str
21+
:param currency: str
22+
:return: dict
23+
"""
24+
25+
payload = json.dumps(
26+
{
27+
"tx_ref": f"{tx_ref}",
28+
"amount": f"{amount}",
29+
"email": f"{email}",
30+
"token": f"{token}",
31+
"currency": f"{currency}",
32+
}
33+
)
34+
headers = {
35+
"Authorization": f"Bearer {auth_token}",
36+
"Content-Type": "application/json",
37+
}
38+
39+
response = requests.post(
40+
url="https://api.flutterwave.com/v3/tokenized-charges",
41+
headers=headers,
42+
data=payload,
43+
)
44+
45+
return dict(response.json())
46+
47+
48+
@handle_api_exceptions
49+
def initiate_bulk_tokenized_charges(
50+
retry_strategy: RetryStrategy, bulk_data: list[ChargeData]
51+
) -> dict:
52+
"""
53+
Tokenize multiple cards at once
54+
:param retry_strategy: RetryStrategy
55+
:param bulk_data: list[ChargeData]
56+
:return: dict
57+
"""
58+
59+
payload = json.dumps(
60+
{
61+
"retry_strategy": asdict(retry_strategy),
62+
"bulk_data": [asdict(i) for i in bulk_data],
63+
}
64+
)
65+
headers = {
66+
"Authorization": f"Bearer {auth_token}",
67+
"Content-Type": "application/json",
68+
}
69+
70+
response = requests.post(
71+
url="https://api.flutterwave.com/v3/bulk-tokenized-charges",
72+
headers=headers,
73+
data=payload,
74+
)
75+
76+
return dict(response.json())
77+
78+
79+
@handle_api_exceptions
80+
def fetch_bulk_tokenized_charges(bulk_id: int):
81+
"""
82+
This endpoint allows you to get bulk tokenized charges transactions
83+
:param bulk_id: int
84+
"""
85+
86+
headers = {
87+
"Authorization": f"Bearer {auth_token}",
88+
"Content-Type": "application/json",
89+
}
90+
91+
response = requests.get(
92+
url=f"https://api.flutterwave.com/v3/bulk-tokenized-charges/{bulk_id}/transactions",
93+
headers=headers,
94+
)
95+
96+
return dict(response.json())
97+
98+
99+
@handle_api_exceptions
100+
def fetch_bulk_tokenized_charges_status(bulk_id: int):
101+
"""
102+
This endpoint allows you to query the status of a bulk tokenized charge
103+
:param bulk_id: int
104+
"""
105+
106+
headers = {
107+
"Authorization": f"Bearer {auth_token}",
108+
"Content-Type": "application/json",
109+
}
110+
111+
response = requests.get(
112+
url=f"https://api.flutterwave.com/v3/bulk-tokenized-charges/{bulk_id}",
113+
headers=headers,
114+
)
115+
116+
return dict(response.json())
117+
118+
119+
@handle_api_exceptions
120+
def update_card_token(email: str, full_name: str, phone_number: str, token: str):
121+
"""
122+
This endpoints allow developers update the details tied to a customer's card token.
123+
:param email: str
124+
:param phone_number: str
125+
:param full_name: str
126+
:param token: str
127+
"""
128+
129+
payload = json.dumps(
130+
{
131+
"email": f"{email}",
132+
"full_name": f"{full_name}",
133+
"phone_number": f"{phone_number}",
134+
}
135+
)
136+
137+
headers = {
138+
"Authorization": f"Bearer {auth_token}",
139+
"Content-Type": "application/json",
140+
}
141+
142+
response = requests.get(
143+
url=f"https://api.flutterwave.com/v3/tokens/{token}",
144+
headers=headers,
145+
data=payload,
146+
)
147+
148+
return dict(response.json())

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = python-flutterwave
3-
version = 1.0.0
3+
version = 1.0.1
44
author = William Otieno
55
author_email = jimmywilliamotieno@gmail.com
66
description = Python Wrapper for interacting with the Flutterwave Payments API

0 commit comments

Comments
 (0)