How to return customer info from paypal? - php

I understand this can be done.
Here's what I've tried:
$request = curl_init();
// Set request options
curl_setopt_array($request, array
(
CURLOPT_URL => 'https://www.paypal.com/cgi-bin/webscr',
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query(array
(
'cmd' => '_notify-synch',
'tx' => $tx,
'at' => "xxxxxxxxxxxxxxxxx",
)),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
// CURLOPT_SSL_VERIFYPEER => TRUE,
// CURLOPT_CAINFO => 'cacert.pem',
));
// Execute request and get response and status code
$response = curl_exec($request);
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
var_dump($response);
die();
// Close connection
curl_close($request);
When I send the curl request to https://www.paypal.com/cgi-bin/webscr, I recieve this error:
"FAIL Error: 4003"
When I send it to https://www.sandbox.paypal.com/cgi-bin/webscr, It returns nothing and a status of 0.
My account is sandbox at the moment. I've had this working but it doesn't work anymore.
Can someone please help.
Thanks

Usually, we return customer information only when Paypal calls back the ipn on your website.
You can use:
https://github.com/Quixotix/PHP-PayPal-IPN
For testing, you can use GET to redirect buyers to paypal, something like this:
# redirect to paypal
$redirect = 'https://www'.$addsand.'.paypal.com/cgi-bin/webscr?rm=2&cmd=_xclick&txn_type=express_checkout&no_note=1&no_shipping=1&return='.$return.'&cancel_return='.$cancel_return.'&notify_url='.$notify_url.'&image_url='.$image_url.'&business='.$paypaluser.'&currency_code='.$paypalcurrency.'&amount='.$price.'&mc_gross='.$price.'&item_name='.$product.'&item_number='.$planid.'&custom='.$uid; header("Location: $redirect"); exit();

Related

PayPal Rest API Live and sandbox payment not captured

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.

POST using cURL and x-www-form-urlencoded in PHP returning Access Denied

I have been able to use the Advanced Rest Client Extension for chrome to send POST queries to an specific HTTPS server and I get Status Code: 200 - OK with the same body fields as the ones I used in this code, but when I run the following code I get this response: 403 - Access Denied.
<?php
$postData = array(
'type' => 'credentials',
'id' => 'exampleid',
'secret_key' => 'gsdDe32dKa'
);
// Setup cURL
$ch = curl_init('https://www.mywebsite.com/oauth/token');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
var_dump($response);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData['published'];
?>
I've noticed as well that when I use Advanced Rest Client Extension for chrome and if I set the Content-Type to application/json I have to enter a login and a password that I don't know what are those because even if I enter the id and secret key that I have in the code it returns 401 Unauthorized. So I'm guessing this code that I wrote is not forcing it to the content-type: application/x-www-form-urlencoded, but I'm not sure. Thank you for any help on this issue!
Can you try like that and see if it helps:
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_USERPWD => 'username:password', //Your credentials goes here
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
CURLOPT_POSTFIELDS => http_build_query($postData),
));
I guess the site expect simple authentication on top of the secret_key that you already provided.
Also it is possible to send a Cookie, so just in case it is good idea to store it and use it again in the next Curl calls.

eway direct payment curl code not working

I am trying to charge the customer using eway payment method. I am implementing the procedure through curl and here is the code to the function.
public function testing_direct() {
$url = 'https://api.sandbox.ewaypayments.com/DirectPayment.json'; // PROD
$postData = array(
'Method' => 'TokenPayment',
'TransactionType' => 'Recurring',
'TokenCustomerID' => '918549937032',
'TotalAmount' => '1995',
'InvoiceNumber' => '123',
'InvoiceDescription' => 'testing'
);
$ch = curl_init($url);
$api_authentication = base64_encode('####;****');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json','Authorization:####'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
$responseData = json_decode($response, TRUE);
print_r($responseData);
die;
}
This code doesnot print anything at all...Nothing in the error log either...any ideas that why it is not working??
Thanks
There are a few things that need to be fix to get that script working, particularly around the authentication which is the most immediate cause of a problem.
There needs to be a check of the HTTP status of the request since curl_exec only returns FALSE if there is an error - e.g. a 404 (page not found) or 401 (unauthorized) is still treated as a successful request.
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
die('HTTP error: '.$code);
}
The actual authentication appears to suffer from 2 main issues: there is a semicolon instead of a colon between the username (API key) and password, and the Basic part of the authentication is missing:
$api_authentication = base64_encode('####:****');
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json','Authorization: Basic '.$api_authentication
),
This could be simplified by just using CURLOPT_USERPWD:
CURLOPT_USERPWD => '####:****',
Apart from that, note that the $postData array needs some extra nesting: TokenCustomerID should be a child of Customer and TotalAmount and the Invoice fields should be children of Payment:
$postData = array(
'Method' => 'TokenPayment',
'TransactionType' => 'Recurring',
'Customer' => array(
'TokenCustomerID' => '911058757297',
),
'Payment' => array(
'TotalAmount' => '1900',
'InvoiceNumber' => '123',
'InvoiceDescription' => 'testing',
),
);
Check out the example in the documentation for a complete example request.
Good luck!

