2

I have used php curl to call an api, I am stuck with this api request format I am not able to process further, below is the same request example.

curl -u <YOUR_KEY>:<YOUR_SECRET> \
-X POST https://api.razorpay.com/v1/payouts \
-H "Content-Type: application/json" \
-d '{
  "account_number": "7878780080316316",
  "fund_account_id": "fa_00000000000001",
  "amount": 1000000,
  "currency": "INR",
  "mode": "IMPS",
  "purpose": "refund",
  "queue_if_low_balance": true,
  "reference_id": "Acme Transaction ID 12345",
  "narration": "Acme Corp Fund Transfer",
  "notes": {
    "notes_key_1":"Tea, Earl Grey, Hot",
    "notes_key_2":"Tea, Earl Grey… decaf."
  }
}'

How to call this api using laravel/PHP ?

  • https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code refer this question – Rudra Mar 07 '20 at 12:04

3 Answers3

0

You can directly use PHP in-built cURL extension for the same

$username='YOUR_KEY';
$password='YOUR_SECRET';

 // set post fields
    $post = [
        'account_number' => '7878780080316316',
         'fund_account_id'=> 'fa_00000000000001',
         'amount'=> 1000000,
         'currency'=> 'INR',
         'mode'=> 'IMPS',
         'purpose'=> 'refund',
         'queue_if_low_balance'=> true,
         'reference_id'=> 'Acme Transaction ID 12345',
         'narration'=> 'Acme Corp Fund Transfer',
         'notes' =>[
                 'notes_key_1'=>'Tea, Earl Grey, Hot',
                 'notes_key_2'=>'Tea, Earl Grey… decaf.',
          ],
    ];

$headers = [
    'Content-Type: application/json',
];

$ch = curl_init("https://api.razorpay.com/v1/payouts ");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// execute!
$response = curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
var_dump($response);

References:

https://www.php.net/manual/en/book.curl.php

Sehdev
  • 4,779
  • 3
  • 8
  • 28
  • using the above code I am getting errors: string(75) "{"error":{"code":"BAD_REQUEST_ERROR","description":"No db records found."}}" . I don't understand what is this db records – Web Developer Mar 09 '20 at 05:27
  • check this https://razorpay.com/docs/api/payment-links/#error-format – Sehdev Mar 09 '20 at 05:39
  • This error is Given on the side is the API response format in case of errors such as form validation error, bad request errors, etc. – Sehdev Mar 09 '20 at 05:40
0

You can use this code to post json on api:

$url = 'https://api.razorpay.com/v1/payouts';
$ch = curl_init($url);
$data = array(
    'username' => 'codexworld',
    'password' => '123456'
);
$payload = '{
  "account_number": "7878780080316316",
  "fund_account_id": "fa_00000000000001",
  "amount": 1000000,
  "currency": "INR",
  "mode": "IMPS",
  "purpose": "refund",
  "queue_if_low_balance": true,
  "reference_id": "Acme Transaction ID 12345",
  "narration": "Acme Corp Fund Transfer",
  "notes": {
    "notes_key_1":"Tea, Earl Grey, Hot",
    "notes_key_2":"Tea, Earl Grey… decaf."
  }
}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Malkhazi Dartsmelidze
  • 3,341
  • 4
  • 10
  • 35
0

Guzzle is a PHP package which provides a clean and easy to work with interface for performing HTTP requests. You might consider using Guzzle instead of curl. http://docs.guzzlephp.org/en/stable/

You would first need to include it in your project:

composer require guzzlehttp/guzzle

And could then write code along these lines:

$client = new \GuzzleHttp\Client();

$authentiation = [
                    '<YOUR_KEY>', 
                    '<YOUR_SECRET>'
    ];

    $postData = [
     'account_number'=> '7878780080316316',
      'fund_account_id'=> 'fa_00000000000001',
      'amount'=> 1000000,
      'currency'=> 'INR',
      'mode'=> 'IMPS',
      'purpose'=> 'refund',
      'queue_if_low_balance'=> true,
      'reference_id'=> 'Acme Transaction ID 12345',
      'narration'=> 'Acme Corp Fund Transfer',
      'notes' =>[
        'notes_key_1'=>'Tea, Earl Grey, Hot',
        'notes_key_2'=>'Tea, Earl Grey… decaf.',
      ],
    ];

    $response = $client->request('POST', 
        'https://api.razorpay.com/v1/payouts', [
             'auth' => $authentication, 
             'form_params' => postData,
        ],
    ]);
James Clark Developer
  • 1,139
  • 2
  • 8
  • 12