Api payment gateway integration PHP - php

My boss received a payment API ( it allows customer to pay in many instalments ) and asked me to integrate it to my website
But I'm a developper beginner and I don't really know where to start.
If I understood correctly, I need to get access token first.
The Authorization type is : OAuth 2.0
Header Prefix : Bearer
Grant Type : Client Credientials
Scope : Resource.WRITE, resource.READ
Authorization data to : Request Header
How can I get the access token and refresh it ?
I tried something like this : ( but it isn't working )
<?php
$clientId = 'myclientid';
$clientSecret = 'myclientsecret';
function getToken(){
$curl = curl_init();
$params = [
CURLOPT_URL => 'https://test.auth.alphacredit.be/api/oauth/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"accept: */*",
"accept-encoding: gzip, deflate",
'Authorization: Bearer',
),
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
'scope' => 'read, write',))
];
curl_setopt_array($curl, $params);
$response = curl_exec($curl);
curl_close($curl);
}

Related

INVALID_REQUEST_CONTENT for walmart API

I'm trying to make a request to the Walmart API (Item Associations) to retrieve shippingTemplate and shipNode per items. I'm following this guide.
https://developer.walmart.com/api/us/mp/items#operation/getItemAssociations
I set the required field to CURLOPT_POSTFIELDS and I encountered this error.
Result from Walmart API
This is my code:
`
$url_returns ="https://marketplace.walmartapis.com/v3/items/associations";
$ch_returns = curl_init();
$qos_returns = uniqid();
$fields = ['items' => ['123456','778990'] ]; // note: not the real data.
$options_returns = array(
CURLOPT_URL => $url_returns,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
"WM_SVC.NAME: Walmart Marketplace",
"WM_QOS.CORRELATION_ID: $qos_returns",
"Authorization: Basic $authorization_key",
"WM_SEC.ACCESS_TOKEN:$token",
"Accept: application/json",
),
CURLOPT_POSTFIELDS => json_encode($fields),
);
curl_setopt_array($ch_returns, $options_returns);
$response_returns = curl_exec($ch_returns);
$code_returns = curl_getinfo($ch_returns, CURLINFO_HTTP_CODE);
curl_close($ch_returns);
$response_returns = json_decode($response_returns,true);`
Am I missing something?
Thank you in advance.
Looks like the list of items whose associations you are trying to fetch are invalid.
Also, could you please double check WM_SVC.NAME header param value
Regards,
Firdos
IOSupport

extract access taken variable from responds from server

I am busy sending a HTTP curl request for request token , When i send the below code to get access token , when i receive the responds i would like the access token value and not entire responds.
$params = array(
'username' => 'nuser',
'password' => 'password',
);
$curlSecondHandler = curl_init();
curl_setopt_array($curlSecondHandler, [
CURLOPT_URL => 'https://localhost/api/v3/authentication/usernamepassword',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: multipart/form-data',
'Authorization: Bearer '
],
CURLOPT_POSTFIELDS => $params,
]);
$response2 = curl_exec($curlSecondHandler);
curl_close($curlSecondHandler);
$arr = json_decode($response2);
echo $response2->data -> accessToken ;
I get this error
Trying to get property of non-object

Missing parameters when requesting OAUTH token survey monkey v3

I'm trying to obtain my "long lived access token" using CURL/PHP but I'm receiving the error "Missing parameters for client_id, client_secret, code, grant_type, redirect_uri".
The URL I'm calling is where you can clearly see the parameters I'm trying to pass in!
https://api.surveymonkey.net/oauth/token?client_secret='.urlencode($client_secret).'&code='.urlencode($short_token).'&redirect_uri='.urlencode($redirect_url).'&client_id='.urlencode($client_id).'&grant_type=authorization_code
I'm also using the content-type of "application/x-www-form-urlencoded" as per the docs (see below).
My CURL request:
function survey_monkey_curl_request($url, $params=[], $request_type = 'get', $access_token) {
print_r($url);
$ch = curl_init();
$headers = [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: bearer " .$access_token
];
$opts = [
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
];
if ($request_type == 'post') {
$opts[CURLOPT_POST] = 1;
//$opts[CURLOPT_POSTFIELDS] = json_encode($params);
}
if ($request_type == 'patch') {
$opts[CURLOPT_CUSTOMREQUEST] = "PATCH";
$opts[CURLOPT_POSTFIELDS] = json_encode($params);
}
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
if ($result === false) {
curl_close($ch);
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $result;
}
Where am I going wrong?
Straight from the documentation it looks like to get the long-lived token you need to post your fields:
//Exchange for long-lived token
curl -i -X POST https://api.surveymonkey.net/oauth/token -d \
"client_secret=YOUR_CLIENT_SECRET \
&code=AUTH_CODE \
&redirect_uri=YOUR_REDIRECT_URI \
&client_id=YOUR_CLIENT_ID \
&grant_type=authorization_code"
https://developer.surveymonkey.com/api/v3/?shell#new-authentication
When you append your parameters onto your url you are sending then as GET request paramters
You need to put your data string into CURL POSTFIELDS and do not json encode
The PHP Answer
<?php
$ch = curl_init();
$data = [
'client_secret' => $YOUR_CLIENT_SECRET,
'code' => $AUTH_CODE,
'redirect_url' => $YOUR_REDIRECT_URI,
'client_id' => $YOUR_CLIENT_ID,
'grant_type' => 'authorization_code'
];//set your data as an array
$headers = [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: bearer " . $access_token
];
$opts = [
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
];
if ($request_type == 'post') {
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = http_build_query($data);// this will build your data string from the array
}
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Fitbit Web API error grant type

following is the error reported by api
{"errors":[
{
"errorType": "invalid_request",
"message": "Missing 'grant_type' parameter value."
}
], "success": false}
//curl request to fetch token with use of auth code used in my code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.fitbit.com/oauth2/token',
CURLOPT_HTTPHEADER => array(
'Authorization:Basic'.base64_encode(FITBIT_CLIENT_ID.':'.FITBIT_CLIENT_SECRET),
'Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'code' => $auth_code,
'client_id' => FITBIT_CLIENT_ID,
'grant_type' => "authorization_code", //auth code received in url params
'redirect_uri' => 'https://www.example.com/auth/fitbit/success'
)
));
$resp = curl_exec($curl);
curl_close($curl);
i am receiving this error. plz help to identify where could be the error.
Wrap your post field's array with to make sure you are doing the encoding (in case special characters).
CURLOPT_POSTFIELDS => http_build_query (array(
...
))
Try putting a space between Authorization header. Standard one to use Authorization: Basic
If still seeing error then use VERBOSE mode with your curl request. And update your question with that verbose output so that we can help you.

How to post a status update to Sina Weibo via OAuth2?

I'm trying to post a status update to the Chinese microblogging website Sina Weibo, via PHP/cURL and OAuth2.
I'm running into this error:
{"error":"auth
faild!","error_code":21301,"request":"/2/statuses/update.json"}
My PHP:
<?php
$ch = curl_init('https://api.weibo.com/2/statuses/update.json');
$headers = array(
'Authorization: Bearer '.$access_token,
'Content-Type: application/x-www-form-urlencoded');
$postData = array('access_token' => '2.00x123456789', 'status' => 'hello');
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
));
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
I authorized the app with the OAuth2 scope all and the token is valid.
What could be the reason for the error?
Remove your http header
'Content-Type: application/x-www-form-urlencoded'
Replace post fields with:
CURLOPT_POSTFIELDS => http_build_query($postData)
And now Be Happy!

Categories