Skip to content

Commit

Permalink
Add trial_ends_at for updateSubscription and skip_trial for `ch…
Browse files Browse the repository at this point in the history
…eckoutOptions` in the `createCheckout` function (#75)

* feat(subscriptions): add trial_ends_at for the updateSubscription function

* feat: add skip_trial for createCheckout function

* docs: update changeset

* test(checkouts): remove skip
  • Loading branch information
keyding authored Apr 19, 2024
1 parent 09fdd1a commit b4d071a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/swift-crabs-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lemonsqueezy/lemonsqueezy.js": patch
---

Add `trial_ends_at` for `updateSubscription` function;
Add `skip_trial` for `checkoutOptions` in the `createCheckout` function;
8 changes: 8 additions & 0 deletions src/checkouts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type CheckoutOptions = {
* If `false`, hide the discount code field
*/
discount: boolean;
/**
* If `true`, remove the free trial
*/
skip_trial: boolean;
quantity: number;
/**
* If `true`, use the dark theme
Expand Down Expand Up @@ -413,6 +417,10 @@ export type NewCheckout = {
* If `false`, hide the discount code field
*/
discount?: boolean;
/**
* If `true`, remove the free trial
*/
skipTrial?: boolean;
/**
* If `true`, use the dark theme
*/
Expand Down
2 changes: 2 additions & 0 deletions src/subscriptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function updateSubscription(
invoiceImmediately,
disableProrations,
pause,
trialEndsAt,
} = updateSubscription;

const attributes = convertKeys({
Expand All @@ -59,6 +60,7 @@ export function updateSubscription(
invoiceImmediately,
disableProrations,
pause,
trialEndsAt,
});

return $fetch<Subscription>({
Expand Down
4 changes: 4 additions & 0 deletions src/subscriptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ export type UpdateSubscription = Partial<{
* Set as `true` to cancel the subscription. You can resume a subscription (before the `ends_at` date) by setting this to `false`.
*/
cancelled: boolean;
/**
* An [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formatted date-time string indicating when the subscription's free trial should end.
*/
trialEndsAt: string | null;
/**
* - Use an integer representing a day of the month (`21` equals `21st day of the month`) to change the day on which subscription invoice payments are collected.
* - Use `null` or `0` to reset the billing anchor to the current date. Doing this will also remove an active trial.
Expand Down
5 changes: 5 additions & 0 deletions test/checkouts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe("Create a checkout", () => {
logo,
desc,
discount,
skip_trial,
quantity,
dark,
subscription_preview,
Expand All @@ -138,6 +139,7 @@ describe("Create a checkout", () => {
logo,
desc,
discount,
skip_trial,
quantity,
dark,
subscription_preview,
Expand Down Expand Up @@ -242,6 +244,7 @@ describe("Create a checkout", () => {
logo: true,
desc: true,
dark: true,
skipTrial: true,
discount: false,
buttonColor: "#ccc",
subscriptionPreview: true,
Expand Down Expand Up @@ -367,6 +370,7 @@ describe("Create a checkout", () => {
logo,
desc,
discount,
skip_trial,
quantity,
dark,
subscription_preview,
Expand All @@ -378,6 +382,7 @@ describe("Create a checkout", () => {
logo,
desc,
discount,
skip_trial,
quantity,
dark,
subscription_preview,
Expand Down
39 changes: 39 additions & 0 deletions test/subscriptions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("List all subscriptions", () => {
expect(_data).toBeDefined();

const { meta, links, data } = _data!;

expect(meta.page).toBeDefined();
expect(links.first).toBeString();
expect(links.last).toBeString();
Expand Down Expand Up @@ -609,6 +610,44 @@ describe("Update a subscription", () => {
expect(cancelled).toBeFalse();
});

it("The subscription should be changed to trial_ends_at", async () => {
function formatISO8601(isoString: string) {
const date = new Date(isoString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
const trialEndsAt = new Date("2024-04-25").toISOString();
const {
statusCode,
error,
data: _data,
} = await updateSubscription(subscriptionId, {
trialEndsAt,
});

expect(statusCode).toEqual(200);
expect(error).toBeNull();
expect(_data).toBeDefined();

const { data, links } = _data!;
expect(data).toBeDefined();
expect(links.self).toEqual(`${API_BASE_URL}${PATH}${subscriptionId}`);

const { id, type, attributes } = data;
expect(id).toEqual(subscriptionId.toString());
expect(type).toBe(DATA_TYPE);
expect(attributes).toBeDefined();

const { trial_ends_at } = attributes;
expect(formatISO8601(trial_ends_at!)).toEqual(formatISO8601(trialEndsAt));
});

it("The payment should be changed to pause", async () => {
const mode = "void";
const billingAnchor = 25;
Expand Down

0 comments on commit b4d071a

Please sign in to comment.