import { z } from 'zod';
import { FinixContext, ToolFactory } from '../../types.js';
import { FinixClient } from '../../utils/finixClient.js';
const createPaymentLinkPrompt = () => `
This tool will create a payment link in Finix.
It takes three arguments:
- merchant_id (str): The ID of the merchant the payment link is created under.
- price (int): The unit price in cents.
- quantity (int): The quantity of the product to include in the payment link.
`;
const createPaymentLinkParameters = () => z.object({
merchant_id: z.string().describe('The ID of the merchant the payment link is created under'),
price: z.number().int().min(1).describe('The unit price in cents'),
quantity: z.number().int().min(1).describe('The quantity of the product to include in the payment link')
});
const createPaymentLinkAnnotations = () => ({
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
readOnlyHint: false,
title: 'Create Payment Link'
});
const createPaymentLink = async (client: FinixClient, _context: FinixContext, params: any): Promise<any> => {
try {
if (!client.hasCredentials()) {
throw new Error('Finix username and password are required for this operation. Please configure FINIX_USERNAME and FINIX_PASSWORD in your environment.');
}
const { merchant_id, price, quantity } = params;
const total_price = price * quantity;
const payload = {
merchant_id,
payment_frequency: 'ONE_TIME',
allowed_payment_methods: ['PAYMENT_CARD', 'ACH'],
nickname: `Payment Link`,
items: [{
name: 'Payment',
quantity,
unit_price: price,
total_price
}]
};
const response = await client.post('/payment_links', payload);
if (response.error) {
throw new Error(`Error creating payment link: ${response.error.message}`);
}
// Return ID and URL so users can actually use the payment link
return {
id: response.data.id,
url: response.data.url
};
} catch (error) {
throw error;
}
};
const tool: ToolFactory = () => ({
method: 'create_payment_link',
name: 'Create Payment Link',
description: createPaymentLinkPrompt(),
parameters: createPaymentLinkParameters(),
annotations: createPaymentLinkAnnotations(),
actions: {
payment_links: {
create: true
}
},
execute: createPaymentLink
});
export default tool;