Welcome folks today in this tutorial we will be integrating instamojo payment gateway in php 7 using instamojo php sdk
. All the source code of the tutorial will be given below. A step by step youtube video is also shown below.
Get Started
In order to get started first of all you need to install this dependency either by composer
or download the library from below source code
`
1 |
$ php composer.phar require instamojo/instamojo-php |
After this make a index.html and copy paste this code to make a simple html form to collect payment
index.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 Integration in PHP</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.php" 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> |
Now we will be making the POST request which is there inside the form. So for that make the pay.php
file inside the root directory of your project and copy paste the following code to it.
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 |
<?php require('./instamojo.php'); const API_KEY ="####yourapikey####"; const AUTH_TOKEN = "####yourauthtoken####"; if(isset($_POST['purpose']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['amount'])) { $api = new Instamojo\Instamojo(API_KEY, AUTH_TOKEN,'https://test.instamojo.com/api/1.1/'); try { $response = $api->paymentRequestCreate(array( "purpose" => $_POST['purpose'], "buyer_name" => $_POST['name'], "amount" => $_POST['amount'], "send_email" => true, "email" => $_POST['email'], "redirect_url" => "http://localhost/instamojopayment/success.html" )); header('Location:'. $response['longurl']); } catch (Exception $e) { print('Error: ' . $e->getMessage()); } } ?> |
So now if you run this application and after submitting the html form it should redirect to this payment page of instamojo
DOWNLOAD SOURCE CODE