package com.hc.core.utils.pay.browser.alipay.processor;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.hc.core.utils.properties.PayProperties;
import com.hc.core.utils.pay.browser.alipay.bean.AliPayBean;
import com.hc.core.utils.pay.browser.alipay.bean.AliPayRefundBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author JIA
 */

@Component("aliPayProcessor")
public class AliPayProcessor {

    @Autowired
    private PayProperties payProperties;

    /**
     * 支付宝支付配置
     *
     * @return 接口对象
     */
    public AlipayClient alipayClient(String aliPayPublicKey) {
        // 1、获得初始化的AliPayClient
        String serverUrl = payProperties.getAliPay().getGatewayUrl();
        String appId = payProperties.getAliPay().getAppId();
        String privateKey = payProperties.getAliPay().getPrivateKey();
        String format = "json";
        String charset = payProperties.getAliPay().getCharset();
        String signType = payProperties.getAliPay().getSignType();
        return new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, aliPayPublicKey, signType);
    }

    /**
     * 支付
     *
     * @param aliPayBean
     * @return 实例对象
     * @throws AlipayApiException
     */
    public String pay(AliPayBean aliPayBean) throws AlipayApiException {

        // 2、设置请求参数
        String aliPayPublicKey = payProperties.getAliPay().getPublicKey();
        String notifyUrl = payProperties.getAliPay().getNotifyUrl();
        String returnUrl = payProperties.getAliPay().getReturnUrl();
        // 构造请求
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        // 连接参数
        AlipayClient alipayClient = alipayClient(aliPayPublicKey);
        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息
        return result;
    }

    /**
     * 退款
     *
     * @param aliPayRefundBean
     * @return 实例对象
     * @throws AlipayApiException
     */
    public String refund(AliPayRefundBean aliPayRefundBean) throws AlipayApiException {
        // 2、设置请求参数
        String aliPayPublicKey = payProperties.getAliPay().getKey();
        // 构造请求
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent(JSON.toJSONString(aliPayRefundBean));
        //连接参数
        AlipayClient alipayClient = alipayClient(aliPayPublicKey);
        // 3、请求支付宝进行退款，并获取支付结果
        String result = alipayClient.execute(request).getBody();
        // 返回退款信息
        return result;
    }


}
