├── LICENSE
├── README.md
├── README_ZH.md
├── pom.xml
└── src
└── main
└── java
└── com
└── huawei
└── iap
└── demo
├── AtDemo.java
├── EnhancedSSLSocketFactory.java
├── OrderService.java
├── SubscriptionService.java
└── notification
├── AppServer.java
├── NotificationType.java
├── StatusUpdateNotification.java
├── StatusUpdateNotificationRequest.java
└── StatusUpdateNotificationResponse.java
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 |
3 | Version 2.0, January 2004
4 |
5 | http://www.apache.org/licenses/
6 |
7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8 |
9 | 1. Definitions.
10 |
11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
16 |
17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
18 |
19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
20 |
21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
22 |
23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
24 |
25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
26 |
27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
28 |
29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
30 |
31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
32 |
33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
34 |
35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
36 |
37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and
38 | You must cause any modified files to carry prominent notices stating that You changed the files; and
39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
41 |
42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
43 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
44 |
45 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
46 |
47 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
48 |
49 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
50 |
51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
52 |
53 | END OF TERMS AND CONDITIONS
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # **iap-java-sample**
2 |
3 | English | [中文](README_ZH.md)
4 |
5 | ## Contents
6 |
7 | * [Introduction](#introduction)
8 | * [Installation](#installation)
9 | * [Environment Requirements](#Environment Requirements)
10 | * [Configuration ](#configuration )
11 | * [Sample Code](#Sample Code)
12 | * [License](#license)
13 |
14 |
15 | ## Introduction
16 | Java sample code encapsulates APIs of the HUAWEI IAP server. It provides many sample programs for your reference or use.
17 | The following describes packages of Java sample code.
18 |
19 | **AtDemo:** Sample code of **AccessToken**. Each method can run independently.
20 | **OrderService:** Sample code of **OrderService**. Each method can run independently.
21 | **SubscriptionService:** Sample code of **SubscriptionService**. Each method can run independently.
22 | **notification:** Sample code of **notification**. Each method can run independently.
23 |
24 | ## Installation
25 | Before using Java sample code, check whether the Java environment has been installed.
26 | Decompress the Java sample code package.
27 |
28 | Copy the Java sample code package in the decompressed folder to the project vendor directory in the path specified by **JAVAPATH**.
29 | Refresh the project and ensure that the file is successfully copied to the destination directory.
30 |
31 | ## Environment Requirements
32 | JDK 1.8 or a later version is recommended.
33 |
34 | ## Configuration
35 | To use functions provided in examples, you need to set related parameters in **AtDemo.java**, **OrderService.java**, **SubscriptionService.java**, and **AppServer.java** in the package.
36 |
37 | The following describes parameters in **AtDemo.java**.
38 | **clientId:** Client ID, which is obtained from app information.
39 | **clientSecret:** Secret access key of an app, which is obtained from app information.
40 | **tokenUrl:** URL for the Huawei OAuth 2.0 service to obtain a token. Please refer to [OAuth 2.0-based Authentication](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/open-platform-oauth-0000001053629189?ha_source=hms1).
41 |
42 | The following describes parameters in **notification/AppServer.java**.
43 | **PUBLIC_KEY:** RSA public key.
44 |
45 | At first, the meaning of **accountFlag** should be clear. If field **accountFlag** in **InappPurchaseData** equals to **1**, the account belongs to telecom carrier (**TOBTOC_SITE_URL**); otherwise, to Huawei (**TOC_SITE_URL**).
46 | For both **OrderService** and **SubscriptionService**, you need to choose appropriate site.
47 | **TOC_SITE_URL:** The TOC_SITE_URL has different URLs at different sites, you should always choose the address of the nearest site to access.
48 | **TOBTOC_SITE_URL:** The site for telecom carrier.
49 |
50 | ## Sample Code
51 | Each method in the Java sample calls an API of the HUAWEI IAP server.
52 | The following describes methods in the Java sample.
53 |
54 |
55 |
56 | 1. AtDemo: getAppAT()
57 |
58 | You can call this method to get an app-level Access Token.
59 | Code location: **src/main/java/com/example/demo/AtDemo.java**
60 |
61 |
62 |
63 | 2. OrderService: verifyToken()
64 |
65 | You can call this method to verify the purchase token in the payment result with the Huawei payment server to confirm the accuracy of the payment result.
66 | The URL is **{rootUrl}/applications/purchases/tokens/verify**. The rootUrl has different URLs at different sites, you should always choose the Order service address of the nearest site to access.
67 | Code location: **src/main/java/com/example/demo/OrderService.java**
68 |
69 |
70 | 3. OrderService: cancelledListPurchase()
71 |
72 | You can call this method to pagination query all purchase information that has been cancelled or has a refund.
73 | The URL is **{rootUrl}/applications/{apiVersion}/purchases/cancelledList**. The rootUrl has different URLs at different sites, you should always choose the Order service address of the nearest site to access.
74 | Code location: **src/main/java/com/example/demo/OrderService.java**
75 |
76 |
77 | 4. SubscriptionService: getSubscription()
78 |
79 | You can call this method to verify a purchased subscription product, such as to obtain the validity period and status.
80 | The URL is **{rootUrl}/sub/applications/{apiVersion}/purchases/get**. The rootUrl has different URLs at different sites, you should always choose the Subscription service address of the nearest site to access.
81 | Code location: **src/main/java/com/example/demo/SubscriptionService.java**
82 |
83 |
84 | 5. SubscriptionService: stopSubscription()
85 |
86 | You can call this method to cancel an already subscribed product, the subscription is still valid during the validity period, and subsequent renewals will be terminated.
87 | The URL is **{rootUrl}/sub/applications/{apiVersion}/purchases/stop**. The rootUrl has different URLs at different sites, you should always choose the Subscription service address of the nearest site to access.
88 | Code location: **src/main/java/com/example/demo/SubscriptionService.java**
89 |
90 |
91 | 6. SubscriptionService: delaySubscription()
92 |
93 | You can call this method to renew a subscription product for a customer until a specified time in the future. After success, the customer's subscription will expire at a future time.
94 | The URL is **{rootUrl}/sub/applications/{apiVersion}/purchases/delay**. The rootUrl has different URLs at different sites, you should always choose the Subscription service address of the nearest site to access.
95 | Code location: **src/main/java/com/example/demo/SubscriptionService.java**
96 |
97 |
98 |
99 | 7. SubscriptionService: returnFeeSubscription()
100 |
101 | You can call this method to refund the last renewal fee of a subscription product, but the subscription product is still valid during the validity period, and subsequent renewals will be performed normally.
102 | The URL is **{rootUrl}/sub/applications/{apiVersion}/purchases/returnFee**. The rootUrl has different URLs at different sites, you should always choose the Subscription service address of the nearest site to access.
103 | Code location: **src/main/java/com/example/demo/SubscriptionService.java**
104 |
105 |
106 | 8. SubscriptionService: withdrawSubscription()
107 |
108 | You can call this method to cancel a subscription, which is equivalent to executing the returnFeeSubscription method, and immediately ending the subscription service and subsequent renewal.
109 | The URL is **{rootUrl}/sub/applications/{apiVersion}/purchases/withdrawal**. The rootUrl has different URLs at different sites, you should always choose the Subscription service address of the nearest site to access.
110 | Code location: **src/main/java/com/example/demo/SubscriptionService.java**
111 |
112 | 9. AppServer: dealNotification()
113 |
114 | You can call this method to handle subscription event notifications.
115 | The information parameter is obtained from subscription event notification.
116 | Code location: **src/main/java/com/example/demo/notification/AppServer.java**
117 |
118 | 10. OrderService: confirmPurchase()
119 |
120 | You can call this method to confirm purchase after sending out product.
121 | The URL is **{rootUrl}/applications/{apiVersion}/purchases/confirm**. The rootUrl has different URLs at different sites, you should always choose the Order service address of the nearest site to access.
122 | Code location: **OrderService.java**
123 |
124 | ## License
125 | IAP Java sample is licensed under [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
126 |
127 |
--------------------------------------------------------------------------------
/README_ZH.md:
--------------------------------------------------------------------------------
1 | ## 应用内支付服务示例代码
2 |
3 | 中文 | [English](README.md)
4 |
5 | ## 目录
6 |
7 | * [简介](#简介)
8 | * [安装](#安装)
9 | * [环境要求](#环境要求)
10 | * [配置 ](#配置 )
11 | * [示例代码](#示例代码)
12 | * [授权许可](#授权许可)
13 |
14 |
15 | ## 简介
16 | 华为应用内支付服务示例代码将华为应用内支付的服务端接口进行封装,提供丰富的示例程序供您参考或使用。示例代码如下:
17 |
18 | AtDemo:AccessToken示例代码,各方法可独立运行。
19 | OrderService:OrderService示例代码,各方法可独立运行。
20 | SubscriptionService:SubscriptionService示例代码,各方法可独立运行。
21 | notification:notification示例代码,各方法可独立运行。
22 |
23 | ## 安装
24 | 使用示例代码前,请确保已安装Java运行环境,并解压示例代码压缩包。
25 | 复制解压后的示例代码包至环境变量JAVAPATH指定的目录下。
26 | 刷新项目,确保您的文件已成功复制至目标目录下。
27 |
28 | ## 环境要求
29 | 建议使用JDK 1.8版本或以上。
30 |
31 | ## 配置
32 | 如需使用示例代码中的功能,需在AtDemo.java、OrderService.java、SubscriptionService.java和AppServer.java中设置相关参数。
33 |
34 | AtDemo.java包括如下参数:
35 | clientId:Client ID,可从应用信息中获取。
36 | clientSecret:Client Secret,可从应用信息中获取。
37 | tokenUrl:Huawei OAuth 2.0服务通过此URL获取令牌,请参考[基于OAuth 2.0开放鉴权](https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/open-platform-oauth-0000001053629189?ha_source=hms1)。
38 |
39 | notification和AppServer.java包括如下参数:
40 | PUBLIC_KEY:RSA公钥。
41 |
42 | 首先,需明确accountFlag定义。如果InappPurchaseData中accountFlag字段等于1, 账户属于电信运营商(TOBTOC_SITE_URL);如果不等于1,则属于华为(TOC_SITE_URL)。
43 | 对于OrderService和SubscriptionService,您需要选择合适的站点。
44 | TOC_SITE_URL:站点不同,URL不同,请选择最近的站点接入。
45 | TOBTOC_SITE_URL:电信运营商站点。
46 |
47 | ## 示例代码
48 | 示例代码中的方法调用华为应用内支付的服务端接口。
49 | 具体方法如下:
50 |
51 | 1. AtDemo: getAppAT()
52 | 您可以调用该方法,获取应用级Access Token。
53 | 代码位置:src/main/java/com/example/demo/AtDemo.java
54 | 2. OrderService: verifyToken()
55 | 您可以调用该方法,使用华为支付服务器,验证支付结果中的购买Token,确保支付结果的准确性。
56 | URL为{rootUrl}/applications/purchases/tokens/verify。站点不同,rootUrl的URL不同,请选择Order服务最近站点接入。
57 | 代码位置:src/main/java/com/example/demo/OrderService.java
58 | 3. OrderService: cancelledListPurchase()
59 | 您可以调用该方法,对取消或退款的购买信息进行分页查询。
60 | URL为{rootUrl}/applications/{apiVersion}/purchases/cancelledList。rootUrl的URL不同, 请选择Order服务最近站点接入。
61 | 代码位置:src/main/java/com/example/demo/OrderService.java
62 | 4. SubscriptionService: getSubscription()
63 | 您可以调用该方法,对已购买的订阅商品进行校验,如获取有效期及状态。
64 | URL为{rootUrl}/sub/applications/{apiVersion}/purchases/get。站点不同,rootUrl的URL不同, 请选择Subscription服务最近站点接入。
65 | 代码位置:src/main/java/com/example/demo/SubscriptionService.java
66 | 5. SubscriptionService: stopSubscription()
67 | 您可以调用该方法,取消已订阅商品。商品在当前订阅周期结束前仍然有效,但后续的续费会终止。rootUrl的URL不同, 请选择Subscription服务最近站点接入。
68 | URL为{rootUrl}/sub/applications/{apiVersion}/purchases/stop。
69 | 代码位置:src/main/java/com/example/demo/SubscriptionService.java
70 | 6. SubscriptionService: delaySubscription()
71 | 您可以调用该方法,将用户的下一个结算日期推迟。调用成功,系统会根据延迟时间更新订阅续订日期。
72 | URL为{rootUrl}/sub/applications/{apiVersion}/purchases/delay。站点不同,rootUrl的URL不同, 请选择Subscription服务最近站点接入。
73 | 代码位置:src/main/java/com/example/demo/SubscriptionService.java
74 | 7. SubscriptionService: returnFeeSubscription()
75 | 您可以调用该方法进行最近一次收据的退费操作。不会对订阅产生额外影响,订阅可以继续正常使用,到期后也会进行自动续费。
76 | URL为{rootUrl}/sub/applications/{apiVersion}/purchases/returnFee。站点不同,rootUrl的URL不同, 请选择Subscription service最近站点接入。
77 | 代码位置:src/main/java/com/example/demo/SubscriptionService.java
78 |
79 | 8. SubscriptionService: withdrawSubscription()
80 | 您可以调用该方法,撤销订阅来终止服务,购买的订阅型商品立即消失,同事这笔订阅费用会立即发起返还(returnFeeSubscription)。站点不同,rootUrl的URL不同, 请选择Subscription服务最近站点接入。
81 | URL为{rootUrl}/sub/applications/{apiVersion}/purchases/withdrawal
82 | 代码位置:src/main/java/com/example/demo/SubscriptionService.java
83 | 9. AppServer: dealNotification()
84 | 您可以调用该方法,处理订阅消息。
85 | 从订阅消息中获取相关参数。
86 | 代码位置:src/main/java/com/example/demo/notification/AppServer.java
87 | 10. OrderService: confirmPurchase()
88 | 您可以调用该方法,在发货后确认购买。
89 | URL为{rootUrl}/applications/{apiVersion}/purchases/confirm。站点不同,rootUrl的URL不同, 请选择Order服务最近站点接入。
90 | 代码位置:OrderService.java
91 |
92 | ## 授权许可
93 | 华为应用内支付服务示例代码经过[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)授权许可。
94 |
95 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
17 |
18 |
20 | 4.0.0
21 |
22 | com.huawei.iap.demo
23 | iap
24 | 1.0-SNAPSHOT
25 |
26 |
27 |
28 |
29 |
30 | org.apache.maven.plugins
31 | maven-compiler-plugin
32 |
33 | 1.8
34 | 1.8
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | commons-codec
45 | commons-codec
46 | 1.9
47 |
48 |
49 | org.apache.commons
50 | commons-lang3
51 | 3.9
52 |
53 |
54 | com.fasterxml.jackson.core
55 | jackson-databind
56 | 2.12.6.1
57 | compile
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/AtDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo;
19 |
20 | import com.alibaba.fastjson.JSONObject;
21 |
22 | import org.apache.commons.codec.binary.Base64;
23 |
24 | import java.io.BufferedReader;
25 | import java.io.IOException;
26 | import java.io.InputStream;
27 | import java.io.InputStreamReader;
28 | import java.io.OutputStream;
29 | import java.net.URL;
30 | import java.net.URLEncoder;
31 | import java.nio.charset.StandardCharsets;
32 | import java.security.KeyManagementException;
33 | import java.security.NoSuchAlgorithmException;
34 | import java.text.MessageFormat;
35 | import java.util.HashMap;
36 | import java.util.Map;
37 |
38 | import javax.net.ssl.HttpsURLConnection;
39 | import javax.net.ssl.SSLContext;
40 | import javax.net.ssl.SSLSocketFactory;
41 |
42 | /**
43 | * 功能描述
44 | *
45 | * @author iap
46 | * @since 2019-12-27
47 | */
48 | public class AtDemo {
49 | // TODO: The values of (clientId, clientSecret, TokenUrl) should be replaced with the actual one.
50 | private static final String CLIENT_SECRET = "appsecret";
51 |
52 | // the client id is your APP ID
53 | private static final String CLIENT_ID = "1234567"; // your app id
54 |
55 | // token url to get the authorization
56 | private static final String TOKEN_URL = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
57 |
58 | /**
59 | * The accessToken.
60 | */
61 | private static String accessToken;
62 |
63 | /**
64 | * Gets App Level AccessToken.
65 | * @return the App Level AccessToken
66 | * @throws Exception the exception
67 | */
68 | public static String getAppAT() throws Exception {
69 | // fetch accessToken
70 | String grantType = "client_credentials";
71 | String msgBody = MessageFormat.format("grant_type={0}&client_secret={1}&client_id={2}", grantType,
72 | URLEncoder.encode(CLIENT_SECRET, "UTF-8"), CLIENT_ID);
73 | String response =
74 | httpPost(TOKEN_URL, "application/x-www-form-urlencoded; charset=UTF-8", msgBody, 5000, 5000, null, false);
75 | JSONObject obj = JSONObject.parseObject(response);
76 | accessToken = obj.getString("access_token");
77 |
78 | // TODO: display the accessToken in console, you can remove it
79 | System.out.println(accessToken);
80 | return accessToken;
81 | }
82 |
83 | /**
84 | * Build Authorization in Header
85 | *
86 | * @param appAt appAt
87 | * @return headers
88 | */
89 | public static Map buildAuthorization(String appAt) {
90 | String oriString = MessageFormat.format("APPAT:{0}", appAt);
91 | String authorization =
92 | MessageFormat.format("Basic {0}", Base64.encodeBase64String(oriString.getBytes(StandardCharsets.UTF_8)));
93 | Map headers = new HashMap<>();
94 | headers.put("Authorization", authorization);
95 | headers.put("Content-Type", "application/json; charset=UTF-8");
96 | return headers;
97 | }
98 |
99 | /**
100 | * Http post function.
101 | * @param httpUrl the http url
102 | * @param data the data
103 | * @param connectTimeout the connect timeout
104 | * @param readTimeout the read timeout
105 | * @param headers the headers
106 | * @return the response as string
107 | * @throws IOException the io exception
108 | */
109 | public static String httpPost(String httpUrl, String contentType, String data, int connectTimeout, int readTimeout,
110 | Map headers, boolean enhancedSafety) throws IOException {
111 | OutputStream output = null;
112 | InputStream in = null;
113 | HttpsURLConnection urlConnection = null;
114 | BufferedReader bufferedReader = null;
115 | InputStreamReader inputStreamReader = null;
116 | try {
117 | URL url = new URL(httpUrl);
118 | urlConnection = (HttpsURLConnection) url.openConnection();
119 | SSLContext sc = SSLContext.getInstance("TLSv1.2");
120 | sc.init(null, null, new java.security.SecureRandom());
121 | SSLSocketFactory sf = sc.getSocketFactory();
122 |
123 | String[] protocolList = null;
124 | String[] cipherList = null;
125 | if (enhancedSafety) {
126 | protocolList = new String[] {"TLSv1.2", "TLSv1.3"};
127 | cipherList =
128 | new String[] {"TLS_AES_128_GCM_SHA256", "TLS_AES_256_GCM_SHA384", "TLS_CHACHA20_POLY1305_SHA256",
129 | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
130 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"};
131 | }
132 | urlConnection.setSSLSocketFactory(new EnhancedSSLSocketFactory(sf, protocolList, cipherList));
133 |
134 | urlConnection.setRequestMethod("POST");
135 | urlConnection.setDoOutput(true);
136 | urlConnection.setDoInput(true);
137 | urlConnection.setRequestProperty("Content-Type", contentType);
138 | if (headers != null) {
139 | for (String key : headers.keySet()) {
140 | urlConnection.setRequestProperty(key, headers.get(key));
141 | }
142 | }
143 | urlConnection.setConnectTimeout(connectTimeout);
144 | urlConnection.setReadTimeout(readTimeout);
145 | urlConnection.connect();
146 |
147 | // POST data
148 | output = urlConnection.getOutputStream();
149 | output.write(data.getBytes("UTF-8"));
150 | output.flush();
151 |
152 | // read response
153 | if (urlConnection.getResponseCode() < 400) {
154 | in = urlConnection.getInputStream();
155 | } else {
156 | in = urlConnection.getErrorStream();
157 | }
158 |
159 | inputStreamReader = new InputStreamReader(in, "UTF-8");
160 | bufferedReader = new BufferedReader(inputStreamReader);
161 | StringBuilder strBuf = new StringBuilder();
162 | String str;
163 | while ((str = bufferedReader.readLine()) != null) {
164 | strBuf.append(str);
165 | }
166 | return strBuf.toString();
167 | } catch (NoSuchAlgorithmException e) {
168 | throw new RuntimeException(e);
169 | } catch (KeyManagementException e) {
170 | throw new RuntimeException(e);
171 | } finally {
172 | if (bufferedReader != null) {
173 | bufferedReader.close();
174 | }
175 | if (inputStreamReader != null) {
176 | inputStreamReader.close();
177 | }
178 | if (in != null) {
179 | in.close();
180 | }
181 | if (urlConnection != null) {
182 | urlConnection.disconnect();
183 | }
184 | }
185 | }
186 |
187 | }
188 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/EnhancedSSLSocketFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo;
19 |
20 | import java.io.IOException;
21 | import java.net.InetAddress;
22 | import java.net.Socket;
23 | import java.net.UnknownHostException;
24 |
25 | import javax.net.ssl.SSLSocket;
26 | import javax.net.ssl.SSLSocketFactory;
27 |
28 | /**
29 | * 增强的SSL Socket工厂
30 | *
31 | * @author iap
32 | * @since 2022-08-04
33 | */
34 | public class EnhancedSSLSocketFactory extends SSLSocketFactory {
35 | private final SSLSocketFactory sf;
36 |
37 | private final String[] protocolList;
38 |
39 | private final String[] cipherList;
40 |
41 | public EnhancedSSLSocketFactory(SSLSocketFactory sf, String[] protocolList, String[] cipherList) {
42 | this.sf = sf;
43 | this.protocolList = protocolList;
44 | this.cipherList = cipherList;
45 | }
46 |
47 | @Override
48 | public String[] getDefaultCipherSuites() {
49 | return sf.getDefaultCipherSuites();
50 | }
51 |
52 | @Override
53 | public String[] getSupportedCipherSuites() {
54 | return sf.getSupportedCipherSuites();
55 | }
56 |
57 | @Override
58 | public Socket createSocket(Socket socket, String s, int i, boolean b) throws IOException {
59 | return wrapSocket(sf.createSocket(socket, s, i, b));
60 | }
61 |
62 | @Override
63 | public Socket createSocket(String s, int i) throws IOException, UnknownHostException {
64 | return wrapSocket(sf.createSocket(s, i));
65 | }
66 |
67 | @Override
68 | public Socket createSocket(String s, int i, InetAddress inetAddress, int i1)
69 | throws IOException, UnknownHostException {
70 | return wrapSocket(sf.createSocket(s, i, inetAddress, i1));
71 | }
72 |
73 | @Override
74 | public Socket createSocket(InetAddress inetAddress, int i) throws IOException {
75 | return wrapSocket(sf.createSocket(inetAddress, i));
76 | }
77 |
78 | @Override
79 | public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) throws IOException {
80 | return wrapSocket(sf.createSocket(inetAddress, i, inetAddress1, i1));
81 | }
82 |
83 | private Socket wrapSocket(Socket socket) {
84 | if (!(socket instanceof SSLSocket)) {
85 | return socket;
86 | }
87 | SSLSocket sslSocket = (SSLSocket) socket;
88 | if (cipherList != null && cipherList.length > 0) {
89 | sslSocket.setEnabledCipherSuites(cipherList);
90 | }
91 | if (protocolList != null && protocolList.length > 0) {
92 | sslSocket.setEnabledProtocols(protocolList);
93 | }
94 | return sslSocket;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/OrderService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo;
19 |
20 | import com.alibaba.fastjson.JSONObject;
21 |
22 | import java.util.HashMap;
23 | import java.util.Map;
24 |
25 | /**
26 | * Order服务对接工具
27 | *
28 | * @author iap
29 | * @since 2019-12-27
30 | */
31 | public class OrderService {
32 | // TODO: replace the (ip:port) to the real one, and if the protocol is https, you should deal with the license
33 | // yourself.
34 | public static final String TOC_SITE_URL = "https://ip:port";
35 |
36 | // site for telecom carrier
37 | public static final String TOBTOC_SITE_URL = "https://orders-at-dre.iap.cloud.huawei.eu";
38 |
39 | public static String getRootUrl(Integer accountFlag) {
40 | if (accountFlag != null && accountFlag == 1) {
41 | return TOBTOC_SITE_URL;
42 | }
43 | return TOC_SITE_URL;
44 | }
45 |
46 | public static void verifyToken(String purchaseToken, String productId, Integer accountFlag) throws Exception {
47 | // fetch the App Level AccessToken
48 | String appAt = AtDemo.getAppAT();
49 | // construct the Authorization in Header
50 | Map headers = AtDemo.buildAuthorization(appAt);
51 |
52 | // pack the request body
53 | Map bodyMap = new HashMap<>();
54 | bodyMap.put("purchaseToken", purchaseToken);
55 | bodyMap.put("productId", productId);
56 |
57 | String msgBody = JSONObject.toJSONString(bodyMap);
58 |
59 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/applications/purchases/tokens/verify",
60 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
61 | // TODO: display the response as string in console, you can replace it with your business logic.
62 | System.out.println(response);
63 | }
64 |
65 | public static void cancelledListPurchase(Long endAt, Long startAt, Integer maxRows, Integer type,
66 | String continuationToken, Integer accountFlag) throws Exception {
67 | // fetch the App Level AccessToken
68 | String appAt = AtDemo.getAppAT();
69 | // construct the Authorization in Header
70 | Map headers = AtDemo.buildAuthorization(appAt);
71 |
72 | // pack the request body
73 | Map bodyMap = new HashMap<>();
74 | bodyMap.put("endAt", endAt);
75 | bodyMap.put("startAt", startAt);
76 | bodyMap.put("maxRows", maxRows);
77 | bodyMap.put("type", type);
78 | bodyMap.put("continuationToken", continuationToken);
79 |
80 | String msgBody = JSONObject.toJSONString(bodyMap);
81 |
82 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/applications/v2/purchases/cancelledList",
83 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
84 | // TODO: display the response as string in console, you can replace it with your business logic.
85 | System.out.println(response);
86 | }
87 |
88 | public static void confirmPurchase(String purchaseToken, String productId, Integer accountFlag) throws Exception {
89 | // fetch the App Level AccessToken
90 | String appAt = AtDemo.getAppAT();
91 | // construct the Authorization in Header
92 | Map headers = AtDemo.buildAuthorization(appAt);
93 |
94 | // pack the request body
95 | Map bodyMap = new HashMap<>();
96 | bodyMap.put("purchaseToken", purchaseToken);
97 | bodyMap.put("productId", productId);
98 |
99 | String msgBody = JSONObject.toJSONString(bodyMap);
100 |
101 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/applications/v2/purchases/confirm",
102 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
103 | // TODO: display the response as string in console, you can replace it with your business logic.
104 | System.out.println(response);
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/SubscriptionService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo;
19 |
20 | import com.alibaba.fastjson.JSONObject;
21 |
22 | import java.util.HashMap;
23 | import java.util.Map;
24 |
25 | /**
26 | * Subscription服务对接工具
27 | *
28 | * @author iap
29 | * @since 2019-12-27
30 | */
31 | public class SubscriptionService {
32 | // TODO: replace the (ip:port) to the real one, and if the protocol is https, you should deal with the license
33 | // yourself.
34 | public static final String TOC_SITE_URL = "https://ip:port";
35 |
36 | // site for telecom carrier
37 | public static final String TOBTOC_SITE_URL = "https://subscr-at-dre.iap.cloud.huawei.eu";
38 |
39 | public static String getRootUrl(Integer accountFlag) {
40 | if (accountFlag != null && accountFlag == 1) {
41 | return TOBTOC_SITE_URL;
42 | }
43 | return TOC_SITE_URL;
44 | }
45 |
46 | public static void getSubscription(String subscriptionId, String purchaseToken, Integer accountFlag)
47 | throws Exception {
48 | // fetch the App Level AccessToken
49 | String appAt = AtDemo.getAppAT();
50 | // construct the Authorization in Header
51 | Map headers = AtDemo.buildAuthorization(appAt);
52 |
53 | // pack the request body
54 | Map bodyMap = new HashMap<>();
55 | bodyMap.put("subscriptionId", subscriptionId);
56 | bodyMap.put("purchaseToken", purchaseToken);
57 |
58 | String msgBody = JSONObject.toJSONString(bodyMap);
59 |
60 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/sub/applications/v2/purchases/get",
61 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
62 | // TODO: display the response as string in console, you can replace it with your business logic.
63 | System.out.println(response);
64 | }
65 |
66 | public static void stopSubscription(String subscriptionId, String purchaseToken, Integer accountFlag)
67 | throws Exception {
68 | // fetch the App Level AccessToken
69 | String appAt = AtDemo.getAppAT();
70 | // construct the Authorization in Header
71 | Map headers = AtDemo.buildAuthorization(appAt);
72 |
73 | // pack the request body
74 | Map bodyMap = new HashMap<>();
75 | bodyMap.put("subscriptionId", subscriptionId);
76 | bodyMap.put("purchaseToken", purchaseToken);
77 |
78 | String msgBody = JSONObject.toJSONString(bodyMap);
79 |
80 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/sub/applications/v2/purchases/stop",
81 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
82 | // TODO: display the response as string in console, you can replace it with your business logic.
83 | System.out.println(response);
84 | }
85 |
86 | public static void delaySubscription(String subscriptionId, String purchaseToken, Long currentExpirationTime,
87 | Long desiredExpirationTime, Integer accountFlag) throws Exception {
88 | // fetch the App Level AccessToken
89 | String appAt = AtDemo.getAppAT();
90 | // construct the Authorization in Header
91 | Map headers = AtDemo.buildAuthorization(appAt);
92 |
93 | // pack the request body
94 | Map bodyMap = new HashMap<>();
95 | bodyMap.put("subscriptionId", subscriptionId);
96 | bodyMap.put("purchaseToken", purchaseToken);
97 | bodyMap.put("currentExpirationTime", currentExpirationTime);
98 | bodyMap.put("desiredExpirationTime", desiredExpirationTime);
99 |
100 | String msgBody = JSONObject.toJSONString(bodyMap);
101 |
102 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/sub/applications/v2/purchases/delay",
103 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
104 | // TODO: display the response as string in console, you can replace it with your business logic.
105 | System.out.println(response);
106 | }
107 |
108 | public static void returnFeeSubscription(String subscriptionId, String purchaseToken, Integer accountFlag)
109 | throws Exception {
110 | // fetch the App Level AccessToken
111 | String appAt = AtDemo.getAppAT();
112 | // construct the Authorization in Header
113 | Map headers = AtDemo.buildAuthorization(appAt);
114 |
115 | // pack the request body
116 | Map bodyMap = new HashMap<>();
117 | bodyMap.put("subscriptionId", subscriptionId);
118 | bodyMap.put("purchaseToken", purchaseToken);
119 |
120 | String msgBody = JSONObject.toJSONString(bodyMap);
121 |
122 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/sub/applications/v2/purchases/returnFee",
123 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
124 | // TODO: display the response as string in console, you can replace it with your business logic.
125 | System.out.println(response);
126 | }
127 |
128 | public static void withdrawSubscription(String subscriptionId, String purchaseToken, Integer accountFlag)
129 | throws Exception {
130 | // fetch the App Level AccessToken
131 | String appAt = AtDemo.getAppAT();
132 | // construct the Authorization in Header
133 | Map headers = AtDemo.buildAuthorization(appAt);
134 |
135 | // pack the request body
136 | Map bodyMap = new HashMap<>();
137 | bodyMap.put("subscriptionId", subscriptionId);
138 | bodyMap.put("purchaseToken", purchaseToken);
139 |
140 | String msgBody = JSONObject.toJSONString(bodyMap);
141 |
142 | String response = AtDemo.httpPost(getRootUrl(accountFlag) + "/sub/applications/v2/purchases/withdrawal",
143 | "application/json; charset=UTF-8", msgBody, 5000, 5000, headers, true);
144 | // TODO: display the response as string in console, you can replace it with your business logic.
145 | System.out.println(response);
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/notification/AppServer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo.notification;
19 |
20 | import com.fasterxml.jackson.databind.ObjectMapper;
21 |
22 | import org.apache.commons.codec.binary.Base64;
23 | import org.apache.commons.lang3.StringUtils;
24 |
25 | import java.nio.charset.StandardCharsets;
26 | import java.security.KeyFactory;
27 | import java.security.PublicKey;
28 | import java.security.spec.X509EncodedKeySpec;
29 |
30 | /**
31 | * 功能描述
32 | *
33 | * @author iap
34 | * @since 2020-02-05
35 | */
36 | public class AppServer {
37 | // the public key
38 | private static final String PUBLIC_KEY = "the public key";
39 |
40 | private static final ObjectMapper MAPPER = new ObjectMapper();
41 |
42 | /**
43 | * deal notification information
44 | *
45 | * @param information the notification information
46 | */
47 | public void dealNotification(String information) throws Exception {
48 | if (StringUtils.isEmpty(information)) {
49 | return;
50 | }
51 | StatusUpdateNotificationRequest request = MAPPER.readValue(information, StatusUpdateNotificationRequest.class);
52 | StatusUpdateNotificationResponse response = new StatusUpdateNotificationResponse();
53 |
54 | if (StringUtils.isEmpty(request.getNotifycationSignature())
55 | || StringUtils.isEmpty(request.getStatusUpdateNotification())) {
56 | // failure, the response set by yourself
57 | response.setErrorCode("1");
58 | response.setErrorMsg("the notification message is empty");
59 | return;
60 | }
61 |
62 | // verify the notification
63 | boolean isCheckOk =
64 | doCheck(request.getStatusUpdateNotification(), request.getNotifycationSignature(), PUBLIC_KEY);
65 | if (!isCheckOk) {
66 | // failure, the response set by yourself
67 | response.setErrorCode("2");
68 | response.setErrorMsg("verify the sign failure");
69 | return;
70 | }
71 |
72 | // TODO replace with your business logic
73 | StatusUpdateNotification statusUpdateNotification =
74 | MAPPER.readValue(request.getStatusUpdateNotification(), StatusUpdateNotification.class);
75 | int notificationType = statusUpdateNotification.getNotificationType();
76 | switch (notificationType) {
77 | case NotificationType.INITIAL_BUY:
78 | break;
79 | case NotificationType.CANCEL:
80 | break;
81 | case NotificationType.RENEWAL:
82 | break;
83 | case NotificationType.INTERACTIVE_RENEWAL:
84 | break;
85 | case NotificationType.NEW_RENEWAL_PREF:
86 | break;
87 | case NotificationType.RENEWAL_STOPPED:
88 | break;
89 | case NotificationType.RENEWAL_RESTORED:
90 | break;
91 | case NotificationType.RENEWAL_RECURRING:
92 | break;
93 | case NotificationType.ON_HOLD:
94 | break;
95 | case NotificationType.PAUSED:
96 | break;
97 | case NotificationType.PAUSE_PLAN_CHANGED:
98 | break;
99 | case NotificationType.PRICE_CHANGE_CONFIRMED:
100 | break;
101 | case NotificationType.DEFERRED:
102 | break;
103 | default:
104 | break;
105 | }
106 |
107 | }
108 |
109 | /**
110 | * check RSA signature
111 | *
112 | * @param content content
113 | * @param sign sign
114 | * @param publicKey publicKey
115 | * @return boolean
116 | */
117 | public static boolean doCheck(String content, String sign, String publicKey) {
118 | if (StringUtils.isEmpty(sign)) {
119 | return false;
120 | }
121 | if (StringUtils.isEmpty(publicKey)) {
122 | return false;
123 | }
124 | try {
125 | KeyFactory keyFactory = KeyFactory.getInstance("RSA");
126 | byte[] encodedKey = Base64.decodeBase64(publicKey);
127 | PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
128 | java.security.Signature signature;
129 | signature = java.security.Signature.getInstance("SHA256WithRSA");
130 | signature.initVerify(pubKey);
131 | signature.update(content.getBytes(StandardCharsets.UTF_8));
132 | byte[] bsign = Base64.decodeBase64(sign);
133 | return signature.verify(bsign);
134 | } catch (RuntimeException e) {
135 | throw e;
136 | } catch (Exception e) {
137 | e.printStackTrace();
138 | }
139 | return false;
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/notification/NotificationType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo.notification;
19 |
20 | /**
21 | * 功能描述
22 | *
23 | * @author iap
24 | * @since 2020-02-05
25 | */
26 | public interface NotificationType {
27 | /**
28 | * The constant INITIAL_BUY.
29 | */
30 | int INITIAL_BUY = 0;
31 |
32 | /**
33 | * The constant CANCEL.
34 | */
35 | int CANCEL = 1;
36 |
37 | /**
38 | * The constant RENEWAL.
39 | */
40 | int RENEWAL = 2;
41 |
42 | /**
43 | * The constant INTERACTIVE_RENEWAL.
44 | */
45 | int INTERACTIVE_RENEWAL = 3;
46 |
47 | /**
48 | * The constant NEW_RENEWAL_PREF.
49 | */
50 | int NEW_RENEWAL_PREF = 4;
51 |
52 | /**
53 | * The constant RENEWAL_STOPPED.
54 | */
55 | int RENEWAL_STOPPED = 5;
56 |
57 | /**
58 | * The constant RENEWAL_RESTORED.
59 | */
60 | int RENEWAL_RESTORED = 6;
61 |
62 | /**
63 | * The constant RENEWAL_RECURRING.
64 | */
65 | int RENEWAL_RECURRING = 7;
66 |
67 | /**
68 | * The constant ON_HOLD.
69 | */
70 | int ON_HOLD = 9;
71 |
72 | /**
73 | * The constant PAUSED.
74 | */
75 | int PAUSED = 10;
76 |
77 | /**
78 | * The constant PAUSE_PLAN_CHANGED.
79 | */
80 | int PAUSE_PLAN_CHANGED = 11;
81 |
82 | /**
83 | * The constant PRICE_CHANGE_CONFIRMED.
84 | */
85 | int PRICE_CHANGE_CONFIRMED = 12;
86 |
87 | /**
88 | * The constant DEFERRED.
89 | */
90 | int DEFERRED = 13;
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/notification/StatusUpdateNotification.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo.notification;
19 |
20 | /**
21 | * 功能描述
22 | *
23 | * @author iap
24 | * @since 2020-02-05
25 | */
26 | public class StatusUpdateNotification {
27 | private String environment;
28 |
29 | private Integer notificationType;
30 |
31 | private String subscriptionId;
32 |
33 | private Long cancellationDate;
34 |
35 | private String orderId;
36 |
37 | private String latestReceipt;
38 |
39 | private String latestReceiptInfo;
40 |
41 | private String latestReceiptInfoSignature;
42 |
43 | private String latestExpiredReceipt;
44 |
45 | private String latestExpiredReceiptInfo;
46 |
47 | private String latestExpiredReceiptInfoSignature;
48 |
49 | private Integer autoRenewStatus;
50 |
51 | private String refundPayOrderId;
52 |
53 | private String productId;
54 |
55 | private String applicationId;
56 |
57 | private Integer expirationIntent;
58 |
59 | public String getEnvironment() {
60 | return environment;
61 | }
62 |
63 | public void setEnvironment(String environment) {
64 | this.environment = environment;
65 | }
66 |
67 | public Integer getNotificationType() {
68 | return notificationType;
69 | }
70 |
71 | public void setNotificationType(Integer notificationType) {
72 | this.notificationType = notificationType;
73 | }
74 |
75 | public String getSubscriptionId() {
76 | return subscriptionId;
77 | }
78 |
79 | public void setSubscriptionId(String subscriptionId) {
80 | this.subscriptionId = subscriptionId;
81 | }
82 |
83 | public Long getCancellationDate() {
84 | return cancellationDate;
85 | }
86 |
87 | public void setCancellationDate(Long cancellationDate) {
88 | this.cancellationDate = cancellationDate;
89 | }
90 |
91 | public String getOrderId() {
92 | return orderId;
93 | }
94 |
95 | public void setOrderId(String orderId) {
96 | this.orderId = orderId;
97 | }
98 |
99 | public String getLatestReceipt() {
100 | return latestReceipt;
101 | }
102 |
103 | public void setLatestReceipt(String latestReceipt) {
104 | this.latestReceipt = latestReceipt;
105 | }
106 |
107 | public String getLatestReceiptInfo() {
108 | return latestReceiptInfo;
109 | }
110 |
111 | public void setLatestReceiptInfo(String latestReceiptInfo) {
112 | this.latestReceiptInfo = latestReceiptInfo;
113 | }
114 |
115 | public String getLatestReceiptInfoSignature() {
116 | return latestReceiptInfoSignature;
117 | }
118 |
119 | public void setLatestReceiptInfoSignature(String latestReceiptInfoSignature) {
120 | this.latestReceiptInfoSignature = latestReceiptInfoSignature;
121 | }
122 |
123 | public String getLatestExpiredReceipt() {
124 | return latestExpiredReceipt;
125 | }
126 |
127 | public void setLatestExpiredReceipt(String latestExpiredReceipt) {
128 | this.latestExpiredReceipt = latestExpiredReceipt;
129 | }
130 |
131 | public String getLatestExpiredReceiptInfo() {
132 | return latestExpiredReceiptInfo;
133 | }
134 |
135 | public void setLatestExpiredReceiptInfo(String latestExpiredReceiptInfo) {
136 | this.latestExpiredReceiptInfo = latestExpiredReceiptInfo;
137 | }
138 |
139 | public String getLatestExpiredReceiptInfoSignature() {
140 | return latestExpiredReceiptInfoSignature;
141 | }
142 |
143 | public void setLatestExpiredReceiptInfoSignature(String latestExpiredReceiptInfoSignature) {
144 | this.latestExpiredReceiptInfoSignature = latestExpiredReceiptInfoSignature;
145 | }
146 |
147 | public Integer getAutoRenewStatus() {
148 | return autoRenewStatus;
149 | }
150 |
151 | public void setAutoRenewStatus(Integer autoRenewStatus) {
152 | this.autoRenewStatus = autoRenewStatus;
153 | }
154 |
155 | public String getRefundPayOrderId() {
156 | return refundPayOrderId;
157 | }
158 |
159 | public void setRefundPayOrderId(String refundPayOrderId) {
160 | this.refundPayOrderId = refundPayOrderId;
161 | }
162 |
163 | public String getProductId() {
164 | return productId;
165 | }
166 |
167 | public void setProductId(String productId) {
168 | this.productId = productId;
169 | }
170 |
171 | public String getApplicationId() {
172 | return applicationId;
173 | }
174 |
175 | public void setApplicationId(String applicationId) {
176 | this.applicationId = applicationId;
177 | }
178 |
179 | public Integer getExpirationIntent() {
180 | return expirationIntent;
181 | }
182 |
183 | public void setExpirationIntent(Integer expirationIntent) {
184 | this.expirationIntent = expirationIntent;
185 | }
186 | }
187 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/notification/StatusUpdateNotificationRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo.notification;
19 |
20 | /**
21 | * 功能描述
22 | *
23 | * @author iap
24 | * @since 2020-02-05
25 | */
26 | public class StatusUpdateNotificationRequest {
27 | private String statusUpdateNotification;
28 |
29 | private String notifycationSignature;
30 |
31 | public String getStatusUpdateNotification() {
32 | return statusUpdateNotification;
33 | }
34 |
35 | public void setStatusUpdateNotification(String statusUpdateNotification) {
36 | this.statusUpdateNotification = statusUpdateNotification;
37 | }
38 |
39 | public String getNotifycationSignature() {
40 | return notifycationSignature;
41 | }
42 |
43 | public void setNotifycationSignature(String notifycationSignature) {
44 | this.notifycationSignature = notifycationSignature;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/huawei/iap/demo/notification/StatusUpdateNotificationResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | package com.huawei.iap.demo.notification;
19 |
20 | /**
21 | * 功能描述
22 | *
23 | * @author iap
24 | * @since 2020-02-05
25 | */
26 | public class StatusUpdateNotificationResponse {
27 | private String errorCode;
28 |
29 | private String errorMsg;
30 |
31 | public String getErrorCode() {
32 | return errorCode;
33 | }
34 |
35 | public void setErrorCode(String errorCode) {
36 | this.errorCode = errorCode;
37 | }
38 |
39 | public String getErrorMsg() {
40 | return errorMsg;
41 | }
42 |
43 | public void setErrorMsg(String errorMsg) {
44 | this.errorMsg = errorMsg;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------