A PayPal sample code with PHP Pear

Times ago, because we entered the web2.0/e-commerce generation all custemers want an e-shop, I was looking for some Pear/PayPal PHP script without success.
Becouse i love pear for speeding up code development etc i wrote my little pear/paypal library, here an example.

$database = new Database($yourconfig);
$order = database->getOrder((int)$order_id);
if($order->status != 'Payable'){
	redirect(base_url().'order/show');
	exit();
}
include('HTTP/Request2.php');
$conf = array('ssl_verify_peer' => false, 'ssl_verify_host' => false);
$req =& new HTTP_Request2("https://api-3t.sandbox.paypal.com/nvp/", HTTP_Request2::METHOD_POST, $conf);
if(empty($order->token) || $order->token_expire < time()){//Create new token
	$SetExpressCheckout = array(
		"USER" => "info_1283974049_biz_api2.XXXXX.com",
		"PWD" => "XXXXX",
		"SIGNATURE" => "XX-XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
		"VERSION"=> "63.0",
		"RETURNURL"=> base_url()."order/paypalreturn/",
		"CANCELURL" => base_url()."order/paypalcancel/",
		"METHOD"=> "SetExpressCheckout",
		"HDRIMG" => "http://example.org/logo.png",
		"LANDINGPAGE" => "billing",
		"CHANNELTYPE" => "merchant",
		"BRANDNAME" => "EXAMPLE GMBH",
		"NOSHIPPING" => '1',
		"PAYMENTREQUEST_0_AMT" => (string)$order->price,
		"PAYMENTREQUEST_0_CURRENCYCODE" => 'EUR',
		//"PAYMENTREQUEST_0_SHIPDISCAMT" => '-1.00',
		//"PAYMENTREQUEST_0_SHIPPINGAMT" => '2.00',
		"PAYMENTREQUEST_0_DESC" => 'Order description',
		"PAYMENTREQUEST_0_PAYMENTACTION"=> "Sale"
	);
	$req->addPostParameter($SetExpressCheckout);
	$body = $req->send()->getBody();
	$body = responseParse($body);
	$database->set_token((int)$order->id, (string)$body['TOKEN'], (int)time()+7200);//Add 2h expire time
	redirect("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=".$body['TOKEN']);
}else //Use cached token
	redirect("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=".$order->token);
Share

One Reply to “A PayPal sample code with PHP Pear”

  1. You post interesting posts here. Your page deserves much more visitors.
    It can go viral if you give it initial boost, i know useful tool that can help you, just type in google:
    svetsern traffic tips

Leave a Reply

Your email address will not be published. Required fields are marked *