Shopify / shopify-app-template-remix

375 stars 151 forks source link

How to dynamically charge discounted/different trial days plans for specific merchants using the `billing` method exposed in the template? #817

Closed muchisx closed 2 months ago

muchisx commented 3 months ago

Apologies for not template, this is just a question (or a feature request?)

Currently the billing helper of the remix template allows to create preset plans as such

  billing: {
    [MY_PLAN]: {
      lineItems: [
        {
          amount: 19.95,
          currencyCode: 'USD',
          interval: BillingInterval.Every30Days,
        },
      ],
    },
...

We want to offer dynamic discounts and trial days to different merchants depending on some variables, some coming from our own business logic, so we can't just hardcode the discounts and trial days onto these preset plans.

How exactly should we go about it when using billing.request and requesting one of the preset plans but additionally add a custom discount/trial days to that preset plan?

paulomarg commented 3 months ago

Hey, thank you for raising this. We set the request method up so that you can pass in overrides, like so:

  await billing.request({
    plan: "Gold plan",
    amount: 10,
    currencyCode: "USD",
    interval: BillingInterval.Every30Days,
  });

Those values will overwrite the plan values so you can set specific values for each shop.

However, I'm noticing that we currently don't have an override for either trial days, or discounts. We'll track this issue and add those overrides as well!

TheGreatSimo commented 3 months ago

True the billing should include free trial days, discounts, and a way to cancel a one-time payment within a certain time frame

paulomarg commented 3 months ago

Turns out the support was already there, we just didn't expose the types properly. With the changes in the linked PR I used this config:

  billing: {
    basic: {
      trialDays: 10,
      lineItems: [
        {
          amount: 10,
          currencyCode: "USD",
          interval: BillingInterval.Every30Days,
          discount: {
            value: {
              amount: 1,
            },
            durationLimitInIntervals: 1,
          },
        },
      ],
    },
  },

and called this - note that the interval needs to match for line item overrides to take:

  await billing.request({
    plan: "basic",
    isTest: true,
    trialDays: 14,
    lineItems: [
      {
        amount: 1,
        currencyCode: "CAD",
        interval: BillingInterval.Every30Days,
        discount: {
          value: { percentage: 0.5 },
          durationLimitInIntervals: 3,
        },
      },
    ],
  });

which led to:

Image

muchisx commented 3 months ago

Amazing, thank you @paulomarg ! Are these docs open source? https://shopify.dev/docs/api/shopify-app-remix/v3/apis/billing I can help with a PR updating it aswell!

paulomarg commented 3 months ago

Good callout, we should have an example for overrides in the docs, but don't sweat it, I can easily add one as part of that PR :)

For context, most of the docs are pulled directly from the TSDoc for the types, so we can provide the same experience in the IDE and the docs. That one in particular comes from the request property here.