第一步:
<!--引入苹果商店订阅消息pom文件-->
<dependency>
<groupId>com.apple.itunes.storekit</groupId>
<artifactId>app-store-server-library</artifactId>
<version>1.1.0</version>
</dependency>
第二步:
准备apple Store 返回消息(JWS串解析)。
apple jws串解析 需要准备的证书文件(网址)、私钥证书(.p8)、issuerId、keyId、bundleId、Environment(进行操作的环境)
证书最好都下载,因为解析时会根据情况选择其中的证书进行解析
第三步–票据验证
Path filePath = Path.of(this.getClass().getClassLoader().getResource("AuthKey_4CUDL5ASS9.p8").getPath());
String encodedKey = Files.readString(filePath);
Environment environment = Environment.SANDBOX;
Set<InputStream> rootCAs = Set.of(
this.getClass().getResourceAsStream("/AppleComputerRootCertificate.cer"),
this.getClass().getResourceAsStream("/AppleIncRootCertificate.cer"),
this.getClass().getResourceAsStream("/AppleRootCA-G2.cer"),
this.getClass().getResourceAsStream("/AppleRootCA-G3.cer")
);
//创建appleStoreServer对象
AppStoreServerAPIClient client =new AppStoreServerAPIClient(encodedKey,keyId,issuerId,bundleId,environment);
//根据传输的订单号获取订单信息
TransactionInfoResponse sendResponse =client.getTransactionInfo(applePayVo.getTransactionId());
Thread.sleep(3000L);
Long appAppleId =null ;
Boolean onlineChecks =false ;
SignedDataVerifier signedDataVerifier =new SignedDataVerifier(rootCAs,bundleId,appAppleId,environment, onlineChecks);
String signedPayLoad =sendResponse.getSignedTransactionInfo();
//对订单信息进行解析得到订单信息
JWSTransactionDecodedPayload payload =signedDataVerifier.verifyAndDecodeTransaction(signedPayLoad);
//进行订单信息处理
第三部-接收appleStore的服务消息
此处需要进行两次JWS解析,接收到的为JWS字符串(对通知内容JWS串进行加密后的结果),解析两次才可以获得JWS字符串的全部结构内容
JSONObject jsonObject=JSONObject.parseObject(data);
//apple服务器通知处理
// try {
Environment environment = Environment.SANDBOX;
Set<InputStream> rootCAs = Set.of(
this.getClass().getResourceAsStream("/AppleComputerRootCertificate.cer"),
this.getClass().getResourceAsStream("/AppleIncRootCertificate.cer"),
this.getClass().getResourceAsStream("/AppleRootCA-G2.cer"),
this.getClass().getResourceAsStream("/AppleRootCA-G3.cer")
);
Long appAppleId =null ;
Boolean onlineChecks =false ;
SignedDataVerifier signedDataVerifier =new SignedDataVerifier(rootCAs,bundleId,appAppleId,environment, onlineChecks);
//得到通知消息的JWS相关数据
ResponseBodyV2DecodedPayload payload =signedDataVerifier.verifyAndDecodeNotification((String) jsonObject.get("signedPayload"));
String data1 =payload.getData().getSignedTransactionInfo();
//对数据中的JWS数据进行解析
JWSTransactionDecodedPayload payload1 =signedDataVerifier.verifyAndDecodeTransaction(data1);
该内容参考apple developer 开发文档