Wxpay.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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use addons\epay\library\Service;
use app\admin\model\Porder;
use think\Db;
use Yansongda\Pay\Pay;
use think\Log;
use fast\Http;
use think\Validate;
/**
* 支付接口**
*/
class Wxpay extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $order_status = [];//订单状态
public function _initialize()
{
parent::_initialize();
$this->user_id = $this->auth->getUserId();
$this->order_status = config('site.order_status');
}
/**
* @ApiTitle (支付订单)
* @ApiSummary (支付订单)
* @ApiMethod (POST)
* @ApiRoute (/api/wxpay/pay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
* @ApiParams (name="pay_order_sn", type="string", required=true, description="支付订单号")
* @ApiReturn
*/
public function pay(){
if($this->request->isPost()){
//创建支付对象
$pay = Pay::wechat(Service::getConfig('wechat'));
$openid = $this->request->post('openid');//小程序传递openid
$pay_order_sn = $this->request->post('pay_order_sn');//支付订单号
if(empty($openid) && empty($pay_order_sn)){
$this->error('无效的参数');
}
//查询订单是否已支付
$is_pay = Db::table('gc_porder')
->where(['pay_order_sn' => $pay_order_sn, 'uid' => $this->user_id])
->where('status','>',0)
->select();
if ($is_pay) {
$this->error('该订单已支付过了');
}
//是否有此订单
$exist = Db::table('gc_porder')
->where(['pay_order_sn' => $pay_order_sn, 'uid' => $this->user_id, 'status' => $this->order_status[0]])
->select();
if (!$exist) {
$this->error('无效的订单');
}
//计算总价格
$total_price = 0;
foreach ($exist as $value) {
$total_price += $value['total_price'];
}
//构建订单信息
$order = [
'out_trade_no' => $pay_order_sn,//支付订单号
'body' => '广西小纸皮再生资源回收有限公司',
'total_fee' => 1,
//'total_fee' => floatval($total_price)*100,//单位:分
'openid' => $openid,
'notify_url' => $this->request->root(true) . '/api/Wxpay/notify',
];
//跳转或输出
$this->success('成功',$pay->miniapp($order));
}else{
$this->error('请求方式错误');
}
}
/**
* 支付成功回调
*/
public function notify(){
$pay = Service::checkNotify('wechat');
if (!$pay) {
$this->error('签名错误');
}
//你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
$data = $pay->verify();
$porderModel = new Porder();
$porderModel->where(['uid' => 26,'p_id'=>6])->update(['status'=>1]);
//下面这句必须要执行,且在此之前不能有任何输出
echo $pay->success();
return;
}
}