Welcome folks today in this tutorial we will be integrating instamojo payment gateway in python 3 using python sdk and web framework called as flask
. The full source code of the application is given below. A step by step youtube video is also shown below.
Get Started
In order to get started we need to first of all install this dependency which is given below
pip install instamojo-wrapper
After installing this make a app.py
file and copy paste the following code which is shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
from flask import Flask, render_template, request,redirect from instamojo_wrapper import Instamojo API_KEY = "####yourapikey####" AUTH_TOKEN = "####yourauthtoken####" api = Instamojo(api_key=API_KEY,auth_token=AUTH_TOKEN,endpoint='https://test.instamojo.com/api/1.1/') app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') @app.route('/success') def success(): return render_template('success.html') @app.route('/pay',methods=['POST','GET']) def pay(): if request.method == 'POST': name = request.form.get('name') purpose = request.form.get('purpose') email = request.form.get('email') amount = request.form.get('amount') response = api.payment_request_create( amount=amount, purpose=purpose, buyer_name=name, send_email=True, email=email, redirect_url="http://localhost:5000/success" ) return redirect(response['payment_request']['longurl']) else: return redirect('/') if __name__ == '__main__': app.run(debug=True) |
Here you need to replace your own private api key and private auth token
from your instamojo dashboard and copy paste inside this script.
Now we need to create the templates home.html
and success.html
for the home route and success route respectively.
home.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<!DOCTYPE html> <html> <head> <title>Instamojo Payment Integration in Python</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> </head> <body> <div class="container"> <h1 class="text-center">Instamojo Payment</h1> <form action="/pay" method="POST"> <div class="form-group"> <input type="text" class="form-control" required name="name" id="" placeholder="name" /> </div> <div class="form-group"> <input type="text" class="form-control" required name="purpose" id="" placeholder="purpose" /> </div> <div class="form-group"> <input type="email" class="form-control" required name="email" id="" placeholder="email" /> </div> <div class="form-group"> <input type="number" class="form-control" required name="amount" id="" placeholder="amount" /> </div> <div class="form-group"> <button class="btn btn-danger btn-block"> Create Payment Link </button> </div> </form> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </html> |
success.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <title>Instamojo Payment Gateway in Python</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> </head> <body> <div class="container"> <h1 class="text-center"> Your Payment is successful </h1> <p>Please check your confirmation email</p> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </html> |
DOWNLOAD SOURCE CODE