admin
2021-07-10 29918ba877731850d001fb2d1a3f3774698241c1
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
237
238
239
240
241
242
243
244
245
package com.ks.vip.service.remote;
 
import org.apache.dubbo.config.annotation.Service;
import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.ks.vip.exception.OrderPayException;
import com.ks.vip.exception.VipOrderException;
import com.ks.vip.mapper.VipOrderMapper;
import com.ks.vip.pojo.DO.VipCombo;
import com.ks.vip.pojo.DO.VipOrder;
import com.ks.vip.pojo.DTO.PayWayInfoDTO;
import com.ks.vip.pojo.DTO.VipOrderDTO;
import com.ks.vip.pojo.Enums.OrderTypeEnum;
import com.ks.vip.pojo.Enums.PayWayEnum;
import com.ks.vip.service.VipCenterService;
import com.ks.vip.service.VipComboService;
import com.ks.vip.service.VipOrederPayService;
import com.ks.vip.util.Constant;
import com.ks.vip.util.PayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.alipay.AlipayH5PayUtil;
import org.yeshi.utils.entity.wx.WXPayOrderInfoV3;
import org.yeshi.utils.wx.WXPayV3Util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
 
@Service(version = "1.0")
public class VipOrderPayServiceImpl implements VipOrederPayService {
 
    Logger logger = LoggerFactory.getLogger(VipOrederPayService.class);
 
    @Resource
    private VipOrderMapper vipOrderMapper;
 
    @Resource
    private VipComboService vipComboService;
 
    @Resource
    private VipCenterService vipCenterService;
 
