Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration Of Razorpayx using curl php

Tags:

php

curl

razorpay

Razorpayx curl code :

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

Trying to implement the same from 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' => "[email protected]",
        'type' => 'customer'
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

Now it is given the error that "Please provide your api key for authentication purposes." I have already passed the keys via curl header; but it throws error. Please guide how to solve this issue.

like image 516
Sachin Avatar asked Nov 21 '25 10:11

Sachin


1 Answers

I had same problem. Basicaly your api-key wont go in header instead you have to send it as user password .The code below is working perfectly.

    $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);
like image 154
Mak Sr Avatar answered Nov 22 '25 23:11

Mak Sr