Skip to main content

OpenAI Integration

This guide demonstrates how to integrate PayLink with OpenAI’s models to create AI assistants that can process payments. The integration uses PayLink’s OpenAI adapter, which formats PayLink tools to be compatible with OpenAI’s function calling API.

Prerequisites

Before you begin, make sure you have:
  • Installed the PayLink SDK
  • An OpenAI API key
  • Access to a PayLink server with payment tools

Installation

Install the required packages:

Basic Integration

First, initialize the PayLink client using the OpenAI adapter:
The PayLinkClient from the openai_adapter.toolkit module automatically formats PayLink tools in a way that’s compatible with OpenAI’s function calling API.

Step 2: Set Up OpenAI Client

Initialize the OpenAI client:

Step 3: Create a Conversation with Tool Calls

Send a user message to OpenAI with the PayLink tools:
The OpenAI model will analyze the user’s request and generate a tool call to the appropriate PayLink function. Once you have the tool call from OpenAI, you can execute it using the PayLink client:

Error Handling

When integrating with payment systems, proper error handling is essential. Here’s how to handle errors:

Tips for Effective Integration

  1. System Instructions: Include clear instructions in your system message about payment capabilities.
  2. Validation: Although OpenAI models can extract payment details, add validation before processing:
  3. User Confirmation: For better user experience, confirm payment details before processing:
  4. Status Updates: Keep the user informed about the payment status:

Next Steps

Now that you’ve integrated PayLink with OpenAI, you can:
  • Explore other PayLink tools like checking payment status or refunds
  • Enhance your assistant with more complex payment workflows
  • Implement user authentication and authorization
Check out our LangChain integration guide for an alternative approach to AI-powered payments.

Troubleshooting

Tool Not Found Error: If you receive a “Tool not found” error, ensure that:
  • The PayLink server is running and accessible
  • You’re using the correct tool name (case-sensitive)
  • Your PayLink SDK is up to date
Payment Failed Error: If the payment fails, check:
  • The phone number is in the correct format (e.g., 254XXXXXXXXX)
  • The amount is within acceptable limits
  • The PayLink server can connect to the payment provider
For more assistance, refer to our troubleshooting guide or contact support.