Flask Instamojo Payment Gateway Integration Using Python 3 SDK Full Tutorial For Beginners 2020

Flask Instamojo Payment Gateway Integration Using Python 3 SDK Full Tutorial For Beginners 2020

 

 

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

 

 

 

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)

 

See also  Python 3 PyGame Tetris Game GUI Script Desktop App Full Project For Beginners

 

 

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

 

 

 

<!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>

 

See also  Python 3 Gmail API Example to Fetch Recently Sent Emails Using Imaplib Library Full Project For Beginners

 

 

success.html

 

 

 

<!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

 

 

 

Leave a Reply