package org.jeecg.modules.pay;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.pay.config.WxPay;

/**
 * 微信付款到账户
 **/
@Slf4j
public class MpWxEntPayService {

    WxPay wxPayConfig;
    WxPayService wxPayService;

    public String entPay(Integer amount, String openId, String desc, String ip, String mchOrderNo) {
        EntPayRequest entPayRequest = new EntPayRequest();
        entPayRequest.setAppid(wxPayConfig.appId);
        entPayRequest.setMchId(wxPayConfig.mchId);
        entPayRequest.setPartnerTradeNo(mchOrderNo);
        entPayRequest.setOpenid(openId);
        entPayRequest.setCheckName("NO_CHECK");
        entPayRequest.setAmount(amount);
        entPayRequest.setDescription(desc);
        entPayRequest.setSpbillCreateIp(ip);
        EntPayResult entPayResult;
        try {
            entPayResult = wxPayService.getEntPayService().entPay(entPayRequest);
            System.err.println("打款结果:" + entPayRequest);
        } catch (WxPayException wpe) {
            return wpe.getErrCodeDes();
        } catch (Exception e) {
            return e.getLocalizedMessage();
        }
        return JSON.toJSONString(entPayResult);
    }


}