Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Examples to examples folder #6

Merged
merged 10 commits into from
Aug 30, 2022
37 changes: 37 additions & 0 deletions examples/initiate_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from chapa import Chapa
import random
import string

def generete_tx_ref(length):
#Generate a transaction reference
tx_ref = string.ascii_lowercase
return ''.join(random.choice(tx_ref) for i in range(length))


def checkout():
data = {
# Required fields
'email': 'user@example.com', # customer/client email address
'amount': 1311.00, # total payment
'first_name': 'Abebe', # customer/client first name
'last_name': 'Bikila', # customer/client last name
'tx_ref': generete_tx_ref(12),

# Optional fields
'callback_url': 'your_callback_url', # after successful payment chapa will redirect your customer/client to this url
'customization': {
'title': 'Example.com',
'description': 'Payment for your services',
}
}

chapa = Chapa('[YOUR_CHAPA_SECRET_KEY]')
response = chapa.initialize(**data)

# After successfull initialization redirect to the `checkout_url`
if response['status'] == 'success':
# do some action
""" Redirect to the checkout page using `response['data']['checkout_url']` """
else:
# If initialization fails display the error message
print(response['message'])
24 changes: 24 additions & 0 deletions examples/verify_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from chapa import Chapa


def verify(transaction_id):
""" Accept the transaction id and verify it """
chapa = Chapa('[YOUR_CHAPA_SECRET_KEY]')
response = chapa.verify(transaction_id)

if response['status'] == 'success':
# do some actions, probably redirect to success page
pass
else:
# do some actions, probably redirect to failed page
pass

"""
If verification response has succeed it looks like
{'message': 'Payment details', 'status': 'success', 'data': {
'first_name': 'Abebe', 'last_name': 'Bikila', 'email': 'user@example.com', 'currency': 'ETB',
'amount': '1,311.00', 'charge': '45.89', 'mode': 'test', 'method': 'test', 'type': 'API',
'status': 'success', 'reference': '[reference]', 'tx_ref': '[tx_ref]',
'customization': {'title': 'Example.com', 'description': 'Payment for your services', 'logo': None},
'meta': None, 'created_at': '2022-08-24T12:29:52.000000Z', 'updated_at': '2022-08-24T12:29:52.000000Z'}}
"""