app-store订阅消息开发\内购票据验证-- Java实现

第一步:

		<!--引入苹果商店订阅消息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 开发文档

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值