SSL Error in payment process using CURL

I'm trying to do the payment gateway integration in php. When i'm doing test mode payment from local payment process is working fine. i have successfully redirected to my payment page.i have used CURL to post the datas to payment gateway server.
But after upload it to server i could not do the payment . I got the following Error.
SSL connect error(35)
My code is as follows.
$request_url= "https://mypaymentserver.com"
$url = $request_url;
$successurl = url::site('payment/textpartnerssuccess', 'http');
$processurl = url::site('payment/textpartnersprocess', 'http');
$failurl = url::site('payment/textpartnersfail', 'http');
//Data bind
$invoiceno = commonfunction::randomkey_generator();
$postData = array(
"url_succesfull" => $successurl,
"url_process" => $processurl,
"url_cancel" => $failurl,
"item_id" => $jobid,
"name" => $jobdetails[0]['job_title'],
"currency" => $this->textpartners_currencycode,
"price" => $amount,
"token" => $invoiceno,
"seller_op_id" => time(),
"shipping_cost" => 0
);
$data = http_build_query($postData, NULL, '&');
// Create a new curl instance
$curl = curl_init();
// Set curl options
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
));
if (($response = curl_exec($curl)) === FALSE)
{
// Get the error code and message
$code = curl_errno($curl);
$error = curl_error($curl);
curl_close($curl);// Close curl
echo $error_msg = 'Payment API request for failed: '.$error.'(' .$code.')'; exit;
Message::error($error_msg);
// Parse the response
parse_str($response, $data);
}
curl_close($curl); // Close curl
// Parse the response
parse_str($response, $data);
Can any one help me? Thanks in advance :)
But after upload it to server i could not do the payment
Error 35 is reported when the client is unable to connect to the SSL server (as a result of a timeout or a protocol error). Check if the server can resolve the name, make an outgoing connection to the named host, and make an outgoing connection across port 443.
Thanks Guys . Finally i have got the solution by adding the following line in curl.
curl_setopt( $ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha' );

Paypal cURL request - ERRORCODE0=81002

I'm trying to do an NVP pay request via cURL to the Paypal servers, but i always end up getting ERRORCODE0=81002, which basically means there's something wrong with my method.
But I just cant seem to find the problem:
//The request parameters
$request_params = array(
'METHOD' => 'PAY',
'VERSION' => '85.0',
'USER' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'PWD' => 'xxxxxxxxxxxxxxxx',
'SIGNATURE' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'CURRENCYCODE' => 'USD',
'RETURNURL' => 'https://localhost',
'CANCELURL' => 'https://localhost'
);
$endpoint = 'https://api-3t.sandbox.paypal.com/nvp?';
//Building the NVP string
$request = http_build_query($request_params);
//cURL settings
$curl_options = array(
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $request,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_TIMEOUT => 30,
);
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
$response = curl_exec($ch);
curl_close($ch);
where did you get this sample from?
I never saw the value "PAY" for "METHOD"
These need to be valid paypal actions. For example most recently I used DoDirectPayment (and I think that is the most used method for payments with paypal pro)
all the operations are available in this table on their site: http://rvyu.com/rcuu
Update: so the main question is: what do you want to do? because I don't think you are making a payment since you are not sending some basic fields (like the amount or card number); after that look for the value that matches the action you need to take

Categories