1.必须使用证书加签方式进行加签
2.使用支付宝提供的软件里面获取CSR文件, 按照文档步骤进行加签, 应用私钥使用的是生成CSR文件时附带的那一个
3.支付功能和提现功能用的加签方式是同一种,如果之前用的是密钥方式, 需要进行修改代码和密钥
4.验签方式也需要修改成证书模式
5.检验应用公钥证书和应用私钥是否匹配
第一步: 初始化支付宝的配置, 只需要执行一次, 可以写一个单例
本内容证书是放在项目里
@Bean
public AlipayClient alipayClient() throws AlipayApiException, FileNotFoundException {
String prefix = "classpath:alipay/";
if (config.isProductionModel()) {
prefix += "prod/";
}else {
prefix += "dev/";
}
File appCertPublicKey = ResourceUtils.getFile(prefix+"appCertPublicKey.crt");
File alipayCertPublicKey_RSA2 = ResourceUtils.getFile(prefix+"alipayCertPublicKey_RSA2.crt");
File alipayRootCert = ResourceUtils.getFile(prefix+"alipayRootCert.crt");
CertAlipayRequest alipayConfig = new CertAlipayRequest();
alipayConfig.setPrivateKey(alipayMechantPrivateKey);//支付宝网关,在应用里看
alipayConfig.setServerUrl(serverUrl);//应用APPID
alipayConfig.setAppId(alipayMechantAppId);//应用私钥
alipayConfig.setCharset("UTF8");
alipayConfig.setSignType("RSA2");
alipayConfig.setEncryptor("");
alipayConfig.setFormat("json");
//应用公钥
alipayConfig.setCertPath(appCertPublicKey.toString());
//支付宝公钥
alipayConfig.setAlipayPublicCertPath(alipayCertPublicKey_RSA2.toString());
//支付宝根证书
alipayConfig.setRootCertPath(alipayRootCert.toString());
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
return alipayClient;
}
第二步:编写提现功能代码
public TransferResult aliPayTransferAccounts(AccountWithdrawRecord accountWithdrawRecord) {
String msg = null;
Integer status = null;
String code = null;
//实际提现金额= 提现金额*费率
String withdrawalMoney = accountWithdrawRecord
.getMoney()
.subtract(accountWithdrawRecord.getServiceFee())
//乘100,微信转账的单位是分
.stripTrailingZeros()
.toPlainString();
AlipayVo vo = new AlipayVo();
vo.setOut_biz_no(accountWithdrawRecord.getTransactionNo());//自定义订单号
vo.setAmount(withdrawalMoney);//转账的金额
vo.setPayee_type("ALIPAY_LOGONID");//账号的类型
vo.setPayee_account(accountWithdrawRecord.getAccount());//用户的账号
vo.setPayer_show_name("广州紫天跳动科技有限公司");//付款人姓名
vo.setPayee_real_name(accountWithdrawRecord.getAccountUserName());//收款人姓名
vo.setRemark("盲盒-佣金提现");//备注
vo.setOrder_title("盲盒大赢家拉新佣金提现");//订单标题
String json = new Gson().toJson(vo);
// 设置请求参数
AlipayFundTransToaccountTransferRequest alipayRequest = new AlipayFundTransToaccountTransferRequest();
alipayRequest.setBizContent(json);
AlipayFundTransToaccountTransferResponse response=null;
try {
response = alipayClient.certificateExecute(alipayRequest);
//System.out.println(JSON.toJSONString(response));
if("10000".equals(response.getCode())){
msg = "[转账成功]";
status = UserWithdrawalsRecordStatus.SUCCESS.getCode();
code = response.getSubCode();
}else{
msg = response.getSubMsg();
status = UserWithdrawalsRecordStatus.FAIL.getCode();
code = response.getSubCode();
}
log.info("提现金额 {} 提现信息 {}",withdrawalMoney,response.getSubMsg());
} catch (AlipayApiException e) {
msg = e.getMessage();
status = UserWithdrawalsRecordStatus.FAIL.getCode();
}
return new TransferResult(status,msg,code);
}