daniele-pecora / nativescript-in-app-purchase

NativeScript plugin to handle in app purchases and subscriptions on Android and iOS.
Apache License 2.0
13 stars 10 forks source link

How do we get iOS subscription receipt data? #6

Open justintoth opened 3 years ago

justintoth commented 3 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

Thanks for making this plugin, it's really helpful since the nativescript-purchase plugin doesn't build on Android with the latest version of NativeScript! Question... I'm integrating this plugin for iOS in-app purchases, however the one thing I can't figure out how to do is to retrieve the receipt data for an iOS subscription. The nativescript-purchase plugin had a receiptData property on their transaction object, however InAppPurchaseTransactionState doesn't seem to have this.

Is there any code involved?

public initIosSubscriptions(user: UserView) {
        console.log('Initializing iOS subscriptions...');
        const productName = this.iosProductName(user);
        console.log(`Product name: ${productName}`);

        // Get iOS subscription product, based on number of units and discounts.
        this.inAppPurchaseManager.list([ productName ], InAppPurchaseType.Subscription)
            .then((result: InAppListProductsResult) => {
                if (!result.products?.length) {
                    console.warn(`Could not find iOS subscription product with name: ${productName}`);
                    alert('Purchasing a subscription is not available on this device! Please try our website.');
                    return;
                }
                this.iosProduct = result.products[0];
                // Found iOS subscription product, set up listener for when we purchase a product.
                console.log('Setting up listener for purchasing products...');
                const purchaseStateUpdateListener: InAppPurchaseStateUpdateListener = {
                    onUpdate: (purchaseTransactionState: InAppPurchaseTransactionState): void => {
                        // Item has been purchased, sync local items list ...
                        console.log('In transaction updated event! purchaseTransactionState: ', purchaseTransactionState);
                        const key = `${purchaseTransactionState.productIdentifier}-${purchaseTransactionState.resultCode}`;
                        if (!this.processedTransactionUpdates[key]) {
                            this.processedTransactionUpdates[key] = true;
                            if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Failed)
                                alert(`Purchase of ${purchaseTransactionState.productIdentifier} failed!`);
                            else if (purchaseTransactionState.transactionReceipt)
                                // Purchase succeeded, store receipt so we can check if the iOS subscription is active from a nightly job.
                                this.userService
                                    .updateIOSSettings({ input: { receiptData: purchaseTransactionState.transactionReceipt } })
                                    .subscribe(updatedUser => {
                                        this.authService.save(updatedUser);
                                        this.iosProductUpdated.emit(purchaseTransactionState.resultCode === InAppPurchaseResultCode.Purchased);
                                    });
                        }
                    },
                    onUpdateHistory: (purchaseTransactionState: InAppPurchaseTransactionState): void => {
                        if (purchaseTransactionState.resultCode === InAppPurchaseResultCode.Restored) {
                            // Item has been  restored, sync local items list ...
                        }
                    }
                };
                InAppPurchaseManager.bootStrapInstance(purchaseStateUpdateListener).then(inAppPurchaseManager => {
                    this.inAppPurchaseManager = inAppPurchaseManager
                });
            })
    }