PayMchNotifyService.java 4.7 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 21 22
/*
 * 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.MchInfo;
import com.jeequan.jeepay.core.entity.MchNotifyRecord;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.utils.JeepayKit;
D
dingzhiwei 已提交
23
import com.jeequan.jeepay.core.utils.StringKit;
D
dingzhiwei 已提交
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
import com.jeequan.jeepay.pay.mq.queue.MqQueue4PayOrderMchNotify;
import com.jeequan.jeepay.pay.rqrs.QueryPayOrderRS;
import com.jeequan.jeepay.service.impl.MchInfoService;
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 MchInfoService mchInfoService;
    @Autowired private MqQueue4PayOrderMchNotify mqPayOrderMchNotifyQueue;


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

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

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

            if(mchNotifyRecord != null){

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

            //构建数据
            MchInfo mchInfo = mchInfoService.getById(dbPayOrder.getMchNo());
            // 封装通知url
            String notifyUrl = createNotifyUrl(dbPayOrder, mchInfo.getPrivateKey());
            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());
            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);
        }
    }


    /**
     * 创建响应URL
     */
    public String createNotifyUrl(PayOrder payOrder, String mchKey) {

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

D
dingzhiwei 已提交
102 103
        // 报文签名
        jsonObject.put("sign", JeepayKit.getSign(jsonObject, mchKey));
D
dingzhiwei 已提交
104

D
dingzhiwei 已提交
105 106
        // 生成通知
        return StringKit.appendUrlQuery(payOrder.getNotifyUrl(), jsonObject);
D
dingzhiwei 已提交
107 108 109 110 111 112
    }


    /**
     * 创建响应URL
     */
113
    public String createReturnUrl(PayOrder payOrder, String appSecret) {
D
dingzhiwei 已提交
114 115 116 117 118 119 120 121 122

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

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

D
dingzhiwei 已提交
123
        // 报文签名
124
        jsonObject.put("sign", JeepayKit.getSign(jsonObject, appSecret));   // 签名
D
dingzhiwei 已提交
125

D
dingzhiwei 已提交
126 127
        // 生成跳转地址
        return StringKit.appendUrlQuery(payOrder.getReturnUrl(), jsonObject);
D
dingzhiwei 已提交
128 129 130 131

    }

}