PayPal Adaptive Payments IMPLICIT Pay API - php

I am basically trying to use the PAY call of Adaptive Payments to programmatically and immediately send funds from my own paypal account to other accounts. According to the documentation, so long as I specify the senderEmail (my own paypal address, used to set up the Adaptive Payments), this should work verbatim.
However, when I make the call, I always get result "CREATED" instead of "COMPLETED". Created means the system still wants me to manually log into PayPal and approve the payments. I really need these payments to occur automatically on the spot. Any help would be appreciated.
Here is my request string:
currencyCode=USD&
returnUrl=http%3A%2F%2Fwww.website.com%2F&
actionType=PAY&
cancelUrl=http%3A%2F%2Fwww.website.com%2F&
receiverList.receiver%280%29.email=receiver%40gmail.com&
receiverList.receiver%280%29.amount=1.00&
requestEnvelope.senderEmail=me%40gmail.com&
clientDetails.deviceId=mydevice&
clientDetails.ipAddress=127.0.0.1&
clientDetails.applicationId=APP-ZZZZZZZZZZZZZ&
requestEnvelope.errorLanguage=en_US&
memo=memo&
feesPayer=EACHRECEIVER&
ipnNotificationUrl=http%3A%2F%2Fwww.website.com%2Fpay.php
And here is the response from PayPal:
[responseEnvelope.timestamp] => 2012-03-01T19:09:57.290-08:00
[responseEnvelope.ack] => Success
[responseEnvelope.correlationId] => 71efd416a2100
[responseEnvelope.build] => 2486531
[payKey] => AP-ZZZZZZZZZZZZZZZ
[paymentExecStatus] => CREATED

Forget everything I said earlier. The problem isn't an inconsistency between Sandbox and Live either, but rather a wrong parameter for 'senderEmail'.
Simply change:
requestEnvelope.senderEmail=me#gmail.com&
To:
senderEmail=me#gmail.com&
For example, the following returns a 'COMPLETED' implicit payment.
<?php
function AdaptiveCall($bodyparams, $method, $payKey) {
try
{
$body_data = http_build_query($bodyparams, "", chr(38));
$url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/".$method."");
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: xxxxxxxxx\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: xxxxxxxxxxx\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: xxxxxxx\r\n" .
"X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: NV\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: NV\r\n"
)
);
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = #fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
//close the stream
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
//print the response to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success") {
echo "<strong>".$method ."</strong><br>";
foreach ($kArray as $key =>$value){
echo $key . ": " .$value . "<br/>";
}
// Return payKey
global $payKey;
if(!empty($kArray['payKey'])) { $payKey = $kArray['payKey']; return($payKey); }
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
}
catch(Exception $e) {
echo "Message: ||" .$e->getMessage()."||";
}
}
//Create Pay body
$bodyparams = array ( "requestEnvelope.errorLanguage" => "en_US",
'actionType' => 'PAY',
'currencyCode' => 'USD',
'receiverList.receiver(0).email' => 'another_account#domain.tld',
'receiverList.receiver(0).amount' => '1.00',
'senderEmail' => 'xxxxxxxxx',
'memo' => 'Test memo',
'ipnNotificationUrl' => 'http://xxxxxxxx',
'cancelUrl' => 'http://xxxxxxxxx',
'returnUrl' => 'http://xxxxxxxxxx'
);
// Call Pay API
AdaptiveCall($bodyparams, "Pay");
?>
Pay response:
responseEnvelope.timestamp: 2012-03-03T09%3A10%3A22.900-08%3A00
responseEnvelope.ack: Success
responseEnvelope.correlationId: 4bc5cfc4a7514
responseEnvelope.build: 2486531
payKey: AP-1XJ7636763429720C
paymentExecStatus: COMPLETED

Related

CallRail Webhook

I am trying to set up a webhook from callrails platform. According to CallRail documentation it appears that my endpoint should response with a HTTP status code of 200? I've done webhooks before but never seen this one before. I am not getting anything after a call is made.
From call rails docs
"Your endpoint should respond with a HTTP status code of 2xx to indicate that the data was received properly. In general, a response status code other than 2xx indicates that the webhook was unable to complete the requested action."
This is the php code I am using. I just want to see the data being posted. Is there something I need to add to properly respond with 200 status code?
include('common.php');
if(!empty($_POST))
{
foreach($_POST as $key => $value)
{
$msg .= 'Key: ' . $key . ' => ' . $value . '<br>';
}
$ret = runner_mail(array('to' => 'xxxx', 'subject' => 'Callrail Call POST', 'htmlbody' => $msg));
$data = json_decode($_POST['body'], true);
if(!empty($data))
{
foreach($data as $key => $value)
{
$message .= 'Key: ' . $key . ' => ' . $value . '<br>';
}
$ret = runner_mail(array('to' => 'xxxxxx', 'subject' => 'Callrail Body Data', 'htmlbody' => $message));
}
}
Thanks for your feedback
for anyone that wants to know here is the answer.
$payload = #file_get_contents('php://input');
$call = json_decode($payload);
//process data
http_response_code(200);

PHP Adaptive payment with Paypal : Your payment can't be completed. Please return to the participating website and try again

