Reputation: 1738
I am trying to integrate my application with a payment gateway.
Their API is based on REST API and uses JSON to transport data. They have this sample code
$ curl -X POST -H "Content-Type: application/json" \
-u APIKEYEXAMPLEAPIKEYEXAMPLE:APIKEYEXAMPLEAPIKEYEXAMPLE \
https://api.netbanx.com/hosted/v1/orders \
-d '{
"merchantRefNum" : "ABCDE12345",
"currencyCode" : "USD",
"totalAmount" : 1234
}'
And I am trying to use this to develop a php code for the same. So far my code is like this, I took in the answer provided below and modified a bit and here is my code.
<?php
//API Url
$url = 'https://api.netbanx.com/hosted/v1/orders';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = array(
'merchantRefNum' => '89983483',
'currencyCode' => 'USD',
'totalAmount' => '1234'
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Set the header authorization
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'Authorization: Basic ZGV2Y2VudHJlNDYxNzpCLXFhMi0wLTU0OGEwM2RkLTMwMmQwMjE1MDA4M2VhZTUwYjQxYzQ0ZDIwMTE4OGM3ZmY4Yjc0ZDIxMzUwY2NiMjdiMDIxNDYwMDc4MGFlMTRkM2E2MTEzY2RmMmJkODc5MjJmZjU3MWQwMGY0ZWU=');
//Execute the request
$result = curl_exec($ch);
?>
My testing API Key devcentre4617:B-qa2-0-548a03dd-302d02150083eae50b41c44d201188c7ff8b74d21350ccb27b0214600780ae14d3a6113cdf2bd87922ff571d00f4ee
As told in their docs, I converted the API Key to base64 added the word Basic
gave a whitespace and got the HTTP Authorizatoin
code as Basic ZGV2Y2VudHJlNDYxNzpCLXFhMi0wLTU0OGEwM2RkLTMwMmQwMjE1MDA4M2VhZTUwYjQxYzQ0ZDIwMTE4OGM3ZmY4Yjc0ZDIxMzUwY2NiMjdiMDIxNDYwMDc4MGFlMTRkM2E2MTEzY2RmMmJkODc5MjJmZjU3MWQwMGY0ZWU=
On execution I keep getting
{"error":{"code":401,"message":"Not authorised"}}
Is something wrong with my code? Or is the issues elsewhere?
UPDATE!!!
This is the working code, the issue was with the API DOCs the testing api is https://api.test.netbanx.com/hosted/v1/orders
<?php
//API Url
$url = 'https://api.test.netbanx.com/hosted/v1/orders';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = array(
'merchantRefNum' => '89983483',
'currencyCode' => 'USD',
'totalAmount' => '1234'
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json and HTTP Authorization code
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("devcentre4617:B-qa2-0-548a03dd-302d02150083eae50b41c44d201188c7ff8b74d21350ccb27b0214600780ae14d3a6113cdf2bd87922ff571d00f4ee") //Base 64 encoding and appending Authorization: Basic
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//Execute the request
$result = curl_exec($ch);
?>
Upvotes: 2
Views: 845
Reputation: 1622
You need to add your username and password like this:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, 'APIKEYEXAMPLEAPIKEYEXAMPLE:APIKEYEXAMPLEAPIKEYEXAMPLE');
Good luck :)
Upvotes: 2