Pay.php
8.0 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Created by PhpStorm.
* User: 86132
* Date: 2020/10/27
* Time: 18:46
*/
namespace app\api\controller;
use think\Db;
use app\common\controller\Api;
/**
* 支付接口
*/
class Pay extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 支付接口
* @ApiTitle (协议支付)
* @ApiSummary (协议支付)
* @ApiMethod (POST)
* @ApiRoute (/api/Pay/AgreementPay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="OrderSn", type="integer", required=true, description="订单号")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function AgreementPay()
{
$UserId = $this->IsToken($this->request->header());
$OrderSn = input('OrderSn');
$UserInfo = Db::name('user')->where('id', $UserId)->find();
$PayOrder = $this->PayOrder();
$domain = $this->request->domain();
$OrderInfo = Db::name('agreement')->where('OrderSn', $OrderSn)->find();
$Money = $OrderInfo['UpMoney'] + $OrderInfo['MonthMoney'] * 3;
//生成交易流水
$data = [
'PayOrder' => $PayOrder,
'OrderSn' => $OrderSn,
'type' => 1,
'status' => 0,
'user_id' => $UserId,
'money' => $Money,
'createtime' => time(),
'updatetime' => time()
];
$InsertPayOrder = Db::name('pay_order')->insert($data);
if (!$InsertPayOrder) {
$this->error('支付订单生成失败', 0);
}
$data = [
'amount' => $Money,
'orderid' => $PayOrder,
'type' => "wechat",
'title' => "协议开通",
'notifyurl' => $domain . "/api/pay/AgreementPayNotify/paytype/wechat",
'returnurl' => $domain . "/api/pay/baidu",
'method' => "miniapp",
'openid' => $UserInfo['openid']
];
$return = \addons\epay\library\Service::submitOrder($data);
echo $return;
}
/**
* 订单接口
* @ApiTitle (协议支付回调)
* @ApiSummary (协议支付回调)
* @ApiMethod (POST)
* @ApiRoute (/api/Pay/AgreementPayNotify)
* @ApiParams (name="paytype", type="string", required=true, description="")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function OrderNotify()
{
$paytype = $this->request->param('paytype');
$pay = \addons\epay\library\Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
die;
}
$data = $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
//$data['out_trade_no'] 支付单号
$order_sn = $data['out_trade_no'];
$order = Db::name('pay_order')->where('PayOrder', $order_sn)->find();
if ($order['status'] == 2) {
return $pay->success('支付成功');
die;
}
//在此编写订单逻辑
//修改支付订单状态
$UpdateStatus = Db::name('pay_order')->where('PayOrder', $data['out_trade_no'])->update(
[
'status' => 1,
'updatetime' => time(),
'WeChatOrder' => $data['transaction_id']
]
);
if (!$UpdateStatus) {
$this->error('修改支付订单状态失败', 0);
die;
}
} catch (Exception $e) {
}
echo $pay->success('支付成功');
}
/**
* 支付接口
* @ApiTitle (充值租金)
* @ApiSummary (充值租金)
* @ApiMethod (POST)
* @ApiRoute (/api/Pay/AddMoney)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="OrderSn", type="integer", required=true, description="订单号")
* @ApiParams (name="Money", type="integer", required=true, description="充值金额")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function AddMoney()
{
$UserId = $this->IsToken($this->request->header());
$OrderSn = input('OrderSn');
$Money = input('Money');
$UserInfo = Db::name('user')->where('id', $UserId)->find();
$PayOrder = $this->PayOrder();
$domain = $this->request->domain();
//生成交易流水
$data = [
'PayOrder' => $PayOrder,
'OrderSn' => $OrderSn,
'type' => 1,
'status' => 0,
'user_id' => $UserId,
'money' => $Money,
'createtime' => time(),
'updatetime' => time()
];
$InsertPayOrder = Db::name('pay_order')->insert($data);
if (!$InsertPayOrder) {
$this->error('支付订单生成失败', 0);
}
$data = [
'amount' => $Money,
'orderid' => $PayOrder,
'type' => "wechat",
'title' => "充值租金",
'notifyurl' => $domain . "/api/pay/AddMoneyNotify/paytype/wechat",
'returnurl' => $domain . "/api/pay/baidu",
'method' => "miniapp",
'openid' => $UserInfo['openid']
];
$return = \addons\epay\library\Service::submitOrder($data);
echo $return;
}
/**
* 订单接口
* @ApiTitle (充值租金回调)
* @ApiSummary (充值租金回调)
* @ApiMethod (POST)
* @ApiRoute (/api/Pay/AddMoneyNotify)
* @ApiParams (name="paytype", type="string", required=true, description="")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function AddMoneyNotify()
{
$paytype = $this->request->param('paytype');
$pay = \addons\epay\library\Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
die;
}
$data = $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
//$data['out_trade_no'] 支付单号
$order_sn = $data['out_trade_no'];
$order = Db::name('pay_order')->where('PayOrder', $order_sn)->find();
if ($order['status'] == 2) {
return $pay->success('支付成功');
die;
}
//在此编写订单逻辑
//修改支付订单状态
$UpdateStatus = Db::name('pay_order')->where('PayOrder', $data['out_trade_no'])->update(
[
'status' => 1,
'updatetime' => time(),
'WeChatOrder' => $data['transaction_id']
]
);
$OrderInfo = Db::name('agreement')->where('OrderSn', $order['OrderSn'])->find();
$Money = $OrderInfo['Money'] + $order['money'];
Db::name('agreement')->where('OrderSn', $order['OrderSn'])->update(['Money' => $Money]);
if (!$UpdateStatus) {
$this->error('修改支付订单状态失败', 0);
die;
}
} catch (Exception $e) {
}
echo $pay->success('支付成功');
}
}