AlipayKit.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
/*
 * 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.channel.alipay;

18
import cn.hutool.core.util.StrUtil;
D
dingzhiwei 已提交
19 20 21 22 23 24 25
import com.alipay.api.AlipayObject;
import com.alipay.api.AlipayRequest;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.params.alipay.AlipayIsvParams;
import com.jeequan.jeepay.core.model.params.alipay.AlipayIsvsubMchParams;
26
import com.jeequan.jeepay.pay.model.MchAppConfigContext;
D
dingzhiwei 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
import org.apache.commons.lang3.StringUtils;

/*
* 【支付宝】支付通道工具包
*
* @author terrfly
* @site https://www.jeepay.vip
* @date 2021/6/8 17:19
*/
public class AlipayKit {


    /** 放置 isv特殊信息 **/
40
    public static void putApiIsvInfo(MchAppConfigContext mchAppConfigContext, AlipayRequest req, AlipayObject model){
D
dingzhiwei 已提交
41 42

        //不是特约商户, 无需放置此值
43
        if(!mchAppConfigContext.isIsvsubMch()){
D
dingzhiwei 已提交
44 45 46 47
            return ;
        }

        // 获取支付参数
48 49
        AlipayIsvParams isvParams = mchAppConfigContext.getIsvConfigContext().getIsvParamsByIfCode(CS.IF_CODE.ALIPAY, AlipayIsvParams.class);
        AlipayIsvsubMchParams isvsubMchParams = mchAppConfigContext.getIsvsubMchParamsByIfCode(CS.IF_CODE.ALIPAY, AlipayIsvsubMchParams.class);
D
dingzhiwei 已提交
50 51 52 53 54 55 56 57 58

        // 子商户信息
        if(req instanceof AlipayTradePayRequest) ((AlipayTradePayRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
        else if(req instanceof AlipayTradeAppPayRequest) ((AlipayTradeAppPayRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
        else if(req instanceof AlipayTradeCreateRequest) ((AlipayTradeCreateRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
        else if(req instanceof AlipayTradePagePayRequest) ((AlipayTradePagePayRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
        else if(req instanceof AlipayTradePrecreateRequest) ((AlipayTradePrecreateRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
        else if(req instanceof AlipayTradeWapPayRequest) ((AlipayTradeWapPayRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
        else if(req instanceof AlipayTradeQueryRequest) ((AlipayTradeQueryRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
terrfly's avatar
terrfly 已提交
59
        else if(req instanceof AlipayTradeRefundRequest) ((AlipayTradeRefundRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
60
        else if(req instanceof AlipayTradeFastpayRefundQueryRequest) ((AlipayTradeFastpayRefundQueryRequest)req).putOtherTextParam("app_auth_token", isvsubMchParams.getAppAuthToken());
D
dingzhiwei 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

        // 服务商信息
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId(isvParams.getPid());

        if(model instanceof AlipayTradePayModel) ((AlipayTradePayModel)model).setExtendParams(extendParams);
        else if(model instanceof AlipayTradeAppPayModel) ((AlipayTradeAppPayModel)model).setExtendParams(extendParams);
        else if(model instanceof AlipayTradeCreateModel) ((AlipayTradeCreateModel)model).setExtendParams(extendParams);
        else if(model instanceof AlipayTradePagePayModel) ((AlipayTradePagePayModel)model).setExtendParams(extendParams);
        else if(model instanceof AlipayTradePrecreateModel) ((AlipayTradePrecreateModel)model).setExtendParams(extendParams);
        else if(model instanceof AlipayTradeWapPayModel) ((AlipayTradeWapPayModel)model).setExtendParams(extendParams);
    }


    public static String appendErrCode(String code, String subCode){
        return StringUtils.defaultIfEmpty(subCode, code); //优先: subCode
    }

    public static String appendErrMsg(String msg, String subMsg){

81
        String result = null;
D
dingzhiwei 已提交
82
        if(StringUtils.isNotEmpty(msg) && StringUtils.isNotEmpty(subMsg) ){
83 84 85
            result = msg + "【" + subMsg + "】";
        }else{
            result = StringUtils.defaultIfEmpty(subMsg, msg);
D
dingzhiwei 已提交
86
        }
87
        return StrUtil.maxLength(result, 253);
D
dingzhiwei 已提交
88 89 90
    }

}