Pay.php
9.1 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
247
248
249
250
251
252
253
254
<?php
use Action\AbstractAction;
use Plugin\Helpers;
use LibModels\Wap\Home\OrderData;
use LibModels\Wap\Home\CartData;
use Plugin\Pay\weixin\JsApiPay;
use Plugin\Pay\weixin\lib\WxPayUnifiedOrder;
use Plugin\Pay\weixin\lib\WxPayApi;
use Plugin\Pay\weixin\lib\WxPayConfig;
use Plugin\Pay\aliwap\AliwapReqparams;
use Plugin\Pay\aliwap\AliwapService;
use Plugin\UdpLog;
/**
* 支付相关的控制器
*
* @name PayController
* @package Shopping
* @copyright yoho.inc
* @version 1.0 (2016-1-23 13:12:40)
* @author fei.hong <fei.hong@yoho.cn>
*/
class PayController extends AbstractAction
{
/**
* 支付宝跳转页
*
* @param int order_code 订单号
*/
public function indexAction()
{
do {
/* 判断是否有订单号参数 */
$orderCode = $this->get('order_code');
if (empty($orderCode)) {
$this->go(SITE_MAIN);
break;
}
/* 判断用户是否登录 */
$uid = $this->getUid(true);
if (!$uid) {
$this->go( Helpers::url('/signin.html', array('refer' => $this->server('HTTP_REFERER', SITE_MAIN))));
break;
}
/* 判断订单信息是否存在 */
$orderDetail = OrderData::viewOrderData($orderCode, $uid, $this->_usession);
if (empty($orderDetail['data'])) {
UdpLog::info("【支付宝支付】校验订单信息,orderCode:{$orderCode},uid:{$uid},返回:", $orderDetail);
$this->helpJsRedirect('没有找到该订单');
break;
}
/* 判断订单是否已取消 */
if (isset($orderDetail['data']['is_cancel']) && $orderDetail['data']['is_cancel'] === 'Y') {
$this->helpJsRedirect('订单已经取消', 'window.location="' . Helpers::url('/home/orders/detail', array('order_code' => $orderCode)) .'";');
break;
}
//锁单接口
$paymentId = $this->get('payment_type', 0);
$prePayInfo = CartData::savePrePayInfo($uid, $orderCode, $paymentId);
UdpLog::info("【锁订单接口】,orderCode:{$orderCode}", array('prePayInfo' => $prePayInfo));
$totalFee = $orderDetail['data']['payment_amount'] * 100;
$reqParams = new AliwapReqparams($orderCode, $totalFee, '有货订单号:' . $orderCode, '',
$orderDetail['data']['create_time'], '', false, $orderDetail['data']['pay_expire']
);
$aliwapService = new AliwapService();
$payRequestPars = $aliwapService->getPayRequestPars($reqParams);
if (empty($payRequestPars)) {
UdpLog::info("【支付宝支付】提交支付宝端口无响应,orderCode:{$orderCode}", array('orderDetail' => $orderDetail, 'payRequestPars' => $payRequestPars));
$this->helpJsRedirect('支付系统繁忙,请稍后再试');
break;
}
//更改支付方式,支付宝为18
$paymentRecod = OrderData::updateOrderPayment($orderCode, $paymentId, $uid);
if (empty($paymentRecod) || $paymentRecod['code'] != 200) {
$this->helpJsRedirect('系统繁忙,请稍后再试');
break;
}
$this->go($payRequestPars['pay_url'] . $payRequestPars['pars']);
}
while (false);
}
/**
* 微信支付 - 获取JSAPI参数
*
* @param int order_code 订单号
* @return json | void
*/
public function wechatwapapiAction()
{
do {
if (!$this->isAjax()) {
break;
}
$uid = $this->getUid(true);
if (!$uid) {
UdpLog::info('【wechat支付】参数校验', 'uid为空');
break;
}
$orderCode = $this->get('order_code');
if (empty($orderCode)) {
UdpLog::info('【wechat支付】参数校验', 'orderCode为空');
break;
}
/* 判断订单信息不存在 */
$orderDetail = OrderData::viewOrderData($orderCode, $uid, $this->_usession);
if (empty($orderDetail['data'])) {
UdpLog::info("【wechat支付】校验订单信息,orderCode:{$orderCode},uid:{$uid},返回:", $orderDetail);
break;
}
$totalFee = strval($orderDetail['data']['payment_amount'] * 100);
$openId = $this->getSession('weixinOpenId' . $orderCode);
if (empty($openId)) {
UdpLog::info('【wechat支付】获取wechat标识为空', array('orderCode' => $orderCode, 'uid' => $uid, 'openId' => $openId));
break;
}
//更改支付方式,微信为22
$paymentRecod = OrderData::updateOrderPayment($orderCode, 22, $uid);
if (empty($paymentRecod) || $paymentRecod['code'] != 200) {
break;
}
//锁单接口
$prePayInfo = CartData::savePrePayInfo($uid, $orderCode, 22);
UdpLog::info("【微信锁订单接口】,orderCode:{$orderCode}", array('prePayInfo' => $prePayInfo));
//到期时间
$_payExpire = empty($orderDetail['data']['pay_expire']) ?
$orderDetail['data']['create_time'] + 7200 : strtotime($orderDetail['data']['pay_expire']);
//统一下单
$tools = new JsApiPay();
$input = new WxPayUnifiedOrder();
$input->SetBody('有货订单号:' . $orderCode);
$input->SetOut_trade_no('YOHOBuy_' . $orderCode); // 商户订单号
$input->SetTotal_fee($totalFee);
$input->SetTime_start(date("YmdHis", (int) $orderDetail['data']['create_time']));
$input->SetTime_expire(date("YmdHis", $_payExpire));
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
UdpLog::info('【微信支付下单】统一下单时返回结果,', array('input' => $input, 'order' => $order));
$jsApiParameters = $tools->GetJsApiParameters($order);
UdpLog::info('【微信支付下单】支付参数,', array('order' => $order, 'jsApiParameters' => $jsApiParameters));
$this->echoJson(array('code' => 200, 'data' => array('jsApiParameters' => json_decode($jsApiParameters))));
} while (false);
}
/**
* 微信支付发送确认
*/
public function wechatPayConfirmAction()
{
$result = array('code' => '400', 'message' => '支付确认发送失败!');
do {
if (!$this->isAjax()) {
break;
}
$uid = $this->getUid();
$orderCode = $this->get('order_code', '');
if (empty($uid) || empty($orderCode)) {
break;
}
$result = OrderData::payConfirm($orderCode, 22, $uid);
} while (false);
$this->echoJson($result);
}
/**
* 选择支付时,时间间隔校验插入
*/
public function addPaymentIntervalAction()
{
$data = array('code'=>400,'message'=>'','data'=>'');
do {
if (!$this->isAjax()) {
break;
}
$uid = $this->getUid(TRUE);
$orderCode = $this->get('orderCode','');
$payment = $this->get('payment',0);
if (!$uid || !$orderCode || !$payment) {
UdpLog::info('【支付时间校验】参数校验', 'orderCode:'.$orderCode.'uid:'.$uid.'payment:'.$payment);
break;
}
$res = CartData::savePrePayInfo($uid, $orderCode, $payment);
$data = array('code'=>$res['code'],'message'=>$res['message'],'data'=>$res['data']);
} while (false);
$this->echoJson($data);
}
/**
* 支付成功回调页面
*/
public function aliwapreturnAction()
{
$aliwapService = new AliwapService();
$payRequestPars = $aliwapService->parseResponse($_GET);
$this->commonShowResult($payRequestPars, 18);
}
/**
* 通用显示结果的方法
* @param type $payResult
* @param type $payment
*/
private function commonShowResult($payResult, $payment)
{
$orderCode = '';
$this->setTitle('支付中心');
$this->setNavHeader('支付中心', Helpers::url('/home/orders'), false);
$view = 'notneedpay';
if ($payResult->payResult == 200) {
$view = 'index';
if (!empty($payResult->orderCode)) {
$orderCode = $payResult->orderCode;
//支付成功,发送一次支付确认
$uid = $this->getUid();
OrderData::payConfirm($payResult->orderCode, $payment, $uid);
}
}
$data = array(
'payBackPage' => true,
'pageFooter' => true,
'order_code' => $orderCode
);
$this->_view->display($view, $data);
}
}