Hello, please sign in or register
You are here: Home

PayPal ExpressCheckout

Paypal express checkout simplification.

The examples i've seen failed to capture the essence of the communication, or atleast, did so with a lot of cruft. I think this is the best way of testing / describing the connections with the PayPal API.

You'll require a paypal developer account. See developer.paypal.com

Please copy the PHP script below. And run from within a browser.

<?php
/**
 * Brief introduction to testing a paypal ExpressCheckout
 * Please adapt with your own credentials
 * 
 * @author Andrew Dodson
 * @since 26-01-2009
 */
if( !empty( $_POST ) ) {
	
	$ch = curl_init();	// Start cURL
	curl_setopt_array( $ch, array( // SetParameters
				CURLOPT_URL		=> 'https://api-3t.sandbox.paypal.com/nvp',
				CURLOPT_SSL_VERIFYPEER=>FALSE,
				CURLOPT_SSL_VERIFYHOST=>FALSE,
				CURLOPT_RETURNTRANSFER=>1,
				CURLOPT_TIMEOUT	=> 60,
				CURLOPT_POST	=> 1,
				CURLOPT_POSTFIELDS => http_build_query(array_filter($_POST))
			));
	$r=curl_exec($ch); // get page from PayPal
	
	if(curl_error($ch))
		$r .= "cURL ERROR: ".curl_errno($ch).": ".curl_error($ch);
	else{ // extract the tokens
		$res = array();
		preg_match_all('/([a-z0-9\_]+)=([^&]+)/i',$r, $matches, PREG_SET_ORDER);
		foreach($matches as $o)
			$res[$o[1]] = urldecode($o[2]);
	}
	curl_close($ch);
}

$thispage = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; // For prepopulating returnurl and cancelurl
$action = '?start';
$desc = "Step 1 - SetExpressCheckout";

$sign = array // included in every request
	(
		///// Signature, please update to be your own
		'USER'		=>'andrew_1232551821_biz_api1.gmail.com',
		'PWD'		=>'HBHYC8XPPJAUPF3J',
		'SIGNATURE'	=>'AC5fpjwmOe6Gsl2TsYo83KngFGaJANltkzYO-P6VO5sa0ctqIIiE2x4f',
		'VERSION'	=>'3.3',

		// Optional
		//the email of the account the transaction is on behalf of (if access is granted)
		//'SUBJECT'	=>'andrew_1232982759_biz@gmail.com',
	);

$form = $sign + array
	(
		///// MANDATORY
		'RETURNURL' =>$thispage.'?return',
		'CANCELURL'	=>$thispage.'?cancel',
		'METHOD'	=>'SetExpressCheckout',
		'AMT'		=>'19.95', // Everything, exclude shipping and taxes if not known
		
		///// OPTIONAL

		'PAYMENTACTION'=>'Sale', //default
		'CURRENCYCODE'=>'GBP', //default 'USD'
		'MAXAMT'	=> '', // An estimate of the total ammount, item value + estimate of the total shipping and tax
		
		// style + display
		'LOCALECODE'=> 'en',
		'HDRIMG'	=>'http://www.perplexed.co.uk/284_toplogo.gif', // Header image to use
		'HDRBORDERCOLOR'=> '00FF00',
		'HDRBACKCOLOR'	=> 'FF0000',
		'PAYFLOWCOLOR'	=> '0000FF',
		
		'EMAIL' 	=> '', // Prepopulate users paypal email address
		
		// customized info
		'DESC'		=> '', // Description of items being purchased
		'CUSTOM'	=> '', // Custom field, 256 chars, e.g. tracking number (returns with DoExpr..)
		'INVNUM'	=> '', // Custom field, 127 chars, tracking numnber (returns with DoExpr.. and GetExpr..)
		
		// Shipping
		'REQCONFIRMSHIPPING' => 0, // 0,1 Use a paypal confirmed shipping address
		'NOSHIPPING'=> 0, // 0,1, If '1' then paypal wont prompt user for a shipping address (for non-tangible goods and services)
		'ShippingAddress' => 1,
		'ADDROVERRIDE'	=> 0, // 0,1, Value 1 indicates that paypal should display the address set in this request
		
	);

/**
 * Redirection after step 1
 */
if($res['ACK']==='Success'&&array_key_exists('start',$_GET))
	$redirect_url = 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token='.$res['TOKEN'];

/**
 * Build Step 2 - GetExpressCheckoutDetails
 */
if(array_key_exists('return',$_GET)){
	$action = '?confirm';
	$desc = "Step 2 - GetExpressCheckoutDetails";
	$form = $sign + array(
		'TOKEN'=>$_GET['token'],
		'METHOD'=>'GetExpressCheckoutDetails'
	);
}

/**
 * Build Step 3 - DoExpressCheckoutPayment
 */
if(array_key_exists('confirm',$_GET)){
	$action = '?done';
	$desc = "Step 3 - DoExpressCheckoutPayment";
	$form = $sign + array(
		'TOKEN'	=>$res['TOKEN'],
		'METHOD'=>'DoExpressCheckoutPayment',
		'AMT'	=>$form['AMT'],
		'PAYMENTACTION'	=> $form['PAYMENTACTION'],
		'CURRENCYCODE'	=> $form['CURRENCYCODE'],
		'PAYERID'=>$res['PAYERID'] 
	);
}

/**
 * Done
 */
if(array_key_exists('done',$_GET)){
	$action = '';
	$desc = "Done";
	$form = array();
}	

?>
<html>
<style>
	*{font-size:10px;font-family:verdana;}
	input{width:300px;}
	pre{background:#eee;}
</style>
<body>
<?php
if(isset($r)){
	print "<h2>Response</h2>";
	if(isset($redirect_url))
		print "Redirect: <a href='$redirect_url' target='_blank'>$redirect_url</a>";
	print "<p>-RAW-</p><pre>$r</pre><p>-ARRAY-</p><pre>".print_r($res, true)."</pre>";
}
?>
<a href='<?=$thispage?>'>reset</a>
<h2>Request: <?=$desc?></h2>
<?php if(!empty($form)){?>
<form method='post' action='<?=$thispage.$action?>'>
<table>
<?php
foreach($form as $k=>$o){
	print '<tr><td>'.$k.'</td><td><input name="'.$k.'" value="'.$o.'" /></td></tr>';
}
?>
</table>
<input type='submit'  />
</form>
<?php } ?>
</body>
</html>

Comments

oakleysunglasses
“I canada goose outlet think it doesn’t swarovski jewelry bode very
Created 21/01/16
linpingping
The michael--kors.org.uk next ralph lauren outlet day patriots jersey Mrs
Created 01/03/16
clibin
Tyrion 5c cases watched her ray ban sunglasses read. His timber...
Created 18/04/16
chenyan
Bond grinned. pandora-bracciali.it "We bcbgmax.in.net only chaussure...
Created 15/06/16
wq123
This hilfiger outlet time swarovski jewelry something really timberland outlet strange, ...
Created 30/06/16
ninestab123
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16
leilei3915
20170408 leilei3915 kate spade outlet coach facto...
Created 08/04/17
michael kors factory outlet
adidas nmd pandora bracelet
Created 16/05/17
wwwww
kobe sneakers michael kors outlet
Created 28/06/17
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share