支付宝后续接口都要升级到公匙证书模式,今天折腾了一下
绿色框里的三个文件是生成工具生成csr文件输出的,其中私匙内容(字符串)下面代码要用
橙色框里的是支付宝后台上传csr后,下载得到的3个文件,包括阿里公匙证书,阿里根证书,自己应用的根证书. 这三个文件下面代码也要用.
java版本sdk maven 配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.64</version>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.8.103.ALL</version>
</dependency>
java代码
import com.alipay.api.AlipayApiException;
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.internal.util.AlipaySignature;
import com.alipay.api.internal.util.AntCertificationUtil;
import com.alipay.api.internal.util.file.FileUtils;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import java.io.File;
import static com.alipay.api.internal.util.file.Charsets.UTF_8;
public class PayTestMain {
public static void main(String args[]) {
PayTestMain genCode = new PayTestMain();
try {
// genCode.sandTran();
genCode.readSnInfo();
} catch (Exception e) {
e.printStackTrace();
}
}
//读取app公匙序列号 和 根证书序列号
private void readSnInfo() {
try {
System.out.println("app_cert_sn=" + AlipaySignature.getCertSN(
"换成绝对路径/appCertPublicKey_xxxxxxxx.crt"));
String rootCertContent = FileUtils.readFileToString(new File("换成绝对路径/alipayRootCert.crt"),
UTF_8);
System.out.println("alipay_root_cert_sn=" + AntCertificationUtil.getRootCertSN(rootCertContent));
} catch (Exception e1) {
e1.printStackTrace();
}
}
//转账功能实现,
//换成正式地址及配置一样使用
private void sandTran() throws AlipayApiException {
String sand_pro_key ="私匙文本字符串";
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//这个是沙箱环境的地址
//正式的是 https://openapi.alipay.com/gateway.do
certAlipayRequest.setServerUrl("https://openapi.alipaydev.com/gateway.do");
certAlipayRequest.setAppId("你的APPID");
certAlipayRequest.setPrivateKey(sand_pro_key);
certAlipayRequest.setFormat("json");
certAlipayRequest.setCharset("UTF-8");
certAlipayRequest.setSignType("RSA2");
//上传csr文件后 完成配置,支付宝后台能下载3个文件
// 应用公匙
certAlipayRequest.setCertPath("换成绝对路径/appCertPublicKey_xxxxxxxx.crt");
// 支付宝公匙
certAlipayRequest.setAlipayPublicCertPath("换成绝对路径/alipayCertPublicKey_RSA2.crt");
//支付宝 根 公匙
certAlipayRequest.setRootCertPath("换成绝对路径/alipayRootCert.crt");
DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
//这里测试的是转账功能 其他接口参考文档
AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
model.setOutBizNo("" + System.currentTimeMillis());
model.setProductCode("TRANS_ACCOUNT_NO_PWD");
model.setBizScene("DIRECT_TRANSFER");
Participant participant = new Participant();
participant.setIdentity("chwhpi1916@sandbox.com");
participant.setIdentityType("ALIPAY_LOGON_ID");
participant.setName("沙箱环境");
model.setPayeeInfo(participant);
String totalAmount = "0.20";
model.setTransAmount(totalAmount);
model.setRemark("测试单笔转账到支付宝");
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
request.setBizModel(model);
//这里必须添加 app公匙证书的序列号和阿里根证书的序列号
//获得方法 是 上面的 readSnInfo()
request.putOtherTextParam("app_cert_sn", "xxxxxxxx");
request.putOtherTextParam("alipay_root_cert_sn", "xxxxxxxxxxxxx");
AlipayFundTransUniTransferResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
}
}