PayMchNotifyService.java 7.3 KB
Newer Older
D
dingzhiwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2021-2031, 河北计全科技有限公司 (https://www.jeequan.com & jeequan@126.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jeequan.jeepay.pay.service;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchNotifyRecord;
import com.jeequan.jeepay.core.entity.PayOrder;
21
import com.jeequan.jeepay.core.entity.RefundOrder;
D
dingzhiwei 已提交
22
import com.jeequan.jeepay.core.utils.JeepayKit;
D
dingzhiwei 已提交
23
import com.jeequan.jeepay.core.utils.StringKit;
D
dingzhiwei 已提交
24
import com.jeequan.jeepay.pay.mq.queue.MqQueue4PayOrderMchNotify;
25 26
import com.jeequan.jeepay.pay.rqrs.payorder.QueryPayOrderRS;
import com.jeequan.jeepay.pay.rqrs.refund.QueryRefundOrderRS;
D
dingzhiwei 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import com.jeequan.jeepay.service.impl.MchNotifyRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
* 商户通知 service
*
* @author terrfly
* @site https://www.jeepay.vip
* @date 2021/6/8 17:43
*/
@Slf4j
@Service
public class PayMchNotifyService {

    @Autowired private MchNotifyRecordService mchNotifyRecordService;
    @Autowired private MqQueue4PayOrderMchNotify mqPayOrderMchNotifyQueue;
46
    @Autowired private ConfigContextService configContextService;
D
dingzhiwei 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


    /** 商户通知信息, 只有订单是终态,才会发送通知, 如明确成功和明确失败 **/
    public void payOrderNotify(PayOrder dbPayOrder){

        try {
            // 通知地址为空
            if(StringUtils.isEmpty(dbPayOrder.getNotifyUrl())){
                return ;
            }

            //获取到通知对象
            MchNotifyRecord mchNotifyRecord = mchNotifyRecordService.findByPayOrder(dbPayOrder.getPayOrderId());

            if(mchNotifyRecord != null){

                log.info("当前已存在通知消息, 不再发送。");
                return ;
            }

67 68 69
            //商户app私钥
            String appSecret = configContextService.getMchAppConfigContext(dbPayOrder.getMchNo(), dbPayOrder.getAppId()).getMchApp().getAppSecret();

D
dingzhiwei 已提交
70
            // 封装通知url
71
            String notifyUrl = createNotifyUrl(dbPayOrder, appSecret);
D
dingzhiwei 已提交
72 73 74 75 76 77
            mchNotifyRecord = new MchNotifyRecord();
            mchNotifyRecord.setOrderId(dbPayOrder.getPayOrderId());
            mchNotifyRecord.setOrderType(MchNotifyRecord.TYPE_PAY_ORDER);
            mchNotifyRecord.setMchNo(dbPayOrder.getMchNo());
            mchNotifyRecord.setMchOrderNo(dbPayOrder.getMchOrderNo()); //商户订单号
            mchNotifyRecord.setIsvNo(dbPayOrder.getIsvNo());
terrfly's avatar
terrfly 已提交
78
            mchNotifyRecord.setAppId(dbPayOrder.getAppId());
D
dingzhiwei 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
            mchNotifyRecord.setNotifyUrl(notifyUrl);
            mchNotifyRecord.setResResult("");
            mchNotifyRecord.setNotifyCount(0);
            mchNotifyRecord.setState(MchNotifyRecord.STATE_ING); // 通知中
            mchNotifyRecordService.save(mchNotifyRecord);

            //推送到MQ
            Long notifyId = mchNotifyRecord.getNotifyId();
            mqPayOrderMchNotifyQueue.send(notifyId + "");

        } catch (Exception e) {
            log.error("推送失败!", e);
        }
    }

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
    /** 商户通知信息,退款成功的发送通知 **/
    public void refundOrderNotify(RefundOrder dbRefundOrder){

        try {
            // 通知地址为空
            if(StringUtils.isEmpty(dbRefundOrder.getNotifyUrl())){
                return ;
            }

            //获取到通知对象
            MchNotifyRecord mchNotifyRecord = mchNotifyRecordService.findByRefundOrder(dbRefundOrder.getRefundOrderId());

            if(mchNotifyRecord != null){

                log.info("当前已存在通知消息, 不再发送。");
                return ;
            }

            //商户app私钥
            String appSecret = configContextService.getMchAppConfigContext(dbRefundOrder.getMchNo(), dbRefundOrder.getAppId()).getMchApp().getAppSecret();

            // 封装通知url
            String notifyUrl = createNotifyUrl(dbRefundOrder, appSecret);
            mchNotifyRecord = new MchNotifyRecord();
            mchNotifyRecord.setOrderId(dbRefundOrder.getRefundOrderId());
            mchNotifyRecord.setOrderType(MchNotifyRecord.TYPE_REFUND_ORDER);
            mchNotifyRecord.setMchNo(dbRefundOrder.getMchNo());
            mchNotifyRecord.setMchOrderNo(dbRefundOrder.getMchRefundNo()); //商户订单号
            mchNotifyRecord.setIsvNo(dbRefundOrder.getIsvNo());
            mchNotifyRecord.setAppId(dbRefundOrder.getAppId());
            mchNotifyRecord.setNotifyUrl(notifyUrl);
            mchNotifyRecord.setResResult("");
            mchNotifyRecord.setNotifyCount(0);
            mchNotifyRecord.setState(MchNotifyRecord.STATE_ING); // 通知中
            mchNotifyRecordService.save(mchNotifyRecord);

            //推送到MQ
            Long notifyId = mchNotifyRecord.getNotifyId();
            mqPayOrderMchNotifyQueue.send(notifyId + "");

        } catch (Exception e) {
            log.error("推送失败!", e);
        }
    }

D
dingzhiwei 已提交
139 140 141 142

    /**
     * 创建响应URL
     */
143
    public String createNotifyUrl(PayOrder payOrder, String appSecret) {
D
dingzhiwei 已提交
144 145 146 147 148

        QueryPayOrderRS queryPayOrderRS = QueryPayOrderRS.buildByPayOrder(payOrder);
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(queryPayOrderRS);
        jsonObject.put("reqTime", System.currentTimeMillis()); //添加请求时间

D
dingzhiwei 已提交
149
        // 报文签名
150
        jsonObject.put("sign", JeepayKit.getSign(jsonObject, appSecret));
D
dingzhiwei 已提交
151

D
dingzhiwei 已提交
152 153
        // 生成通知
        return StringKit.appendUrlQuery(payOrder.getNotifyUrl(), jsonObject);
D
dingzhiwei 已提交
154 155 156
    }


157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    /**
     * 创建响应URL
     */
    public String createNotifyUrl(RefundOrder refundOrder, String appSecret) {

        QueryRefundOrderRS queryPayOrderRS = QueryRefundOrderRS.buildByRefundOrder(refundOrder);
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(queryPayOrderRS);
        jsonObject.put("reqTime", System.currentTimeMillis()); //添加请求时间

        // 报文签名
        jsonObject.put("sign", JeepayKit.getSign(jsonObject, appSecret));

        // 生成通知
        return StringKit.appendUrlQuery(refundOrder.getNotifyUrl(), jsonObject);
    }


D
dingzhiwei 已提交
174 175 176
    /**
     * 创建响应URL
     */
177
    public String createReturnUrl(PayOrder payOrder, String appSecret) {
D
dingzhiwei 已提交
178 179 180 181 182 183 184 185 186

        if(StringUtils.isEmpty(payOrder.getReturnUrl())){
            return "";
        }

        QueryPayOrderRS queryPayOrderRS = QueryPayOrderRS.buildByPayOrder(payOrder);
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(queryPayOrderRS);
        jsonObject.put("reqTime", System.currentTimeMillis()); //添加请求时间

D
dingzhiwei 已提交
187
        // 报文签名
188
        jsonObject.put("sign", JeepayKit.getSign(jsonObject, appSecret));   // 签名
D
dingzhiwei 已提交
189

D
dingzhiwei 已提交
190 191
        // 生成跳转地址
        return StringKit.appendUrlQuery(payOrder.getReturnUrl(), jsonObject);
D
dingzhiwei 已提交
192 193 194 195

    }

}