Start typing to search...

IAP

Manages in-app purchases on the backend of your Koji app.

Constructor

Instantiates the IAP class.

Parameters

Example

const iap = new KojiBackend.IAP({ res });

Methods

.captureTransaction(receiptId)

Captures a pending transaction.

Note
If your IAP product is defined with the captureOnPurchase key set to false, the transaction is held in a pending state until you manually invoke captureTransaction. Funds are not available in the seller’s account until the transaction is captured. If you do not capture the transaction before the captureExpiryPeriod, the transaction is automatically reversed and the buyer is refunded. This period can be specified in the product definition from 0 to 7 days (default is 0).

Parameters

  • receiptId - String, Unique identifier for the transaction receipt.

Returns

Promise<Void>

Example

iap.captureTransaction(receiptId);

Source: backend/iap/index.ts#L276

.loadProduct(sku)

Gets the properties of a specified product, which enables the app to leverage dynamic product information. For example, you can check the stock for a product with limited quantity (via the numAvailable property), and indicate the number of remaining items.

Parameters

  • sku - String, Identifier for the product.

Returns

Promise<IapProduct>, Properties of the specified product.

Example

const product = await iap.loadProduct(sku);

Source: backend/iap/index.ts#L320

.refundTransaction(receiptId)

Refunds a transaction.

Note
Only unsettled transactions can be refunded.

Parameters

  • receiptId - String, Unique identifier for the transaction receipt.

Returns

Promise<Void>

Example

iap.refundTransaction(receiptId);

Source: backend/iap/index.ts#L298

.resolveReceiptById(receiptId)

Gets a specific transaction receipt by its ID, which can be used to facilitate fulfillment. For example, use a dynamic receipt to upload a video response from the seller and then share that response with the buyer. Or, capture product options, such as color or size, to display to the seller in an admin view.

Parameters

  • receiptId - String, Unique identifier for the receipt.

Returns

Promise<IapReceipt>, Object for the specified receipt.

Example

const receipt = await iap.resolveReceiptById(id);

// Use custom attributes for a video response
this.setState({
 instructions: receipt.attributes.message,
 video: receipt.attributes.video,
});

Source: backend/iap/index.ts#L195

.resolveReceiptsByIAPToken(iapToken)

Gets all receipts for the current user, which can be used to validate purchases for specific products.

Parameters

  • iapToken - String, Short-lived IAP token for the current user.

Returns

Promise<IapReceipt>, Array of receipts for the user's purchases.

Example

const receipts = await iap.resolveReceiptsByIAPToken(iapToken);

Source: backend/iap/index.ts#L160

.resolveReceiptsBySku(sku)

Gets all receipts for a specified product, which can be used to aggregate sales data.

Parameters

  • sku - String, Identifier for the product. Products are defined in the entitlements file and registered or updated when the project is deployed.

Returns

Promise<IapReceipt>, Array of receipts for the specified product.

Example

const receipts = await iap.resolveReceiptBySku(sku);

Source: backend/iap/index.ts#L217

.updateReceipt(receiptId, attributes, notificationMessage)

Updates the custom attributes for a specified receipt. For example, if a user purchases a credit toward a product on the Koji app and then uses it, you can update the receipt to indicate that the credit has been consumed and is not available for future sessions.

Parameters

  • receiptId - String, Unique identifier for the receipt.

  • attributes - [index: string]: any, Object of key-value paired attributes to store with the receipt.

  • notificationMessage - String (Optional), Custom message to sent the user when the receipt is updated (up to 80 characters). If undefined, the message will read: `Your receipt for PRODUCT_NAME was updated.`

Returns

Promise<Any>, Confirmation of the update, if the request was successful, or an error message, if not.

Example

iap.updateReceipt(id, { consumed: true }, 'You have successfully redeemed your credit.');

Source: backend/iap/index.ts#L242

Interfaces

BackendConfigurationInput

Configuration information for the Koji app.

Properties

  • projectId - String (Optional), Unique identifier for the Koji app. Will override data passed through `res`.

  • projectToken - String (Optional), Secret key for the Koji app. Will override data passed through `res`.

  • res - Response (Optional), Express response object. Used in conjunction with middleware to scope environment variables for customized versions of the original Koji app. For the original definition see [[https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L127 | @types/express]].

IapProduct

Specific product for purchase. Products are defined in the entitlements file of the app and are registered or updated when the project is deployed. Use [[loadProduct]] to retrieve the properties associated with a product’s SKU.

