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

Apple cancellationDate is not in ms #291

Open naciriii opened 4 years ago

naciriii commented 4 years ago

Summary:

When trying to validate a receipt of a cancelled subscription the cancellation date is returned in datetime str format instead of epoch for this , we need either to transform cancellation_date to epoch or use the cancellation_date_ms which exists in the receipt attribute . I'll create a PR for that today

Code sample

Properties declaration

Update the PURCHASE _DATE to PURCHASE_DATE_MS field

var REC_KEYS = {
    IN_APP: 'in_app',
    LRI: 'latest_receipt_info',
    BUNDLE_ID: 'bundle_id',
    BID: 'bid',
    TRANSACTION_ID: 'transaction_id',
    ORIGINAL_TRANSACTION_ID: 'original_transaction_id',
    PRODUCT_ID: 'product_id',
    ITEM_ID: 'item_id',
    ORIGINAL_PURCHASE_DATE_MS: 'original_purchase_date_ms',
    EXPIRES_DATE_MS: 'expires_date_ms',
    EXPIRES_DATE: 'expires_date',
    EXPIRATION_DATE: 'expiration_date',
    EXPIRATION_INTENT: 'expiration_intent',
    CANCELLATION_DATE_MS: 'cancellation_date_ms',
    PURCHASE_DATE_MS: 'purchase_date_ms',
    IS_TRIAL: 'is_trial_period'
}

Get cancellationDate as int from CANCELLATION_DATE_MS field

     var receipt = purchase[REC_KEYS.LRI] || purchase.receipt;
    data.push({
        bundleId: receipt[REC_KEYS.BUNDLE_ID] || receipt[REC_KEYS.BID],
        appItemId: receipt[REC_KEYS.ITEM_ID],
        originalTransactionId:    receipt[REC_KEYS.ORIGINAL_TRANSACTION_ID],
        transactionId:    receipt[REC_KEYS.TRANSACTION_ID],
        productId: receipt[REC_KEYS.PRODUCT_ID],
        originalPurchaseDate: receipt[REC_KEYS.ORIGINAL_PURCHASE_DATE_MS],
        purchaseDate: receipt[REC_KEYS.PURCHASE_DATE_MS],
        quantity: parseInt(receipt.quantity, 10),
        expirationDate: getSubscriptionExpireDate(receipt),
        isTrial: bool(receipt[REC_KEYS.IS_TRIAL]),
        cancellationDate: parseInt(receipt[REC_KEYS.CANCELLATION_DATE_MS]) || 0
    });