oem-inapppurchases
v1.0.11
Published
Node.js and TypeScript helpers for verifying Google Play subscriptions and Apple App Store receipts.
Maintainers
Readme
oem-inapppurchases
Node.js and TypeScript package for server-side verification of:
- Google Play subscriptions
- Apple App Store receipts
The package exports:
PaymentGatewayFactoryto select a store implementationInAppPurchasesto run a shared verification flow
Installation
npm install oem-inapppurchasesExports
import { InAppPurchases, PaymentGatewayFactory } from "oem-inapppurchases";Gateway Names
Use these values with getPaymentGatewayObject(...):
playstoreappstore
Payloads
Play Store
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| email | string | Yes | Google service account email |
| privateKey | string | Yes | Google service account private key |
| packageName | string | Yes | Android package name |
| productId | string | Yes | Subscription product ID |
| purchaseToken | string | Yes | Purchase token returned by the client app |
App Store
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| secretKey | string | Yes | App Store shared secret |
| sandBox | number | Optional | Backward-compatible alias for internal Appstore API selection (0 or 9) |
| dumpData | string | Yes | Base64-encoded receipt data |
Basic Usage
import { InAppPurchases, PaymentGatewayFactory } from "oem-inapppurchases";
const factory = new PaymentGatewayFactory();
const gateway = factory.getPaymentGatewayObject("playstore");
const iap = new InAppPurchases({
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL!,
privateKey: process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY!.replace(/\\n/g, "\n"),
packageName: "com.example.app",
productId: "premium_monthly",
purchaseToken: "purchase-token-from-device"
});
const result = await iap.verifyPayment(gateway);
console.log(result);Store-Specific Notes
Google Play
verifyPayment()uses the Google Play Android Publisher subscriptions API and acknowledges the subscription when it is still pending acknowledgement.verifyPaymentWithData()usessubscriptionsv2.get()and returns a normalized payload throughInAppPurchases.verifyPaymentWithData(...).
Example:
const gateway = new PaymentGatewayFactory().getPaymentGatewayObject("playstore");
const iap = new InAppPurchases({
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL!,
privateKey: process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY!.replace(/\\n/g, "\n"),
packageName: "com.example.app",
productId: "premium_monthly",
purchaseToken: "purchase-token-from-device"
});
const result = await iap.verifyPaymentWithData(gateway);Typical normalized Play Store shape:
{
status: true,
data: {
packageName: "com.example.app",
token: "purchase-token-from-device",
subscriptionState: "...",
acknowledgementState: "...",
startTime: "...",
latestOrderId: "...",
regionCode: "...",
raw: { ... }
}
}App Store
Example:
const gateway = new PaymentGatewayFactory().getPaymentGatewayObject("appstore");
const iap = new InAppPurchases({
secretKey: process.env.APPLE_SHARED_SECRET!,
sandBox: 0,
dumpData: "base64-receipt-data"
});
const result = await iap.verifyPayment(gateway);Notes:
verifyPayment()andverifyPaymentWithData()both wrap the AppleverifyReceiptresponse as{ status: true, data: ... }.- The current implementation retries transient App Store statuses
21005and21009. verifyPaymentWithData()also contains a legacy fallback path for21007whensandBoxis set to9.
API
new PaymentGatewayFactory().getPaymentGatewayObject(name)
Returns the store verifier instance for:
playstoreappstore
Throws Payment Gateway Not Found for unsupported values.
new InAppPurchases(payload).verifyPayment(gateway)
Runs verification and returns:
{
status: true,
data: any
}new InAppPurchases(payload).verifyPaymentWithData(gateway)
Runs verification and returns:
{
status: boolean,
data: any
}For Play Store, this is the normalized subscription payload shown above. For App Store, this is the receipt verification response body.
Errors
Common thrown errors include:
Payment Gateway Not Found- validation errors for missing required fields
Unable to verify paymentFailed to verify purchase:<statusText>
Development
npm run build
npm run devLicense
ISC
