pay_alipay.php
5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* @class pay_alipay
* @brief 支付宝【双向接口】插件类
*/
class pay_alipay extends PaymentPlugin{
//支付插件名称
public $name = '支付宝';
//提交地址
public function submitUrl(){
return 'https://mapi.alipay.com/gateway.do?_input_charset=utf-8';
}
//取得配制参数
public static function config()
{
return array(
array('field'=>'partner_id','caption'=>'合作身份者id','type'=>'string'),
array('field'=>'partner_key','caption'=>'安全检验码key','type'=>'string'),
);
}
//同步处理
public function callback($callbackData,&$paymentId,&$money,&$message,&$orderNo)
{
//除去待签名参数数组中的空值和签名参数
$filter_param = $this->filterParam($callbackData);
//对待签名参数数组排序
$para_sort = $this->argSort($filter_param);
//生成签名结果
$payment = new Payment($paymentId);
$payment_plugin = $payment->getPaymentPlugin();
$classConfig = $payment_plugin->getClassConfig();
$mysign = $this->buildSign($para_sort,$classConfig['partner_key']);
if($callbackData['sign'] == $mysign)
{
//回传数据
$orderNo = $callbackData['out_trade_no'];
$money = $callbackData['total_fee'];
if($callbackData['trade_status'] == 'TRADE_FINISHED' || $callbackData['trade_status'] == 'WAIT_SELLER_SEND_GOODS')
{
return true;
}
}
else
{
$message = '签名不正确';
}
return false;
}
//异步处理
public function asyncCallback($callbackData,&$paymentId,&$money,&$message,&$orderNo)
{
//除去待签名参数数组中的空值和签名参数
$filter_param = $this->filterParam($callbackData);
//对待签名参数数组排序
$para_sort = $this->argSort($filter_param);
//生成签名结果
$payment = new Payment($paymentId);
$payment_plugin = $payment->getPaymentPlugin();
$classConfig = $payment_plugin->getClassConfig();
$mysign = $this->buildSign($para_sort,$classConfig['partner_key']);
if($callbackData['sign'] == $mysign)
{
//回传数据
$orderNo = $callbackData['out_trade_no'];
$money = $callbackData['total_fee'];
if($callbackData['trade_status'] == 'TRADE_FINISHED' || $callbackData['trade_status'] == 'WAIT_SELLER_SEND_GOODS')
{
return true;
}
}
return false;
}
//后期与服务同步处理类
public function afterAsync()
{
$payment = new Payment($this->paymentId);
$paymentObj = $payment->getPayment();
return new AlipayDelivery($paymentObj['partner_id'],$paymentObj['partner_key']);
}
//打包数据
public function packData($payment)
{
$return = array();
//基本参数
$return['service'] = 'trade_create_by_buyer';
$return['seller_id'] = $payment['M_PartnerId'];
$return['partner'] = $payment['M_PartnerId'];
$return['_input_charset'] = 'utf-8';
$return['payment_type'] = 1;
$return['return_url'] = $this->callbackUrl;
$return['notify_url'] = $this->asyncCallbackUrl;
$return['subject'] = $payment['R_Name'];
$return['out_trade_no'] = $payment['M_OrderNO'];
$return['price'] = number_format($payment['M_Amount'], 2, '.', '');
$return['quantity'] = 1;
$return['logistics_fee'] = "0.00";
$return['logistics_type'] = "EXPRESS";
$return['logistics_payment'] = "SELLER_PAY";
if(isset($payment['P_Name']))
{
$return['receive_name'] = $payment['P_Name'];
$return['receive_address'] = $payment['P_Address'];
$return['receive_zip'] = $payment['P_PostCode'];
$return['receive_phone'] = $payment['P_Telephone'];
$return['receive_mobile'] = $payment['P_Mobile'];
}
//过虑无效参数
$filter_param = $this->filterParam($return);
//对待签名参数数组排序
$para_sort = $this->argSort($filter_param);
//生成签名
$mysign = $this->buildSign($para_sort, $payment['M_PartnerKey']);
//签名结果与签名方式加入请求提交参数组中
$return['sign'] = $mysign;
$return['sign_type'] = 'MD5';
return $return;
}
private function filterParam($para)
{
$filter_param = array();
foreach($para as $key => $val)
{
if($key == "sign" || $key == "sign_type" || $val == "")
{
continue;
}
else
{
$filter_param[$key] = $para[$key];
}
}
return $filter_param;
}
private function argSort($para)
{
ksort($para);
reset($para);
return $para;
}
//生成签名
private function buildSign($sort_para,$key,$sign_type = "MD5")
{
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
$prestr = $this->createLinkstring($sort_para);
//把拼接后的字符串再与安全校验码直接连接起来
$prestr = $prestr.$key;
$mysgin = md5($prestr);
return $mysgin;
}
//生成连接字符串
private function createLinkstring($para){
$arg = "";
foreach($para as $key => $val){
$arg.=$key."=".$val."&";
}
//去掉最后一个&字符
$arg = trim($arg,'&');
//如果存在转义字符,那么去掉转义
if(get_magic_quotes_gpc()){
$arg = stripslashes($arg);
}
return $arg;
}
}