package org.wfc.payment.pay.alipay.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayCommerceCityfacilitatorVoucherGenerateModel;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.domain.AlipayFundAccountQueryModel;
import com.alipay.api.domain.AlipayFundAuthOrderFreezeModel;
import com.alipay.api.domain.AlipayFundCouponOrderAgreementPayModel;
import com.alipay.api.domain.AlipayFundTransCommonQueryModel;
import com.alipay.api.domain.AlipayFundTransOrderQueryModel;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.AlipayOpenAuthTokenAppModel;
import com.alipay.api.domain.AlipayOpenAuthTokenAppQueryModel;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeCancelModel;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.domain.AlipayTradeOrderSettleModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.AlipayFundAuthOrderFreezeResponse;
import com.alipay.api.response.AlipayFundCouponOrderAgreementPayResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wfc.payment.domain.AliPayBean;
import org.wfc.payment.domain.vo.AjaxResult;
import org.wfc.payment.pay.alipay.service.IAliPayService;
import org.wfc.payment.utils.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * <p>支付宝支付</p>
 *
 * @author cyc
 */
@RestController
@RequestMapping("/aliPay")
public class AliPayController extends AbstractAliPayApiController {
	private static final Logger log = LoggerFactory.getLogger(AliPayController.class);

	@Resource
	private AliPayBean aliPayBean;

	@Resource
	private IAliPayService aliPayService;

	private final AjaxResult result = new AjaxResult();
	// 普通公钥模式
     private final static String NOTIFY_URL = "/payment/aliPay/callback";
     private final static String PRE_NOTIFY_URL = "/u-api";
     private final static String TEST_PRE_NOTIFY_URL = "/proxy-default";
	/**
	 * 证书模式
	 */
//	private final static String NOTIFY_URL = "/aliPay/cert_notify_url";
    private final static String RETURN_URL = "/u";
	/**
	 * 证书模式
	 */
//	private final static String RETURN_URL = "/aliPay/cert_return_url";

	@Override
	public AliPayApiConfig getApiConfig() throws AlipayApiException {
		AliPayApiConfig aliPayApiConfig;
		try {
			aliPayApiConfig = AliPayApiConfigKit.getApiConfig(aliPayBean.getAppId());
		} catch (Exception e) {
			aliPayApiConfig = AliPayApiConfig.builder()
				.setAppId(aliPayBean.getAppId())
				.setAliPayPublicKey(aliPayBean.getPublicKey())
				.setAppCertPath(aliPayBean.getAppCertPath())
				.setAliPayCertPath(aliPayBean.getAliPayCertPath())
				.setAliPayRootCertPath(aliPayBean.getAliPayRootCertPath())
				.setCharset("UTF-8")
				.setPrivateKey(aliPayBean.getPrivateKey())
				.setServiceUrl(aliPayBean.getServerUrl())
				.setSignType("RSA2")
				// 普通公钥方式
				.build();
				// 证书模式
//				.buildByCert();

		}
		return aliPayApiConfig;
	}

	@GetMapping("/test")
	public AliPayApiConfig test() {
		AliPayApiConfig aliPayApiConfig = AliPayApiConfigKit.getAliPayApiConfig();
		String charset = aliPayApiConfig.getCharset();
		log.info("charset>" + charset);
		return aliPayApiConfig;
	}


	/**
	 * app支付
	 */
	@PostMapping(value = "/appPay")
	public AjaxResult appPay() {
		try {
			AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
			model.setBody("我是测试数据-By Javen");
			model.setSubject("App支付测试-By Javen");
			model.setOutTradeNo(StringUtils.getOutTradeNo());
			model.setTimeoutExpress("30m");
			model.setTotalAmount("0.01");
			model.setPassbackParams("callback params");
			model.setProductCode("QUICK_MSECURITY_PAY");
			String orderInfo = AliPayApi.appPayToResponse(model, aliPayBean.getDomain() + NOTIFY_URL).getBody();
			result.success(orderInfo);
		} catch (AlipayApiException e) {
			e.printStackTrace();
			result.addError("system error:" + e.getMessage());
		}
		return result;
	}

	@PostMapping(value = "/wapPay")
	public void wapPay(HttpServletResponse response, @RequestParam Long orderId) {

		String returnUrl = aliPayBean.getDomain() + RETURN_URL;
		String notifyUrl = aliPayBean.getDomain() + PRE_NOTIFY_URL + NOTIFY_URL;
		if (StrUtil.isNotBlank(aliPayBean.getTestDomain())) {
			notifyUrl = aliPayBean.getTestDomain() + PRE_NOTIFY_URL + NOTIFY_URL;
		}
		aliPayService.wapPay(response, orderId, returnUrl, notifyUrl);

	}


	/**
	 * PC支付
	 */
	@PostMapping(value = "/pcPay")
	public void pcPay(HttpServletResponse response, @RequestParam Long orderId) {

		String returnUrl = aliPayBean.getDomain() + RETURN_URL;
		String notifyUrl = aliPayBean.getDomain() + PRE_NOTIFY_URL + NOTIFY_URL;
		if (StrUtil.isNotBlank(aliPayBean.getTestDomain())) {
			notifyUrl = aliPayBean.getTestDomain() + PRE_NOTIFY_URL + NOTIFY_URL;
		}

		aliPayService.pcPay(response, orderId, returnUrl, notifyUrl);

	}

	@PostMapping(value = "/tradePay")
	public String tradePay(@RequestParam("authCode") String authCode, @RequestParam("scene") String scene) {
		String subject = null;
		String waveCode = "wave_code";
		String barCode = "bar_code";
		if (scene.equals(waveCode)) {
			subject = "Javen 支付宝声波支付测试";
		} else if (scene.equals(barCode)) {
			subject = "Javen 支付宝条形码支付测试";
		}
		String totalAmount = "100";
		String notifyUrl = aliPayBean.getDomain() + NOTIFY_URL;

		AlipayTradePayModel model = new AlipayTradePayModel();
		model.setAuthCode(authCode);
		model.setSubject(subject);
		model.setTotalAmount(totalAmount);
		model.setOutTradeNo(StringUtils.getOutTradeNo());
		model.setScene(scene);
		try {
			return AliPayApi.tradePayToResponse(model, notifyUrl).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 扫码支付
	 */
	@PostMapping(value = "/tradePreCreatePay")
	public String tradePreCreatePay() {
		String subject = "Javen 支付宝扫码支付测试";
		String totalAmount = "86";
		String storeId = "123";
//        String notifyUrl = aliPayBean.getDomain() + NOTIFY_URL;
		String notifyUrl = aliPayBean.getDomain() + "/aliPay/cert_notify_url";

		AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
		model.setSubject(subject);
		model.setTotalAmount(totalAmount);
		model.setStoreId(storeId);
		model.setTimeoutExpress("5m");
		model.setOutTradeNo(StringUtils.getOutTradeNo());
		try {
			String resultStr = AliPayApi.tradePrecreatePayToResponse(model, notifyUrl).getBody();
			JSONObject jsonObject = JSONObject.parseObject(resultStr);
			return jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 单笔转账到支付宝账户
	 * https://docs.open.alipay.com/309/106235/
	 */
	@PostMapping(value = "/transfer")
	public String transfer() {
		String totalAmount = "66";
		AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
		model.setOutBizNo(StringUtils.getOutTradeNo());
		model.setPayeeType("ALIPAY_LOGONID");
		model.setPayeeAccount("gxthqd7606@sandbox.com");
		model.setAmount(totalAmount);
		model.setPayerShowName("测试退款");
		model.setPayerRealName("沙箱环境");
		model.setRemark("javen测试单笔转账到支付宝");

		try {
			return AliPayApi.transferToResponse(model).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping(value = "/transferQuery")
	public String transferQuery(@RequestParam(required = false, name = "outBizNo") String outBizNo,
								@RequestParam(required = false, name = "orderId") String orderId) {
		AlipayFundTransOrderQueryModel model = new AlipayFundTransOrderQueryModel();
		if (StringUtils.isNotEmpty(outBizNo)) {
			model.setOutBizNo(outBizNo);
		}
		if (StringUtils.isNotEmpty(orderId)) {
			model.setOrderId(orderId);
		}

		try {
			return AliPayApi.transferQueryToResponse(model).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@PostMapping(value = "/uniTransfer")
	public String uniTransfer() {
		String totalAmount = "1";
		AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
		model.setOutBizNo(StringUtils.getOutTradeNo());
		model.setTransAmount(totalAmount);
		model.setProductCode("TRANS_ACCOUNT_NO_PWD");
		model.setBizScene("DIRECT_TRANSFER");
		model.setOrderTitle("统一转账-转账至支付宝账户");
		model.setRemark("IJPay 测试统一转账");

		Participant payeeInfo = new Participant();
		payeeInfo.setIdentity("gxthqd7606@sandbox.com");
		payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
		payeeInfo.setName("沙箱环境");
		model.setPayeeInfo(payeeInfo);

		try {
			return AliPayApi.uniTransferToResponse(model, null).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping(value = "/uniTransferQuery")
	public String uniTransferQuery(@RequestParam(required = false, name = "outBizNo") String outBizNo,
								   @RequestParam(required = false, name = "orderId") String orderId) {
		AlipayFundTransCommonQueryModel model = new AlipayFundTransCommonQueryModel();
		if (StringUtils.isNotEmpty(outBizNo)) {
			model.setOutBizNo(outBizNo);
		}
		if (StringUtils.isNotEmpty(orderId)) {
			model.setOrderId(orderId);
		}

		try {
			return AliPayApi.transCommonQueryToResponse(model, null).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping(value = "/accountQuery")
	public String accountQuery(@RequestParam(required = true, name = "aliPayUserId") String aliPayUserId) {
		AlipayFundAccountQueryModel model = new AlipayFundAccountQueryModel();
		model.setAlipayUserId(aliPayUserId);
		model.setAccountType("ACCTRANS_ACCOUNT");
		try {
			return AliPayApi.accountQueryToResponse(model, null).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 资金授权冻结接口
	 */
	@PostMapping(value = "/authOrderFreeze")
	public AlipayFundAuthOrderFreezeResponse authOrderFreeze(@RequestParam("auth_code") String authCode) {
		try {
			AlipayFundAuthOrderFreezeModel model = new AlipayFundAuthOrderFreezeModel();
			model.setOutOrderNo(StringUtils.getOutTradeNo());
			model.setOutRequestNo(StringUtils.getOutTradeNo());
			model.setAuthCode(authCode);
			model.setAuthCodeType("bar_code");
			model.setOrderTitle("资金授权冻结-By IJPay");
			model.setAmount("36");
			model.setProductCode("PRE_AUTH");

			return AliPayApi.authOrderFreezeToResponse(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * 红包协议支付接口
	 * https://docs.open.alipay.com/301/106168/
	 */
	@PostMapping(value = "/agreementPay")
	public AlipayFundCouponOrderAgreementPayResponse agreementPay() {
		try {
			AlipayFundCouponOrderAgreementPayModel model = new AlipayFundCouponOrderAgreementPayModel();
			model.setOutOrderNo(StringUtils.getOutTradeNo());
			model.setOutRequestNo(StringUtils.getOutTradeNo());
			model.setOrderTitle("红包协议支付接口-By IJPay");
			model.setAmount("36");
			model.setPayerUserId("2088102180432465");

			return AliPayApi.fundCouponOrderAgreementPayToResponse(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 下载对账单
	 */
	@PostMapping(value = "/dataDataServiceBill")
	public String dataDataServiceBill(@RequestParam("billDate") String billDate) {
		try {
			AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
			model.setBillType("trade");
			model.setBillDate(billDate);
			return AliPayApi.billDownloadUrlQuery(model);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 退款
	 */
	@PostMapping(value = "/tradeRefund")
	public String tradeRefund(@RequestParam Long orderId) {

		return aliPayService.tradeRefund(orderId);
	}

	/**
	 * 交易查询
	 */
	@GetMapping(value = "/tradeQuery")
	public String tradeQuery(@RequestParam(required = false, name = "outTradeNo") String outTradeNo, @RequestParam(required = false, name = "tradeNo") String tradeNo) {
		try {
			AlipayTradeQueryModel model = new AlipayTradeQueryModel();
			if (StringUtils.isNotEmpty(outTradeNo)) {
				model.setOutTradeNo(outTradeNo);
			}
			if (StringUtils.isNotEmpty(tradeNo)) {
				model.setTradeNo(tradeNo);
			}
			return AliPayApi.tradeQueryToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping(value = "/tradeQueryByStr")
	public String tradeQueryByStr(@RequestParam(required = false, name = "outTradeNo") String outTradeNo, @RequestParam(required = false, name = "tradeNo") String tradeNo) {
		AlipayTradeQueryModel model = new AlipayTradeQueryModel();
		if (StringUtils.isNotEmpty(outTradeNo)) {
			model.setOutTradeNo(outTradeNo);
		}
		if (StringUtils.isNotEmpty(tradeNo)) {
			model.setTradeNo(tradeNo);
		}

		try {
			return AliPayApi.tradeQueryToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 创建订单
	 * {"alipay_trade_create_response":{"code":"10000","msg":"Success","out_trade_no":"081014283315033","trade_no":"2017081021001004200200274066"},"sign":"ZagfFZntf0loojZzdrBNnHhenhyRrsXwHLBNt1Z/dBbx7cF1o7SZQrzNjRHHmVypHKuCmYifikZIqbNNrFJauSuhT4MQkBJE+YGPDtHqDf4Ajdsv3JEyAM3TR/Xm5gUOpzCY7w+RZzkHevsTd4cjKeGM54GBh0hQH/gSyhs4pEN3lRWopqcKkrkOGZPcmunkbrUAF7+AhKGUpK+AqDw4xmKFuVChDKaRdnhM6/yVsezJFXzlQeVgFjbfiWqULxBXq1gqicntyUxvRygKA+5zDTqE5Jj3XRDjVFIDBeOBAnM+u03fUP489wV5V5apyI449RWeybLg08Wo+jUmeOuXOA=="}
	 */
	@PostMapping(value = "/tradeCreate")
	public String tradeCreate(@RequestParam("outTradeNo") String outTradeNo) {

		String notifyUrl = aliPayBean.getDomain() + NOTIFY_URL;

		AlipayTradeCreateModel model = new AlipayTradeCreateModel();
		model.setOutTradeNo(outTradeNo);
		model.setTotalAmount("88.88");
		model.setBody("Body");
		model.setSubject("Javen 测试统一收单交易创建接口");
		//买家支付宝账号，和buyer_id不能同时为空
		model.setBuyerLogonId("abpkvd0206@sandbox.com");
		try {
			AlipayTradeCreateResponse response = AliPayApi.tradeCreateToResponse(model, notifyUrl);
			return response.getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 撤销订单
	 */
	@PostMapping(value = "/tradeCancel")
	public String tradeCancel(@RequestParam(required = false, name = "outTradeNo") String outTradeNo, @RequestParam(required = false, name = "tradeNo") String tradeNo) {
		try {
			AlipayTradeCancelModel model = new AlipayTradeCancelModel();
			if (StringUtils.isNotEmpty(outTradeNo)) {
				model.setOutTradeNo(outTradeNo);
			}
			if (StringUtils.isNotEmpty(tradeNo)) {
				model.setTradeNo(tradeNo);
			}

			return AliPayApi.tradeCancelToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 关闭订单
	 */
	@PostMapping(value = "/tradeClose")
	public String tradeClose(@RequestParam("outTradeNo") String outTradeNo, @RequestParam("tradeNo") String tradeNo) {
		try {
			AlipayTradeCloseModel model = new AlipayTradeCloseModel();
			if (StringUtils.isNotEmpty(outTradeNo)) {
				model.setOutTradeNo(outTradeNo);
			}
			if (StringUtils.isNotEmpty(tradeNo)) {
				model.setTradeNo(tradeNo);
			}

			return AliPayApi.tradeCloseToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 结算
	 */
	@PostMapping(value = "/tradeOrderSettle")
	public String tradeOrderSettle(@RequestParam("tradeNo") String tradeNo) {
		try {
			AlipayTradeOrderSettleModel model = new AlipayTradeOrderSettleModel();
			model.setOutRequestNo(StringUtils.getOutTradeNo());
			model.setTradeNo(tradeNo);

			return AliPayApi.tradeOrderSettleToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取应用授权URL并授权
	 */
	@PostMapping(value = "/toOauth")
	public void toOauth(HttpServletResponse response) {
		try {
			String redirectUri = aliPayBean.getDomain() + "/aliPay/redirect_uri";
			String oauth2Url = AliPayApi.getOauth2Url(aliPayBean.getAppId(), redirectUri);
			response.sendRedirect(oauth2Url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 应用授权回调
	 */
	@PostMapping(value = "/redirect_uri")
	public String redirectUri(@RequestParam("app_id") String appId, @RequestParam("app_auth_code") String appAuthCode) {
		try {
			System.out.println("app_id:" + appId);
			System.out.println("app_auth_code:" + appAuthCode);
			//使用app_auth_code换取app_auth_token
			AlipayOpenAuthTokenAppModel model = new AlipayOpenAuthTokenAppModel();
			model.setGrantType("authorization_code");
			model.setCode(appAuthCode);
			return AliPayApi.openAuthTokenAppToResponse(model).getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 查询授权信息
	 */
	@GetMapping(value = "/openAuthTokenAppQuery")
	public String openAuthTokenAppQuery(@RequestParam("appAuthToken") String appAuthToken) {
		try {
			AlipayOpenAuthTokenAppQueryModel model = new AlipayOpenAuthTokenAppQueryModel();
			model.setAppAuthToken(appAuthToken);
			return AliPayApi.openAuthTokenAppQueryToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 批量付款到支付宝账户有密接口
	 */
	@PostMapping(value = "/batchTrans")
	public void batchTrans(HttpServletResponse response) {
		try {
			String signType = "MD5";
			String notifyUrl = aliPayBean.getDomain() + NOTIFY_URL;
			Map<String, String> params = new HashMap<>(15);
			params.put("partner", "PID");
			params.put("sign_type", signType);
			params.put("notify_url", notifyUrl);
			params.put("account_name", "xxx");
			params.put("detail_data", "流水号1^收款方账号1^收款账号姓名1^付款金额1^备注说明1|流水号2^收款方账号2^收款账号姓名2^付款金额2^备注说明2");
			params.put("batch_no", String.valueOf(System.currentTimeMillis()));
			params.put("batch_num", 1 + "");
			params.put("batch_fee", 10.00 + "");
			params.put("email", "xx@xxx.com");

			AliPayApi.batchTrans(params, aliPayBean.getPrivateKey(), signType, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 地铁购票核销码发码
	 */
	@PostMapping(value = "/voucherGenerate")
	public String voucherGenerate(@RequestParam("tradeNo") String tradeNo) {
		try {
			//需要支付成功的订单号
//			String tradeNo = getPara("tradeNo");

			AlipayCommerceCityfacilitatorVoucherGenerateModel model = new AlipayCommerceCityfacilitatorVoucherGenerateModel();
			model.setCityCode("440300");
			model.setTradeNo(tradeNo);
			model.setTotalFee("8");
			model.setTicketNum("2");
			model.setTicketType("oneway");
			model.setSiteBegin("001");
			model.setSiteEnd("002");
			model.setTicketPrice("4");
			return AliPayApi.voucherGenerateToResponse(model).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	@PostMapping(value = "/return_url")
	public String returnUrl(HttpServletRequest request) {
		try {
			// 获取支付宝GET过来反馈信息
			Map<String, String> map = AliPayApi.toMap(request);
			for (Map.Entry<String, String> entry : map.entrySet()) {
				System.out.println(entry.getKey() + " = " + entry.getValue());
			}

			boolean verifyResult = AlipaySignature.rsaCheckV1(map, aliPayBean.getPublicKey(), "UTF-8",
				"RSA2");

			if (verifyResult) {
				// 请在这里加上商户的业务逻辑程序代码
				System.out.println("return_url 验证成功");

				return "success";
			} else {
				System.out.println("return_url 验证失败");
				// 
				return "failure";
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
			return "failure";
		}
	}

	@PostMapping(value = "/cert_return_url")
	public String certReturnUrl(HttpServletRequest request) {
		try {
			// 获取支付宝GET过来反馈信息
			Map<String, String> map = AliPayApi.toMap(request);
			for (Map.Entry<String, String> entry : map.entrySet()) {
				System.out.println(entry.getKey() + " = " + entry.getValue());
			}

			boolean verifyResult = AlipaySignature.rsaCertCheckV1(map, aliPayBean.getAliPayCertPath(), "UTF-8",
				"RSA2");

			if (verifyResult) {
				// 请在这里加上商户的业务逻辑程序代码
				System.out.println("certReturnUrl 验证成功");

				return "success";
			} else {
				System.out.println("certReturnUrl 验证失败");
				// 
				return "failure";
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
			return "failure";
		}
	}


	@PostMapping(value = "/callback")
	public String notifyUrl(HttpServletRequest request) {

		return aliPayService.notifyUrl(request, aliPayBean.getPublicKey());
	}

	@PostMapping(value = "/cert_notify_url")
	public String certNotifyUrl(HttpServletRequest request) {
		try {
			// 获取支付宝POST过来反馈信息
			Map<String, String> params = AliPayApi.toMap(request);

			for (Map.Entry<String, String> entry : params.entrySet()) {
				System.out.println(entry.getKey() + " = " + entry.getValue());
			}

			boolean verifyResult = AlipaySignature.rsaCertCheckV1(params, aliPayBean.getAliPayCertPath(), "UTF-8", "RSA2");

			if (verifyResult) {
				// 请在这里加上商户的业务逻辑程序代码 异步通知可能出现订单重复通知 需要做去重处理
				System.out.println("certNotifyUrl 验证成功succcess");
				return "success";
			} else {
				System.out.println("certNotifyUrl 验证失败");
				// 
				return "failure";
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
			return "failure";
		}
	}

}
