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
<?php
namespace app\api\controller\plus\business;
 
use app\api\model\plus\business\Order as BaseOrder;
use app\api\controller\Controller;
use app\api\model\plus\business\Business as CardModel;
use app\api\model\plus\business\Grade as CardGradeModel;
use app\common\enum\order\OrderTypeEnum;
use app\api\service\pay\PayService;
use app\common\enum\order\OrderPayTypeEnum;
use app\api\service\order\paysuccess\type\BusinessCardPaySuccessService;
use app\common\model\app\App as AppModel;
 
/**
 * 名片订单控制器
 */
class Order extends Controller
{
    /**
     * 创建名片购买订单
     */
    public function create()
    {
        // 获取用户信息
        $user = $this->getUser();
        
        // 获取请求参数
        $params = $this->request->param();
 
        
        // 验证参数
        if (!isset($params['business_card_id']) || !isset($params['grade_id'])) {
            return $this->renderError('缺少必要参数');
        }
        
        // 获取名片信息
        $businessCard = CardModel::detail($params['business_card_id']);
        if (!$businessCard) {
            return $this->renderError('名片信息不存在');
        }
        // 获取名片等级信息
        $grade = CardGradeModel::detail($params['grade_id']);
        if (!$grade || $grade['grade_id'] != $params['grade_id']) {
            return $this->renderError('名片等级信息不存在');
        }
 
        // 如果价格为0,直接标记为已支付
        if ($grade['price'] == 0) {
            return $this->renderError('免费购买,无需支付');
        }
        // 检查是否已经购买过
        $orderModel = new BaseOrder();
        if ($orderModel->checkCardPurchase($user['user_id'], $params['business_card_id'])) {
            return $this->renderError('您已购买过该名片');
        }
        $order_no = $orderModel->orderNo();
        // 创建订单
        $orderData = [
            'user_id' => $user['user_id'],
            'business_card_id' => $params['business_card_id'],
            'grade_id' => $params['grade_id'],
            'order_no' => $order_no,
            'pay_price' => $grade['price'],
            'order_source' => isset($params['order_source']) ? $params['order_source'] : 10, // 默认查看名片
            'app_id' => $user['app_id']
        ];
        
        // 保存订单
        if (!$orderModel->save($orderData)) {
            return $this->renderError('订单创建失败');
        }
        $orderModel = $orderModel->where('order_no', $order_no)->find();
        // 需要支付,构建支付参数
        $payType = isset($params['pay_type']) ? $params['pay_type'] : OrderPayTypeEnum::WECHAT;
 
        return $this->renderSuccess('订单创建成功', [
            'order_id' => $orderModel['order_id'],
            'order_no' => $orderModel['order_no'],
            'pay_type' => $payType,
            'order_type' => OrderTypeEnum::BUSINESS_CARD,
            'pay_price' => $grade['price']
        ]);
    }
 
    /**
     * 订单支付
     */
    public function pay()
    {
        // 获取用户信息
        $user = $this->getUser();
 
        // 获取请求参数
        $params = $this->request->param();
        $orderId = $params['order_id'];
 
        // 获取订单详情
        $orderModel = BaseOrder::getDetail($orderId);
        if (!$orderModel) {
            return $this->renderError('订单不存在');
        }
 
        // 验证订单是否属于当前用户
        if ($orderModel['user_id'] != $user['user_id']) {
            return $this->renderError('订单不属于当前用户');
        }
 
        // 订单已支付
        if ($orderModel['pay_status'] == 20) {
            return $this->renderError('订单已支付');
        }
        if ($this->request->isGet()) {
            // 开启的支付类型
            $payTypes = AppModel::getPayType($orderModel['app_id'], $params['pay_source']);
            // 支付金额
            $payPrice = $orderModel['pay_price'];
            $balance = $this->getUser()['balance'];
            return $this->renderSuccess('', compact('payTypes', 'payPrice', 'balance'));
        }
        $payType = $params['payType'];
        $paySource = $params['pay_source'];
        // 构建支付参数
        $payment = $orderModel->orderPayment($user, $orderModel, $payType,$params);
 
        return $this->renderSuccess('', [
            'order_id' => $orderId,
            'business_card_id' => $orderModel['business_card_id'],
            'payPrice' => $orderModel['pay_price'],
            'pay_type' => $params['use_balance']==1 ? OrderPayTypeEnum::BALANCE : $payment['payType'],  // 支付方式
            'payment' => $payment['payment'],   // 微信支付参数
            'order_type' => OrderTypeEnum::BUSINESS_CARD, //订单类型
            'use_balance' => $params['use_balance'],// 是否使用余额
            'return_Url' => $params['pay_source'] == 'h5' ? urlencode(base_url() . "h5/pages/order/myorder") : '', //h5支付跳转地址
        ]);
    }
}