    @Resource
    private JedisPool jedisPool;
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayWayInfoDTO addOrderWithPay(VipOrderDTO t, String notifyUrl, String returnUrl, String ip) throws VipOrderException {
        if (t.getComboId() == null || t.getComboId() <= 0) {
            throw new VipOrderException(1, "套餐ID不能为空");
        }
        if (StringUtil.isNullOrEmpty(t.getUid())) {
            throw new VipOrderException(1, "用户ID不能为空");
        }
        if (t.getPayWay() == null) {
            throw new VipOrderException(1, "付款方式不能为空");
        }
        if (StringUtil.isNullOrEmpty(t.getThreeOrderId())) {
            throw new VipOrderException(1, "付款订单号不能为空");
        }
 
        // 检查是否已记录
        VipOrder order = vipOrderMapper.getByThreeOrderId(t.getThreeOrderId());
        if (order != null) {
            throw new VipOrderException(1, "该付款订单号已存在");
        }
 
        // 判断订单类型: 首单、续费
        OrderTypeEnum orderType = OrderTypeEnum.buyNewly;
        long count = vipOrderMapper.countByTypeAndUid(t.getUid(), orderType);
        if (count > 0) {
            orderType = OrderTypeEnum.buyRenew;
        }
 
        // 插入订单信息
        VipOrder newOrder = new VipOrder();
        newOrder.setState(VipOrder.STATE_INIT);
        newOrder.setComboId(t.getComboId());
        newOrder.setUid(t.getUid());
        newOrder.setThreeOrderId(t.getThreeOrderId());
        newOrder.setPayWay(t.getPayWay().name());
        newOrder.setType(orderType);
        newOrder.setPayMoney(t.getPayMoney());
        newOrder.setCreateTime(new Date());
        vipOrderMapper.insertSelective(newOrder);
 
        if (returnUrl != null) {
            returnUrl += "?id=" + newOrder.getId();
        }
        // 支付方式
        try {
            return payOrder(newOrder, notifyUrl, returnUrl, ip);
        } catch (OrderPayException e) {
            e.printStackTrace();
            throw new VipOrderException(VipOrderException.CODE_CREATE_PAY_ORDER_FAIL, e.getMsg());
        }
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayWayInfoDTO payOrder(VipOrder order, String notifyUrl, String returnUrl, String ip) throws OrderPayException {
        if (order.getState() == VipOrder.STATE_PAY) {
            throw new OrderPayException(OrderPayException.CODE_ALREADY_PAY, "已经完成支付");
        }
        String goodsTitle = Constant.ORDER_PAY_TITLE;
        String orderId = order.getThreeOrderId() + "";
        switch (order.getPayWay()) {
            //生成支付宝支付订单
            case "alipay":
                String form = PayUtil.getVipChargeAlipayForm(goodsTitle, orderId, order.getPayMoney(), notifyUrl, returnUrl);
                if (StringUtil.isNullOrEmpty(form)) {
                    throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, "创建支付宝支付订单失败");
                }
                //暂存2分钟
                String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                Jedis jedis = jedisPool.getResource();
                try {
                    jedis.set(id, form);
                    jedis.expire(id, 120);
                } finally {
                    jedis.close();
                }
                return new PayWayInfoDTO(PayWayEnum.alipay, null, id);
            case "weChat":
                try {
                    String payUrl = PayUtil.createWXOrder(ip, orderId, order.getPayMoney(), goodsTitle, notifyUrl, returnUrl);
                    return new PayWayInfoDTO(PayWayEnum.weChat, payUrl, null);
                } catch (Exception e) {
                    logger.error("创建微信支付出错", e);
                    throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, e.getMessage());
                }
        }
        return null;
    }
 
    @Override
    public String getAlipayForm(String formId) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.get(formId);
        } finally {
            jedis.close();
        }
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public VipOrder checkOrderPayState(Long id) throws VipOrderException, OrderPayException {
        VipOrder order = vipOrderMapper.selectByPrimaryKey(id);
        if (order == null) {
            throw new VipOrderException(VipOrderException.CODE_NOT_EXIST, "订单不存在");
        }
        if (order.getState() == VipOrder.STATE_PAY) {
            return order;
        }
        switch (order.getPayWay()) {
            //支付宝
            case "alipay":
                AlipayTradeQueryResponse res = null;
                try {
                    res = AlipayH5PayUtil.queryOrder(PayUtil.getAlipayApp(), order.getThreeOrderId(), null);
                    //支付成功
                    if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
                        return paySuccess(order);
                    } else {
                        throw new OrderPayException(OrderPayException.CODE_PAY_FAIL, "支付宝付款未成功");
                    }
                } catch (AlipayApiException e) {
                    throw new OrderPayException(OrderPayException.CODE_API_ERROR, "支付宝接口请求出错:" + e.getErrMsg());
                }
                //微信
            case "weChat":
                WXPayOrderInfoV3 info = null;
                try {
                    info = WXPayV3Util.getPayOrderInfo(order.getThreeOrderId(), PayUtil.getWXAPP());
                } catch (Exception e) {
                    throw new OrderPayException(OrderPayException.CODE_API_ERROR, "微信支付接口请求出错:" + e.getMessage());
                }
                if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
                    paySuccess(order);
                    return order;
                } else {
                    throw new OrderPayException(OrderPayException.CODE_PAY_FAIL, "微信付款未成功");
                }
            default:
                throw new OrderPayException(OrderPayException.CODE_PAY_WAY_NOT_EXIST, "付款方式不存在");
        }
    }
 
    @Override
    public String getWXPaySuccessOutOrderNo(String notifyContent) throws Exception {
        return WXPayV3Util.getPaySuccessOutOrderNo(notifyContent, PayUtil.getWXAPP());
    }
 
    @Override
    public String getAlipayPaySuccessOutOrderNo(Map<String, String> params) throws Exception {
        return AlipayH5PayUtil.getPaySuccessNotifyOutOrderNo(params, PayUtil.getAlipayApp());
    }
 
 
    /**
     * 支付成功更新处理
     *
     * @param order
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public VipOrder paySuccess(VipOrder order) throws VipOrderException {
 
        VipCombo vipCombo = vipComboService.selectByPrimaryKey(order.getComboId());
        if (vipCombo == null) {
            throw new VipOrderException(1, "该套餐不存在");
        }
        // 有效天数
        Integer validDays = vipCombo.getValidDays();
 
        // 判断订单类型
        OrderTypeEnum orderType = OrderTypeEnum.buyNewly;
        long count = vipOrderMapper.countByTypeAndUid(order.getUid(), orderType);
        if (count > 0) {
            orderType = OrderTypeEnum.buyRenew;
        }
 
        // order.setThreeOrderId(); TODO
        order.setState(VipOrder.STATE_PAY);
        order.setPayTime(new Date());
        order.setUpdateTime(new Date());
        vipOrderMapper.updateByPrimaryKeySelective(order);
 
        // 更新等级
        vipCenterService.saveVipCenter(order.getUid(), vipCombo.getGradeId(), validDays * 24, order.getId());
 
        return order;
    }
 
}