文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Paypal高级API付款代码范例

Paypal高级API付款代码范例

时间:2010-09-04  来源:cevin

  1 <?php
2
3 class PaypalAPI {
4 private $api_authentication_mode = '';// 3TOKEN UNIPAY
5 private $api_account = '';
6 private $api_password = '';
7 private $api_signature = '';
8 private $api_subject = '';
9 private $paypalPaymentUrl = 'https://www.paypal.com/webscr&cmd=_express-checkout&token=%s';
10 private $paypalApiUrl = 'https://api-3t.paypal.com/nvp';
11 private $paypalVersion = '60.0';
12 private $query = array();
13 private $goodNum = 0;
14 private $amt = 0;
15 private $error = '';
16 /**
17 * 获取支付URL
18 */
19 public function getPaymentUrl() {
20 $this->initializeQuery( 'payment' );
21 $response = $this->request( $this->query );
22 if( !$response ) {
23 return FALSE;
24 } else {
25 return vsprintf($this->paypalPaymentUrl, $response['TOKEN']);
26 }
27 }
28 /**
29 * 验证 TOKEN 是否合法
30 */
31 public function validateToken($token) {
32 $this->initializeQuery( 'validate' );
33 $this->setQuery('TOKEN', $token);
34 return $this->request( $this->query );
35 }
36 /**
37 * 验证信息
38 */
39 public function getError() {
40 return $this->error;
41 }
42 /**
43 * 添加商品
44 */
45 public function addGood($goodName, $price, $num=1, $desc='') {
46 $this->setQuery('L_NAME'.$this->goodNum, $goodName);
47 $this->setQuery('L_AMT'.$this->goodNum, $price);
48 $this->setQuery('L_QTY'.$this->goodNum, $num);
49 if( !empty($desc) ) {
50 $this->setQuery('L_DESC'.$this->goodNum, $desc);
51 }
52 $this->amt += $num*$price;
53 $this->setQuery('AMT', $this->amt);
54 $this->goodNum++;
55 return $this;
56 }
57 /**
58 * 设置属性
59 */
60 public function setAttr($key, $val) {
61 $this->$key = $val;
62 return $this;
63 }
64 /**
65 *
66 */
67 public function setQuery($name, $val) {
68 $this->query[$name] = $val;
69 return $this;
70 }
71 private function initializeQuery( $type ) {
72 $query = Array();
73 $method = Array( 'validate'=>'GetExpressCheckoutDetails', 'payment'=>'SetExpressCheckout');
74 $this->setQuery('METHOD', isset($method[$type]) ? $method[$type] : '');
75 $this->setQuery('VERSION', $this->paypalVersion);
76 switch( $this->api_authentication_mode ) {
77 case '3TOKEN':
78 $this->setQuery('PWD', $this->api_password);
79 $this->setQuery('USER', $this->api_account);
80 $this->setQuery('SIGNATURE', $this->api_signature);
81 break;
82 case 'UNIPAY':
83 $this->setQuery('SUBJECT', $this->api_subject);
84 break;
85 default:
86 throw new Exception("faild authentication of <b>{$this->api_authentication_mode}</b>");
87 break;
88 }
89 }
90 private function request( $post ) {
91 $ch = curl_init();
92 $chOptions = array(
93 CURLOPT_URL => $this->paypalApiUrl,
94 CURLOPT_POST => TRUE,
95 CURLOPT_POSTFIELDS => is_array($post) ? http_build_query($post) : $post,
96 CURLOPT_VERBOSE => TRUE,
97 CURLOPT_SSL_VERIFYPEER => FALSE,
98 CURLOPT_SSL_VERIFYHOST => FALSE,
99 CURLOPT_RETURNTRANSFER => TRUE,
100 );
101 curl_setopt_array($ch, $chOptions);
102 $response = curl_exec($ch);
103 if( curl_errno($ch) ) {
104 $errno = curl_errno($ch);
105 $error = curl_error($ch);
106 curl_close($ch);
107 throw new Exception( " {$errno} - {$error} " );
108 }
109 curl_close($ch);
110 parse_str( urldecode($response), $response );
111 if( $response['ACK'] != 'Success' ) {
112 $this->error = $response;
113 return false;
114 }
115 return $response;
116 }
117 }
118 $paypal = new PaypalAPI();
119 $paymentUrl = $paypal->setAttr('api_account', '高级API帐号')->setAttr('api_password', 'API帐号密码而非paypal帐户密码')->setAttr('api_signature', '密钥')
120 ->addGood('test',10)
121 ->addGood('针织毛衣',103.5,2)
122 ->setQuery('RETURNURL', 'http://www.baidu.com/')
123 ->setQuery('CANCELURL', 'http://www.baidu.com/')
124 ->getPaymentUrl();
125 if( $paymentUrl ) {
126 echo "<a href='$paymentUrl' target='_blank'>To pay</a>";
127 } else {
128 print_r($paypal->getError());
129 }

 

如果顺利,你将会看到类似的页面:

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载