Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.8 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  httpClient,
  HttpMethod,
  AuthenticationType,
} from '@activepieces/pieces-common';
import { stripeAuth } from '../..';
import { stripeCommon } from '../common';

import { priceOutputSchema } from '../output-schemas';
export const stripeCreatePrice = createAction({
  name: 'create_price',
  classification: 'WRITE',
  auth: stripeAuth,
  displayName: 'Create Price',
  description: 'Create a one-time or recurring price for a product.',
  audience: 'human',
  aiMetadata: {
    description:
      'Creates a price for an existing Stripe product, in either one-time or recurring mode depending on the selected billing interval. Use after creating a product to define what to charge before building a subscription or payment link. Requires the product ID; not idempotent, as each call creates a new price.',
    idempotent: false,
  },
  propertyGroups: [
    {
      key: 'price',
      display: 'section',
      label: 'Price',
      icon: 'tag',
      props: ['product', 'unit_amount', 'currency'],
    },
    {
      key: 'billing',
      display: 'section',
      label: 'Billing',
      icon: 'calendar',
      props: ['recurring_interval'],
    },
  ],
  props: {
    product: stripeCommon.product, 
    unit_amount: Property.Number({
      displayName: 'Amount',
      description: "In the currency's main unit, e.g. 10.50 for $10.50.",
      required: true,
    }),
    currency: Property.StaticDropdown({
      displayName: 'Currency',
      required: true,
      options: {
        options: [
          { label: 'US Dollar', value: 'usd' },
          { label: 'Euro', value: 'eur' },
          { label: 'Pound Sterling', value: 'gbp' },
          { label: 'Australian Dollar', value: 'aud' },
          { label: 'Canadian Dollar', value: 'cad' },
          { label: 'Swiss Franc', value: 'chf' },
          { label: 'Chinese Yuan', value: 'cny' },
          { label: 'Japanese Yen', value: 'jpy' },
          { label: 'Indian Rupee', value: 'inr' },
          { label: 'Singapore Dollar', value: 'sgd' },
        ],
      },
    }),
    recurring_interval: Property.StaticDropdown({
      displayName: 'Billing Interval',
      description: 'Pick One-Time for a single payment.',
      required: true,
      defaultValue: 'one_time',
      options: {
        options: [
          { label: 'One-Time', value: 'one_time' },
          { label: 'Daily', value: 'day' },
          { label: 'Weekly', value: 'week' },
          { label: 'Monthly', value: 'month' },
          { label: 'Yearly', value: 'year' },
        ],
      },
    }),
    recurring_interval_count: Property.Number({
      displayName: 'Interval Count',
      description:
        'Bill every N intervals, e.g. 3 with Monthly bills quarterly.',
      required: false,
      advanced: true,
    }),
  },
  outputSchema: priceOutputSchema,
  async run(context) {
    const {
      product,
      unit_amount,
      currency,
      recurring_interval,
      recurring_interval_count,
    } = context.propsValue;

    const unitAmountInCents = Math.round(unit_amount * 100);

    const body: { [key: string]: unknown } = {
      product: product,
      unit_amount: unitAmountInCents,
      currency: currency,
    };

    if (recurring_interval !== 'one_time') {
      body['recurring[interval]'] = recurring_interval;
      if (recurring_interval_count) {
        body['recurring[interval_count]'] = recurring_interval_count;
      }
    }

    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: `${stripeCommon.baseUrl}/prices`,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: context.auth.secret_text,
      },
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: body,
    });

    return response.body;
  },
});