npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

modern-in-app-purchase

v1.0.2

Published

Modern in-app purchase validation for Apple and Google using official libraries

Readme

modern-in-app-purchase

A modern, robust, and type-safe in-app purchase validation library for Apple and Google using their official SDKs (@apple/app-store-server-library and googleapis).

Installation

npm install modern-in-app-purchase

Usage with NestJS

This library is designed to be easily integrated into a NestJS application. Below is an example of how to wrap it in a NestJS Service and Module.

1. Create a Configuration Service or use ConfigModule

Ensure you have your Apple and Google credentials available in your environment variables.

2. Create the IAP Service

Create a service to wrap the library's functions.

// in-app-purchase.service.ts
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import iap, { Platform, Receipt, ValidationResponse } from 'modern-in-app-purchase';

@Injectable()
export class InAppPurchaseService implements OnModuleInit {
  constructor(private readonly configService: ConfigService) {}

  onModuleInit() {
    // Configure the library once when the module initializes
    iap.config({
      apple: {
        issuerId: this.configService.get<string>('APPLE_ISSUER_ID'),
        keyId: this.configService.get<string>('APPLE_KEY_ID'),
        privateKey: this.configService.get<string>('APPLE_PRIVATE_KEY'),
        bundleId: this.configService.get<string>('APPLE_BUNDLE_ID'),
        environment: this.configService.get<'Sandbox' | 'Production'>('APPLE_ENVIRONMENT', 'Sandbox'),
      },
      google: {
        clientEmail: this.configService.get<string>('GOOGLE_CLIENT_EMAIL'),
        privateKey: this.configService.get<string>('GOOGLE_PRIVATE_KEY').replace(/\\n/g, '\n'),
      },
    });
  }

  /**
   * Validate a receipt for a given platform
   * @param platform 'apple' or 'google'
   * @param receipt The receipt data containing transactionId or purchaseToken
   */
  async validateReceipt(platform: Platform, receipt: Receipt): Promise<ValidationResponse> {
    return await iap.validate(platform, receipt);
  }

  /**
   * Check if a validation response indicates a valid subscription/purchase
   */
  isValid(response: ValidationResponse): boolean {
    return iap.isValid(response);
  }
}

3. Create the IAP Module

// in-app-purchase.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { InAppPurchaseService } from './in-app-purchase.service';

@Module({
  imports: [ConfigModule],
  providers: [InAppPurchaseService],
  exports: [InAppPurchaseService],
})
export class InAppPurchaseModule {}

4. Use in your Resolver or Controller

// subscription.resolver.ts
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { InAppPurchaseService } from './in-app-purchase.service';
import { Platform } from 'modern-in-app-purchase';

@Resolver()
export class SubscriptionResolver {
  constructor(private readonly iapService: InAppPurchaseService) {}

  @Mutation(() => Boolean)
  async verifySubscription(
    @Args('platform') platform: Platform, // 'apple' or 'google'
    @Args('transactionId', { nullable: true }) transactionId?: string, // for Apple
    @Args('productId', { nullable: true }) productId?: string, // for Google
    @Args('purchaseToken', { nullable: true }) purchaseToken?: string, // for Google
  ) {
    // Validate the receipt
    const response = await this.iapService.validateReceipt(platform, {
      transactionId,
      productId,
      purchaseToken,
    });

    if (this.iapService.isValid(response)) {
      // The receipt is valid. Update the user's subscription status in your database.
      console.log('Subscription is valid!', response);
      return true;
    } else {
      // The receipt is invalid or expired.
      console.log('Subscription validation failed:', response.message);
      return false;
    }
  }
}

Types

This library provides full TypeScript support. You can import the types directly:

import { 
  ConfigOptions, 
  Platform, 
  Receipt, 
  ValidationResponse,
  APPLE, 
  GOOGLE 
} from 'modern-in-app-purchase';