package com.cyl.wechat;

import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;


@Service
@Slf4j
@ConditionalOnProperty(prefix = "wechat", name = "enabled", havingValue = "true")
public class WechatPayService {

    @Autowired
    private JsapiService service;


    /**
     * jsapi下单
     * @param orderNo 订单号
     * @param desc  订单描述
     * @param totalAmount  总金额，单位：分
     * @param openId   用户openid
     * @return  prepay_id
     */
    public String jsapiPay(String orderNo,String desc,Integer totalAmount,String openId, Long memberId,String appId){
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(appId);
        prepayRequest.setMchid(WechatPayData.merchantId);
        prepayRequest.setDescription(desc);
        prepayRequest.setOutTradeNo(orderNo);
        prepayRequest.setAttach(String.valueOf(memberId));
        prepayRequest.setNotifyUrl(WechatPayData.notifyUrl);
        Amount amount = new Amount();
        amount.setTotal(totalAmount);
        prepayRequest.setAmount(amount);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        prepayRequest.setPayer(payer);
        log.info("【支付】开始调用jsapiPay支付请求参数：", prepayRequest);
        PrepayResponse response = service.prepay(prepayRequest);
        log.info("【支付】开始调用jsapiPay支付请求结果：", response);
        return response.getPrepayId();
    }

    /**
     * jsapi 保证金缴纳
     * @param orderNo 订单号
     * @param desc  订单描述
     * @param totalAmount  总金额，单位：分
     * @param openId   用户openid
     * @return  prepay_id
     */
    public String jsapiPayForBCHSHI(String orderNo,String desc,Integer totalAmount,String openId, Long memberId,String appId){
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(appId);
        prepayRequest.setMchid("1665639691");
        prepayRequest.setDescription("{}");
        prepayRequest.setOutTradeNo(System.currentTimeMillis()+"00001");
        prepayRequest.setAttach(String.valueOf(memberId));

        prepayRequest.setNotifyUrl("http://h5.xzaiyp.top/no-auth/wechat/notifyForBCHSH");
        Amount amount = new Amount();
        amount.setTotal(totalAmount);
        prepayRequest.setAmount(amount);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        prepayRequest.setPayer(payer);
        log.info("【支付】开始调用jsapiPay支付请求参数：", prepayRequest);
        PrepayResponse response = service.prepay(prepayRequest);
        log.info("【支付】开始调用jsapiPay支付请求结果：", response);
        return response.getPrepayId();
    }


    /**
     * jsapi 订单退款
     * @param orderNo 订单号
     * @param desc  订单描述
     * @param totalAmount  总金额，单位：分
     * @param openId   用户openid
     * @return  prepay_id
     */
    public String refundOrder(String orderNo,String desc,Integer totalAmount,String openId, Long memberId,String appId){
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(appId);
        prepayRequest.setMchid("1665639691");
        prepayRequest.setDescription("{}");
        prepayRequest.setOutTradeNo(System.currentTimeMillis()+"00001");
        prepayRequest.setAttach(String.valueOf(memberId));

        prepayRequest.setNotifyUrl("http://h5.xzaiyp.top/no-auth/wechat/notifyForBCHSH");
        Amount amount = new Amount();
        amount.setTotal(totalAmount);
        prepayRequest.setAmount(amount);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        prepayRequest.setPayer(payer);
        log.info("【支付】开始调用jsapiPay支付请求参数：", prepayRequest);
        PrepayResponse response = service.prepay(prepayRequest);
        log.info("【支付】开始调用jsapiPay支付请求结果：", response);


        return response.getPrepayId();
    }



}
