package org.jeecg.modules.pay;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.jeecg.modules.pay.config.WxPay;

import java.io.File;

@SuppressWarnings("all")
public class MpWxPayService {

    public boolean dev = false;
    public WxPay wxPay;
    public WxPayService wxPayService;


    /**
     * 生成二维码
     * @param codeUrl
     * @param logoFile
     * @param sideLength
     * @return
     */
    public Object createScanPayQrcodeMode2(String codeUrl, File logoFile, Integer sideLength){
        try {
            byte[] scanPayQrcodeMode2 = wxPayService.createScanPayQrcodeMode2(codeUrl, null, null);
            return scanPayQrcodeMode2;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }


    /**
     * 扫码支付
     * @param productName
     * @param clientIp
     * @param productId
     * @param price
     * @param orderNo
     * @param openId
     * @param body
     * @return
     */
    public Object createQrOrder(String productName, String clientIp,
                              String productId, Integer price, String orderNo, String body) {
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");         //设备号
        request.setTradeType("NATIVE");        //交易类型
        request.setBody(productName);         //商品描述
        request.setOutTradeNo(orderNo);       //商户订单号
        request.setDetail(productName);       //商品详情
        request.setTotalFee(price);           //总金额|分计
        request.setSpbillCreateIp(clientIp);  //终端IP
        request.setNotifyUrl("http://h5.xzaiyp.top/massage-api/post/notify");//设置回调路径
        request.setProductId(productId);      //商品id
        if (dev) {
            request.setTotalFee(price);
//            request.setNotifyUrl(wxPay.notifyUrlDev);
            request.setNotifyUrl("http://h5.xzaiyp.top/massage-api/post/notify");

        }
        try {
            Object order = wxPayService.unifiedOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }


    /**
     * 微信吊起支付
     * @param productName
     * @param clientIp
     * @param productId
     * @param price
     * @param orderNo
     * @param openId
     * @param body
     * @return
     */
    public Object createOrder(String productName, String clientIp,
                              String productId, Integer price, String orderNo,
                              String openId, String body){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");         //设备号
        request.setTradeType("JSAPI");        //交易类型
        request.setBody(productName);         //商品描述
        request.setOutTradeNo(orderNo);       //商户订单号
        request.setDetail(productName);       //商品详情
        request.setTotalFee(price);           //总金额|分计
        request.setSpbillCreateIp(clientIp);  //终端IP
//        request.setNotifyUrl(wxPay.notifyUrl);//设置回调路径
        request.setNotifyUrl("https://tai-testapi.sanhemovie.com/massage-api/post/notify");//设置回调路径
        request.setProductId(productId);      //商品id
        request.setOpenid(openId);            //JSAPI OPENID
        if (dev){
            request.setTotalFee(price);
//            request.setNotifyUrl(wxPay.notifyUrlDev);
            request.setNotifyUrl("https://tai-testapi.sanhemovie.com/massage-api/post/notify");//设置回调路径
        }
        try {
            Object order = wxPayService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }




    /**
     * 微信吊起支付  -- 加油小程序
     * @param productName
     * @param clientIp
     * @param productId
     * @param price
     * @param orderNo
     * @param openId
     * @param body
     * @return
     */
    public Object createOrderGo(String productName, String clientIp,
                              String productId, Integer price, String orderNo,
                              String openId, String body){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");         //设备号
        request.setTradeType("JSAPI");        //交易类型
        request.setBody(productName);         //商品描述
        request.setOutTradeNo(orderNo);       //商户订单号
        request.setDetail(productName);       //商品详情
        request.setTotalFee(price);           //总金额|分计
        request.setSpbillCreateIp(clientIp);  //终端IP
        request.setNotifyUrl(wxPay.notifyUrl);//设置回调路径
//        request.setNotifyUrl("http://h5.xzaiyp.top/a-notice-api/pay-api/info/notify");//设置回调路径
        request.setProductId(productId);      //商品id
        request.setOpenid(openId);            //JSAPI OPENID
        if (dev){
            request.setTotalFee(price);
            request.setNotifyUrl(wxPay.notifyUrlDev);
//            request.setNotifyUrl("http://h5.xzaiyp.top/a-notice-api/pay-api/info/notify");//设置回调路径
        }
        try {
            Object order = wxPayService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }



    /**
     * 微信吊起支付  -- 付费小程序
     * @param productName
     * @param clientIp
     * @param productId
     * @param price
     * @param orderNo
     * @param openId
     * @param body
     * @return
     */
    public Object createOrderPay(String productName, String clientIp,
                                String productId, Integer price, String orderNo,
                                String openId, String body){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");         //设备号
        request.setTradeType("JSAPI");        //交易类型
        request.setBody(productName);         //商品描述
        request.setOutTradeNo(orderNo);       //商户订单号
        request.setDetail(productName);       //商品详情
        request.setTotalFee(price);           //总金额|分计
        request.setSpbillCreateIp(clientIp);  //终端IP
//        request.setNotifyUrl(wxPay.notifyUrl);//设置回调路径
        request.setNotifyUrl("http://h5.xzaiyp.top/a-notice-api/pay-api/info/notify");//设置回调路径
        request.setProductId(productId);      //商品id
        request.setOpenid(openId);            //JSAPI OPENID
        if (dev){
            request.setTotalFee(price);
//            request.setNotifyUrl(wxPay.notifyUrlDev);
            request.setNotifyUrl("http://h5.xzaiyp.top/a-notice-api/pay-api/info/notify");//设置回调路径
        }
        try {
            Object order = wxPayService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }











    /**
     * 微信吊起支付
     * @param productName
     * @param clientIp
     * @param productId
     * @param price
     * @param orderNo
     * @param openId
     * @param body
     * @return
     */
    public Object createUserOrder(String productName, String clientIp,
                              String productId, Integer price, String orderNo,
                              String openId, String body){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");         //设备号
        request.setTradeType("JSAPI");        //交易类型
        request.setBody(productName);         //商品描述
        request.setOutTradeNo(orderNo);       //商户订单号
        request.setDetail(productName);       //商品详情
        request.setTotalFee(price);           //总金额|分计
        request.setSpbillCreateIp(clientIp);  //终端IP
//        request.setNotifyUrl(wxPay.notifyUrl);//设置回调路径
        request.setNotifyUrl("https://admin.csnaicheng.com/h5-api/api/notify");//设置回调路径
        request.setProductId(productId);      //商品id
        request.setOpenid(openId);            //JSAPI OPENID
        if (dev){
            request.setTotalFee(price);
//            request.setNotifyUrl(wxPay.notifyUrlDev);
            request.setNotifyUrl("https://admin.csnaicheng.com/h5-api/api/notify");//设置回调路径
        }
        try {
            Object order = wxPayService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }














    public WxPayOrderNotifyResult notify(String requestBody){
        WxPayOrderNotifyResult notify = null;
        try {
            notify = wxPayService.parseOrderNotifyResult(requestBody);
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return notify;
    }


    public Object createAppOrder(String productName, String clientIp,
                                 String productId, Integer price,
                                 String orderNo, String body,
                                 String openid){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");
        request.setBody(productName);
        request.setOutTradeNo(orderNo);
        request.setDetail(productName);
        request.setTotalFee(price);
        request.setSpbillCreateIp(clientIp);
        request.setNotifyUrl(wxPay.notifyUrl);
        request.setTradeType(WxPayConstants.TradeType.JSAPI);
        request.setProductId(productId);
        request.setAppid(wxPay.appId);
        request.setOpenid(openid);
        if (dev){
            request.setTotalFee(price);
            request.setNotifyUrl(wxPay.notifyUrlDev);
            request.setAppid(wxPay.appId);
            request.setOpenid(openid);
        }
        try {
            Object order = wxPayService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }


    public Object createWebOrder(String productName, String clientIp,
                                 String productId, Integer price,
                                 String orderNo, String body,
                                 String openid){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setDeviceInfo("WEB");
        request.setBody(productName);
        request.setOutTradeNo(orderNo);
        request.setDetail(productName);
        request.setTotalFee(price);
        request.setSpbillCreateIp(clientIp);
        request.setNotifyUrl(wxPay.notifyUrl);
        request.setTradeType(WxPayConstants.TradeType.MWEB);
        request.setProductId(productId);
        request.setAppid(wxPay.appId);
        request.setOpenid(openid);
        if (dev){
            request.setTotalFee(price);
            request.setNotifyUrl(wxPay.notifyUrlDev);
            request.setAppid(wxPay.appId);
            request.setOpenid(openid);
        }
        try {
            Object order = wxPayService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
    }

    public WxPayOrderNotifyResult appNotify(String requestBody){
        WxPayOrderNotifyResult notify = null;
        try {
            notify = wxPayService.parseOrderNotifyResult(requestBody);
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return notify;
    }
}