I am implementing code for php with adaptive payment after the transaction complete at last page i get the error that Your payment can't be completed. Please return to the participating website and try again. what is the wrong with this code. Here is my code.
enter code here
<?php
//turn php errors on
//ini_set("track_errors", true);
//set PayPal Endpoint to sandbox
$url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/Pay");
$API_UserName = "s.patel_api1.sactp.ca"; //TODO
$API_Password = "PA9YLDURHKTU52LH"; //TODO
$API_Signature = "An5ns1Kso7MWUdW4ErQKJJJ4qi4-AOPwV4AdrC6q3hsN81pmNWMXn7f-"; //TODO
$receiver_email = "rushi.mtech#gmail.com"; //TODO
$amount = 25; //TODO
//Default App ID for Sandbox
$API_AppID = "APP-80W284485P519543T";
$API_RequestFormat = "NV";
$API_ResponseFormat = "NV";
$bodyparams = array ( "requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"cancelUrl" => "http://localhost/success.php",
"returnUrl" => "http://localhost/cancel.php",
"currencyCode" => "CAD",
"receiverList.receiver.email" => $receiver_email,
"receiverList.receiver.amount" => $amount
);
// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38));
try
{
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
"X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n"
));
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = #fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
//close the stream
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
$rushi;
//print the response to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success") {
foreach ($kArray as $key =>$value){
// echo $key . ": " .$value . "<br/>";
if($key == "payKey")
{
$rushi = $value;
echo $rushi;
}
}
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
}
catch(Exception $e) {
echo "Message: ||" .$e->getMessage()."||";
}
?>
Plus i embaded html fromhere please guide me

Set ipnNotificationUrl variable in PayPal

I'm trying to use the PayPal AdaptivePayments API and I'm trying to set the url for paypal IPN which is supposed to be "ipnNotificationUrl" but I don't know where to add it in the request.
Here is my code:
ini_set("track_errors", true);
//set PayPal Endpoint to sandbox
$url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/Pay");
/*
*******************************************************************
PayPal API Credentials
Replace <API_USERNAME> with your API Username
Replace <API_PASSWORD> with your API Password
Replace <API_SIGNATURE> with your Signature
*******************************************************************
*/
//PayPal API Credentials
$API_UserName = "sbapi_1287090601_biz_api1.paypal.com"; //TODO
$API_Password = "1287090610"; //TODO
$API_Signature = "ANFgtzcGWolmjcm5vfrf07xVQ6B9AsoDvVryVxEQqezY85hChCfdBMvY"; //TODO
//Default App ID for Sandbox
$API_AppID = "APP-80W284485P519543T";
$API_RequestFormat = "NV";
$API_ResponseFormat = "NV";
//Create request payload with minimum required parameters
$bodyparams = array ( "requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"currencyCode" => "USD",
"cancelUrl" => "http://www.paypal.com",
"returnUrl" => "http://www.paypal.com",
"receiverList.receiver(0).email" => "r_1_1266351681_biz#paypal.com", //TODO
"receiverList.receiver(0).amount" => "20.0", //TODO
"receiverList.receiver(1).email" => "r_2_1266352427_biz#paypal.com", //TODO
"receiverList.receiver(1).amount" => "30.0" //TODO
);
// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38));
try
{
//create request and add headers
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
"X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n"
));
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = #fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
//close the stream
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
//set url to approve the transaction
$payPalURL = "https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=" . $kArray["payKey"];
//print the url to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success") {
echo '<p>' . $payPalURL . '</p>';
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
/*
//optional code to redirect to PP URL to approve payment
If ( $kArray["responseEnvelope.ack"] == "Success") {
header("Location:". $payPalURL);
exit;
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
*/
}
catch(Exception $e) {
echo "Message: ||" .$e->getMessage()."||";
}
Thanks a lot!
It should be in $bodyparms array:
"returnUrl" => "http://www.paypal.com",
"ipnNotificationUrl" => "http://www.baseem.com/ipnhandler.php",

How to obtain Pay API key from PayPal for Adaptive Payments

