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.6 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  Property,
} from '@activepieces/pieces-framework';
import { stripeCommon } from '../common';
import { StripeWebhookInformation } from '../common/types';
import { stripeAuth } from '../..';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { isEmpty } from '@activepieces/pieces-framework';

type StripeWebhookPayload = {
  data: {
    object: {
      [key: string]: unknown;
    };
  };
};

import { refundOutputSchema } from '../output-schemas';
export const stripeNewRefund = createTrigger({
  auth: stripeAuth,
  name: 'new_refund',
  classification: 'READ',
  displayName: 'New Refund',
  description: 'Fires when a refund is created, full or partial.',
  aiMetadata: {
    description:
      'Fires when a refund is created in Stripe (the refund.created event), for a full or partial refund, emitting the refund record. Optional filters narrow firing to a specific charge ID or payment intent ID. Use to react to money returned to a customer, such as updating accounting or notifying them.',
  },
  propertyGroups: [
    {
      key: 'filters',
      display: 'section',
      label: 'Filters',
      icon: 'filter',
      props: ['charge', 'payment_intent'],
    },
  ],
  props: {
    charge: Property.ShortText({
      displayName: 'Charge ID',
      description: 'Only refunds for this charge. Empty: all.',
      placeholder: 'ch_...',
      required: false,
    }),
    payment_intent: Property.ShortText({
      displayName: 'Payment Intent ID',
      description: 'Only refunds for this payment. Empty: all.',
      placeholder: 'pi_...',
      required: false,
    }),
  },
  outputSchema: refundOutputSchema,
  sampleData: {
    id: 're_1Nispe2eZvKYlo2Cd31jOCgZ',
    object: 'refund',
    amount: 1000,
    balance_transaction: 'txn_1Nispe2eZvKYlo2CYezqFhEx',
    charge: 'ch_1NirD82eZvKYlo2CIvbtLWuY',
    created: 1692942318,
    currency: 'usd',
    metadata: {},
    payment_intent: 'pi_1GszsK2eZvKYlo2CfhZyoZLp',
    reason: 'requested_by_customer',
    receipt_number: null,
    status: 'succeeded',
  },
  type: TriggerStrategy.WEBHOOK,

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

  async onDisable(context) {
    const webhookInfo = await context.store.get<StripeWebhookInformation>(
      '_new_refund_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/refunds',
      headers: {
        Authorization: 'Bearer ' + context.auth.secret_text,
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      queryParams: {
        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 refundObject = payloadBody.data.object;
    const { charge, payment_intent } = context.propsValue;

    if (charge && refundObject['charge'] !== charge) {
      return [];
    }
    if (payment_intent && refundObject['payment_intent'] !== payment_intent) {
      return [];
    }

    return [refundObject];
  },
});