Skip to content

Instantly share code, notes, and snippets.

@idesignzone
Last active January 10, 2024 14:32
Show Gist options
  • Save idesignzone/962746e23f967286f0608f0b7cc276c2 to your computer and use it in GitHub Desktop.
Save idesignzone/962746e23f967286f0608f0b7cc276c2 to your computer and use it in GitHub Desktop.
cordova-plugin-purchase vue3 composable
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,
};
}
@idesignzone
Copy link
Author

Change "subscription_product_id" with your subscription product id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment