diff --git a/usage-subscription-template/api/actions/billingPeriodTracking.js b/usage-subscription-template/api/actions/billingPeriodTracking.js index 5761c21b..a0aba646 100644 --- a/usage-subscription-template/api/actions/billingPeriodTracking.js +++ b/usage-subscription-template/api/actions/billingPeriodTracking.js @@ -1,4 +1,7 @@ -import { BillingPeriodTrackingGlobalActionContext } from "gadget-server"; +import { + ActionOptions, + BillingPeriodTrackingGlobalActionContext, +} from "gadget-server"; import { DateTime } from "luxon"; /** @@ -71,6 +74,7 @@ export async function run({ params, logger, api, connections }) { } ); + // Updating billing period information await api.internal.shopifyShop.update(shop.id, { billingPeriodStart: DateTime.fromJSDate(new Date(shop.billingPeriodEnd)) .plus({ milliseconds: 1 }) @@ -82,7 +86,7 @@ export async function run({ params, logger, api, connections }) { } } -// Action timeout set to 5 minutes (300,000 milliseconds) +/** @type { ActionOptions } */ export const options = { timeoutMS: 900000, triggers: { diff --git a/usage-subscription-template/api/actions/chargeShop.js b/usage-subscription-template/api/actions/chargeShop.js index a772246e..a13369f0 100644 --- a/usage-subscription-template/api/actions/chargeShop.js +++ b/usage-subscription-template/api/actions/chargeShop.js @@ -7,23 +7,25 @@ import { getCappedAmount } from "../../utilities"; export async function run({ params, logger, api, connections }) { const { shop, order } = params; - const { amountUsedInPeriod } = await api.shopifyShop.findOne(shop.id, { - amountUsedInPeriod: true, - }); - - let remainder = 0; - + // Creating an instance of the Shopify Admin API const shopify = await connections.shopify.forShopId(shop.id); // Returning early if the shop is uninstalled and no Shopify instance is created if (!shopify) return logger.warn({ - message: - "Shop uninstalled - Cannot charge shop because the shop is uninstalled", + message: "BILLING - Shop uninstalled", shopId: shop.id, }); - const activeSubscription = await api.shopifyAppSubscription.maybeFindOne( + // Fetching the amount used in the period + const { amountUsedInPeriod } = await api.shopifyShop.findOne(shop.id, { + amountUsedInPeriod: true, + }); + + let remainder = 0; + + // Fetching the subscription at the time + const subscription = await api.shopifyAppSubscription.maybeFindOne( shop?.activeSubscriptionId, { select: { @@ -32,19 +34,18 @@ export async function run({ params, logger, api, connections }) { } ); - if (!activeSubscription) + if (!subscription) return logger.warn({ - message: - "NO ACTIVE SUBSCRIPTION - Cannot charge overages because the shop has no active subscription", + message: "BILLING - No subscription found for the shop", shopId: shop.id, }); // Pulling out the capped amount from the active subscription - const cappedAmount = getCappedAmount(activeSubscription); + const cappedAmount = getCappedAmount(subscription); if (!cappedAmount) return logger.warn({ - message: "NO CAPPED AMOUNT - Active subscription missing a capped amount", + message: "BILLING - No capped amount found for the shop", shopId: shop.id, }); @@ -59,13 +60,16 @@ export async function run({ params, logger, api, connections }) { }); } + // Calculating the available amount const availableAmount = cappedAmount - amountUsedInPeriod; + // Setting a remainder if the price is greater than the available amount if (price >= availableAmount) { remainder = price - availableAmount; price = availableAmount; } + // Creating the usage charge with the Shopify Billing API const result = await shopify.graphql( `mutation ($description: String!, $price: MoneyInput!, $subscriptionLineItemId: ID!) { appUsageRecordCreate(description: $description, price: $price, subscriptionLineItemId: $subscriptionLineItemId) { @@ -92,9 +96,11 @@ export async function run({ params, logger, api, connections }) { } ); + // Throwing an error if the charge fails if (result?.appUsageRecordCreate?.userErrors?.length) throw new Error(result.appUsageRecordCreate.userErrors[0].message); + // Creating the usage charge record in the database await api.internal.usageRecord.create({ id: result.appUsageRecordCreate.appUsageRecord.id.split("/")[4], price, @@ -104,11 +110,11 @@ export async function run({ params, logger, api, connections }) { }, }); - if (remainder) { + // Updating the overage amount if there is a remainder + if (remainder) await api.internal.shopifyShop.update(shop.id, { overage: remainder, }); - } } /** @type { ActionOptions } */ diff --git a/usage-subscription-template/api/actions/getPlansAtShopCurrency.js b/usage-subscription-template/api/actions/getPlansAtShopCurrency.js index 29b13750..9abba6d6 100755 --- a/usage-subscription-template/api/actions/getPlansAtShopCurrency.js +++ b/usage-subscription-template/api/actions/getPlansAtShopCurrency.js @@ -10,6 +10,7 @@ import { convertCurrency } from "../../utilities"; export async function run({ params, logger, api, connections }) { const response = []; + // Getting the current shop's currency const shop = await api.shopifyShop.maybeFindOne( connections.shopify.currentShopId, {