支付宝开放平台:小程序文档 - 支付宝文档中心
这里申请证书密钥那些就不说了,按照文档去操作就可以了
注意:开发文档明确说明要用公钥加签
- 接口加签方式为 公钥证书 模式加签时,alipayClient 对象初始化的 Java 语言示例代码如下:
注意:若使用 公钥证书 模式进行加签,需额外引入如下 JAR 包:
- bcprov-jdk15on
- commons-logging
- fastjson
替换一下sdk的加签方式就好
代码实现:配置yml
引入Maven依赖:
<!--支付宝--> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.40.26.ALL</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.70</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.3.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.53</version> </dependency>
创建支付宝工具类 AlipayUtil:
import com.alipay.api.*; 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.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.zbkj.common.alipay.PaymentParameter; import com.zbkj.common.model.transfer.TransferRecord; import com.zbkj.service.service.TransferRecordService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AlipayUtil { @Value("${alipay.privateKey}") private String privateKey; @Value("${alipay.appId}") private String appId; @Value("${alipay.appCertPath}") private String appCertPath; @Value("${alipay.alipayPublicCertPath}") private String alipayPublicCertPath; @Value("${alipay.rootCertPath}") private String rootCertPath; public boolean transferMoney(PaymentParameter paymentParameter)throws AlipayApiException { /** 初始化 **/ CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); /** 支付宝网关 **/ certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do"); /** 应用id**/ certAlipayRequest.setAppId(appId); /** 应用私钥**/ certAlipayRequest.setPrivateKey(privateKey); /** 应用公钥证书路径,下载后保存位置的绝对路径 **/ certAlipayRequest.setCertPath(appCertPath); /** 支付宝公钥证书路径,下载后保存位置的绝对路径 **/ certAlipayRequest.setAlipayPublicCertPath(alipayPublicCertPath); /** 支付宝根证书路径,下载后保存位置的绝对路径 **/ certAlipayRequest.setRootCertPath(rootCertPath); /** 设置签名类型 **/ certAlipayRequest.setSignType("RSA2"); /** 设置请求格式,固定值json **/ certAlipayRequest.setFormat("json"); /** 设置编码格式 **/ certAlipayRequest.setCharset("UTF-8"); AlipayClient alipayClient = null; try { alipayClient = new DefaultAlipayClient(certAlipayRequest); } catch (AlipayApiException e1) { e1.printStackTrace(); } /** 实例化具体API对应的request类**/ AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); /** 设置业务参数,具体接口参数传值以文档说明为准**/ AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel(); /** 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一 **/ // 输出结果 model.setOutBizNo(paymentParameter.getOutBizNo()); /** 转账金额,TRANS_ACCOUNT_NO_PWD产品取值最低0.1 **/ model.setTransAmount(paymentParameter.getTransAmount()); /** 产品码,单笔无密转账到支付宝账户固定为:TRANS_ACCOUNT_NO_PWD **/ model.setProductCode("TRANS_ACCOUNT_NO_PWD"); /** 场景码,单笔无密转账到支付宝账户固定为:DIRECT_TRANSFER **/ model.setBizScene("DIRECT_TRANSFER"); /** 转账业务的标题,用于在支付宝用户的账单里显示 **/ model.setOrderTitle("商家提现"); Participant participant = new Participant(); /** 参与方的唯一标识,收款支付宝账号或者支付宝吧账号唯一会员ID **/ participant.setIdentity(paymentParameter.getIdentity()); /** 参与方的标识类型:ALIPAY_USER_ID 支付宝的会员ID **/ participant.setIdentityType("ALIPAY_LOGON_ID"); /** 参与方真实姓名,如果非空,将校验收款支付宝账号姓名一致性。当identity_type=ALIPAY_LOGON_ID时,本字段必填 **/ participant.setName(paymentParameter.getName()); model.setPayeeInfo(participant); /** 业务备注 **/ //model.setRemark("商家提现"); request.setBizModel(model); AlipayFundTransUniTransferResponse response = null; try { /**调用支付宝转账*/ response = alipayClient.certificateExecute(request); Integer code = Integer.valueOf(response.getCode()); if (code == 10000){ /**成功后的逻辑处理*/ return true; }else { /**失败后的逻辑处理*/ return false; } }catch (AlipayApiException e) { e.printStackTrace(); } return false; } }
传参类PaymentParameter 就不细说了 ,根据sdk自己需要的参数去创建就好,这里只是简单的调用,具体要更具自己的业务需求去实现
标签:alipay,支付宝,Java,单笔,certAlipayRequest,import,model,com From: https://blog.csdn.net/wj520131/article/details/144854983