Skip to content

Commit c418ab7

Browse files
committed
Add Record usage method for recurring billing
1 parent 00b44b6 commit c418ab7

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.cryptolens.methods;
2+
3+
import io.cryptolens.internal.BasicResult;
4+
import io.cryptolens.internal.HelperMethods;
5+
import io.cryptolens.models.RecordUsageModel;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* These methods are related to the <a href="https://help.cryptolens.io/recurring-payments/index">recurring billing</a> module.
12+
* They can all be accessed using an access token with 'Subscription' permission.
13+
*/
14+
public class Subscription {
15+
/**
16+
* This method records uses Stripe's metered billing to record usage for a certain subscription.
17+
* In order to use this mehtod, you need to have set up recurring billing. A record will be created using Stripe's API with action set to 'increment'.
18+
* More info: https://app.cryptolens.io/docs/api/v3/RecordUsage
19+
* @param token An access token with "GetProducts" permission.
20+
* @return BasicResult object. Null can be returned if an error occurs.
21+
*/
22+
public static BasicResult RecordUsage (String token, RecordUsageModel model) {
23+
Map<String, String> extraParams = new HashMap<>();
24+
extraParams.put("token", token);
25+
26+
return HelperMethods.SendRequestToWebAPI("subscription/recordusage/", model, extraParams, BasicResult.class);
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.cryptolens.models;
2+
3+
public class RecordUsageModel {
4+
public int ProductId;
5+
public String Key;
6+
public int Amount;
7+
8+
public RecordUsageModel(int productId, String key, int amount) {
9+
ProductId = productId;
10+
Key = key;
11+
Amount = amount;
12+
}
13+
}

src/test/java/io/cryptolens/ProductTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void testApp() throws Exception {
6565

6666
}
6767

68-
public void testAdd() throws Exception{
68+
public void testGetProducts() throws Exception{
6969

7070
init();
7171

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.cryptolens;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
import io.cryptolens.internal.BasicResult;
6+
import io.cryptolens.methods.Helpers;
7+
import io.cryptolens.methods.ProductMethods;
8+
import io.cryptolens.methods.Subscription;
9+
import io.cryptolens.models.GetProductsResult;
10+
import io.cryptolens.models.LicenseKey;
11+
import io.cryptolens.models.RecordUsageModel;
12+
import io.cryptolens.models.RegisterEventModel;
13+
import junit.framework.Test;
14+
import junit.framework.TestCase;
15+
import junit.framework.TestSuite;
16+
17+
import java.lang.reflect.Type;
18+
import java.nio.file.Files;
19+
import java.nio.file.Paths;
20+
import java.util.HashMap;
21+
22+
23+
/**
24+
* Unit test for simple App.
25+
*/
26+
public class SubscriptionTest
27+
extends TestCase
28+
{
29+
30+
HashMap<String, String> APIKey = null;
31+
LicenseKey license = null;
32+
String machineCode = "2FE620C9C62F6A8BBD17F2AF49E12434B7C2CFC67FD2F48C2CB090893C4B4694";
33+
34+
/**
35+
* Create the test case
36+
*
37+
* @param testName name of the test case
38+
*/
39+
public SubscriptionTest(String testName )
40+
{
41+
super( testName );
42+
}
43+
44+
/**
45+
* @return the suite of tests being tested
46+
*/
47+
public static Test suite()
48+
{
49+
return new TestSuite( SubscriptionTest.class );
50+
}
51+
52+
public void init() throws Exception{
53+
String api_content = new String(Files.readAllBytes(Paths.get("apikeys.json")), "UTF-8");
54+
Type type = new TypeToken<HashMap<String, String>>(){}.getType();
55+
APIKey = new Gson().fromJson(api_content, type);
56+
57+
assertTrue( true );
58+
59+
}
60+
/**
61+
* Rigourous Test :-)
62+
*/
63+
public void testApp() throws Exception {
64+
65+
66+
}
67+
68+
public void testRecordUsage() throws Exception{
69+
70+
init();
71+
72+
BasicResult res = Subscription.RecordUsage(APIKey.get("subscriptionmethods"),
73+
new RecordUsageModel(3349, "CMXKC-GUQRW-EJUGS-RRPUR", 1));
74+
75+
if(!Helpers.IsSuccessful(res)) {
76+
fail("Could not register usage");
77+
}
78+
79+
}
80+
}

0 commit comments

Comments
 (0)