Skip to main content

Lovable Prompt

Add restore purchases functionality to my app using the Despia SDK from: https://www.npmjs.com/package/despia-nativeFirst, install the package: npm install despia-nativeThen import it: import despia from 'despia-native'Retrieve purchase history by calling: despia("getpurchasehistory://", ["restoredData"])This will query the native app stores and return all purchases associated with the current device/account, including active subscriptions, expired subscriptions, and one-time purchases.The response array includes objects with :
  • transactionId - unique identifier for this specific transaction
  • originalTransactionId - identifier linking to the original purchase (useful for subscription renewals)
  • productId - the product identifier configured in App Store Connect / Google Play Console
  • type - either "subscription" or "product" (one-time purchase)
  • entitlementId - the entitlement/access level this purchase grants
  • isActive - boolean indicating if the purchase currently grants access
  • willRenew - boolean indicating if a subscription will auto-renew
  • purchaseDate - ISO timestamp of the most recent transaction
  • originalPurchaseDate - ISO timestamp of the initial purchase
  • expirationDate - ISO timestamp when access expires (null for lifetime purchases)
  • store - either "app_store" or "play_store"
  • country - user’s country code
  • environment - "production" or "sandbox"
  • receipt - the raw receipt data for server-side validation
Use this to implement “Restore Purchases” buttons required by App Store guidelines, and to verify user entitlements on app launch.
This feature requires native capabilities which will be fully provided by the “despia-native” npm package, no additional native libraries are needed!
Please follow the installation instructions for the “despia-native” npm package closely, and do not modify my instructions. Implementation as mentioned is critical.

How it Works

Despia queries the native platform’s billing system to retrieve all purchases associated with the current user’s App Store or Google Play account. This includes active subscriptions, expired subscriptions, consumables, and non-consumable (lifetime) purchases. The data is normalized into a consistent format across both iOS and Android platforms.

Installation

Install the Despia package from NPM:
npm install despia-native

Usage

1. Import the SDK

import despia from 'despia-native';

2. Retrieve Purchase History

const data = await despia("getpurchasehistory://", ["restoredData"]);
const purchases = data.restoredData;
console.log(purchases);

3. Example Response (iOS)

[
    {
        "transactionId": "1000000987654321",
        "originalTransactionId": "1000000123456789",
        "productId": "com.app.premium.monthly",
        "type": "subscription",
        "entitlementId": "premium",
        "externalUserId": "abc123",
        "isAnonymous": false,
        "isActive": true,
        "willRenew": true,
        "purchaseDate": "2024-01-15T14:32:05Z",
        "originalPurchaseDate": "2023-06-20T09:15:33Z",
        "expirationDate": "2024-02-15T14:32:05Z",
        "store": "app_store",
        "country": "USA",
        "receipt": "MIIbngYJKoZIhvcNAQcCoIIbajCCG2YCAQExDzAN...",
        "environment": "production"
    },
    {
        "transactionId": "1000000555555555",
        "originalTransactionId": "1000000555555555",
        "productId": "com.app.removeads",
        "type": "product",
        "entitlementId": "no_ads",
        "externalUserId": "abc123",
        "isAnonymous": false,
        "isActive": true,
        "willRenew": false,
        "purchaseDate": "2023-12-01T08:00:00Z",
        "originalPurchaseDate": "2023-12-01T08:00:00Z",
        "expirationDate": null,
        "store": "app_store",
        "country": "USA",
        "receipt": "MIIbngYJKoZIhvcNAQcCoIIbajCCG2YCAQExDzAN...",
        "environment": "production"
    }
]

4. Example Response (Android)

[
    {
        "transactionId": "GPA.3372-4150-9088-12345",
        "originalTransactionId": "GPA.3372-4150-9088-12345",
        "productId": "com.app.premium.monthly",
        "type": "subscription",
        "entitlementId": "premium",
        "externalUserId": "abc123",
        "isAnonymous": false,
        "isActive": true,
        "willRenew": true,
        "purchaseDate": "2024-01-15T14:32:05Z",
        "originalPurchaseDate": "2023-06-20T09:15:33Z",
        "expirationDate": "2024-02-15T14:32:05Z",
        "store": "play_store",
        "country": "US",
        "receipt": "kefhajglhaljhfajkfajk.AO-J1OxBnT3hAjkl5FjpKc9...",
        "environment": "production"
    },
    {
        "transactionId": "GPA.3372-4150-9088-67890",
        "originalTransactionId": "GPA.3372-4150-9088-67890",
        "productId": "com.app.removeads",
        "type": "product",
        "entitlementId": "no_ads",
        "externalUserId": "abc123",
        "isAnonymous": false,
        "isActive": true,
        "willRenew": false,
        "purchaseDate": "2023-12-01T08:00:00Z",
        "originalPurchaseDate": "2023-12-01T08:00:00Z",
        "expirationDate": null,
        "store": "play_store",
        "country": "US",
        "receipt": "minodkpfokbofclncmaa.AO-J1Oy2fXpTml7rKxE3vNc9...",
        "environment": "production"
    }
]

5. Check Active Entitlements

const data = await despia("getpurchasehistory://", ["restoredData"]);
const purchases = data.restoredData;

// Filter for active purchases only
const activePurchases = purchases.filter(p => p.isActive);

// Check if user has premium access
const hasPremium = activePurchases.some(p => p.entitlementId === "premium");

if (hasPremium) {
    // Grant premium features
}

Resources

  • NPM Package
  • View full NPM documentation for additional configuration options

Lovable Integration

This SDK is optimized for Lovable’s prompt-based AI builder, enabling quick integration of native purchase restoration into your generated apps. For additional support or questions, please contact our support team at [email protected]