PayController.php
5.9 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
<?php
namespace app\user\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use think\Log;
use think\Validate;
/**
* 微信支付,退款,提现DEMO
* Class PayController
* @package app\portal\controller
*/
class PayController extends HomeBaseController
{
public function _initialize(){
parent::_initialize();
require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Api.php";
require_once EXTEND_PATH . "WxpayAPI/example/WxPay.JsApiPay.php";
require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Notify.php";
require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Merch.php";
require_once EXTEND_PATH . "WxpayAPI/example/log.php";
}
/**
* 微信支付
* @throws \WxPayException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index(){
//todo 支付逻辑处理
$order_sn=cmf_get_order_sn();
$tools = new \JsApiPay();
$user=cmf_get_current_user();
if(empty($user)){
$this->error('用户尚未登录');
}
$openId = $user['openid'];
if(empty($openId)){
$this->error('微信OPENID不能为空');
}
$real_price=0.01;//测试金额
$total_fee=floatval($real_price)*100;
if($total_fee<=0){
$this->error('支付金额不能小于0');
}
$timeStamp = time();
$randomStr = cmf_random_string(8);
$arithmetic['timeStamp']= $timeStamp;
$arithmetic['randomStr']= $randomStr;
$arithmetic['orderSn']= $order_sn;
$signature = arithmetic($arithmetic);
$notifyParam=array('t'=>$timeStamp,'r'=>$randomStr,'s'=>$signature,'o'=>$order_sn);
$url=url('portal/pay/notify',$notifyParam,true,true);
$input = new \WxPayUnifiedOrder();
$input->SetBody("服务器续费");
$input->SetAttach($order_sn);
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($total_fee);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag($total_fee);
$input->SetNotify_url($url);
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = \WxPayApi::unifiedOrder($input);
if(!empty($order['return_code'])){
if($order['return_code']=='FAIL'){
$this->error('支付参数错误','',$order);
}
}
$jsApiParameters = $tools->GetJsApiParameters($order);
$this->success('获取支付参数成功','',json_decode($jsApiParameters,true));
}
/**
* 支付回调
*/
public function notify(){
$param=$this->request->param();
$signature = $param['s'];
$arithmetic['timeStamp']= $param['t'];
$arithmetic['randomStr']= $param['r'];
$arithmetic['orderSn']= $param['o'];
$str = arithmetic($arithmetic);
if($str != $signature){
$data['code'] = 40002;
$data['msg'] = '签名验证失败';
Log::write('签名验证失败'.$param['o']);
exit;
}
$notify = new \WxPayNotify();
$notify->Handle(false);
$xml = file_get_contents("php://input");
$base = new \WxPayResults();
$data = $base->FromXml($xml);
if($base->CheckSign() == true){
if ($data["return_code"] == "FAIL") {
Log::write('支付失败1','error');
}elseif($data["result_code"] == "FAIL"){
Log::write('支付失败2','error');
}else{
//todo 回调处理
}
}
}
/**
* 退款
* @throws \WxPayException
*/
public function refund(){
$param = $this->request->param();
$signature = $param['s'];
$arithmetic['timeStamp']= $param['t'];
$arithmetic['randomStr']= $param['r'];
$arithmetic['orderSn']= $param['o'];
$str = arithmetic($arithmetic);
if($str != $signature){
$this->error('签名验证失败');
}
/*微信退款*/
require_once EXTEND_PATH."WxpayAPI/lib/WxPay.Api.php";
require_once EXTEND_PATH."WxpayAPI/example/log.php";
if(isset($info["transaction_id"]) && empty($info["transaction_id"])){
$transaction_id = $info["transaction_id"];
$total_fee = $info["real_price"]*100;
$refund_fee = $info["real_price"]*100;
$input = new \WxPayRefund();
$input->SetTransaction_id($transaction_id);
$input->SetTotal_fee($total_fee);
$input->SetRefund_fee($refund_fee);
$input->SetOut_refund_no(\WxPayConfig::MCHID.date("YmdHis"));
$input->SetOp_user_id(\WxPayConfig::MCHID);
$ret=\WxPayApi::refund($input);
if($ret['result_code']=='SUCCESS'){
//todo 退款成功处理
$this->success('退款成功');
}else{
$this->error("退款失败:".$ret['err_code_des']);
}
}else{
$this->error('缺少退款标识');
}
}
/**
* 提现
*/
public function withdraw_cash(){
$param = $this->request->param();
$signature = $param['s'];
$arithmetic['timeStamp']= $param['t'];
$arithmetic['randomStr']= $param['r'];
$arithmetic['orderSn']= $param['o'];
$str = arithmetic($arithmetic);
if($str != $signature){
$this->error('签名验证失败');
}
$price=1;
$openid='';
$merch=new \MerchPay();
$trade_no = cmf_get_order_sn();
$res=$merch->pay($openid,$trade_no,$price,'提现');
if($res['result_code']=='SUCCESS'){
//todo 提现成功处理
}else{
$this->error('操作失败:'.$res['return_msg']);
}
}
}