AlipaywapController.php
8.2 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
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/7/21
* Time: 10:45
*/
namespace api\portal\controller;
use api\portal\controller\NotifyCommonController;
use think\Db;
use traits\controller\Jump;
use api\portal\model\OrderModel;
class AlipaywapController extends NotifyCommonController
{
public function _initialize()
{
require_once VENDOR_PATH.'alipay_wap/config.php';
require_once VENDOR_PATH.'alipay_wap/wappay/service/AlipayTradeService.php';
require_once VENDOR_PATH.'alipay_wap/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
require_once VENDOR_PATH.'alipay_wap/wappay/buildermodel/AlipayTradeRefundContentBuilder.php';
require_once VENDOR_PATH.'alipay_wap/wappay/buildermodel/AlipayTradeFastpayRefundQueryContentBuilder.php';
$this->config = $config;
}
public function alipay() {
$data = $this->request->param();
$config = $this->config;
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = trim($data['order_sn']);
//订单名称,必填
$subject = trim($data['name']);
//付款金额,必填
// $total_amount = trim($data['total_amount']);
$total_amount = 0.01;
//商品描述,可空
$body = '';
//超时时间
$timeout_express="1m";
$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$payResponse = new \AlipayTradeService($config);
$result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);
var_dump($result);
}
// 退款
public function refund($data) {
// $data = $this->request->param();
$config = $this->config;
//商户订单号,商户网站订单系统中唯一订单号
$out_trade_no = '';
if(!empty($data['out_trade_no'])) {
$out_trade_no = trim($data['out_trade_no']);
}
//支付宝交易号
if(!empty($data['trade_no'])) {
$trade_no = trim($data['trade_no']);
}
//请二选一设置
//需要退款的金额,该金额不能大于订单金额,必填
$refund_amount = trim($data['refund_amount']);
//退款的原因说明
$refund_reason = trim($data['refund_reason']);
//标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
// $out_request_no = trim($data['WIDTRout_request_no']);
$out_request_no = '';
//构造参数
$RequestBuilder=new \AlipayTradeRefundContentBuilder();
$RequestBuilder->setOutTradeNo($out_trade_no);
$RequestBuilder->setTradeNo($trade_no);
$RequestBuilder->setRefundAmount($refund_amount);
$RequestBuilder->setOutRequestNo($out_request_no);
$RequestBuilder->setRefundReason($refund_reason);
$aop = new \AlipayTradeService($config);
/**
* alipay.trade.refund (统一收单交易退款接口)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
$response = $aop->Refund($RequestBuilder);
return $response;
}
// 退款查询
public function refundquery($data) {
$config = $this->config;
//商户订单号,商户网站订单系统中唯一订单号
$out_trade_no = '';
if(!empty($data['out_trade_no'])) {
$out_trade_no = trim($data['out_trade_no']);
}
//支付宝交易号
if(!empty($data['trade_no'])) {
$trade_no = trim($data['trade_no']);
}
//请二选一设置
//请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填
$out_request_no = trim($data['out_request_no']);
//构造参数
$RequestBuilder=new \AlipayTradeFastpayRefundQueryContentBuilder();
$RequestBuilder->setOutTradeNo($out_trade_no);
$RequestBuilder->setTradeNo($trade_no);
$RequestBuilder->setOutRequestNo($out_request_no);
$aop = new \AlipayTradeService($config);
/**
* 退款查询 alipay.trade.fastpay.refund.query (统一收单交易退款查询)
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
$response = $aop->refundQuery($RequestBuilder);
return $response;
}
// 回调
public function notify_url() {
$config = $this->config;
$arr=$_POST;
$alipaySevice = new \AlipayTradeService($config);
$alipaySevice->writeLog(var_export($_POST,true));
$result = $alipaySevice->check($arr);
if($result) {
//商户订单号
$out_trade_no = $_POST['out_trade_no'];
//支付宝交易号
$trade_no = $_POST['trade_no'];
//交易状态
$trade_status = $_POST['trade_status'];
if($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
$where['order_sn'] = $out_trade_no;
// $where['money'] = $data['total_amount'];
$res = $this->changeOrderStatus($where,$trade_no,4,'pc支付宝支付');
if(!$res) {
echo 'fail';exit;
}
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
}
else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
$where['order_sn'] = $out_trade_no;
// $where['money'] = $data['total_amount'];
$res = $this->changeOrderStatus($where,$trade_no,4,'pc支付宝支付');
if(!$res) {
echo 'fail';exit;
}
//注意:
//付款完成后,支付宝系统发送该交易状态通知
}
echo "success"; //请不要修改或删除
}else {
//验证失败
echo "fail"; //请不要修改或删除
}
}
//
public function return_url() {
$config = $this->config;
$arr = $_GET;
$alipaySevice = new \AlipayTradeService($config);
$result = $alipaySevice->check($arr);
// var_dump($_GET);
// echo '<br/>';
if ($result) {
//商户订单号
$out_trade_no = htmlspecialchars($_GET['out_trade_no']);
//支付宝交易号
$trade_no = htmlspecialchars($_GET['trade_no']);
$data = $_GET;
$where['order_sn'] = $data['out_trade_no'];
// $where['money'] = $data['total_amount'];
$this->changeOrderStatus($where,$data['trade_no'],4,'手机支付宝支付');
$status = Db::name('Order')->where($where)->value('status');
if($status == 2) {
$this->redirect('http://'.$_SERVER['HTTP_HOST']);
} else {
$this->redirect('http://'.$_SERVER['HTTP_HOST']);
}
}
}
}