@@ -31,9 +31,17 @@ import kotlinx.coroutines.flow.first
3131import kotlinx.coroutines.launch
3232import dev.hyo.openiap.ProductAndroid
3333import dev.hyo.openiap.ProductQueryType
34+ import dev.hyo.openiap.ProductRequest
3435import dev.hyo.openiap.ProductType
3536import dev.hyo.openiap.Purchase
3637import dev.hyo.openiap.PurchaseAndroid
38+ import dev.hyo.openiap.PurchaseInput
39+ import dev.hyo.openiap.RequestPurchaseProps
40+ import dev.hyo.openiap.RequestPurchaseAndroidProps
41+ import dev.hyo.openiap.RequestPurchasePropsByPlatforms
42+ import dev.hyo.openiap.RequestSubscriptionAndroidProps
43+ import dev.hyo.openiap.RequestSubscriptionPropsByPlatforms
44+ import dev.hyo.openiap.utils.toPurchaseInput
3745import dev.hyo.martie.util.findActivity
3846
3947@OptIn(ExperimentalMaterial3Api ::class )
@@ -74,11 +82,12 @@ fun PurchaseFlowScreen(
7482 val connected = iapStore.initConnection()
7583 if (connected) {
7684 iapStore.setActivity(activity)
77- iapStore.fetchProducts (
85+ val request = ProductRequest (
7886 skus = IapConstants .INAPP_SKUS ,
7987 type = ProductQueryType .InApp
8088 )
81- iapStore.getAvailablePurchases()
89+ iapStore.fetchProducts(request)
90+ iapStore.getAvailablePurchases(null )
8291 }
8392 } catch (_: Exception ) { }
8493 }
@@ -107,10 +116,11 @@ fun PurchaseFlowScreen(
107116 scope.launch {
108117 try {
109118 iapStore.setActivity(activity)
110- iapStore.fetchProducts (
119+ val request = ProductRequest (
111120 skus = IapConstants .INAPP_SKUS ,
112121 type = ProductQueryType .InApp
113122 )
123+ iapStore.fetchProducts(request)
114124 } catch (_: Exception ) { }
115125 }
116126 },
@@ -238,13 +248,32 @@ fun PurchaseFlowScreen(
238248 isPurchasing = status.isPurchasing(androidProduct.id),
239249 onPurchase = {
240250 scope.launch {
241- val reqType = if (androidProduct.type == ProductType .Subs )
242- ProductQueryType .Subs else ProductQueryType .InApp
243251 iapStore.setActivity(activity)
244- iapStore.requestPurchase(
245- skus = listOf (androidProduct.id),
246- type = reqType
247- )
252+ if (androidProduct.type == ProductType .Subs ) {
253+ val props = RequestPurchaseProps (
254+ request = RequestPurchaseProps .Request .Subscription (
255+ RequestSubscriptionPropsByPlatforms (
256+ android = RequestSubscriptionAndroidProps (
257+ skus = listOf (androidProduct.id)
258+ )
259+ )
260+ ),
261+ type = ProductQueryType .Subs
262+ )
263+ iapStore.requestPurchase(props)
264+ } else {
265+ val props = RequestPurchaseProps (
266+ request = RequestPurchaseProps .Request .Purchase (
267+ RequestPurchasePropsByPlatforms (
268+ android = RequestPurchaseAndroidProps (
269+ skus = listOf (androidProduct.id)
270+ )
271+ )
272+ ),
273+ type = ProductQueryType .InApp
274+ )
275+ iapStore.requestPurchase(props)
276+ }
248277 }
249278 },
250279 onClick = {
@@ -263,23 +292,7 @@ fun PurchaseFlowScreen(
263292 )
264293 }
265294 }
266-
267- // Active Purchases Section
268- if (androidPurchases.isNotEmpty()) {
269- item {
270- SectionHeaderView (title = " Active Purchases" )
271- }
272-
273- items(androidPurchases) { androidPurchase ->
274- ActivePurchaseCard (
275- purchase = androidPurchase,
276- onClick = {
277- selectedPurchase = androidPurchase
278- }
279- )
280- }
281- }
282-
295+
283296 // Instructions Card
284297 item {
285298 InstructionCard ()
@@ -297,7 +310,7 @@ fun PurchaseFlowScreen(
297310 onClick = {
298311 scope.launch {
299312 try {
300- val restored = iapStore.restorePurchases( )
313+ val restored = iapStore.getAvailablePurchases( null )
301314 iapStore.postStatusMessage(
302315 message = " Restored ${restored.size} purchases" ,
303316 status = PurchaseResultStatus .Success
@@ -322,10 +335,11 @@ fun PurchaseFlowScreen(
322335 onClick = {
323336 scope.launch {
324337 try {
325- iapStore.fetchProducts (
338+ val request = ProductRequest (
326339 skus = IapConstants .INAPP_SKUS ,
327340 type = ProductQueryType .InApp
328341 )
342+ iapStore.fetchProducts(request)
329343 } catch (_: Exception ) { }
330344 }
331345 },
@@ -380,21 +394,22 @@ fun PurchaseFlowScreen(
380394 }
381395
382396 // 4) Finish transaction
383- val ok = iapStore.finishTransaction(purchase, isConsumable)
384- if (! ok) {
385- iapStore.postStatusMessage(
386- message = " finishTransaction failed" ,
387- status = PurchaseResultStatus .Error ,
388- productId = purchase.productId
389- )
390- } else {
391- iapStore.loadPurchases()
397+ val purchaseInput = purchase.toPurchaseInput()
398+ try {
399+ iapStore.finishTransaction(purchaseInput, isConsumable)
400+ iapStore.getAvailablePurchases(null ) // Reload purchases after finishing
392401 iapStore.postStatusMessage(
393402 message = " Purchase finished successfully" ,
394403 status = PurchaseResultStatus .Success ,
395404 productId = purchase.productId
396405 )
397406 selectedProduct = null
407+ } catch (e: Exception ) {
408+ iapStore.postStatusMessage(
409+ message = " finishTransaction failed: ${e.message} " ,
410+ status = PurchaseResultStatus .Error ,
411+ productId = purchase.productId
412+ )
398413 }
399414 } catch (e: Exception ) {
400415 iapStore.postStatusMessage(
@@ -412,13 +427,32 @@ fun PurchaseFlowScreen(
412427 onDismiss = { selectedProduct = null },
413428 onPurchase = {
414429 uiScope.launch {
415- val reqType = if (product.type == ProductType .Subs )
416- ProductQueryType .Subs else ProductQueryType .InApp
417430 iapStore.setActivity(activity)
418- iapStore.requestPurchase(
419- skus = listOf (product.id),
420- type = reqType
421- )
431+ if (product.type == ProductType .Subs ) {
432+ val props = RequestPurchaseProps (
433+ request = RequestPurchaseProps .Request .Subscription (
434+ RequestSubscriptionPropsByPlatforms (
435+ android = RequestSubscriptionAndroidProps (
436+ skus = listOf (product.id)
437+ )
438+ )
439+ ),
440+ type = ProductQueryType .Subs
441+ )
442+ iapStore.requestPurchase(props)
443+ } else {
444+ val props = RequestPurchaseProps (
445+ request = RequestPurchaseProps .Request .Purchase (
446+ RequestPurchasePropsByPlatforms (
447+ android = RequestPurchaseAndroidProps (
448+ skus = listOf (product.id)
449+ )
450+ )
451+ ),
452+ type = ProductQueryType .InApp
453+ )
454+ iapStore.requestPurchase(props)
455+ }
422456 }
423457 },
424458 isPurchasing = status.isPurchasing(product.id)
0 commit comments