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

4.3 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { checkoutComAuth, getEnvironmentFromApiKey } from '../common/auth';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';

export const createCustomerAction = createAction({
  name: 'create_customer',
  classification: 'WRITE',
  auth: checkoutComAuth,
  displayName: 'Create Customer',
  description: 'Store a customer\'s details in a customer object to reuse in future payments. You can link payment instruments and set a default instrument.',
  audience: 'both',
  aiMetadata: { description: 'Creates a new Checkout.com customer record from an email (required) plus optional name, phone, metadata, and a default payment instrument, so the customer can be referenced in later payments. Choose it to register a payer before charging them or saving their card. Not idempotent: each call creates a separate customer even with identical input.', idempotent: false },
  props: {
    email: Property.ShortText({
      displayName: 'Email',
      description: 'The customer\'s email address',
      required: true,
    }),
    name: Property.ShortText({
      displayName: 'Name',
      description: 'The customer\'s name',
      required: false,
    }),
    phone_country_code: Property.ShortText({
      displayName: 'Phone Country Code',
      description: 'The international country calling code (e.g., +1, +44). Required if phone number is provided.',
      required: false,
    }),
    phone_number: Property.ShortText({
      displayName: 'Phone Number',
      description: 'The phone number (6-25 characters). Required if country code is provided.',
      required: false,
    }),
    metadata: Property.Object({
      displayName: 'Metadata',
      description: 'Additional information about the customer. Maximum 10 key-value pairs, each up to 100 characters.',
      required: false,
    }),
    default: Property.ShortText({
      displayName: 'Default Instrument ID',
      description: 'The ID of the payment instrument to set as this customer\'s default',
      required: false,
    }),
  },
  async run(context) {
    const { email, name, phone_country_code, phone_number, metadata, default: defaultInstrument } = context.propsValue;
    
    const { baseUrl } = getEnvironmentFromApiKey(context.auth.secret_text);
    
    const body: Record<string, any> = { email };
    
    if (name) {
      body['name'] = name;
    }
    
    if (phone_country_code || phone_number) {
      if (!phone_country_code || !phone_number) {
        throw new Error('Both phone country code and phone number are required when providing phone information');
      }
      
      if (phone_country_code.length < 1 || phone_country_code.length > 7) {
        throw new Error('Country code must be between 1 and 7 characters');
      }
      
      if (phone_number.length < 6 || phone_number.length > 25) {
        throw new Error('Phone number must be between 6 and 25 characters');
      }
      
      body['phone'] = {
        country_code: phone_country_code,
        number: phone_number,
      };
    }
    
    if (metadata) {
      const metadataKeys = Object.keys(metadata);
      if (metadataKeys.length > 10) {
        throw new Error('Metadata can have a maximum of 10 key-value pairs');
      }
      
      for (const [key, value] of Object.entries(metadata)) {
        if (key.length > 100) {
          throw new Error(`Metadata key "${key}" exceeds 100 characters`);
        }
        if (typeof value === 'string' && value.length > 100) {
          throw new Error(`Metadata value for key "${key}" exceeds 100 characters`);
        }
      }
      
      body['metadata'] = metadata;
    }
    
    if (defaultInstrument) {
      body['default'] = defaultInstrument;
    }
    
    try {
      const response = await httpClient.sendRequest({
        method: HttpMethod.POST,
        url: `${baseUrl}/customers`,
        headers: {
          Authorization: `Bearer ${context.auth.secret_text}`,
          'Content-Type': 'application/json',
        },
        body,
      });
      
      return response.body;
    } catch (error: any) {
      if (error.response?.status === 422) {
        throw new Error(`Invalid data: ${error.response.body?.error_codes?.join(', ') || 'Please check your input data'}`);
      }
      throw error;
    }
  },
});