Skip to content

Commit

Permalink
Adds comments to chargeShops and getPlansAtShopCurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
DevAOC committed Jun 26, 2024
1 parent af7c61c commit 6b88458
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { BillingPeriodTrackingGlobalActionContext } from "gadget-server";
import {
ActionOptions,
BillingPeriodTrackingGlobalActionContext,
} from "gadget-server";
import { DateTime } from "luxon";

/**
Expand Down Expand Up @@ -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 })
Expand All @@ -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: {
Expand Down
38 changes: 22 additions & 16 deletions usage-subscription-template/api/actions/chargeShop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
});

Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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 } */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down

0 comments on commit 6b88458

Please sign in to comment.