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.3 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { linkaAuth } from '../auth';
import { linkaCommon } from '../common/common';

export const newPayment = createTrigger({
  auth: linkaAuth,
  name: 'newPayment',
  displayName: 'New Payment',
  description: 'Triggers when a new payment is created',
  aiMetadata: {
    description:
      'Fires when a payment is recorded in the Linka/Sperse CRM (via the Payment.Created webhook), delivering the related contact, invoice, and transaction details. Use to start a flow when a customer pays.',
  },
  props: {},
  type: TriggerStrategy.WEBHOOK,
  sampleData: {
    contact: {
      email: '[email protected]',
      fullName: 'Test User',
      id: 636,
      phone: '98877676565',
    },
    invoice: {
      currencyId: 'USD',
      date: '2024-06-10T00:00:00Z',
      description: 'just a description',
      discountTotal: 0,
      grandTotal: 100,
      id: 457,
      number: 'INV - 20240610 - 746C',
      shippingTotal: 0,
      taxTotal: 0,
      lines: [
        {
          description: 'Me Spacial',
          productId: 810,
          quantity: 1,
          rate: 100,
          total: 100,
          unitId: 'Month',
        },
      ],
    },
    transaction: {
      amount: 100,
      currencyId: 'USD',
      date: '2024-06-10T09:36:01.832Z',
      gatewayName: null,
      gatewayTransactionId: null,
      id: 403,
      isSuccessful: true,
      type: 'Sale',
    },
    eventTime: '2024-06-10T09:36:08',
    eventType: 'Payment.Created',
  },
  async onEnable(context) {
    const webhookId = await linkaCommon.subscribeWebhook(
      'Payment.Created',
      context.auth.props.base_url,
      context.auth.props.api_key,
      context.webhookUrl
    );

    await context.store?.put<WebhookInformation>('_new_payment_trigger', {
      webhookId: webhookId,
    });
  },
  async onDisable(context) {
    const response = await context.store?.get<WebhookInformation>(
      '_new_payment_trigger'
    );

    if (response !== null && response !== undefined) {
      await linkaCommon.unsubscribeWebhook(
        context.auth.props.base_url,
        context.auth.props.api_key,
        response.webhookId
      );
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});

interface WebhookInformation {
  webhookId: number;
}