使用curl PHP整合Razorpayx

时间:2019-03-25 06:27:24

标签: php api curl razorpay

Razorpayx卷曲代码:

curl -u <YOUR_KEY>:<YOUR_SECRET> \-X POST https://api.razorpay.com/v1/contacts \-H "Content-Type: application/json" \-d '{  "name": "Gaurav Kumar",  "email": "gaurav.kumar@example.com",  "contact": "9123456789",  "type": "employee",  "reference_id": "Acme Contact ID 12345",  "notes": {    "note_key": "Beam me up Scotty"  }}'

尝试从curl-php实现相同的功能

$ch = curl_init();

$curlConfig = array(
    CURLOPT_URL            => "https://api.razorpay.com/v1/contacts/",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => array(
        'api-key' => '<KEY>:<SECRET-KEY>'

        ),
    CURLOPT_POSTFIELDS     => array(
        'name' => 'ABCD',
        'email' => "abcd@gmail.com",
        'type' => 'customer'
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

现在,出现错误“请提供您的api密钥以进行身份​​验证”。 我已经通过curl标头传递了密钥;但是会引发错误。请指导如何解决此问题。

3 个答案:

答案 0 :(得分:1)

我正在与我的电子商务平台集成,如果是银行转帐和以下代码,则必须向用户触发一个链接,它很有魅力

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.razorpay.com/v1/payment_links/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n  \"upi_link\": \"true\",\n  \"amount\": 100,\n  \"currency\": \"INR\",\n  \"reference_id\": \"#456\",\n  \"description\": \"Payment for policy no #23456\",\n  \"customer\": {\n    \"name\": \"Gaurav Kumar\",\n    \"contact\": \"+919999999999\",\n    \"email\": \"null\"\n  },\n  \"expire_by\": \"1526282829\",\n  \"notify\": {\n    \"sms\": true\n  },\n  \"reminder_enable\": true,\n  \"notes\": {\n    \"policy_name\": \"Jeevan Bima\"\n  }\n}");
curl_setopt($ch, CURLOPT_USERPWD, '[YOUR_KEY_ID]' . ':' . '[YOUR_KEY_SECRET]');

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

下面的 JSON 应该可以传递你自己的键和值

{
  "amount": 1000,
  "currency": "INR",
  "accept_partial": true,
  "first_min_partial_amount": 100,
  "expire_by": 1691097057,
  "reference_id": "TS1989",
  "description": "Payment for policy no #23456",
  "customer": {
    "name": "Gaurav Kumar",
    "contact": "+919999999999",
    "email": "gaurav.kumar@example.com"
  },
  "notify": {
    "sms": true,
    "email": true
  },
  "reminder_enable": true,
  "notes": {
    "policy_name": "Jeevan Bima"
  },
  "callback_url": "https://example-callback-url.com/",
  "callback_method": "get"
}

enter code here

参考 - https://razorpay.com/docs/payment-links/api/new/create/standard/

答案 1 :(得分:0)

我有同样的问题。基本上,您的api-key不会进入标头,而是您必须将其作为用户密码发送。下面的代码运行正常。

    $ch = curl_init();
    $fields = array();
    $fields["name"] = $name;
    $fields["email"] = $email;
    $fields["contact"] = $phone;
    $fields["reference_id"] = "customer".$phone;
    $fields["type"] = "customer";
    curl_setopt($ch, CURLOPT_URL, 'https://api.razorpay.com/v1/contacts');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "api-key: key-secret");
    $headers = array();
    $headers[] = 'Accept: application/json';
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $data = curl_exec($ch);

    if (empty($data) OR (curl_getinfo($ch, CURLINFO_HTTP_CODE != 200))) {
       $data = FALSE;
    } else {
        return json_decode($data, TRUE);
    }
    curl_close($ch);

答案 2 :(得分:0)

下面为我工作

$data =http_build_query(
      json_decode('{
    "account_number": "2323230009571676",
    "fund_account_id": "fa_GHf8DVjqbYtAXH",
    "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."
    }
    }',true)
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://api.razorpay.com/v1/payouts");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, env('RZP_API_KEY') . ':' . env('RZP_API_SECRETES'));
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec($ch);
    echo $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);