2323
2424import androidx .annotation .NonNull ;
2525
26+ import java .util .Date ;
27+ import java .io .OutputStream ;
2628import java .io .BufferedReader ;
2729import java .io .InputStreamReader ;
2830import 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 }
0 commit comments