Skip to content

Commit 25bfeaf

Browse files
kedarchandrayanShlok GildaDhananjay Patil
authored
Redemptions (#47)
* Added services for redemptions related services. * Updated readme for redemptions service. * Updated readme for redeemable SKUs service. * Bug fix. * Updated readme. * readme updated Co-authored-by: Shlok Gilda <shlok@ost.com> Co-authored-by: Dhananjay Patil <dhananjay@ost.com>
1 parent 4f59876 commit 25bfeaf

13 files changed

Lines changed: 444 additions & 3 deletions

File tree

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ OST_KIT_RECOVERY_OWNER_ADDRESS=RecoveryOwnerAddressOfUser
99
OST_KIT_SESSION_ADDRESS=SessionAddressOfUser
1010
OST_KIT_RULE_ADDRESS=AddressForExecutingRule
1111
OST_KIT_USER2_TOKEN_HOLDER_ADDRESS=TokenHolderAddressForReceiver
12-
OST_KIT_TRANSACTION_ID=TransactionIdUsedToTestGet
12+
OST_KIT_TRANSACTION_ID=TransactionIdUsedToTestGet
13+
OST_KIT_REDEMPTION_ID=RedemptionIdAssociatedWithUser
14+
OST_KIT_REDEEMABLE_SKU_ID=RedeemableSkuId

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[OST JAVA SDK v2.2.3](https://github.com/ostdotcom/ost-sdk-java/tree/v2.2.3)
2+
---
3+
4+
* Added redemptions module to call user redemptions management OST APIs.
5+
* Added redeemable SKUs module to call redeemable SKUs OST APIs.
6+
17
[OST JAVA SDK v2.2.2](https://github.com/ostdotcom/ost-sdk-java/tree/v2.2.2)
28
---
39

README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,114 @@ For executing transactions, you need to understand the 4 modules described below
852852
Boolean response = webhooksService.verifySignature( version, stringifiedData, requestTimestamp, signature, webhookSecret );
853853
System.out.println("response: " + response );
854854
```
855+
856+
857+
### Redemption Modules
858+
859+
Two modules of redemption, "Redeemable SKUs" and "User Redemptions", are described below.
860+
861+
#### Redeemable SKUs Module
862+
863+
* Initialize Redeemable SKUs service object to perform redeemable skus specific actions.
864+
865+
```java
866+
com.ost.services.RedeemableSkus redeemableSkusService = services.redeemableSkus;
867+
```
868+
869+
* Get Redeemable SKU detail using the redeemable sku id.
870+
871+
```java
872+
// Mandatory API parameters
873+
874+
// Fetch details of following redeemable sku.
875+
String redeemableSkuId = 'c2c__';
876+
877+
HashMap <String,Object> params = new HashMap<String,Object>();
878+
params.put("redeemable_sku_id", redeemableSkuId);
879+
880+
JsonObject response = redeemableSkusService.get( params );
881+
System.out.println("response: " + response.toString() );
882+
```
883+
884+
* Get Redeemable SKUs List. Pagination is supported by this API.
885+
886+
```java
887+
// Mandatory API parameters
888+
// NOTE: No mandatory parameters.
889+
890+
// Optional API parameters
891+
892+
// Limit.
893+
long limit = 10;
894+
895+
// Array of redeemable SKU ids.
896+
ArrayList<Object> redemptionSkuIdsArray = new ArrayList<Object>();
897+
redemptionSkuIdsArray.add("1001");
898+
redemptionSkuIdsArray.add("1002");
899+
900+
// Pagination identifier from the previous API call response. Not needed for page one.
901+
String paginationIdentifier = "eyJ___";
902+
903+
HashMap <String,Object> params = new HashMap<String,Object>();
904+
params.put("redemption_ids", redemptionSkuIdsArray);
905+
params.put("pagination_identifier", paginationIdentifier);
906+
params.put("limit", limit);
907+
908+
JsonObject response = redeemableSkusService.getList( params );
909+
System.out.println("response: " + response.toString() );
910+
```
911+
#### User Redemptions Module
912+
913+
* Initialize Redemptions service object to perform user redemption specific actions.
914+
915+
```java
916+
com.ost.services.Redemptions redemptionsService = services.redemptions;
917+
```
918+
919+
* Get User redemption details using the userId and redemptionId.
920+
921+
```java
922+
// Mandatory API parameters
923+
924+
// UserId of user for whom redemption details needs to be fetched.
925+
String userId = "c2c6___";
926+
927+
// Unique identifier of the redemption of user.
928+
String redemptionId = "c2c___";
929+
930+
HashMap <String,Object> params = new HashMap<String,Object>();
931+
params.put("user_id", userId);
932+
params.put("redemption_id", redemptionId);
933+
934+
JsonObject response = redemptionsService.get( params );
935+
System.out.println("response: " + response.toString() );
936+
```
937+
938+
* Get User Redemptions List. Pagination is supported by this API.
939+
940+
```java
941+
// Mandatory API parameters
942+
String userId = "c2c6___";
943+
944+
// Optional API parameters
945+
946+
// Limit.
947+
long limit = 10;
948+
949+
// Array of user redemption uuids.
950+
ArrayList<Object> redemptionIdsArray = new ArrayList<Object>();
951+
redemptionIdsArray.add("eyJ___");
952+
redemptionIdsArray.add("eyJ___");
953+
954+
// Pagination identifier from the previous API call response. Not needed for page one.
955+
String paginationIdentifier = "eyJ___";
956+
957+
HashMap <String,Object> params = new HashMap<String,Object>();
958+
params.put("user_id", userId);
959+
params.put("redemption_ids", redemptionIdsArray);
960+
params.put("pagination_identifier", paginationIdentifier);
961+
params.put("limit", limit);
962+
963+
JsonObject response = redemptionsService.getList( params );
964+
System.out.println("response: " + response.toString() );
965+
```

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.2.2
1+
2.2.3

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.ost</groupId>
55
<artifactId>ost-sdk-java</artifactId>
6-
<version>2.2.2-SNAPSHOT</version>
6+
<version>2.2.3-SNAPSHOT</version>
77
<name>OST SDK for Java</name>
88
<description>OST Platform SDK for Java</description>
99
<packaging>jar</packaging>

src/main/java/com/Test.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.ost;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.ost.services.Manifest;
7+
import com.ost.services.OSTAPIService;
8+
import com.ost.services.Webhooks;
9+
import okio.Buffer;
10+
import okio.ByteString;
11+
12+
import javax.crypto.Mac;
13+
import javax.crypto.spec.SecretKeySpec;
14+
15+
import java.io.IOException;
16+
import java.security.InvalidKeyException;
17+
import java.security.NoSuchAlgorithmException;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
21+
import static com.google.common.base.Charsets.UTF_8;
22+
23+
public class Test {
24+
private static final String HMAC_SHA256 = "HmacSHA256";
25+
26+
public static void main(String args[]) throws OSTAPIService.MissingParameter, IOException, OSTAPIService.InvalidParameter {
27+
28+
HashMap <String,Object> sdkConfig = new HashMap<String,Object>();
29+
sdkConfig.put("apiEndpoint","http://developmentost.com:7001/testnet/v2");
30+
sdkConfig.put("apiKey","d2e5ce61e385b444b9439a57990664ab");
31+
sdkConfig.put("apiSecret","92e540a9086da42efaa971dfc994ac9a5070a4ebe3eb0fabaa5e5a35478a8cf0");
32+
33+
HashMap <String,Object> nestedparam = new HashMap<String,Object>();
34+
// This is the timeout in seconds for which the socket connection will remain open
35+
// The value of timeout will always be of type long
36+
nestedparam.put("timeout", (long) 60);
37+
sdkConfig.put("config", nestedparam);
38+
39+
OSTSDK ostObj = new OSTSDK(sdkConfig);
40+
Manifest services = (Manifest) ostObj.services;
41+
Webhooks webhooksService = services.webhooks;
42+
43+
// String version = System.getProperty("java.version");
44+
// String randomUrl = "https://www.yourdomain" + (System.currentTimeMillis() / 1000) + "-" + version + ".com";
45+
//
46+
HashMap<String, Object> params = new HashMap<String, Object>();
47+
// ArrayList<String> arrayList = new ArrayList<String>();
48+
// arrayList.add("devices/authorization_initiate");
49+
// params.put("url", randomUrl);
50+
// params.put("status", "active");
51+
// params.put("topics", arrayList);
52+
// HashMap<String, Object> params = new HashMap<String, Object>();
53+
params.put("webhook_id", "b036aff5-75a3-466d-a20c-a956b198fd14");
54+
JsonObject response = webhooksService.deleteWebhook( params );
55+
System.out.println("response: " + response.toString() );
56+
57+
}
58+
59+
public static boolean verifySignature( String version, String stringifiedData, String requestTimestamp, String signature ) {
60+
61+
if(!(stringifiedData instanceof String)){
62+
Gson gsonObj = new Gson();
63+
stringifiedData = gsonObj.toJson(stringifiedData);
64+
}
65+
66+
Buffer hmacInputBuffer = new Buffer();
67+
hmacInputBuffer.writeUtf8(requestTimestamp);
68+
hmacInputBuffer.writeByte('.');
69+
hmacInputBuffer.writeUtf8(version);
70+
hmacInputBuffer.writeByte('.');
71+
hmacInputBuffer.writeUtf8(stringifiedData);
72+
73+
String webhookSecret = "09121ae7614856777fa36d63aca828e0ef14be77fb48fa149e0c0b50fec847a7";
74+
75+
SecretKeySpec keySpec = new SecretKeySpec( webhookSecret.getBytes( UTF_8 ), HMAC_SHA256);
76+
Mac mac;
77+
try {
78+
mac = Mac.getInstance(HMAC_SHA256);
79+
mac.init(keySpec);
80+
} catch ( NoSuchAlgorithmException e) {
81+
throw new IllegalStateException(e);
82+
} catch (InvalidKeyException e) {
83+
throw new IllegalStateException(e);
84+
}
85+
86+
byte[] bytes = hmacInputBuffer.readByteArray();
87+
byte[] result = mac.doFinal(bytes);
88+
89+
String computedSignature = ByteString.of(result).hex();
90+
91+
System.out.println("The computedSignature is : "+ computedSignature);
92+
93+
return signature.indexOf(computedSignature) >= 0;
94+
}
95+
}

src/main/java/com/ost/services/Manifest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Manifest extends OSTServiceManifest {
1717
public Chains chains;
1818
public BaseTokens baseTokens;
1919
public Webhooks webhooks;
20+
public Redemptions redemptions;
21+
public RedeemableSkus redeemableSkus;
2022

2123

2224
public Manifest( Map<String, Object> params) {
@@ -38,6 +40,8 @@ protected void init() {
3840
this.chains = new Chains( this.request );
3941
this.baseTokens = new BaseTokens( this.request );
4042
this.webhooks = new Webhooks( this.request);
43+
this.redemptions = new Redemptions( this.request);
44+
this.redeemableSkus = new RedeemableSkus( this.request);
4145
}
4246

4347
}

src/main/java/com/ost/services/OSTAPIService.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ public String getTransactionId(Map<String, Object> params) throws MissingParamet
7777
return specialCharacterEscape(transactionId);
7878
}
7979

80+
public String getRedemptionId(Map<String, Object> params) throws MissingParameter, InvalidParameter {
81+
String redemptionId = "";
82+
if (null == params || !params.containsKey("redemption_id") || null == params.get("redemption_id")) {
83+
throw new MissingParameter("redemption_id");
84+
}
85+
if (!isValidParameter(params.get("redemption_id"))) {
86+
throw new InvalidParameter("redemption_id");
87+
}
88+
redemptionId = params.get("redemption_id").toString();
89+
params.remove("redemption_id");
90+
return specialCharacterEscape(redemptionId);
91+
}
92+
93+
public String getRedeemableSkuId(Map<String, Object> params) throws MissingParameter, InvalidParameter {
94+
String redeemableSkuId = "";
95+
if (null == params || !params.containsKey("redeemable_sku_id") || null == params.get("redeemable_sku_id")) {
96+
throw new MissingParameter("redeemable_sku_id");
97+
}
98+
if (!isValidParameter(params.get("redeemable_sku_id"))) {
99+
throw new InvalidParameter("redeemable_sku_id");
100+
}
101+
redeemableSkuId = params.get("redeemable_sku_id").toString();
102+
params.remove("redeemable_sku_id");
103+
return specialCharacterEscape(redeemableSkuId);
104+
}
105+
80106
public String getRecoveryOwnerAddress(Map<String, Object> params) throws MissingParameter, InvalidParameter {
81107
String recoveryOwnerAddress = "";
82108
if (null == params || !params.containsKey("recovery_owner_address") || null == params.get("recovery_owner_address")) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.ost.services;
2+
3+
import com.google.gson.JsonObject;
4+
import com.ost.lib.OSTRequestClient;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
9+
public class RedeemableSkus extends OSTAPIService {
10+
private static String servicePrefix = "/redeemable-skus";
11+
private static String serviceSuffix = "";
12+
13+
public RedeemableSkus(OSTRequestClient ostRequestClient) {
14+
super(ostRequestClient, servicePrefix, serviceSuffix);
15+
}
16+
17+
/**
18+
* Get all redeemable SKUs
19+
* @param params Request Params
20+
* @return API Response
21+
*/
22+
public JsonObject getList( Map<String,Object> params ) throws IOException, InvalidParameter, MissingParameter {
23+
String resource = this.urlPrefix + "/";
24+
return this.request.get(resource, params);
25+
}
26+
27+
/**
28+
* Get a redeemable SKU
29+
* @param params Request Params
30+
* @return API Response
31+
*/
32+
public JsonObject get( Map<String,Object> params ) throws MissingParameter, IOException, InvalidParameter {
33+
String resource = this.urlPrefix + "/" + this.getRedeemableSkuId( params ) + "/";;
34+
return this.request.get(resource, params);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.ost.services;
2+
3+
import com.google.gson.JsonObject;
4+
import com.ost.lib.OSTRequestClient;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
9+
public class Redemptions extends OSTAPIService {
10+
private static String servicePrefix = "/users";
11+
private static String serviceSuffix = "/redemptions";
12+
13+
public Redemptions(OSTRequestClient ostRequestClient) {
14+
super(ostRequestClient, servicePrefix, serviceSuffix);
15+
}
16+
17+
/**
18+
* Get all redemptions for a user
19+
* @param params Request Params
20+
* @return API Response
21+
*/
22+
public JsonObject getList( Map<String,Object> params ) throws IOException, InvalidParameter, MissingParameter {
23+
String resource = this.urlPrefix + "/" + this.getUserId( params ) + this.urlSuffix + "/";
24+
return this.request.get(resource, params);
25+
}
26+
27+
/**
28+
* Get a redemption info
29+
* @param params Request Params
30+
* @return API Response
31+
*/
32+
public JsonObject get( Map<String,Object> params ) throws MissingParameter, IOException, InvalidParameter {
33+
String resource = this.urlPrefix + "/" + this.getUserId( params ) + this.urlSuffix + "/" + this.getRedemptionId(params);
34+
return this.request.get(resource, params);
35+
}
36+
}

0 commit comments

Comments
 (0)