I get response after successful android paypal payment. The response was below.
{
"response": {
"state": "approved",
"id": "PAY-6PU626847B294842SKPEWXHY",
"create_time": "2014-07-18T18:46:55Z",
"intent": "sale"
},
"client": {
"platform": "Android",
"paypal_sdk_version": "2.11.0",
"product_name": "PayPal-Android-SDK",
"environment": "mock"
},
"response_type": "payment"
}
{
"short_description": "Pay List Payment",
"amount": "100",
"intent": "sale",
"currency_code": "USD"
}
then I use the PAY-6PU626847B294842SKPEWXHY pay key and get the transaction details using PHP curl GET request with the following code.
$accessToken='<Access Token>';
$curl = curl_init("https://api.sandbox.paypal.com/v1/payments/payment/PAY-6PU626847B294842SKPEWXHY");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
print_r($response);
exit;
After hit this i get the following response not get the transaction detail.
{"name":"INVALID_RESOURCE_ID","message":"The requested resource ID was not found","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID","debug_id":"ddc1e3a55f10e"}
anybody have idea thanks in advance.
The following works for me.
1. MAKE CURL POST TO GET ACCESS TOKEN
$uri = 'https://api.sandbox.paypal.com/v1/oauth2/token';
//for live production use $uri = 'https://api.paypal.com/v1/oauth2/token';
$clientId = '<YOUR-CLIENT-ID-HERE>';
$secret = '<YOUR-SECRET-HERE>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
$access_token = '';
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
$access_token = $json->access_token;
}
curl_close($ch);
2. MAKE CURL POST TO GET TRANSACTION DETAILS LIKE payment status,amount, date etc.
$url = "https://api.sandbox.paypal.com/v2/payments/captures/<YOUR-PAYMENT-ID-HERE>";
$accessToken=$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
print_r($response);
exit;
3. MAKE CURL POST TO GET MORE TRANSACTION DETAILS (This give more detailed response than step 2. so you can us this instead of step 2
$url = "https://api.sandbox.paypal.com/v2/checkout/orders/<YOUR-order-ID-HERE>";
$accessToken=$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
print_r($response);
exit;
Related
I am getting an error for the following PHP code:
$curl = curl_init("https://api.openai.com/v1/engines/davinci/completions");
$data = array(
'prompt' => 'how many sundays in 2023',
'max_tokens' => 256,
'temperature' => 0.7,
'model' => 'text-davinci-003'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer sk-MY-API-KEY']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result);
print $result->choices[0]->text;
I correctly provided the API Key, but getting this error:
Error message: You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY)
All Engines endpoints are deprecated.
This is the correct Completions endpoint:
https://api.openai.com/v1/completions
Working example
If you run php test.php in CMD, the OpenAI API will return the following completion:
string(23) "
This is indeed a test"
test.php
<?php
$ch = curl_init();
$url = 'https://api.openai.com/v1/completions';
$api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';
$post_fields = '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}';
$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result);
var_dump($response->choices[0]->text);
?>
I'm trying to fetch Subscription details by its Subscription ID, but i'm unable to get it. It raised this error: {"name":"INVALID_RESOURCE_ID","message":"INVALID_RESOURCE_ID","information_link":"https://developer.paypal.com/docs/api/orders/v1/#error-INVALID_RESOURCE_ID","debug_id":"9fc43dc5ea630"}
Below is my code:
$accessToken='A21AAEk8KaihyonOUmmnSsx_2MLCXocZIMHjRxJreQtQ8Tck7fpxC_Q17u-OPRlnAdYWHRRwbiz-hLfBlbOJj_7SwOYVDoOaw';
$curl = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
$result = json_decode($response);
print_r($response);
[![Error Screenshot][1]][1]
[1]: https://i.stack.imgur.com/xcvlW.png
What I am trying to do is I would like to verify transaction ID of Paypal payments. But paypal sends me back a:
{"name":"INVALID_RESOURCE_ID","message":"Requested resource ID was not found.","information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments\/#errors"}
my current code right now is this:
$curl = curl_init("https://api.sandbox.paypal.com/v1/payments/payment/<transaction_id>");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . '<changed this to my access token>',
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
$result = json_decode($response);
You're using the wrong endpoint of Paypal API. Based on your description of what you want to do, you should be using /v1/checkout/orders/ as per the documentation because it gives you all the details of a certain transaction.
Do it like this:
$curl = curl_init("https://api.sandbox.paypal.com/v1/checkout/orders/<transaction_id>");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . '<access_token>',
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
$result = json_decode($response);
Source: https://developer.paypal.com/docs/api/orders/v1/
Error
{"message":"Unexpected token '","body":"{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}"}
Code
$data = "{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}";
$url = "https://api.godaddy.com/v1/domains/{domain}/records";
$headers = array(
'Content-Type: application/json',
'Accept : application/json',
'Authorization : sso-key {key}:{token}' );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
print $response ;
exit;
The issue is solved by the help of #Slaiv206 And below the working code.
$data = '[{ "type":"A", "name":"tarunDhiman", "data":"255.255.255.0", "ttl":3600 }]';
$url = "https://api.godaddy.com/v1/domains/{domain}/records";
$headers = array(
'Content-Type: application/json',
'Accept : application/json',
'Authorization : sso-key {key}:{secret}' );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
print $response ;
exit;
I think the header Authorization string is:
Authorization: sso-key {KEY}:{SECRET}
and not:
Authorization : sso-key {key}:{token}
and in the json string use double quotes instead single quotes:
'{ "type":"A", "name":"tarunDhiman", "data":"166.62.81.221", "ttl":3600 }'
i would like to post json data to remote url, and the url will return json format data. here is my code:
$post_array=Array(
"trips"=>array(
"departure_code"=> "SIN",
"arrival_code"=> "HKT",
"outbound_date"=>"2014-02-29",
"inbound_date"=> "2014-03-05"
),
"adults_count"=> 1
);
$content = json_encode($post_array);
$curl = curl_init($search_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
//$response = json_decode($result);
var_dump($result);
curl_close($curl);
The results should be:
{
"id": "pNQFapkhRQ6ZYxql4MQDbQ",
"key": "[SIN:HKT:2014-01-29:2014-02-05]~1~0~XX~FI",
"trips": [
{
"id": "SIN:HKT:2014-01-29:2014-02-05",
"departure_code": "SIN",
"departure_name": "Singapore",
"trip_type": "standard"
}
],
"cabin": "economy",
"adults_count": 1,
}
I got string(35) "{"message":"Problems parsing JSON"}" error. Thanks for help.
what is the $search_url ?
Do you have any documentation for this service ?
Try to change this line:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
to:
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
array(
"Content-type: application/json",
"Content-Length: ".strlen($content)
)
);
Here I'm showing you a simple demo of the cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length:'.strlen($data_string)
));
$json_response = curl_exec($curl);
$curl_errorno = curl_errno($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);