Paypal express checkout integration with PayPal REST API - php

I'm trying to integrate Paypal express checkout integration with PayPal REST API.
JS file looks like this:
paypal.Button.render({
env: 'sandbox', // sandbox | production
commit: true,
payment: function() {
var CREATE_URL = 'createpayment';
return paypal.request.get(CREATE_URL)
.then(function(res) {
return res.paymentID;
});
},
onAuthorize: function(data, actions) {
var EXECUTE_URL = 'executepayment';
var data = {
paymentID: data.paymentID,
payerID: data.payerID
};
return paypal.request.post(EXECUTE_URL, data)
.then(function (res) {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
Now I can't figure out what should be in CREATE_URL and EXECUTE_URL url's php files. I have read a lot of their docs. I understand that in php I should make first request to get token, then I should make create-payment request and after execute-payment curl reuqest. But I have no-luck.
I also don't understand how should I pass payment value to CREATE_URL php file.
Several days ago I found here some good answer of my question with all examples, but it's about two days I can't find it.
Update: ALso I need to pass input amount to php file from payment request. And I can't do that using return paypal.request.get(CREATE_URL, data)
request.
Can anybody help me please.
Thank you.

Share some examples for server PHP code.
PHP Code in CREATE_URL:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'USER' => 'xxxxx',
'PWD' => 'xxxxx',
'SIGNATURE' => 'xxxxx',
'METHOD' => 'SetExpressCheckout',
'VERSION' => '108',
'LOCALECODE' => 'en_US',
'PAYMENTREQUEST_0_AMT' => 100,
'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
'PAYMENTREQUEST_0_ITEMAMT' => 100,
'L_PAYMENTREQUEST_0_NAME0' => 'test name',
'L_PAYMENTREQUEST_0_DESC0' => 'test desc',
'L_PAYMENTREQUEST_0_QTY0' => 1,
'L_PAYMENTREQUEST_0_AMT0' => 100,
'CANCELURL' => 'http://localhost/cancel.html',
'RETURNURL' => 'http://localhost/success.html'
)));
$response = curl_exec($curl);
curl_close($curl);
$nvp = array();
if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
foreach ($matches['name'] as $offset => $name) {
$nvp[$name] = urldecode($matches['value'][$offset]);
}
}
if (isset($nvp['ACK']) && $nvp['ACK'] == 'Success') {
echo $nvp['TOKEN'];
} else {
//failed
}
?>
PHP Code in EXECUTE_URL:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'USER' => 'xxxxx',
'PWD' => 'xxxxx',
'SIGNATURE' => 'xxxxx',
'METHOD' => 'DoExpressCheckoutPayment',
'VERSION' => '108',
'LOCALECODE' => 'en_US',
'TOKEN' => $_POST['TOKEN'],
'PayerID' => $_POST['PAYERID'],
'PAYMENTREQUEST_0_AMT' => 100,
'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
'PAYMENTREQUEST_0_ITEMAMT' => 100,
'L_PAYMENTREQUEST_0_NAME0' => 'test name',
'L_PAYMENTREQUEST_0_DESC0' => 'test desc',
'L_PAYMENTREQUEST_0_QTY0' => 1,
'L_PAYMENTREQUEST_0_AMT0' => 100,
)));
$response = curl_exec($curl);
curl_close($curl);
$nvp = array();
if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
foreach ($matches['name'] as $offset => $name) {
$nvp[$name] = urldecode($matches['value'][$offset]);
}
}
print_r($nvp);

Related

paygate payweb3 integration in php website

