2011年(109)
分类: 系统运维
2011-05-18 15:35:18
class PaypalAPI {
private $api_authentication_mode = '';// 3TOKEN UNIPAY
private $api_account = '';
private $api_password = '';
private $api_signature = '';
private $api_subject = '';
private $paypalPaymentUrl = '%s';
private $paypalApiUrl = '';
private $paypalVersion = '60.0';
private $query = array();
private $goodNum = 0;
private $amt = 0;
private $error = '';
/**
* 获取支付URL
*/
public function getPaymentUrl() {
$this->initializeQuery( 'payment' );
$response = $this->request( $this->query );
if( !$response ) {
return FALSE;
} else {
return vsprintf($this->paypalPaymentUrl, $response['TOKEN']);
}
}
/**
* 验证 TOKEN 是否合法
*/
public function validateToken($token) {
$this->initializeQuery( 'validate' );
$this->setQuery('TOKEN', $token);
return $this->request( $this->query );
}
/**
* 验证信息
*/
public function getError() {
return $this->error;
}
/**
* 添加商品
*/
public function addGood($goodName, $price, $num=1, $desc='') {
$this->setQuery('L_NAME'.$this->goodNum, $goodName);
$this->setQuery('L_AMT'.$this->goodNum, $price);
$this->setQuery('L_QTY'.$this->goodNum, $num);
if( !empty($desc) ) {
$this->setQuery('L_DESC'.$this->goodNum, $desc);
}
$this->amt += $num*$price;
$this->setQuery('AMT', $this->amt);
$this->goodNum++;
return $this;
}
/**
* 设置属性
*/
public function setAttr($key, $val) {
$this->$key = $val;
return $this;
}
/**
*
*/
public function setQuery($name, $val) {
$this->query[$name] = $val;
return $this;
}
private function initializeQuery( $type ) {
$query = Array();
$method = Array( 'validate'=>'GetExpressCheckoutDetails', 'payment'=>'SetExpressCheckout');
$this->setQuery('METHOD', isset($method[$type]) ? $method[$type] : '');
$this->setQuery('VERSION', $this->paypalVersion);
switch( $this->api_authentication_mode ) {
case '3TOKEN':
$this->setQuery('PWD', $this->api_password);
$this->setQuery('USER', $this->api_account);
$this->setQuery('SIGNATURE', $this->api_signature);
break;
case 'UNIPAY':
$this->setQuery('SUBJECT', $this->api_subject);
break;
default:
throw new Exception("faild authentication of {$this->api_authentication_mode}");
break;
}
}
private function request( $post ) {
$ch = curl_init();
$chOptions = array(
CURLOPT_URL => $this->paypalApiUrl,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => is_array($post) ? http_build_query($post) : $post,
CURLOPT_VERBOSE => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
);
curl_setopt_array($ch, $chOptions);
$response = curl_exec($ch);
if( curl_errno($ch) ) {
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
throw new Exception( " {$errno} - {$error} " );
}
curl_close($ch);
parse_str( urldecode($response), $response );
if( $response['ACK'] != 'Success' ) {
$this->error = $response;
return false;
}
return $response;
}
}
$paypal = new PaypalAPI();
$paymentUrl = $paypal->setAttr('api_account', '高级API帐号')->setAttr('api_password', 'API帐号密码而非paypal帐户密码')->setAttr('api_signature', '密钥')
->addGood('test',10)
->addGood('针织毛衣',103.5,2)
->setQuery('RETURNURL', '')
->setQuery('CANCELURL', '')
->getPaymentUrl();
if( $paymentUrl ) {
echo "To pay";
} else {
print_r($paypal->getError());
}
复制代码
如果顺利,你将会看到类似的页面: