voltrue2 / in-app-purchase

A Node.js module for in-App-Purchase for iOS, Android, Amazon and Windows.
http://iap.gracenode.org
Other
1.05k stars 289 forks source link

Which fields are recommended to check if subscription hasn't expired and will renew? #283

Open booboothefool opened 5 years ago

booboothefool commented 5 years ago

I see the fields:

response
  latest_receipt_info
  pending_renewal_info
purchaseData

I noticed they all have a way of doing it. For latest_receipt_info seems like have to sort by date then use the expiration, pending_renewal_info => auto_renew_status and purchaseData => expirationDate, etc.

I am not sure what they specify exactly because they seem really similar. What is the difference between latest_receipt_info and purchaseData for example?

What is the recommended approach to extend my user's subscription to a later date to store in my backend?

elidrissizakaria6 commented 4 years ago

Hello, i want to know if you found the best way to do this ? thanks u a lot !

chr4ss1 commented 4 years ago

Use cancellationDate & expirationDate that you get from this package.

    const validatedData = await iap.validate(receipt);

    if (!iap.isValidated(validatedData)) {
        throw new Error('validation failed..');
    }

    const latestPurchaseDataItems = await iap.getPurchaseData(validatedData, {});

const latestPurchaseData = latestPurchaseDataItems[0]
const toStoreInDb = {
            cancellationDateMs: latestPurchaseData.cancellationDate ? latestPurchaseData.cancellationDate : null,
            expirationDateMs: parseInt(latestPurchaseData.expirationDate),
        };

You will then need to implement cron job that runs every hour on server side that starts to refresh existing receipts in the database when it is close to expiring. Once every hour for day before and after.

    const daysDelta = 1;
    const d1 = getTodaysDateDaysSubtracted(daysDelta);
    const d2 = getTodaysDateDaysAdded(daysDelta);

    const purchaseReceipts = await mongoExecute(async (db) => {
        return await db
            .collection('PurchaseReceipt')
            .find({
                type: constants.PurchaseTypes.subscription,
                source: 'purchase',

                'data.cancellationDateMs': { $eq: null },
                'data.expirationDateMs': {
                    $gte: d1.getTime(),
                    $lte: d2.getTime()
                },
            })
            .hint('expirationDateMs')
            .toArray();
    });