I am trying to integrate paygate payment gateway integration into my PHP API. I have gone through their documentation but it is not clear for me. There were three endpoints
initiate
return
query
I don't which endpoint to use. Now I am using initiate endpoint, but when testing in postman I am getting an error
ERROR=DATA_CHK error
Can anyone guide me to sort this out...
public function subscription(Request $request) {
$validator = Validator::make(
$request->all(),
array(
'subscription' => 'required|integer',
'email' => 'required|exists:users,email',
'amount' => 'required|integer',
'payment_mode' => 'required|integer',
),
array(
'exists' => 'The :attribute doesn\'t exists',
)
);
if ($validator->fails()) {
$error_messages = implode(',', $validator->messages()->all());
$response_array = array('success' => false, 'error' => Helper::get_error_message(101), 'error_code' => 101, 'error_messages'=>$error_messages);
} else {
$encryptionKey = 'AQ2Uted8BidhkIEv6oYgrqwS';
$data = array(
'PAYGATE_ID' => 1039433100014,
'REFERENCE' => 'pgtest_123456789',
'AMOUNT' => 3299,
'CURRENCY' => 'ZAR',
'RETURN_URL' => 'http://localhost/avvata/public/home',
'TRANSACTION_DATE' => date('Y-m-d H:i:s'),
'LOCALE' => 'en-za',
'COUNTRY' => 'ZAF',
'EMAIL' => $request->email,
);
$checksum = md5(implode('', $data) . $encryptionKey);
$data['CHECKSUM'] = $checksum;
$fieldsString = http_build_query($data);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, 'https://secure.paygate.co.za/payweb3/initiate.trans');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString);
//execute post
$result = curl_exec($ch);
echo $result;
exit();
//close connection
curl_close($ch);
}`
Thanks in advance :-)

PayPal DoDirectPayment working without cvv

Hi I implement PayPal DoDirectPayment in my website, I want to required expire date And CVV but payment taking without those with this warning "This transaction was approved. However, the Card Security Code provided had too few, too many, or invalid character types but, as per your account option settings, was not required in the approval process"
I'm using PHP.
My Code is
$api_endpoint = 'https://api-3t.sandbox.paypal.com/nvp';
$request = array
(
'METHOD' => 'DoDirectPayment',
'USER' => 'sell3_api1.pay.com',
'PWD' => '75DQHCABLDFSDF',
'SIGNATURE' => 'AFcWxV21C7fd0asdasdCpasdsdAtxzafSFsaKZ3unSUBjX9r-',
'VERSION' => '55.0',
'PAYMENTACTION' => 'Sale',
'FIRSTNAME' => $current_user->user_login,
'LASTNAME' => '.',
'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
'ACCT' => $acct,
'CREDITCARDTYPE'=>
'EXPDATE' => $month.$year,
'CVV2' => $cvv,
'AMT' => $amt,
'CURRENCYCODE' => 'USD',
);
$nvp_string = '';
foreach ($request as $key => $value) {
$nvp_string .= '&'.$key.'='.urlencode($value);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_URL, $api_endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);
$result = curl_exec($curl);
curl_close($curl);
parse_str($result);
How can I solve It.

sslv3 alert handshake failure under magento

I develop a paypal pro in magento.
my paypal code look below:
$api_username = 'sdk-three_api1.sdk.com';
$api_password = 'QFZCWN5HZM8VBG7Q';
$api_signature = 'A.d9eRKfd1yVkRrtmMfCFLTqa6M9AyodL0SJkhYztxUi8W9pCXF6.4NI';
$api_version = '57.0';
$api_endpoint = 'https://api-3t.sandbox.paypal.com/nvp';
$request_params = array
(
'METHOD' => 'DoDirectPayment',
'USER' => $api_username,
'PWD' => $api_password,
'SIGNATURE' => $api_signature,
'VERSION' => $api_version,
'PAYMENTACTION' => 'Sale',
'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
'CREDITCARDTYPE' => $params['creditCardType'],
'ACCT' => $params['creditCardNumber'],
'EXPDATE' => $params['expDateMonth'].$params['expDateYear'],
'CVV2' => $params['cvv2Number'],
'FIRSTNAME' => 'Tester',
'LASTNAME' => 'Testerson',
'STREET' => '707 W. Bay Drive',
'CITY' => 'Largo',
'STATE' => 'FL',
'COUNTRYCODE' => 'US',
'ZIP' => '33770',
'AMT' => $plan_data['amount'],
'CURRENCYCODE' => 'USD',
'DESC' => 'Testing Payments Pro'
);
$nvp_string = '';
foreach($request_params as $var=>$val)
{
$nvp_string .= '&'.$var.'='.urlencode($val);
}
//var_dump($nvp_string); die;
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_URL, $api_endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, 'SSLv3');
//curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
$result = curl_exec($curl);
if (curl_errno($curl))
{
echo "CURL send a error during perform operation: ".curl_error($curl);
}
else
{
curl_close($curl);
}
// Parse the API response
$nvp_response_array = parse_str($result);
var_dump($result);
But i getting an error like
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake
failure
And if i not added this two line
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, 'SSLv3');
then give me an error like
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
Any help?
There was some updates to Sandbox recently, updates that will need to be applied on Live at a later time, this post will help you

Telegram Bot custom keyboard in PHP

I'm trying to make a Telegram Bot in PHP with a custom keyboard. The message is delivered, but the custom keyboard won't work. $keyb = array('keyboard' => array(array("A", "B"))); also no succes.
The sendMessage method referrers to ReplyKeyboardMarkup for the object. Making an array for ReplyKeyboardMarkup doesn't work. Also tried to json_encode($keyb) but that's also not the solution.
I searched in GitHub for examples but I haven't found one where the custom keyboard is used. Telegram runs on iPhone and desktop, both up-to-date.
Sample code:
$url = "https://api.telegram.org/bot<token>/sendMessage";
$keyb = array('ReplyKeyboardMarkup' => array('keyboard' => array(array("A", "B"))));
$content = array('chat_id' => <chat_id>, 'reply_markup' => $keyb, 'text' => "Test");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //fix http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
The docs seem to indicate you need to provide the reply_markup parameter as a JSON serialised object... kinda stupid for a form POST endpoint:
$replyMarkup = array(
'keyboard' => array(
array("A", "B")
)
);
$encodedMarkup = json_encode($replyMarkup);
$content = array(
'chat_id' => <chat_id>,
'reply_markup' => $encodedMarkup,
'text' => "Test"
);
Does this one work?
$keyboard = array(array("[Destaques]","[Campinas e RMC]","[esportes]"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
$url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=oi&reply_markup=".$reply;
file_get_contents($url);
This code works fine!
$keyboard = [
'keyboard' => [
[
['text' => 'hourly', 'callback_data' => 'Every hour'],
['text' => 'daily', 'callback_data' => 'Every day'],
['text' => 'weekly', 'callback_data' => 'Every week'],
['text' => 'monthly', 'callback_data' => 'Every month'],
['text' => 'yearly', 'callback_data' => 'Every year'],
] ]
];
this works for me

Paypal NVP Authentication Failure

Currently using PHP + cURL to test the NVP SetExpressCheckout Paypal feature (sandbox mode) on xampp,
no matter what I do, I receive the following error:
Error Code 10002 : Authentication/Authorization Failed. You do not have permission to make this API call.
$nvp = array(
'METHOD' => 'SetExpressCheckout',
'VERSION' => '98',
'USER' => $user,
'PWD' => $pwd,
'SIGNATURE' => $signature,
'RETURNURL' => $returnurl,
'CANCELURL' => $cancelurl,
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
'PAYMENTREQUEST_0_AMT' => '20.00',
'PAYMENTREQUEST_0_CURRENCYCODE' => $currency,
'REQCONFIRMSHIPPING' => '0',
'NOSHIPPING' => '1',
'ALLOWNOTE' => '0',
'LOCALECODE' => 'US'
'L_PAYMENTREQUEST_0_NAME0' = 'A product name';
'L_PAYMENTREQUEST_0_AMT0' = '20.00';
'L_PAYMENTREQUEST_0_QTY0' = '1';
'L_PAYMENTREQUEST_0_ITEMCATEGORY0' = 'Digital';
);
$request = 'https://api-3t.sandbox.paypal.com/nvp?' . http_build_query($nvp);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = urldecode(curl_exec($curl));
curl_close($curl);
The weird thing is if I type curl request(the same as $request variable) in the browser I get the SUCCESS and token from the endpoint,
I don't know if it has something to do with the curl itself or if it's something else, I tried to completely
disable my firewall but the error persists...
Your array is malformed.
'L_PAYMENTREQUEST_0_NAME0' = 'A product name';
That's a good way to start debugging

Categories