Instamojo Payment

  • Account

    Sanbox

    Create Client ID & Client Secret

    1. go to https://test.instamojo.com/integrations

    2. create new credentials (scroll down the screen > Generate Credentials) Test account

    
                                Card Number: 4242 4242 4242 4242
    
                                Expiry: 01/25
    
                                CVV: 111
                                
  • Coding

    Generate token

    
    
                           
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.instamojo.com/oauth2/token/');     
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    
    $payload = Array(
        'grant_type' => 'client_credentials',
        'client_id' => '21fe14a60057ece6c76496175cb0238a2ffd87be',
        'client_secret' => '-KWPoDO!_l?MZQ-DsZeQ0TXXcvuDn;mxmAtGho8Xijn=iH6F'
      );
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    $response = curl_exec($ch);
    curl_close($ch); 
    
    echo $response;
    
    
    
    

    Create payment

    
    
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'https://api.instamojo.com/v2/payment_requests/');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Authorization: Bearer y70kak2K0Rg7J4PAL8sdW0MutnGJEl'));
    
    $payload = Array(
      'purpose' => 'FIFA 16',
      'amount' => '2500',
      'buyer_name' => 'John Doe',
      'email' => 'foo@example.com',
      'phone' => '9999999999',
      'redirect_url' => 'http://www.example.com/redirect/',
      'send_email' => 'True',
      'send_sms' => 'True',
      'webhook' => 'http://www.example.com/webhook/',
      'allow_repeated_payments' => 'False',
    }
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    $response = curl_exec($ch);
    curl_close($ch); 
    
    
    
    

    Get payment request details

    
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.instamojo.com/v2/payment_requests/9ef9b530ff8e440dbbc29f2157bc69ad/');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Authorization: Bearer y70kak2K0Rg7J4PAL8sdW0MutnGJEl'));
    
    $response = curl_exec($ch);
    curl_close($ch); 
    
    
    
    
  • Live Example

    Request

    
                        //https://api.instamojo.com/
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/oauth2/token/');     
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    
    $payload = Array(
        'grant_type' => 'client_credentials',
        'client_id' => 'test_xBcatAF0xvY03uA9CuPA6dgp5QNJkHaVSZ7',
        'client_secret' => 'test_U6jDzhDsVPAYkgaUWGLSA6Gey48JIC61Rd7Lb8kTcH992MSROfEOfHIMFp3LfsWoxB2Nn1is1QDifLhO25TnuCZYxe7jLpX9Lqp5EFBpevI6ECofK96c0KnDFJr'
      );
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    $response = curl_exec($ch);
    curl_close($ch); 
    
    
    
    $response=json_decode($response,1);
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/v2/payment_requests/');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Authorization: Bearer '.$response['access_token']));
    
    $payload = Array(
        'order_id'=>1,
      'purpose' => 'FIFA 16',
      'amount' => '10',
      'buyer_name' => 'John Doe',
      'email' => 'foo@example.com',
      'phone' => '9999999999',
      'redirect_url' => 'http://localhost/workshop/mycooks/success.php?order_id=1',
      'send_email' => 'True',
      'send_sms' => 'True',
      'webhook' => 'http://www.example.com/webhook/',
      'allow_repeated_payments' => 'False'
    );
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    $response = curl_exec($ch);
    curl_close($ch); 
    
    print_r($response);
    $response=json_decode($response,1);
    $pay_ulr = $response['longurl'];
      
    
    
    header("Location: $pay_ulr");
    
    

    Validate Response

    
    
    print_r($_REQUEST);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/oauth2/token/');     
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    
    $payload = Array(
        'grant_type' => 'client_credentials',
        'client_id' => 'test_xBcatAF0xvY03uA9CuPA6dgp5QNJkHaVSZ7',
        'client_secret' => 'test_U6jDzhDsVPAYkgaUWGLSA6Gey48JIC61Rd7Lb8kTcH992MSROfEOfHIMFp3LfsWoxB2Nn1is1QDifLhO25TnuCZYxe7jLpX9Lqp5EFBpevI6ECofK96c0KnDFJr'
      );
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    $response = curl_exec($ch);
    curl_close($ch); 
    
    
    
    $response=json_decode($response,1);
    
    $ch = curl_init();
    
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/v2/payment_requests/'.$_REQUEST['payment_request_id']);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Authorization: Bearer '.$response['access_token']));
    
    $response = curl_exec($ch);
    print_r($response);
    curl_close($ch);