Order.php
4.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace addons\recharge\model;
use addons\epay\library\Service;
use app\common\library\Auth;
use app\common\model\User;
use fast\Random;
use think\Exception;
use think\Model;
/**
* 充值订单模型
*/
class Order Extends Model
{
// 表名
protected $name = 'recharge_order';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = '';
// 追加属性
protected $append = [
];
/**
* 发起订单支付
* @param float $money
* @param string $paytype
* @throws Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function submitOrder($money, $paytype = 'wechat')
{
$auth = Auth::instance();
$user_id = $auth->isLogin() ? $auth->id : 0;
$order = self::where('user_id', $user_id)
->where('amount', $money)
->where('status', 'created')
->order('id', 'desc')
->find();
$request = \think\Request::instance();
if (!$order) {
$orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
$data = [
'orderid' => $orderid,
'user_id' => $user_id,
'amount' => $money,
'payamount' => 0,
'paytype' => $paytype,
'ip' => $request->ip(),
'useragent' => $request->server('HTTP_USER_AGENT'),
'status' => 'created'
];
$order = self::create($data);
}
$epay = get_addon_info('epay');
if ($epay && $epay['state']) {
$notifyurl = $request->root(true) . '/index/recharge/epay/type/notify/paytype/' . $paytype;
$returnurl = $request->root(true) . '/index/recharge/epay/type/return/paytype/' . $paytype;
$config = [
'notify_url' => $notifyurl,
'return_url' => $returnurl
];
//创建支付对象
$pay = Service::createPay($paytype, $config);
if ($paytype == 'alipay') {
//支付宝支付,请根据你的需求,仅选择你所需要的即可
$order = [
'out_trade_no' => $order->orderid,//你的订单号
'total_amount' => $order->amount,//单位元
'subject' => "充值",
];
$pay->web($order)->send();
} else {
//微信支付,请根据你的需求,仅选择你所需要的即可
$order = [
'out_trade_no' => $order->orderid,//你的订单号
'body' => "充值",
'total_fee' => $order->amount * 100, //单位分
];
$pay->wap($order)->send();
}
} else {
$result = \think\Hook::listen('recharge_order_submit', $order);
if (!$result) {
throw new Exception("请先在后台安装配置企业支付插件");
}
}
}
/**
* 订单结算
* @param int $orderid
* @param string $payamount
* @param string $memo
* @return bool
* @throws \think\exception\DbException
*/
public static function settle($orderid, $payamount = null, $memo = '')
{
$order = self::getByOrderid($orderid);
if (!$order) {
return false;
}
if ($order['status'] != 'paid') {
$order->payamount = $payamount ? $payamount : $order->amount;
$order->paytime = time();
$order->status = 'paid';
$order->memo = $memo;
$order->save();
// 最新版本可直接使用User::money($order->user_id, $order->amount, '充值');来更新
// 更新会员余额
$user = User::get($order->user_id);
if ($user) {
$before = $user->money;
$after = $user->money + $order->amount;
//更新会员信息
$user->save(['money' => $after]);
//写入日志
MoneyLog::create(['user_id' => $order->user_id, 'money' => $order->amount, 'before' => $before, 'after' => $after, 'memo' => '充值']);
}
}
return true;
}
}