Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #979 from Shopify/allow_billing_request_trial_days…
Browse files Browse the repository at this point in the history
…_override

Allow billing request trialDays override
  • Loading branch information
paulomarg authored Sep 5, 2023
2 parents 47d1469 + 05c395a commit 9b6c0ec
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-foxes-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-api': minor
---

Add new trialDays parameter to billing.request, so that apps can override the default config value.
6 changes: 6 additions & 0 deletions docs/reference/billing/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ Which URL to redirect the merchant to after the charge is confirmed.

Whether to return the `confirmationUrl` as a `string`, or to return a more detailed object (see below).

### trialDays

`number`

Override value for the `trialDays` config option. Only applies to recurring purchases.

## Return

### if `returnObject` parameter is `false` (default)
Expand Down
68 changes: 68 additions & 0 deletions lib/billing/__tests__/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,72 @@ describe('shopify.billing.request', () => {
});
});
});

it('applies trialDays override when set', async () => {
shopify = shopifyApi({
...testConfig,
billing: {
[Responses.PLAN_1]: {
amount: 5,
currencyCode: 'USD',
interval: BillingInterval.Every30Days,
replacementBehavior: BillingReplacementBehavior.ApplyImmediately,
trialDays: 10,
},
},
});

queueMockResponses([Responses.PURCHASE_SUBSCRIPTION_RESPONSE]);

await shopify.billing.request({
session,
plan: Responses.PLAN_1,
returnObject: true,
trialDays: 20,
});

expect({
...GRAPHQL_BASE_REQUEST,
data: {
query: expect.stringContaining('appSubscriptionCreate'),
variables: expect.objectContaining({
trialDays: 20,
}),
},
}).toMatchMadeHttpRequest();
});

it('applies a trialDays override of 0', async () => {
shopify = shopifyApi({
...testConfig,
billing: {
[Responses.PLAN_1]: {
amount: 5,
currencyCode: 'USD',
interval: BillingInterval.Every30Days,
replacementBehavior: BillingReplacementBehavior.ApplyImmediately,
trialDays: 10,
},
},
});

queueMockResponses([Responses.PURCHASE_SUBSCRIPTION_RESPONSE]);

await shopify.billing.request({
session,
plan: Responses.PLAN_1,
returnObject: true,
trialDays: 0,
});

expect({
...GRAPHQL_BASE_REQUEST,
data: {
query: expect.stringContaining('appSubscriptionCreate'),
variables: expect.objectContaining({
trialDays: 0,
}),
},
}).toMatchMadeHttpRequest();
});
});
15 changes: 13 additions & 2 deletions lib/billing/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface RequestInternalParams {

interface RequestSubscriptionInternalParams extends RequestInternalParams {
billingConfig: BillingConfigSubscriptionPlan;
trialDays?: number;
}

interface RequestOneTimePaymentInternalParams extends RequestInternalParams {
Expand All @@ -37,6 +38,7 @@ interface RequestOneTimePaymentInternalParams extends RequestInternalParams {

interface RequestUsageSubscriptionInternalParams extends RequestInternalParams {
billingConfig: BillingConfigUsagePlan;
trialDays?: number;
}

export function request(config: ConfigInterface) {
Expand All @@ -46,6 +48,7 @@ export function request(config: ConfigInterface) {
isTest = true,
returnUrl: returnUrlParam,
returnObject = false,
trialDays = undefined,
}: Params): Promise<BillingRequestResponse<Params>> {
if (!config.billing || !config.billing[plan]) {
throw new BillingError({
Expand Down Expand Up @@ -90,6 +93,7 @@ export function request(config: ConfigInterface) {
client,
returnUrl,
isTest,
trialDays,
});
data = mutationUsageResponse.data.appSubscriptionCreate;
break;
Expand All @@ -101,6 +105,7 @@ export function request(config: ConfigInterface) {
client,
returnUrl,
isTest,
trialDays,
});
data = mutationRecurringResponse.data.appSubscriptionCreate;
}
Expand Down Expand Up @@ -129,13 +134,16 @@ async function requestRecurringPayment({
client,
returnUrl,
isTest,
trialDays,
}: RequestSubscriptionInternalParams): Promise<RecurringPaymentResponse> {
const trialDaysValue =
trialDays === undefined ? billingConfig.trialDays : trialDays;
const mutationResponse = await client.query<RecurringPaymentResponse>({
data: {
query: RECURRING_PURCHASE_MUTATION,
variables: {
name: plan,
trialDays: billingConfig.trialDays,
trialDays: trialDaysValue,
replacementBehavior: billingConfig.replacementBehavior,
returnUrl,
test: isTest,
Expand Down Expand Up @@ -180,15 +188,18 @@ async function requestUsagePayment({
client,
returnUrl,
isTest,
trialDays,
}: RequestUsageSubscriptionInternalParams): Promise<RecurringPaymentResponse> {
const trialDaysValue =
trialDays === undefined ? billingConfig.trialDays : trialDays;
const mutationResponse = await client.query<RecurringPaymentResponse>({
data: {
query: RECURRING_PURCHASE_MUTATION,
variables: {
name: plan,
returnUrl,
test: isTest,
trialDays: billingConfig.trialDays,
trialDays: trialDaysValue,
replacementBehavior: billingConfig.replacementBehavior,
lineItems: [
{
Expand Down
1 change: 1 addition & 0 deletions lib/billing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface BillingRequestParams {
isTest?: boolean;
returnUrl?: string;
returnObject?: boolean;
trialDays?: number;
}

export interface BillingRequestResponseObject {
Expand Down

0 comments on commit 9b6c0ec

Please sign in to comment.