Pay.php
6.71 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
<?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->_request->server('HTTP_REFERER'))) );
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;
}
$totalFee = $orderDetail['data']['payment_amount'] * 100;
$reqParams = new AliwapReqparams($orderCode, $totalFee, '有货订单号:' . $orderCode, '', $orderDetail['data']['create_time'], '', false);
$aliwapService = new AliwapService();
$payRequestPars = $aliwapService->getPayRequestPars($reqParams);
if (empty($payRequestPars)) {
UdpLog::info("【支付宝支付】提交支付宝端口无响应,orderCode:{$orderCode}", array('orderDetail' => $orderDetail, 'payRequestPars' => $payRequestPars));
$this->helpJsRedirect('支付系统繁忙,请稍后再试');
break;
}
// 记录或修改支付方式 说明:由于在web站没有支付宝手机支付的选项,这里就记录支付方式就按照web端支付宝的支付方式平台号为2
// 此处是咨询过JAVA开发任明明,按照老代码的实现方式判断,更新支付方式
$paymentRecod = OrderData::updateOrderPayment($orderCode, 2, $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');
if (empty($openId)) {
UdpLog::info('【wechat支付】获取wechat标识为空', 'orderCode:'.$orderCode.'uid:'.$uid.'返回openId:'.$openId);
break;
}
//统一下单
$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", (int) $orderDetail['data']['create_time'] + 7200));
$input->SetNotify_url(WxPayConfig::NOTIFY_URL);
$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 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);
}
}