Recharge.php
3.5 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
<?php
namespace app\index\controller;
use addons\recharge\model\MoneyLog;
use app\common\controller\Frontend;
use think\Exception;
/**
*
*/
class Recharge extends Frontend
{
protected $layout = 'default';
protected $noNeedLogin = ['pay', 'epay'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
/**
* 充值余额
* @return string
*/
public function recharge()
{
$config = get_addon_config('recharge');
$moneyList = [];
foreach ($config['moneylist'] as $index => $item) {
$moneyList[] = ['value' => $item, 'text' => $index, 'default' => $item === $config['defaultmoney']];
}
$paytypeList = [];
foreach (explode(',', $config['paytypelist']) as $index => $item) {
$paytypeList[] = ['value' => $item, 'image' => '/assets/addons/recharge/img/' . $item . '.png', 'default' => $item === $config['defaultpaytype']];
}
$this->view->assign('addonConfig', $config);
$this->view->assign('moneyList', $moneyList);
$this->view->assign('paytypeList', $paytypeList);
$this->view->assign('title', __('Recharge'));
return $this->view->fetch();
}
/**
* 余额日志
* @return string
*/
public function moneylog()
{
$moneyloglist = MoneyLog::where(['user_id' => $this->auth->id])
->order('id desc')
->paginate(10);
$this->view->assign('title', __('Balance log'));
$this->view->assign('moneyloglist', $moneyloglist);
return $this->view->fetch();
}
/**
* 创建订单并发起支付请求
* @throws \think\exception\DbException
*/
public function submit()
{
$money = $this->request->request('money');
$paytype = $this->request->request('paytype');
if ($money <= 0) {
$this->error('充值金额不正确');
}
$config = get_addon_config('recharge');
if (isset($config['minmoney']) && $money < $config['minmoney']) {
$this->error('充值金额不能低于' . $config['minmoney'] . '元');
}
try {
\addons\recharge\model\Order::submitOrder($money, $paytype ? $paytype : 'wechat');
} catch (Exception $e) {
$this->error($e->getMessage());
}
return;
}
/**
* 企业支付通知和回调
* @throws \think\exception\DbException
*/
public function epay()
{
$type = $this->request->param('type');
$paytype = $this->request->param('paytype');
if ($type == 'notify') {
$pay = \addons\epay\library\Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
return;
}
$data = $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
\addons\recharge\model\Order::settle($data['out_trade_no'], $payamount);
} catch (Exception $e) {
}
echo $pay->success();
} else {
$pay = \addons\epay\library\Service::checkReturn($paytype);
if (!$pay) {
$this->error('签名错误');
}
$data = $pay->verify();
//你可以在这里定义你的提示信息,但切记不可在此编写逻辑
$this->success("恭喜你!充值成功!", url("user/index"));
}
return;
}
}