I have a problem with payment integrationwith PayPal.
I am using REST API and this my code for creating an order:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v2/checkout/orders",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{
"intent": "CAPTURE",
"purchase_units": [
{
"reference_id": "PUHF",
"amount": {
"currency_code": "PLN",
"value": "100.00"
}
}
],
"application_context": {
"return_url": "http://www.mywebside.com",
"cancel_url": ""
}
}',
CURLOPT_HTTPHEADER => array(
'accept: application/json',
'accept-language: en_US',
'authorization: Bearer '.$access_token.'',
'content-type: application/json'
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I work in a sandbox environment. I go to the payment page and transfer virtual money.
When it redirects me to my site, then I check the order status. Status has value = "APPROVED" not "COMPLETED" and money is also not credited to the account. What it depends on?
You need two API calls, one to 'Set Up Transaction' and create the order, followed by one to 'Capture Transaction' after the approval, as documented here:
https://developer.paypal.com/docs/checkout/reference/server-integration/
If you do not capture an order, it will stay in an approved state.
For the best user experience, do not use any redirects. At all. Keep your site loaded in the background, and present the user with a modern in-context login for the approval. Here is the UI for that: https://developer.paypal.com/demo/checkout/#/pattern/server
Related
Developed PHP Paypal Integration via REST API.
when creating payment intent to get url to redirect user to Payment gateway it works fine.
I get the redirect url as well.
Example redirect url live mode
https://www.paypal.com/checkoutnow?token=7JR976187U6560045
But when we go to Payment page we can select either to logged in to Paypal account or pay as a guest using credit or debit card.
But for the logged in user it shows select the payment source (card) to pay but when we click on proceed or review it always not going to proceed to next step or to thank you page it reload back to same page without showing any error or warning.
This happens in Sandbox mode as well.
When we select pay via Credit card without logging in it it loads the card details entering page but after adding the cart it will not accept the payment and shows card was declined message. Cards has funds. Something happening in Sandbox with test card details.
below is sample code used for generate payment intent.
//first get the access token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic " . base64_encode(PAYPAL_ID.":".PAYPAL_SECRET),
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
exit();
}
$responseData = json_decode($response);
$accessToken = $responseData->access_token;
$requestBody = [
'intent' => 'CAPTURE',
'purchase_units' => [[
'amount' => [
'currency_code' => 'EUR',
'value' => $send_total, //cart total
],
]],
'redirect_urls' => [
'return_url' => $thank_you_link,
'cancel_url' => $cart_link,
]
];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.paypal.com/v2/checkout/orders",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($requestBody),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer $accessToken"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
exit();
}
$responseData = json_decode($response);
//var_dump($responseData);
if ($responseData->status !== 'CREATED') {
echo "Order creation failed: " . $responseData->debug_id;
exit();
}
$orderId = $responseData->id;
$_SESSION['paypal_id'] = $orderId;
$approveUrl = '';
foreach ($responseData->links as $link) {
if ($link->rel === 'approve') {
$approveUrl = $link->href;
break;
}
}
if (!$approveUrl) {
echo "Approve URL not found";
exit();
}
$data_back = array();
$data_back['url'] = $approveUrl;
Tried both live mode and sandbox mode.
You are missing the capture step.
When redirecting away from your site to PayPal (not recommended), the order you created must have a return_url so that the payer can be redirected back after approving the payment. Then you need to capture the payment with another API call.
Rather than redirecting away, the best user experience is to pair API order creation and capture with the JS SDK for the approval flow. This keeps your site loaded in the background at all times, and is documented here (the sample there uses Node.js for the backend, but you can of course implement it in any environment including PHP).
There will be no PayPal transaction until you capture the payment. After the capture API call you can then display a message of success or failure. The server route that does the capture API call should also verify the captured amount was correct in the API response before storing the result as a successful payment and doing anything automated with the result.
I am trying to add a payment platform. I am able to make a request succesfully and I am getting a successful response. I want to get the URL from the response and redirect the user to the URL. My research shows I am supposed to add CURLOPT_FOLLOWLOCATION => true to my code which I did, curl_getinfo is showing the my php file path. Below is an initial request and response
Request.
$payload = json_encode(["merchant_id"=>"Your merchant ID", "transaction_id"=>"000000000000",
"desc"=>"Payment Using Checkout Page", "amount"=>"000000000100",
"redirect_url"=>"https://yoursite.com", "email"=>"mail#customer.com"]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://test.theteller.net/checkout/initiate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ".base64_encode('Your API Username:Your API Key')."",
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
$status = $data['status'];
$code = $data['code'];
$redirecturl = $data['checkout_url'];
echo $status;
if ($status == 'success' && $code == 200)
{
header('Location: www.google.com');
exit();
}
}
Response.
{
"status": "success",
"code": 200,
"reason": "Token successfully generated",
"token": "eU1xSFN5Ky92MUt5dmpnT",
"checkout_url": "https://test.theteller.net/checkout/checkout/eU1xSFN5Ky92MUt5dmpnT"
}
This the the redirect I get http://localhost:8080/payment/www.google.com
Question: How do I redirect to checkout_url in the response?
I am creating api function in my one of my cs cart website and where submit the order through api function.
This is for a new windows server, running MySQL 5, PHP 5.6 and IIS. and I am trying, on postman, to validate what's the data has been transfer.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my-testwebsite.com/api.php?_d=orders&ajax_custom=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"user_id\": \"4\",\r\n \"shipping_id\": \"0\",\r\n \"payment_id\": \"6\",\r\n \"products\": {\r\n \"1000323256\": {\r\n \"product_id\": \"1002\",\r\n \"amount\": \"100\"\r\n },\r\n \"103101663\": {\r\n \"product_id\": \"1002\",\r\n \"amount\":\"100\"\r\n }\r\n }\r\n}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic dmluY2VudC5hcm9ja2lhcmFqQGZ1amlmaWxtLmNvbTplUUE3NzdmN2RrSUR0ODVieVoyM1R0NDYxVzMwRGNTMQ==",
"Content-Type: application/json",
"Postman-Token: 050a8bff-d6da-4a0d-b6e8-4cb01bd2c112",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
Please follow CS-Cart Docs > Developer Guide > REST API > Supported Entities > Orders > Create an Order
I have LinkedIn app (Created from here https://www.linkedin.com/developer/apps) with scope of r_basicprofile, r_emailaddress, rw_company_admin, w_share
After completing auth process successfully i got access token from linkedin api and it's working to get authenticated user information.
But while i try to use Rich Media Shares API then it's not working for me
I am getting following error
{
"serviceErrorCode":100,
"message":"Not enough permissions to access media resource",
"status":403
}
Here is the request code sample
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.linkedin.com/media/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-
data; name=\"source\"; filename=\"iphone5.jpeg\"\r\nContent-Type:
image/jpeg\r\n[**image_binary_data**]\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer [**accesstoken**]",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Thanks
You need to be a LinkedIn developer "Partner" in order to access the "media" controller/endpoint:
https://developer.linkedin.com/partner-programs
I wrote a query through 'postman' which looks like this
POST: http://someurl
{
"elasticQuery": //Elastic Query is a user defined field.
{
"_source": [ "field 1", "field 2", "field 3" ],
"query":
{
"match_all":{}
}
},
"index": "indexName",
"scroll": "True"
}
This returns lots of JSON formatted documents from the site with 3 given fields.
I am wondering how I would format this query using PHP to do a curl call and recieve the same documents.
I am not sure if this is even possible but your help will be greatly appreciated.
Thanks.
Try the following:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "YOUR_URL_HERE",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"elasticQuery\":{\"_source\":[\"field 1\",\"field 2\",\"field 3\"],\"query\":{\"match_all\":{}}},\"index\":\"indexName\",\"scroll\":\"True\"}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Remember to replace the CURLOPT_URL string with your own URL. As a further tip, you can get Postman to generate this for you. With the request open in a tab, towards the top right below the "Send" button, there's a small bit of text which says "Code". Select it and you should have the option to generate PHP cURL.