Integrations

Action endpoints

Action functions are responsible for performing specific operations on subscriptions, such as applying discounts, canceling subscriptions, or changing prices. These functions will interact with your payment provider's API to perform the requested actions and return the updated data in the format expected by Churnkey.

Common Patterns

For each action, you'll implement an asynchronous function that:

  1. Accepts relevant parameters
  2. Performs the action using your payment provider's API
  3. Returns the updated subscription data
  4. Handles errors appropriately

Cancel Subscription

The Cancel action cancels a subscription or multiple subscriptions.

Options:

OptionTypeDescription
customerIdstringThe ID of the customer
subscriptionIdsarray(Optional) Array of subscription IDs to cancel
scheduledAtstringWhen to cancel the subscription ('immediate' or 'end-of-period')
unpausebooleanWhether to unpause the subscription on cancel
function cancelSubscription(options) {
  if (options.subscriptionIds) {
    return cancelMany(options.subscriptionIds, options);
  } else {
    return cancelAll(options.customerId, options);
  }
}

async function cancelOne(subscription, options) {
  switch (options.scheduledAt) {
    case 'immediate':
      // Implement immediate cancellation
      break;
    case 'end-of-period':
      // Implement end-of-period cancellation
      break;
  }
}

Apply Coupon

The Apply Coupon action applies a coupon to a subscription or multiple subscriptions.

Options:

OptionTypeDescription
customerIdstringThe ID of the customer
subscriptionIdsarray(Optional) Array of subscription IDs to apply the coupon to
idstringID of the coupon in the provider system
function applyCoupon(options) {
  if (options.subscriptionIds) {
    return applyMany(options.subscriptionIds, options);
  } else {
    return applyAll(options.customerId, options);
  }
}

async function applyOne(subscription, options) {
  // Implement coupon application logic
}

Extend Trial

The Extend Trial action extends the trial period of a subscription or multiple subscriptions.

Options:

OptionTypeDescription
customerIdstringThe ID of the customer
subscriptionIdsarray(Optional) Array of subscription IDs to extend trial
durationobjectThe duration of the trial extension (e.g., { type: 'days', value: 7 })
function extendTrial(options) {
  if (options.subscriptionIds) {
    return extendMany(options.subscriptionIds, options);
  } else {
    return extendAll(options.customerId, options);
  }
}

async function extendOne(subscription, options) {
  // Implement trial extension logic based on options.duration
}

Pause Subscription

The Pause action pauses a subscription or multiple subscriptions.

Options:

OptionTypeDescription
customerIdstringThe ID of the customer
subscriptionIdsarray(Optional) Array of subscription IDs to pause
durationobjectThe duration of the pause
startDatestringWhen to start the pause ('immediate' or 'period-end')
allowAnnualbooleanWhether to allow pausing annual subscriptions
function pauseSubscription(options) {
  if (options.subscriptionIds) {
    return pauseMany(options.subscriptionIds, options);
  } else {
    return pauseAll(options.customerId, options);
  }
}

async function pauseOne(subscription, options) {
  switch (options.startDate) {
    case 'immediate':
      // Implement immediate pause
      break;
    case 'period-end':
      // Implement end-of-period pause
      break;
  }
  // Handle options.duration and options.allowAnnual
}

Change Price

The Change Price action changes the price of a subscription or multiple subscriptions.

Options:

OptionTypeDescription
customerIdstringThe ID of the customer
subscriptionIdsarray(Optional) Array of subscription IDs to change price
newPriceIdstringID of the new price to apply
proratebooleanWhether to prorate the price change (if supported)
function changePrice(options) {
  if (options.subscriptionIds) {
    return changePriceMany(options.subscriptionIds, options);
  } else {
    return changePriceAll(options.customerId, options);
  }
}

async function changePriceOne(subscription, options) {
  // Implement price change logic
  // Handle options.prorate if supported by your provider
}

Note: The actual options for ChangePrice may vary based on the implementation. This is a general example.

Implementing Actions

When implementing these actions for your payment provider:

  1. Create functions for each action (e.g., cancelSubscription, applyCoupon, extendTrial, pauseSubscription, changePrice).
  2. Implement the action-specific logic within the One function (e.g., cancelOne, applyOne, extendOne, pauseOne, changePriceOne).
  3. Handle errors appropriately and return updated subscription data if required.

Example implementation for the Cancel action using a hypothetical payment provider:

async function cancelOne(subscription, options) {
  try {
    const result = await paymentProviderAPI.cancelSubscription(subscription.id, {
      cancelAtPeriodEnd: options.scheduledAt === 'end-of-period',
      unpause: options.unpause,
    });
    return result;
  } catch (error) {
    console.error(`Failed to cancel subscription ${subscription.id}:`, error);
    throw error;
  }
}

Remember to implement proper error handling, logging, and adapt the code to work with your specific payment provider's API.

In the Integration API Setup section, we'll cover how to bring all these components together into a cohesive integration setup.