Skip to content

Commit bb8c664

Browse files
refactor: custom verification/synchronization
1 parent d48a065 commit bb8c664

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

android/src/main/java/com/capacitor_subscriptions/capacitor/Subscriptions.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import androidx.annotation.NonNull;
2525

26+
import java.util.Date;
27+
import java.io.OutputStream;
2628
import java.io.BufferedReader;
2729
import java.io.InputStreamReader;
2830
import java.net.HttpURLConnection;
@@ -42,8 +44,9 @@ public class Subscriptions {
4244
private final BillingClient billingClient;
4345
private int billingClientIsConnected = 0;
4446

45-
private String googleVerifyEndpoint = "";
46-
private String googleBid = "";
47+
private String apiEndpoint = "";
48+
private String jwt = "";
49+
private String bid = "";
4750

4851
public Subscriptions(SubscriptionsPlugin plugin, BillingClient billingClient) {
4952

@@ -74,9 +77,10 @@ public String echo(String value) {
7477
return value;
7578
}
7679

77-
public void setGoogleVerificationDetails(String googleVerifyEndpoint, String bid) {
78-
this.googleVerifyEndpoint = googleVerifyEndpoint;
79-
this.googleBid = bid;
80+
public void setApiVerificationDetails(String apiEndpoint, String jwt, String bid) {
81+
this.apiEndpoint = apiEndpoint;
82+
this.jwt = jwt;
83+
this.bid = bid;
8084

8185
Log.i("SET-VERIFY", "Verification values updated");
8286
}
@@ -183,13 +187,13 @@ public void getLatestTransaction(String productIdentifier, PluginCall call) {
183187
found = true;
184188

185189
JSObject data = new JSObject();
186-
String expiryDate = getExpiryDateFromGoogle(productIdentifier, currentPurchaseHistoryRecord.get("purchaseToken").toString());
187-
if (expiryDate != null) {
188-
data.put("expiryDate", expiryDate);
189-
}
190190
Calendar calendar = Calendar.getInstance();
191191
calendar.setTimeInMillis(Long.parseLong((currentPurchaseHistoryRecord.get("purchaseTime").toString())));
192192
String orderId = currentPurchaseHistoryRecord.optString("orderId", ""); // Usamos optString para obtener un valor por defecto si la clave no existe
193+
String expiryDate = getExpiryDateFromApi(orderId);
194+
if (expiryDate != null) {
195+
data.put("expiryDate", expiryDate);
196+
}
193197
data.put("productIdentifier", currentPurchaseHistoryRecord.get("productId"));
194198
data.put("originalId", orderId);
195199
data.put("transactionId", orderId);
@@ -249,8 +253,8 @@ public void getCurrentEntitlements(PluginCall call) {
249253

250254
Purchase currentPurchase = purchaseList.get(i);
251255

252-
String expiryDate = this.getExpiryDateFromGoogle(currentPurchase.getProducts().get(0), currentPurchase.getPurchaseToken());
253256
String orderId = currentPurchase.getOrderId();
257+
String expiryDate = this.getExpiryDateFromApi(orderId);
254258

255259
String dateFormat = "dd-MM-yyyy hh:mm";
256260
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.getDefault());
@@ -351,14 +355,25 @@ public void purchaseProduct(String productIdentifier, String accountId, PluginCa
351355

352356
}
353357

354-
private String getExpiryDateFromGoogle(String productIdentifier, String purchaseToken) {
358+
private String getExpiryDateFromApi(String transactionId) {
355359

356360
try {
357361

358362
// Compile request to verify purchase token
359-
URL obj = new URL(this.googleVerifyEndpoint + "&bid=" + this.googleBid + "&subId=" + productIdentifier + "&purchaseToken=" + purchaseToken);
363+
URL obj = new URL(this.apiEndpoint);
360364
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
361365
con.setRequestMethod("POST");
366+
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
367+
con.setRequestProperty("Authorization", "Bearer " + this.jwt);
368+
con.setDoOutput(true);
369+
370+
// JSON-Body erzeugen
371+
String body = "{\"transaction_id\": \"" + transactionId + "\"}";
372+
373+
try (OutputStream os = con.getOutputStream()) {
374+
byte[] input = body.getBytes(StandardCharsets.UTF_8);
375+
os.write(input, 0, input.length);
376+
}
362377

363378
// Try to receive response from server
364379
try (BufferedReader br = new BufferedReader(
@@ -373,20 +388,20 @@ private String getExpiryDateFromGoogle(String productIdentifier, String purchase
373388

374389
// If the response was successful, extract expiryDate and put it in our response data property
375390
if (con.getResponseCode() == 200) {
376-
377391
JSObject postResponseJSON = new JSObject(googleResponse.toString());
378-
JSObject googleResponseJSON = new JSObject(postResponseJSON.get("googleResponse").toString()); // <-- note the typo in response object from server
379-
JSObject payloadJSON = new JSObject(googleResponseJSON.get("payload").toString());
380392

381-
String dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'";
393+
String dateFormat = "yyyy-MM-dd HH:mm:ss";
382394
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.getDefault());
395+
396+
// Direkt den gewünschten Schlüssel auslesen
397+
String expiryString = postResponseJSON.getString("expiryDate");
398+
Date date = simpleDateFormat.parse(expiryString);
399+
383400
Calendar calendar = Calendar.getInstance();
384-
calendar.setTimeInMillis(Long.parseLong((payloadJSON.get("expiryTimeMillis").toString())));
401+
calendar.setTime(date);
385402

386403
Log.i("EXPIRY", simpleDateFormat.format(calendar.getTime()));
387-
388404
return simpleDateFormat.format(calendar.getTime());
389-
390405
} else {
391406
return null;
392407
}

android/src/main/java/com/capacitor_subscriptions/capacitor/SubscriptionsPlugin.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ public void load() {
8989
}
9090

9191
@PluginMethod
92-
public void setGoogleVerificationDetails(PluginCall call) {
93-
String googleVerifyEndpoint = call.getString("googleVerifyEndpoint");
92+
public void setApiVerificationDetails(PluginCall call) {
93+
String apiEndpoint = call.getString("apiEndpoint");
94+
String jwt = call.getString("jwt");
9495
String bid = call.getString("bid");
9596

9697
Log.i("SET-VERIFY", "Verification values updated");
9798

98-
if(googleVerifyEndpoint != null && bid != null) {
99-
implementation.setGoogleVerificationDetails(googleVerifyEndpoint, bid);
99+
if(apiEndpoint != null && jwt != null && bid != null) {
100+
implementation.setApiVerificationDetails(apiEndpoint, jwt, bid);
100101
} else {
101102
call.reject("Missing required parameters");
102103
}
@@ -164,18 +165,18 @@ public void getCurrentEntitlements(PluginCall call) {
164165
public void manageSubscriptions(PluginCall call) {
165166

166167
String productIdentifier = call.getString("productIdentifier");
167-
String bid = call.getString("bid");
168+
String jwt = call.getString("jwt");
168169

169170
if(productIdentifier == null) {
170171
call.reject("Must provide a productID");
171172
}
172173

173-
if(bid == null) {
174+
if(jwt == null) {
174175
call.reject("Must provide a bundleID");
175176
}
176177

177178
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
178-
Uri.parse("https://play.google.com/store/account/subscriptions?sku=" + productIdentifier + "&package=" + bid));
179+
Uri.parse("https://play.google.com/store/account/subscriptions?sku=" + productIdentifier + "&package=" + jwt));
179180
getActivity().startActivity(browserIntent);
180181
}
181182

0 commit comments

Comments
 (0)