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

2.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 { refundOutputSchema } from '../output-schemas';
export const stripeCreateRefund = createAction({
  name: 'create_refund',
  classification: 'WRITE',
  auth: stripeAuth,
  displayName: 'Create Refund',
  description: 'Refund all or part of a successful payment.',
  audience: 'human',
  aiMetadata: {
    description:
      'Issues a refund against a payment identified by its PaymentIntent ID; refunds the full amount by default, or a partial amount if one is given, with an optional reason. Use to return funds to a customer for a captured payment. Not idempotent: each call creates a new refund and moves money again.',
    idempotent: false,
  },
  propertyGroups: [
    {
      key: 'refund',
      display: 'section',
      label: 'Refund',
      icon: 'tag',
      props: ['payment_intent', 'amount', 'reason'],
    },
  ],
  props: {
    payment_intent: stripeCommon.paymentIntent, 
    amount: Property.Number({
      displayName: 'Amount',
      description:
        'Amount to refund, e.g. 12.99. Empty refunds the full payment.',
      required: false,
    }),
    reason: Property.StaticDropdown({
      displayName: 'Reason',
      required: false,
      options: {
        options: [
          { label: 'Duplicate', value: 'duplicate' },
          { label: 'Fraudulent', value: 'fraudulent' },
          { label: 'Requested by Customer', value: 'requested_by_customer' },
        ],
      },
    }),
    metadata: Property.Json({
      displayName: 'Metadata',
      description: 'Extra key/value data to store on the refund.',
      required: false,
      advanced: true,
    }),
  },
  outputSchema: refundOutputSchema,
  async run(context) {
    const { payment_intent, amount, reason, metadata } = context.propsValue;

    const body: { [key: string]: unknown } = {
      payment_intent: payment_intent,
    };

    if (amount !== undefined && amount !== null) {
      body.amount = Math.round(amount * 100);
    }
    if (reason) {
      body.reason = reason;
    }
    if (metadata && typeof metadata === 'object') {
      Object.keys(metadata).forEach((key) => {
        body[`metadata[${key}]`] = (metadata as Record<string, string>)[key];
      });
    }

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

    return response.body;
  },
});