package com.izouma.awesomeadmin.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.StringUtils;
import com.alipay.api.request.*;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import net.sf.json.JSONObject;

import java.util.Map;

public class AlipayClientFactory {

    // 手机网页支付 网站支付  
    public static String ydAndPc_Pay(Map<String, String> maps) throws AlipayApiException {

        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        String NotifyUrl = maps.get("NotifyUrl");
        String ReturnUrl = maps.get("ReturnUrl");
        // 后台回调  
        if (!StringUtils.isEmpty(NotifyUrl)) {
            alipayRequest.setNotifyUrl(NotifyUrl);
            // bizContent 中不需要 公共参数  
            maps.remove("NotifyUrl");
        }
        // 页面回调  
        if (!StringUtils.isEmpty(ReturnUrl)) {
            alipayRequest.setReturnUrl(ReturnUrl);
            // bizContent 中不需要 公共参数  
            maps.remove("ReturnUrl");
        }
        String bizCon = JSONObject.fromObject(maps).toString();
        alipayRequest.setBizContent(bizCon);
        String form = "";
        try {
            form = AlipayClientFactory.getAlipayClientInstance().pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            form = "err";
            e.printStackTrace();
        } // 调用SDK生成表单  
        return form;
    }


    // PC网页支付
    public static String PC_Pay(Map<String, String> maps) throws AlipayApiException {

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();

        maps.put("product_code", "FAST_INSTANT_TRADE_PAY");//销售产品码，与支付宝签约的产品码名称。 注：目前仅支持FAST_INSTANT_TRADE_PAY

        String NotifyUrl = maps.get("NotifyUrl");
        String ReturnUrl = maps.get("ReturnUrl");
        // 后台回调
        if (!StringUtils.isEmpty(NotifyUrl)) {
            alipayRequest.setNotifyUrl(NotifyUrl);
            // bizContent 中不需要 公共参数
            maps.remove("NotifyUrl");
        }
        // 页面回调
        if (!StringUtils.isEmpty(ReturnUrl)) {
            alipayRequest.setReturnUrl(ReturnUrl);
            // bizContent 中不需要 公共参数
            maps.remove("ReturnUrl");
        }
        String bizCon = JSONObject.fromObject(maps).toString();
        alipayRequest.setBizContent(bizCon);
        String form = "";
        try {
            form = AlipayClientFactory.getAlipayClientInstance().pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            form = "err";
            e.printStackTrace();
        } // 调用SDK生成表单
        return form;
    }


    public static AlipayClient getAlipayClientInstance() {

        AlipayClient alipayClient = new DefaultAlipayClient(PropertiesFileLoader.getProperties("ALIPAY_GATEWAY"),
                PropertiesFileLoader.getProperties("ALIPAY_APP_ID"), PropertiesFileLoader.getProperties("ALIPAY_APP_PRIVATE_KEY"), "json", "utf-8",
                PropertiesFileLoader.getProperties("ALIPAY_ALIPAY_PUBLIC_KEY"),PropertiesFileLoader.getProperties("ALIPAY_SIGN_TYPE"));
        return alipayClient;
    }


    // 条码支付 
    public static AlipayTradePayResponse pay(String appAuthToken, String bizContent) throws AlipayApiException {

        AlipayTradePayRequest request = new AlipayTradePayRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        request.setBizContent(bizContent);
        return AlipayClientFactory.getAlipayClientInstance().execute(request);
    }


    // 扫码支付 
    public static AlipayTradePrecreateResponse precreate(String appAuthToken, String bizContent) throws AlipayApiException {

        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        request.setBizContent(bizContent);
        return AlipayClientFactory.getAlipayClientInstance().execute(request);
    }


    // 订单撤销 
    public static AlipayTradeCancelResponse cancel(String appAuthToken, String bizContent) throws AlipayApiException {

        AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        request.setBizContent(bizContent);
        return AlipayClientFactory.getAlipayClientInstance().execute(request);
    }


    // 申请退款 
    public static AlipayTradeRefundResponse refund(String appAuthToken, String bizContent) throws AlipayApiException {

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        request.setBizContent(bizContent);
        return AlipayClientFactory.getAlipayClientInstance().execute(request);
    }


    // 申请退款 
    public static boolean refund(String outTradeNo, double refundAmount) {

        String bizContent = "{" + "    \"out_trade_no\":\"" + outTradeNo + "\"," + "    \"refund_amount\":" + refundAmount + ","
                + "    \"refund_reason\":\"正常退款\"" + "  }";

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent(bizContent);
        AlipayTradeRefundResponse response;
        try {
            response = AlipayClientFactory.getAlipayClientInstance().execute(request);
            if (response.isSuccess()) {
                System.out.println("调用成功");
                return true;
            } else {
                System.out.println("调用失败");

            }
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}
