AlipayTradeService.php
9.5 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/* *
* 功能:支付宝手机网站alipay.trade.close (统一收单交易关闭接口)业务参数封装
* 版本:2.0
* 修改日期:2016-11-01
* 说明:
* 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
*/
require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'./../../AopSdk.php';
require dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'./../../config.php';
class AlipayTradeService {
//支付宝网关地址
public $gateway_url = "https://openapi.alipay.com/gateway.do";
//支付宝公钥
public $alipay_public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArNEHZmEHK65rLQmuYhYpXTsWMFf3SPLfI0WNNjm6LeUBtD/3IocM/NrkOJZ3sRRCBY8qYOWvJANG6iq/Nae0Z4bmQ25qHIesnU2ev4PurT24AMEugJwG62KUrOfZo7fmjMTsyCdcCW7W0XYwYmcz6R3a6y/APENpb33kO4lls+ly3FmocitbmAVrIcbX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0CgYEAzZog5kZKu2kens0tVpX0p7JFRSzuVuVXOuQjLsaZ81a1TPdASHVKhmAU7G6xEGJUhY+Et/KD7PEBDAxaTIXqzsEUWzMhNOuyaauf8dbL/GlPAo6BYMCcm8vC9k0FwfH6IZmGlr4gYi6ckaTJ9jiF9ppUPVVEWdK49kN9JCmJlesCgYEAzt4ue5YaBAVNFOlilHveA4pWZBhalkXE71fe3pTWJwyi4FSudUx7W3EkhWwDN/bdnHhFnOdGFsvdrFv/L7E0hZaDgLz7L+uKtsZfN567bFrxxUuFP9X4ng3mzVF8mOvY3VM3d3z8pNAgDmJCZFe9XGytIlv5ZpvSX08tlCOD6skCgYBu0wu01GLoDkuhEbvElmQgGKnaFdQLJugUKHEGf6rwTebAK+6MoByhvhzJLQloaXhh3fCOLr0J9Qa5ddcetHjTJaRRXIgjvICZERfQBhZCYoliOWpnuqswF29p0C6wUjyf699uVeGiyrk+kHMEs4cSXDHYNaP4PuvQYqSL34/4LQKBgQCuaFmh7j+kvebUxRaDRJmuhuYarzT3Hg86P6+wl6DRZL1Bs1DYW7ydkgOhCE5XqWUI6jEsD2aJInMDPNMghGmhA474ZGp5ZwEmIItNHgkvjsHcQupPKVEUbruOzVJDEILl3TH/JjG3XtZ6yPd8dFr3gSBdspvGVUmijHMSpl/3VQ==';
//应用id
public $appid = "2018101661696470";
//编码格式
public $charset = "UTF-8";
public $token = NULL;
//返回数据格式
public $format = "json";
//签名方式
public $signtype = "RSA";
function __construct($alipay_config){
$this->gateway_url = $alipay_config['gatewayUrl'];
$this->appid = $alipay_config['app_id'];
$this->private_key = $alipay_config['merchant_private_key'];
$this->alipay_public_key = $alipay_config['alipay_public_key'];
$this->charset = $alipay_config['charset'];
$this->signtype=$alipay_config['sign_type'];
if(empty($this->appid)||trim($this->appid)==""){
throw new Exception("appid should not be NULL!");
}
if(empty($this->private_key)||trim($this->private_key)==""){
throw new Exception("private_key should not be NULL!");
}
if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
throw new Exception("alipay_public_key should not be NULL!");
}
if(empty($this->charset)||trim($this->charset)==""){
throw new Exception("charset should not be NULL!");
}
if(empty($this->gateway_url)||trim($this->gateway_url)==""){
throw new Exception("gateway_url should not be NULL!");
}
}
function AlipayWapPayService($alipay_config) {
$this->__construct($alipay_config);
}
/**
* alipay.trade.wap.pay
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @param $return_url 同步跳转地址,公网可访问
* @param $notify_url 异步通知地址,公网可以访问
* @return $response 支付宝返回的信息
*/
function wapPay($builder,$return_url,$notify_url) {
$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);
$request = new AlipayTradeWapPayRequest();
$request->setNotifyUrl($notify_url);
$request->setReturnUrl($return_url);
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request,true);
// $response = $response->alipay_trade_wap_pay_response;
return $response;
}
function aopclientRequestExecute($request,$ispage=false) {
$aop = new AopClient ();
$aop->gatewayUrl = $this->gateway_url;
$aop->appId = $this->appid;
$aop->rsaPrivateKey = $this->private_key;
$aop->alipayrsaPublicKey = $this->alipay_public_key;
$aop->apiVersion ="1.0";
$aop->postCharset = $this->charset;
$aop->format= $this->format;
$aop->signType=$this->signtype;
// 开启页面信息输出
$aop->debugInfo=false;
if($ispage)
{
$result = $aop->pageExecute($request,"post");
echo $result;
}
else
{
$result = $aop->Execute($request);
}
//打开后,将报文写入log文件
$this->writeLog("response: ".var_export($result,true));
return $result;
}
/**
* alipay.trade.query (统一收单线下交易查询)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
function Query($builder){
$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);
$request = new AlipayTradeQueryRequest();
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request);
$response = $response->alipay_trade_query_response;
var_dump($response);
return $response;
}
/**
* alipay.trade.refund (统一收单交易退款接口)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
function Refund($builder){
$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);
$request = new AlipayTradeRefundRequest();
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request);
$response = $response->alipay_trade_refund_response;
var_dump($response);
return $response;
}
/**
* alipay.trade.close (统一收单交易关闭接口)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
function Close($builder){
$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);
$request = new AlipayTradeCloseRequest();
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request);
$response = $response->alipay_trade_close_response;
var_dump($response);
return $response;
}
/**
* 退款查询 alipay.trade.fastpay.refund.query (统一收单交易退款查询)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
function refundQuery($builder){
$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);
$request = new AlipayTradeFastpayRefundQueryRequest();
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request);
var_dump($response);
return $response;
}
/**
* alipay.data.dataservice.bill.downloadurl.query (查询对账单下载地址)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
function downloadurlQuery($builder){
$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);
$request = new alipaydatadataservicebilldownloadurlqueryRequest();
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request);
$response = $response->alipay_data_dataservice_bill_downloadurl_query_response;
var_dump($response);
return $response;
}
/**
* 验签方法
* @param $arr 验签支付宝返回的信息,使用支付宝公钥。
* @return boolean
*/
function check($arr){
$aop = new AopClient();
$aop->alipayrsaPublicKey = $this->alipay_public_key;
$result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
return $result;
}
//请确保项目文件有可写权限,不然打印不了日志。
function writeLog($text) {
// $text=iconv("GBK", "UTF-8//IGNORE", $text);
//$text = characet ( $text );
file_put_contents ( dirname ( __FILE__ ).DIRECTORY_SEPARATOR."./../../log.txt", date ( "Y-m-d H:i:s" ) . " " . $text . "\r\n", FILE_APPEND );
}
/** *利用google api生成二维码图片
* $content:二维码内容参数
* $size:生成二维码的尺寸,宽度和高度的值
* $lev:可选参数,纠错等级
* $margin:生成的二维码离边框的距离
*/
function create_erweima($content, $size = '200', $lev = 'L', $margin= '0') {
$content = urlencode($content);
$image = '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$lev.'|'.$margin.'&chl='.$content.'" widht="'.$size.'" height="'.$size.'" />';
return $image;
}
}
?>