package shop.alien.util.common;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;

public class AlipayTradeAppPay {

    public static String executeAlipayPayment(String outTradeNo, String totalAmount, String subject) {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setOutTradeNo(outTradeNo);
            model.setTotalAmount(totalAmount);
            model.setSubject(subject);
            model.setPassbackParams(UrlEncode.getUrlEncode(outTradeNo));
            request.setBizModel(model);
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            String orderStr = "";
            if (response.isSuccess()) {
                orderStr = response.getBody();
            } else {
                System.out.println("调用失败 response：" + response);
                orderStr = "调用失败";
            }
            return orderStr;
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 用户端配置
     *
     * @return
     */
    private static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCBKWeug/nzFhN2wXFj0G4wlUkcSSnYr4PNX0Q6TmEKr3UTfescvGLNhFOQgZD4XDj5CSuNyPffT55UjtkAGR2oXZhBqHSE9tZKfIm10m4QJOBvxlKhhMicDVqgo6SJ8E3Lr/J44BFb5K6ze4sO36y+1Am4nTiwU7xgtA5BGiYt/O11GxSx2cFSgS59vuFN4CekkP/iWPbOxEaErOL9upKriJEODayvoUm6sVpE6pc0P1Hz8MtA24lB+PRsJp/a7Zs0BW2h0XNL8dZ5dZ4YfTclT01AHIFQHkodl3epRBpiaSlI0AQ4nK3x7rB40WTaYRiFWUsLyfHuCPQ28CgSHuztAgMBAAECggEABHzQymJxe72nnXepHeOvA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NfBs7XOhiBaXF25mul34hHhAxVuUIlEmOqyEkLO/ZWioOtWy//gRZIFL8qHoCaM8lS8xwT/xOz8nfDiu2hpISEXAquU2PNdP4yGiLg4qyafrvdctRw4TtpHEOrtYYyt8=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgSlnroP58xYTdsFxY9BuMJVJHEkp2K+DzV9EOk5hCq91E33rHLxizYRTkIGQ+Fw4+Qkrjcj330+eVI7ZABkdqF2YQah0hPbWSnyJtdJuECTgb8ZSoYTInA1aoKOkifBNy6/yeOARW+Sus3uLDt+svtQJuJ04sFO8YLQOQRomLfztdRsUsdnBUoEufb7hTeAnpJD/4lj2zsRGhKzi/bqSq4iRDg2sr6FJurFaROqXND9R8/DLQNuJQfj0bCaf2u2bNAVtodFzS/HWeXWeGH03JU9NQByBUB5KHZd3qUQaYmkpSNAEOJyt8e6weNFk2mEYhVlLC8nx7gj0NvAoEh7s7QIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004192631044");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
