PayController.php
4.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
<?php
namespace api\index\controller;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\Db;
/**
* 微信支付,退款,提现DEMO
* Class PayController
* @package app\portal\controller
*/
class PayController extends HomeBaseController
{
protected $options;
function _initialize()
{
parent::_initialize();
$this->options = [
'app_id' => 'wxd7010deb3b696146',
'secret' => '282f4cc8536523076c9d16af6331ffb6',
'payment' => [
'merchant_id' => '1563087351',
'key' => 'P1GSxsjA4Ts3g6V95FVE49bNQLClGckS',
'cert_path' => '../wechat/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => '../wechat/cert/apiclient_key.pem', // XXX: 绝对路径!!!!
]
];
}
/**
* 微信支付
* @param $data
* @return mixed
*/
public function index($data)
{
$attributes = [
'trade_type' => 'NATIVE',
'body' => '废品回收',
'detail' => '站内充值',
'out_trade_no' => $data['num'],
'total_fee' => $data['total'] * 100, // 单位:分
'notify_url' => cmf_api_url('index/pay/notify', '', '', true), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'openid' => $data['openid'], // trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识,
];
$order = new Order($attributes);
$app = new Application($this->options);
$payment = $app->payment;
$result = $payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
return $result->code_url;
} else {
$this->error('支付参数错误', '', $result);
}
}
/**
* 支付回调
* @throws \EasyWeChat\Core\Exceptions\FaultException
*/
public function notify()
{
cache('nnn', 111);
$app = new Application($this->options);
$response = $app->payment->handleNotify(function ($notify, $successful) {
cache('notify', $notify);
cache('successful', $successful);
/*这里是支付回调逻辑处理,一下是DEMO*/
// 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
$out_trade_no=$notify->out_trade_no;
$order = Db::name('order')->where('num',$out_trade_no)->find();
if (!$order) { // 如果订单不存在
return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
// 如果订单存在
// 检查订单是否已经更新过支付状态
if ($order['pay_time']>0) {
return true;
}
// 用户是否支付成功
if ($successful) {
$update['pay_time']=time();
$update['status'] = 2;
$arr = Db::name('pay')
->where('id',$order['pay_id'])
->field('integral')
->find();
$list = Db::name('integral')
->where('user_id',$order['user_id'])
->find();
$res['now'] = $list['now'];
$res['before_wallet'] = $list['before_wallet'];
$res['total'] = $arr['integral'];
$res['after_wallet'] = $arr['before_wallet'] + $arr['integral'];
$res['add_total'] = $res['add_total'] + $arr['integral'];
$res['type'] = 1;
Db::name('integral')
->where('user_id',$order['user_id'])
->update($res);
} else { // 用户支付失败
$update['status']=1;
}
Db::name('order')->where('num',$out_trade_no)->update($update);
return true; // 返回处理完成
});
$response->send();
}
/**
* 查询订单
*/
public function checkOrder()
{
$app = new Application($this->options);
$payment = $app->payment;
$orderNo = "2018080397545210";//商户系统内部的订单号(out_trade_no)
$result = $payment->query($orderNo);
var_dump($result);
}
/**
* 生成签名DEMO
* @return string
*/
public function getSignatureDemo()
{
$timeStamp = time();
$randomStr = cmf_random_string(8);
$arithmetic['timeStamp'] = $timeStamp;
$arithmetic['randomStr'] = $randomStr;
$signature = arithmetic($arithmetic);
$notifyParam = array('t' => $timeStamp, 'r' => $randomStr, 's' => $signature);
$url = url('your url 表达式', $notifyParam, true, true);
return $url;
}
}