package com.chaozhanggui.system.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.chaozhanggui.system.config.AliPayConfig;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lyf
 */
@Service
public class AliPayService {
    @Resource
    AliPayConfig aliPayConfig;
    public AlipayFundTransUniTransferResponse alipayTransfer(String amount, String accountNo, String accountName,String remake,
                                     String cashNumber) throws AlipayApiException {
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setPrivateKey(aliPayConfig.getPrivateKey());
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(aliPayConfig.getAlipayAppId());
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setEncryptor("");
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath(aliPayConfig.getAppCertPublicKeyPath());
        alipayConfig.setAlipayPublicCertPath(aliPayConfig.getAlipayCertPublicKeyPath());
        alipayConfig.setRootCertPath(aliPayConfig.getAlipayRootCertPath());
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        //商家侧唯一订单号
        model.setOutBizNo(cashNumber);
        model.setRemark(remake);
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
        model.setBizScene("DIRECT_TRANSFER");
        //收款方信息
        Participant payeeInfo = new Participant();
        //登录号
        payeeInfo.setIdentity(accountNo);
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        //实名
        payeeInfo.setName(accountName);
        model.setPayeeInfo(payeeInfo);
        //打款金额
        model.setTransAmount(amount);
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setOrderTitle("");
        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        return response;
    }

}
