To accept Paystack payments in Flask, create a route that initializes a transaction with the Paystack API using the requests library, redirects the customer to Paystack checkout, and verifies the payment in a callback route before granting value.
pip install flask requests python-dotenv
# app.py
import os
import uuid
import requests as http_requests
from flask import Flask, request, redirect, render_template, jsonify
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
PAYSTACK_SECRET = os.environ.get('PAYSTACK_SECRET_KEY')
BASE_URL = os.environ.get('BASE_URL', 'http://localhost:5000')
@app.route('/pay', methods=['POST'])
def initialize_payment():
email = request.form.get('email')
amount = float(request.form.get('amount', 5000))
reference = 'order_' + uuid.uuid4().hex[:12]
response = http_requests.post(
'https://api.paystack.co/transaction/initialize',
json={
'email': email,
'amount': int(amount * 100),
'reference': reference,
'callback_url': BASE_URL + '/payment/callback',
},
headers={
'Authorization': 'Bearer ' + PAYSTACK_SECRET,
'Content-Type': 'application/json',
},
)
data = response.json()
if data.get('status'):
# TODO: Save order to database
return redirect(data['data']['authorization_url'])
return 'Initialization failed: ' + data.get('message', 'Unknown error'), 400
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
@app.route('/payment/callback')
def payment_callback():
reference = request.args.get('reference') or request.args.get('trxref')
if not reference:
return 'Missing reference', 400
response = http_requests.get(
'https://api.paystack.co/transaction/verify/' + reference,
headers={'Authorization': 'Bearer ' + PAYSTACK_SECRET},
)
data = response.json()
if data.get('status') and data['data']['status'] == 'success':
amount = data['data']['amount'] / 100
currency = data['data']['currency']
# TODO: Verify amount matches database, mark order as paid
return 'Payment successful! ' + currency + ' ' + str(amount)
return 'Payment not successful', 400
If your frontend is a separate SPA (React, Vue, etc.), return JSON instead of redirecting:
@app.route('/api/pay', methods=['POST'])
def api_initialize():
data = request.get_json()
email = data.get('email')
amount = data.get('amount', 5000)
reference = 'order_' + uuid.uuid4().hex[:12]
response = http_requests.post(
'https://api.paystack.co/transaction/initialize',
json={
'email': email,
'amount': int(amount * 100),
'reference': reference,
},
headers={
'Authorization': 'Bearer ' + PAYSTACK_SECRET,
'Content-Type': 'application/json',
},
)
result = response.json()
if result.get('status'):
return jsonify({
'authorization_url': result['data']['authorization_url'],
'access_code': result['data']['access_code'],
'reference': result['data']['reference'],
})
return jsonify({'error': result.get('message')}), 400
@app.route('/api/verify')
def api_verify():
reference = request.args.get('reference')
if not reference:
return jsonify({'error': 'Missing reference'}), 400
response = http_requests.get(
'https://api.paystack.co/transaction/verify/' + reference,
headers={'Authorization': 'Bearer ' + PAYSTACK_SECRET},
)
data = response.json()
if data.get('status') and data['data']['status'] == 'success':
return jsonify({
'success': True,
'amount': data['data']['amount'] / 100,
'currency': data['data']['currency'],
})
return jsonify({'success': False}), 400
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course