package cn.pluss.platform.ali.impl;

import cn.pluss.platform.ali.AlipayService;
import cn.pluss.platform.exception.MsgException;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {
    @Override
    public void remit(String account, String realName, String money) {
        try {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
//            certAlipayRequest.setAppId("2021001189650608");
//            certAlipayRequest.setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDWIvHm57tQURFF+UKIxNnnZcxnBPZ5oXwQlJjmUzv3qInD/I9TBBBvlRsY1ugRioH7KGwpUI0vjBuSzZYKx8ckktAIb03j75nSHOFD0Mn23y73bjVznj2NVLeVvDKPP8vfFO+7VYM2vBNHrXlvBSjfb/0gP0wZa1H2uqwKVdRHnTJgllzINKcO8ttosRqUZlj8oysCbV8BXDQ5B8QPKsvbtbva9v8giwp3Utk490Ufqh/cX0IL28PMBBp0SL7ptrbN9mT7FA02+T9Me1BevXBHBsqykgZfbuptEIg/tn93lBCM4cP70nXMz4hTYSPHp9f1QZjsdMxtOEhiWNoJeO6jAgMBAAECggEAaTr3AW/zfv63rFlXtvp10t8V2bJ4pUEURNmhYd6ZY5UxTly42iTvJsZxcH17wdL9GT2+GXd6Sllh23yoNVgDpxd2oA6CinbecoC6uqbJ+UWoQPOp1M14lhGBvlWjyv2g5FnXjYnyV12JY/n96O7yLQtNqyEv9nXk64jErreLAD17u8mQKDWx0E3s6j3JewCS/gpCoiPVGr4Aoh5pZQqZPvdirFtnLhPbphQAe/xRxMXn3SCTh/I+ggAlPf4pi20nYSJH/yV3RnPMg5GmwQm8Yr46ducbUyeQWjN2Gktaixz3ZGKX9RDabahHU5S4/RiCVF0/Mt6qun3j+AXC/NfkMQKBgQDqOPhRC1mzZE86KlfWpIW48hvjEY3UQW89OGpLfsGrh5JU4xhI+7ahFQZ212xIH3Ee4lqzLcfQIGZUZOAytQtSVOQ54FvQQYO1DlAr8RdFoNQAua9Z08If9LGNg7+b+dWDcnfAf07oV+zqBMy3+3+oQAupv/0v2C4szNxqQOBl7wKBgQDqC+GsyGXt0PRhWXr8jN3LoKv/z1+GLrk5EnW7DW4NYdI3OshY4F284QV57PFvS93HKh7hHinGlSniwR39yCmU6SsF+NSERsBQnm1y6Nof7i2Ywoz3yYKBzcplwlwN/3D7hH/SjlxKFAf0REcXIMSv/rmN8VQ6BfzIJ1ln9QXWjQKBgQDbRPQqou6Zw3lsIYHT3neVeYpj2+Yj5BIohOz1ujfCxgtWzrbIdGU3jcBcdzXWmyDCYfO5NE2P6RUDQuCkd9qee/ygtgod/4c2c6zYRj6cH4D5INwowtZEJkYmDkXN1mhtQzcajdQoLVAMkOu5/Yc30qVYh/SV2tL3e92I1y18HwKBgCzj3oMfZL16aIULfpVWB2x5AsRr5+x6pbgWnFImP0d9za4eLjMtxYzmtAAwvBYWvRDoOUgAWw7lqUT+uVvlumP/XOsmUMyWLWjJGkEB/GRHcKNuHU+hbNCswfdGkhV1Oxxiy4l9GKJ0uRnWxrPe6xq+hqR89ySx5UwWkAVqb5Y5AoGAPEc//P14ppYSx/G3f2rFML/G2pc6OgSABtbbKSgziX11AbVEzaqmmQI39TMkaLSdBAzqzmaLg3ZYnHsvBLg1il/7xE/d7nYlkaCwbEC+M1MzPGzXODt+5jusUVzO4Uk4bX9asBQJsWVhC851eoi/G4o7VZ4tal2fz17pyiuZq40=");
//            certAlipayRequest.setFormat("json");
//            certAlipayRequest.setCharset("UTF-8");
//            certAlipayRequest.setSignType("RSA2");
//            certAlipayRequest.setCertPath ( "/home/syb/cert/appCertPublicKey_2021001189650608.crt" );
//            certAlipayRequest.setAlipayPublicCertPath (  "/home/syb/cert/alipayCertPublicKey_RSA2 (2).crt"  );
//            certAlipayRequest.setRootCertPath ( "/home/syb/cert/alipayRootCert (2).crt" );

            certAlipayRequest.setAppId("2021003133649094");
            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDP0ky+6wTP7mTiyuA3rfUXW9kibwU3q4V8R/zWE5zGOIaMv/bQ3La/0WWUGHd9K/bsgQ6GLBE+i74QU/3RzfdrbTvrL6GrLupMxxgdiikmvkhPRwkWVPghjNmbJhG6+gva1K8BfE0QtbP3oghnBc78TSyp02aoKFIjo98+21fXUQZs8yagoZBojoQ8zcSr09TsCB4BCm+nSEhD7yEa7HyPcX+FkMtRFXt0XsxnBwV/9T95vaBNGKQoNdvY5evIVRnSInc4E4k8MU3sm+YxrPHWMKWZJn2SP/1kPXIGc/IZkf6AZGeZXMNL36kz8k6Z/1f/TsOKioHX5J498ueuc1HvAgMBAAECggEARDtKoUAuQ0qE6wUq2n0NxA6O2sRSlTWGTQihlXO4/EegvWNlWKuugInybM4fpQQjPQEeUKT1QHDHBTteUVzrR+UEUDJCRR7RJEYVv9UVr7e3q/epnyBkmzdQVqUIGzhvXfiIQNebwcm2fa5ATfrVO3bnHBbhVIJwfW0XJd8EFxfWonUDjtPfk8wTEjmNqz6vXhpk3ncV7MMY07ehbYaMA4NbbysABKRP0S9515JUX0NkBtxnhVm2ojcoc1Wjy3//TN8RXpC17E1Tyd/9pQIxmosLjS1gcX1WzphMZU0Y/74yHiE0WQCKP3J6PkdBTQDIqubyRdErJ9TYSe9+u3OZyQKBgQDrtap3LItv8IjH6O5Lg4xMU9r9dFW8QVc3dD1yn+wWq5x54mnUo5d6cTa7HqZ9SgS7gxWQZG/km9R2cJLQsATlQjdHyEoNKtqdgl3vB8MGK+Lwh6vNhE2z2svRJqQpzZfs1dKwI74mASDzwu/HjdkOFO2BVzjG3IPmrDRmN+7w8wKBgQDhthAKdbkhb7vjlgckGqDyy28QVb1YfZ4YzwW5aZiAhBmQhfZZEB5/KAYKRkToFg6Nqz6cBHwb1rH1YtOua8Dgr5XsqPQz9+MCCrUKxVt/1ziWhkpaNxEobT7QYtJiaGvuLxGP7uqpJGDpePCLHQTxmocsj8UgEE49HDj4qlO6FQKBgFGip5mC2gA5BIaITA/nTa4Z1Ny3cAI1dymDnsyWSsRHR3iHTDQTPKFxmI0SPoNiIWV4Lv3Fa2N3/QvQTWjtraRhXJgF7G/HCdRHlJT20TrP9FduBbi2O9swyip0AaNgy1+BZukOWAr9lRYXy8gHfsd7xA5p0QY+1YgM2FEWyjC1AoGBAKaSAsKFYGuRltp9BKYbVL35bexjKgodIkhyt2m+yQHG+0rR4hyWLbtvYQ3CD+YrKPBZ+SmQkbrFta0/ErL86Gmoiv8NLLMRdCE0n6jkPwNYZHWJg2OSkgIApUOKOnLPVx9+8voCuoC6zR5f1z0C3Hwkqy8ypqpj/ilRI6Y3pPSFAoGAaxBlqCUBFEhs/FnYf9neoPzx9WYDmJY7PgNgZ/s0H9vaGqyWPLWyF95/dTZ5yrBsABQyuIfDa2/m8y1YJaIJ/qU29FDq/GMv6IX4zDgm2iaWNZFnMoW7qSEkpVISSLoVxat4D556YI6FrQeoUAS+L1iEFgS33ln1tTyqb7mSe00=";
            certAlipayRequest.setPrivateKey(privateKey);
            certAlipayRequest.setFormat("json");
            certAlipayRequest.setCharset("UTF-8");
            certAlipayRequest.setSignType("RSA2");
            certAlipayRequest.setCertPath ( "/home/syb/cert/20220622/appCertPublicKey_2021003133649094.crt" );
            certAlipayRequest.setAlipayPublicCertPath (  "/home/syb/cert/20220622/alipayCertPublicKey_RSA2.crt"  );
            certAlipayRequest.setRootCertPath ( "/home/syb/cert/20220622/alipayRootCert.crt" );

//            certAlipayRequest.setAppId("2021003133649094");
//            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDP0ky+6wTP7mTiyuA3rfUXW9kibwU3q4V8R/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0SPoNiIWV4Lv3Fa2N3/QvQTWjtraRhXJgF7G/HCdRHlJT20TrP9FduBbi2O9swyip0AaNgy1+BZukOWAr9lRYXy8gHfsd7xA5p0QY+1YgM2FEWyjC1AoGBAKaSAsKFYGuRltp9BKYbVL35bexjKgodIkhyt2m+yQHG+0rR4hyWLbtvYQ3CD+YrKPBZ+SmQkbrFta0/ErL86Gmoiv8NLLMRdCE0n6jkPwNYZHWJg2OSkgIApUOKOnLPVx9+8voCuoC6zR5f1z0C3Hwkqy8ypqpj/ilRI6Y3pPSFAoGAaxBlqCUBFEhs/FnYf9neoPzx9WYDmJY7PgNgZ/s0H9vaGqyWPLWyF95/dTZ5yrBsABQyuIfDa2/m8y1YJaIJ/qU29FDq/GMv6IX4zDgm2iaWNZFnMoW7qSEkpVISSLoVxat4D556YI6FrQeoUAS+L1iEFgS33ln1tTyqb7mSe00=";
//            certAlipayRequest.setPrivateKey(privateKey);
//            certAlipayRequest.setFormat("json");
//            certAlipayRequest.setCharset("UTF-8");
//            certAlipayRequest.setSignType("RSA2");
//            certAlipayRequest.setCertPath ( "C:\\Users\\DJH\\Downloads\\appCertPublicKey_2021003133649094.crt" );
//            certAlipayRequest.setAlipayPublicCertPath (  "C:\\Users\\DJH\\Downloads\\alipayCertPublicKey_RSA2.crt"  );
//            certAlipayRequest.setRootCertPath ( "C:\\Users\\DJH\\Downloads\\alipayRootCert.crt" );
            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
            // 构建request内容
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            JSONObject bizJson = new JSONObject();
            JSONObject payeeInfo = new JSONObject();
            // 身份信息设置为帐号
            payeeInfo.put("identity_type", "ALIPAY_LOGON_ID");
            payeeInfo.put("identity", account);
            payeeInfo.put("name", realName);

            bizJson.put("out_biz_no", UUID.randomUUID().toString().replace("-", ""));
            bizJson.put("trans_amount", money);
            bizJson.put("product_code", "TRANS_ACCOUNT_NO_PWD");
            bizJson.put("biz_scene", "DIRECT_TRANSFER");
            bizJson.put("payee_info", payeeInfo);
            bizJson.put("order_title", "收银呗分润");
            bizJson.put("remark", "收银呗分润");

            JSONObject bizParam = new JSONObject();
            bizParam.put("payer_show_name", "收银呗");

            bizJson.put("business_params", bizParam);

            request.setBizContent(bizJson.toString());

            AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
            if (response.isSuccess()) {
                log.info("调用成功");
            } else {
                log.error(response.getSubMsg());
                throw new MsgException(response.getSubMsg());
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new MsgException(e.getMessage());
        }
    }
}
