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.5 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  Property,
} from '@activepieces/pieces-framework';
import { stripeCommon } from '../common';
import { StripeWebhookInformation } from '../common/types';
import { stripeAuth } from '../..';
import { isEmpty } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
type StripeWebhookPayload = {
  data: {
    object: {
      [key: string]: unknown;
    };
  };
};

import { checkoutSessionOutputSchema } from '../output-schemas';
export const stripeCheckoutSessionCompleted = createTrigger({
  auth: stripeAuth,
  name: 'checkout_session_completed',
  classification: 'READ',
  displayName: 'Checkout Session Completed',
  description: 'Fires when a customer completes a Checkout session.',
  aiMetadata: {
    description:
      'Fires when a Stripe Checkout Session is successfully completed (the checkout.session.completed event), emitting the completed session including customer and payment details. An optional customer ID filter narrows firing to one customer. Use to react to a completed hosted checkout, such as fulfilling an order or granting access.',
  },
  props: {
    customer: Property.ShortText({
      displayName: 'Customer ID',
      description: 'Only sessions for this customer. Empty: all.',
      placeholder: 'cus_...',
      required: false,
    }),
  },
  outputSchema: checkoutSessionOutputSchema,
  sampleData: {
    id: 'cs_test_a11YYufWQzNY63zpQ6QSNRQhkUpVph4WRmzW0zWJO2znZKdVujZ0N0S22u',
    object: 'checkout.session',
    amount_subtotal: 2198,
    amount_total: 2198,
    created: 1679600215,
    currency: 'usd',
    customer: 'cus_NWSaVkvdacCUi4',
    customer_details: {
      email: '[email protected]',
      name: 'Jenny Rosen',
      phone: null,
    },
    livemode: false,
    mode: 'payment',
    payment_intent: 'pi_12345ABC',
    payment_status: 'paid',
    status: 'complete',
    success_url: 'https://example.com/success',
    url: null,
  },
  type: TriggerStrategy.WEBHOOK,

  async onEnable(context) {
    const webhook = await stripeCommon.subscribeWebhook(
      'checkout.session.completed',
      context.webhookUrl,
      context.auth.secret_text
    );
    await context.store.put<StripeWebhookInformation>(
      '_checkout_session_completed_trigger',
      {
        webhookId: webhook.id,
      }
    );
  },

  async onDisable(context) {
    const webhookInfo = await context.store.get<StripeWebhookInformation>(
      '_checkout_session_completed_trigger'
    );
    if (webhookInfo !== null && webhookInfo !== undefined) {
      await stripeCommon.unsubscribeWebhook(
        webhookInfo.webhookId,
        context.auth.secret_text
      );
    }
  },
  async test(context) {
    const response = await httpClient.sendRequest<{ data: { id: string }[] }>({
      method: HttpMethod.GET,
      url: 'https://api.stripe.com/v1/checkout/sessions',
      headers: {
        Authorization: 'Bearer ' + context.auth.secret_text,
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      queryParams: {
        status: 'complete',
        limit: '5',
      },
    });

    if (isEmpty(response.body) || isEmpty(response.body.data)) return [];

    return response.body.data;
  },
  async run(context) {
    const payloadBody = context.payload.body as StripeWebhookPayload;
    const sessionObject = payloadBody.data.object;
    const { customer } = context.propsValue;

    if (customer && sessionObject['customer'] !== customer) {
      return [];
    }

    return [sessionObject];
  },
});