I am at my wits end trying to get this work but cant understand it. I have to use Adaptive payments in a webapp which is created using cakephp framework. I have API credentials like API Username, Password and Signature. But the main problem i am facing is how to call the Pay API to get the API key which is the first thing required to make this work. I am new to this so i dont have much knowledge about this and after lot digging in google i am asking this question. Can someone please give steps for using Adaptive Payments..
Thanks in advance for your help!
You have to follow only three steps:
1- You send a PayRequest message to PayPal
{"returnUrl":"http://example.com/returnURL.htm", \ "requestEnvelope":{"errorLanguage":"en_US"},"currencyCode":"USD", \ "receiverList":{"receiver":[{"email":"david#example.com", \
"amount":"10.00",}]},"cancelUrl":"http://example.com/cancelURL.htm",\ "actionType":"PAY"}
2- You receive a response with a pay key.
{"responseEnvelope":\ {"timestamp":"2009-10-06T14:30:39.383-07:00","ack":"Success",\ "correlationId":"cfe8f8783f1d3","build":"DEV"},\ "payKey":"AP-17266198048308436","paymentExecStatus":"CREATED"}
3- You must redirect the sender’s browser to PayPal to approve the payment.
This would be an example code for step 1 (it works on my local server):
<?php
//turn php errors on
ini_set("track_errors", true);
//set PayPal Endpoint to sandbox
$url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/Pay");
$api_appid = 'APP-80W284485P519543T'; // para sandbox
//PayPal API Credentials
$API_UserName = "sbapi_1287090601_biz_api1.paypal.com"; //TODO
$API_Password = "1287090610"; //TODO
$API_Signature = "ANFgtzcGWolmjcm5vfrf07xVQ6B9AsoDvVryVxEQqezY85hChCfdBMvY"; //TODO
$receiver_email = "fake#email.com"; //TODO
$amount = 25; //TODO
//Default App ID for Sandbox
$API_AppID = "APP-80W284485P519543T";
$API_RequestFormat = "NV";
$API_ResponseFormat = "NV";
//Create request payload with minimum required parameters
$bodyparams = array ( "requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"cancelUrl" => "http://cancelUrl",
"returnUrl" => "http://returnUrl",
"currencyCode" => "EUR",
"receiverList.receiver.email" => $receiver_email,
"receiverList.receiver.amount" => $amount
);
// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38));
try
{
//create request and add headers
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
"X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n"
));
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = #fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
//close the stream
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
//print the response to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success") {
foreach ($kArray as $key =>$value){
echo $key . ": " .$value . "<br/>";
}
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
}
catch(Exception $e) {
echo "Message: ||" .$e->getMessage()."||";
}
?>
You have many examples here: https://www.x.com/developers/paypal/documentation-tools/paypal-code-samples

PayPal AdaptivePayments PaymentDetail PayKey

I am using the PayPal Pay API, with Adaptive (Chained) Payments. I am trying to forward a user to paypal and afterwards back to my predefined return_url.
The problem is: I need to have a PayKey within my return-url. Reason for that: I need to call a PaymentDetail API to review the payment within the return_url. And, I don't want to use IPN since I need the validation with some token right on my return Url.
The problem I have is: The PayKey is beeing generated with all the parameters, including the return-url (hence after I build the actual array from which I get my $response from. I can't put the PayKey in the return-Url since it's not generated at this point yet.
//Create request payload with minimum required parameters
$bodyparams = array ("requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"currencyCode" => "USD",
"cancelUrl" => "http://www.paypal.com",
"returnUrl" => $return_url . "&payKey=${payKey}", **// Does not work - PAYKEY NEEDED TO ADD???**
"receiverList.receiver(0).email" => "account1#hotmail.com", //TODO
"receiverList.receiver(0).amount" => $price, //TODO
"receiverList.receiver(0).primary" => "true", //TODO
"receiverList.receiver(1).email" => "account2#hotmail.com", //TODO
"receiverList.receiver(1).amount" => $receiver_gets, //TODO
"receiverList.receiver(1).primary" => "false" //TODO
);
// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38)); // Generates body data
try
{
//create request and add headers
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
"X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n"
));
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = #fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
//set url to approve the transaction
$payPalURL = "https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=" . $kArray["payKey"]; **// Here it works fine, since the PayKey is generated at this point ...**
//print the url to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success") {
echo '<p>' . $payPalURL . '</p>';
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
Can somebody help?
I was at this for ages too myself. I finally figured it out. Paypal docs are hard to follow. I found the answer in the paypal adaptive pdf guide which I downloaded. It specifies to add payKey=${payKey} to the end of your return_url. I just tried it there and the paypal get request to my return url now contains the paykey.
So in rails which I'm using the return_url looks like this. Writing a php variable (I think) into the url as instructed by the guide
:return_url => "http://***********.com/paypal-return?payKey=${payKey}"
It seems that you're missing a step in the sequence.
The first step is to send your transaction parameters to
https://svcs.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah
[sandbox]https://svcs.sandbox.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah
You'll get the paykey in this response.
Once you've retrieved the paykey successfully, you'll call:
https://www.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx
[sandbox]https://www.sandbox.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx
In the second call, the payKey represents the rest of your transaction so you don't have to build another giant query string.
Change your code for:
//Create request payload with minimum required parameters
$bodyparams = array ("requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"currencyCode" => "USD",
"cancelUrl" => "http://www.paypal.com",
"returnUrl" => $return_url . '&payKey=${payKey}', **// That's right**
"receiverList.receiver(0).email" => "account1#hotmail.com", //TODO
"receiverList.receiver(0).amount" => $price, //TODO
"receiverList.receiver(0).primary" => "true", //TODO
"receiverList.receiver(1).email" => "account2#hotmail.com", //TODO
"receiverList.receiver(1).amount" => $receiver_gets, //TODO
"receiverList.receiver(1).primary" => "false" //TODO
);
PHP interprets ${payKey} as variable between the double quotes.
Change double quotes (") for simple quotes (')
Apparently you can embed dynamic variables in the return URL: https://www.x.com/thread/49785
Unfortunately, {$payKey} is invalid. so it must be $paykey or $pay_key. Good luck!

Categories