Tim Nash "stuff" Blog

Paypal most handy API

0

If you have ever suffered the problem of waiting for PayPals IPN (Instant Payment Notification), you will know it’s rarely instant. Indeed, it has an annoying tendency to not come at all. However, all is not lost. Developers using PayPal Adaptive API have a secret weapon.

Adaptive Payments – PaymentDetails

If you ever need to know the status of a Adaptive Payment then you can make a call to:
https://svcs.paypal.com/AdaptivePayments/PaymentDetails
Sending over a PayKey and an error language as post keys, the example below using PHP and CURL with the response language is JSON


$headers[] = 'X-PAYPAL-SECURITY-USERID: '.$api_handler_username;
$headers[] = 'X-PAYPAL-SECURITY-PASSWORD: '. $api_password;
$headers[] = 'X-PAYPAL-SECURITY-SIGNATURE: '.$api_signature;
$headers[] = 'X-PAYPAL-REQUEST-DATA-FORMAT: NV';
$headers[] = 'X-PAYPAL-RESPONSE-DATA-FORMAT: JSON';
$headers[] = 'X-PAYPAL-APPLICATION-ID: '. $api_key;
$headers[] = 'X-PAYPAL-SERVICE-VERSION: 1.3.0';
$post_data_array['payKey'] = $paykey;
$post_data_array['requestEnvelope.errorLanguage'] = 'en_US';
$post_data = http_build_query($post_data_array, '', chr(38));

//time to fire off CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // add POST fields
$return = curl_exec($ch);
if(curl_errno($ch))
{
die(‘ERROR: ‘ . curl_error($ch));
curl_close($ch);

$json = json_decode($return);
if($json->responseEnvelope->ack == ‘Success’){
if($json->status == ‘COMPLETED’){
//Success Get details and commit
}
else {

//failure grab the status to find out whats going on
}
}

I find the simplest way to use the above is combined with a queueing system, or simple cron job, taking pending transactions, processing through and changing status, if completed. The advantage to this method is you can also check the timestamps and set a point at which it is abandoned.

Do you still need IPN

Depending on your scenario yes, for example if the transaction is refunded, chargeback, held etc. you may also wish to use IPNs. This way, if someone takes an exceptionally long time, or PayPal has a flaky moment, and you have marked the transaction as abandoned, you can get a return from IPN and change the notification accordingly. What’s more, there is no need to verify IPN. When you receive it, you simply make a PaymentDetails request making data poisoning much harder.

Consulting
Having trouble getting PayPal APIs bending to your will? Need help with Adaptive Payments or Payments Pro? Through Coding Futures we provide training and/or bespoke development services using PayPal technologies to change the way people pay for goods and services. End Corporate Message.
Consulting

While I no longer offer personal consultancy if you are interested in going further then please let us know at Coding Futures


Currently No Comments

Add a comment



*Required

You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.