Properties

  • appId - String, Name of the Koji app from which the product was purchased.

  • captureExpiryPeriod - Number, Period within which a pending transaction must be captured before it is reversed, from 0 to 7 days. Default is 0.

  • captureOnPurchase - Boolean, Whether to capture transactions immediately. If set to false, transactions are held in a pending state until they are manually captured with [[captureTransaction]].

  • dateCreated - Date, Date the product was registered or updated, which happens when the app is deployed.

  • dynamicReceipt - Boolean, Whether the transaction receipt can display the `receipt` context of the app. If enabled, use {@doclink core-frontend-playerstate#context | Koji.playerState.context} to detect the `receipt` context, and use {@doclink core-frontend-playerstate#receiptType | Koji.playerState.receiptType} to determine whether the buyer or the seller is viewing the receipt.

  • fulfillment - 'email' | 'phone' | 'address' (Optional), Type of user information collected for order fulfillment. Defined in the entitlements file.

  • id - String, Unique identifier for this version of the product.

  • isActive - Boolean, Indicator of whether the product is still available for purchase.

  • isConsumable - Boolean, Indicator of whether a product can be purchased more than once. Defined in the entitlements file.

  • name - String, Description displayed when the user was prompted to purchase the product. Defined in the entitlements file.

  • numAvailable - Number (Optional), Remaining number of times the product can be sold. Calculated based on the total inventory defined in the entitlements file, less the number of purchases.

  • owner - UserArtifact (Optional), Object that represents the seller.

  • ownerUserId - String, Koji username of the seller.

  • price - Number, Purchase price of the product. Defined in the entitlements file.

  • priceIsUnset - Boolean, Indicator of whether a purchase price is defined for the product. Defined in the entitlements file.

  • purchases - IapReceipt[] (Optional), Array of [[IapReceipt]] objects representing purchases of the product.

  • quantity - Number (Optional), Total number of times the product can be sold (inventory threshold). Defined in the entitlements file.

  • sku - String, Identifier of the purchased product. Defined in the entitlements file.

IapReceipt

Receipt for a user’s purchase of a product. Resolve receipts with [[resolveReceiptById]], [[resolveReceiptsByIAPToken]], or [[resolveReceiptsBySku]]. Add custom attributes for to a receipt with [[updateReceipt]].

Properties

  • attributes - [index: string]: any, Object containing a list of custom key-value pairs associated with the receipt. You can use [[updateReceipt]] to update these values. Additionally, the fulfillment information (email, phone, or address) and customMessage are included this object, if set at purchase time.

  • datePurchased - Date, Date of the purchase.

  • dateRefunded - Date (Optional), Date of the refund, if the transaction has been refunded. Refunds can occur either manually or due to capture expiry of a pending transaction.

  • productId - String, Unique identifier for the product.

  • purchasedPrice - Number, Price the user paid for the product.

  • receiptId - String, Unique identifier for the receipt.

  • transactionIds - , Object containing references to the associated transaction receipts – `credit` for the user receiving the funds (seller), `debit` for the user sending the funds (buyer). To link to the transaction receipt in the user’s Koji wallet, use the format `https://withkoji.com/payments/transactions/TXN_ID`.

UserArtifact

Information about a Koji user.

Properties

  • displayName - String, The user's display name (if access was granted).

  • href - String, The user's profile URL.

  • id - String, Unique identifier for the user.

  • isVerified - Boolean, Whether the user is verified.

  • profilePicture - String, The user's profile picture (if access was granted).

  • username - String, The user's username (if access was granted).

IapReceipt

Receipt for a user’s purchase of a product. Resolve receipts with [[resolveReceiptById]], [[resolveReceiptsByIAPToken]], or [[resolveReceiptsBySku]]. Add custom attributes for to a receipt with [[updateReceipt]].

Properties

  • attributes - [index: string]: any, Object containing a list of custom key-value pairs associated with the receipt. You can use [[updateReceipt]] to update these values. Additionally, the fulfillment information (email, phone, or address) and customMessage are included this object, if set at purchase time.

  • datePurchased - Date, Date of the purchase.

  • dateRefunded - Date (Optional), Date of the refund, if the transaction has been refunded. Refunds can occur either manually or due to capture expiry of a pending transaction.

  • productId - String, Unique identifier for the product.

  • purchasedPrice - Number, Price the user paid for the product.

  • receiptId - String, Unique identifier for the receipt.

  • transactionIds - , Object containing references to the associated transaction receipts – `credit` for the user receiving the funds (seller), `debit` for the user sending the funds (buyer). To link to the transaction receipt in the user’s Koji wallet, use the format `https://withkoji.com/payments/transactions/TXN_ID`.

UserArtifact

Information about a Koji user.

Properties

  • displayName - String, The user's display name (if access was granted).

  • href - String, The user's profile URL.

  • id - String, Unique identifier for the user.

  • isVerified - Boolean, Whether the user is verified.

  • profilePicture - String, The user's profile picture (if access was granted).

  • username - String, The user's username (if access was granted).