Last active
January 10, 2024 14:32
-
-
Save idesignzone/962746e23f967286f0608f0b7cc276c2 to your computer and use it in GitHub Desktop.
cordova-plugin-purchase vue3 composable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ref } from "vue"; | |
import { Capacitor } from "@capacitor/core"; | |
import "cordova-plugin-purchase/www/store"; | |
export function useInAppPurchase() { | |
const iapProduct = ref(); | |
const iapOrder = ref(); | |
if (Capacitor.isNativePlatform()) { | |
const { store, ProductType, Platform } = CdvPurchase; | |
refreshUI(); | |
store.register({ | |
type: ProductType.PAID_SUBSCRIPTION, | |
id: "subscription_product_id", | |
platform: Platform.GOOGLE_PLAY, | |
}); | |
store.when().productUpdated(refreshUI).approved(finishPurchase); | |
store.initialize([Platform.GOOGLE_PLAY, ProductType.PAID_SUBSCRIPTION]); | |
} | |
function finishPurchase(p) { | |
p.finish(); | |
refreshUI(); | |
} | |
function refreshUI() { | |
const { store, ProductType, Platform } = CdvPurchase; | |
iapProduct.value = store.get( | |
"subscription_product_id", | |
Platform.GOOGLE_PLAY, | |
ProductType.PAID_SUBSCRIPTION | |
); | |
iapOrder.value = store.findInLocalReceipts(iapProduct.value); | |
} | |
function orderProduct() { | |
const { store, ProductType, Platform } = CdvPurchase; | |
iapProduct.value = store.get( | |
"subscription_product_id", | |
Platform.GOOGLE_PLAY, | |
ProductType.PAID_SUBSCRIPTION | |
); | |
iapProduct.value.getOffer().order(); | |
} | |
return { | |
refreshUI, | |
iapProduct, | |
iapOrder, | |
orderProduct, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change
"subscription_product_id"
with your subscription product id.