PHP – Amazon Simple Pay IPN

The following php example helps you integrate the Amazon Simple Pay IPN to your web application.

Code Example:

<?php
$sandbox = true;
$action = ($sandbox) ? "https://authorize.payments-sandbox.amazon.com/pba/paypipeline" : "https://authorize.payments.amazon.com/pba/paypipeline";
$signature = md5(uniqid());
?>
<form action="<?php echo $action;?>" method="post" name="frm_amazon">
<input type="hidden" name="accessKey" 		value="YOUR_AMAZON_ACCESS_KEY" >
<input type="hidden" name="signatureVersion"value="2" >
<input type="hidden" name="immediateReturn" value="0" >
<input type="hidden" name="signature" 		value="<?php echo $signature;?>" >
<input type="hidden" name="signatureMethod" value="HmacSHA256" >
<input type="hidden" name="cobrandingStyle" value="logo" >
<input type="hidden" name="processImmediate"value="1" >

<input type="hidden" name="amount" 		value="USD 14.58" >
<input type="hidden" name="description" value="Description of the order" >
<input type="hidden" name="referenceId" value="<?php echo rand(1,99999);?>" > 
<input type="hidden" name="ipnUrl" 		value="http://www.yoursite.com/amazon_ipn.php" >
<input type="hidden" name="returnUrl"	value="http://www.yoursite.com/return.php" >            
<input type="hidden" name="abandonUrl"	value="http://www.yoursite.com/" >
<input type="image" src="https://authorize.payments-sandbox.amazon.com/pba/images/payNowButton.png" border="0" />
</form>

amazon_ipn.php

            
<?php
$sandbox = true;
$fpsServiceEndPoint = ($sandbox) ? "https://fps.sandbox.amazonaws.com/" : "https://fps.amazonaws.com/";
		
$params["paymentReason"] = $_REQUEST['paymentReason'];
$params["signatureMethod"] = $_REQUEST['signatureMethod']; 
$params["transactionAmount"] = $_REQUEST['transactionAmount'];
$params["transactionId"] = $_REQUEST['transactionId']; 
$params["status"] = $_REQUEST['status'];		
$params["buyerEmail"] = $_REQUEST['buyerEmail']; 
$params["signatureVersion"] = $_REQUEST['signatureVersion']; 
$params["referenceId"] = $_REQUEST['referenceId']; 		
$params["recipientEmail"] = $_REQUEST['recipientEmail'];
$params["transactionDate"] = $_REQUEST['transactionDate']; 
$params["buyerName"] = $_REQUEST['buyerName']; 
$params["operation"] = $_REQUEST["operation"]; 
$params["recipientName"] = $_REQUEST['recipientName']; 
$params["signatureVersion"] = $_REQUEST['signatureVersion']; 
$params["certificateUrl"] = $_REQUEST['certificateUrl'];
$params["paymentMethod"] = $_REQUEST['paymentMethod']; 		 
$params["signature"] = $_REQUEST['signature']; 
		
$urlEndPoint = "http://www.yoursite.com/ipn_amazon.php"; //Your url end point receiving the ipn.

$url = $fpsServiceEndPoint . '?Action=VerifySignature&UrlEndPoint=' . rawurlencode($urlEndPoint);
$queryString = rawurlencode(http_build_query($params, '', '&'));

$url .= '&HttpParameters=' . $queryString . '&Version=2008-09-17';

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_FILETIME, false);
curl_setopt($curlHandle, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
#curl_setopt($curlHandle, CURLOPT_CAINFO, 'ca-bundle.crt');
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curlHandle, CURLOPT_MAXREDIRS, 0);
#curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_NOSIGNAL, true);
#curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Amazon FPS Standard PHP5 Library 2.1');
// Handle the encoding if we can.
if (extension_loaded('zlib')){
	curl_setopt($curlHandle, CURLOPT_ENCODING, '');
}

// Execute the request
$response = curl_exec($curlHandle);		
$xml = new SimpleXMLElement($response);
$result = (string) $xml->VerifySignatureResult->VerificationStatus;


if($result === 'Success'){
	// insert order			
}
else{
	// reject order
}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail