package com.zcloud.common.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author fangjiakai
 * @date 2023/12/07 14:59
 */
@Component
public class AliPayUtil {
    @Autowired
    private AliPayBean aliPayBean;
    @Resource
    private AlipayClient alipayClient;

    public String pay(String outTradeNo, String totalAmount,String subject,String userAgent,String notify) throws AlipayApiException {
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(outTradeNo);
        model.setTotalAmount(totalAmount);
        model.setSubject(subject);
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setTimeoutExpress("5m"); //设置订单5分钟后失效
//        if(userAgent.contains("Windows")){
//            AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
//            alipayTradePagePayRequest.setNotifyUrl(aliPayBean.getHttpProxyDomain()+"/pay/alipay/notify");
//            alipayTradePagePayRequest.setBizModel(model);
//            return alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
//        }else{
        AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
        alipayTradeWapPayRequest.setNotifyUrl(aliPayBean.getHttpProxyDomain()+notify);
        alipayTradeWapPayRequest.setBizModel(model);
        return alipayClient.pageExecute(alipayTradeWapPayRequest).getBody();
//        }

//        response.setContentType("text/html;charset=utf-8");
//        response.getWriter().write(result);
//        response.getWriter().flush();
//        response.getWriter().close();

    }

}
