Skip to main content
Now that you’ve tested your payment provider connection, you’re ready to integrate PayLink into your AI agent. This guide shows you how to build an agent using LangChain that can process payments for orders.

Install LangChain

Make sure you have LangChain installed:

Create Your Agent

1

Configure environment variables

Create a .env file with the following environment variables:
Replace the placeholder values with your actual API keys and credentials.
2

Create the orders tool

First, create a tool to retrieve orders from your database. This example uses a simple in-memory database:
3

Initialize PayLink and create the agent

Set up PayLink tools and create your agent:
4

Running the Agent

Start the LangGraph development server:
This will start the agent and make it available at http://localhost:2024.
5

Interacting with the Agent

Once the server is running, you can interact with your agent through the LangGraph Studio interface or via API calls.
6

Example conversation flow

Here’s an example of how a conversation with your agent might flow:
  1. Agent: “I can help you with orders and payments. What would you like to do?”
  2. User: “Show me pending orders”
  3. Agent: Uses get_orders tool with payment_status="pending"
  4. Agent: “I found 2 pending orders. Would you like me to process payment for any of them?”
  5. User: “Yes, process payment for Item 2”
  6. Agent: Prepares payment request and pauses for approval
  7. System: “Approval Required: Review the payment request for Item 2 (400 Ksh)”
  8. User: Reviews and approves
  9. Agent: Executes payment via PayLink
Example agent response showing successful payment initiation

View Execution Traces

You can view detailed execution traces of your agent’s interactions in LangSmith. This allows you to inspect tool calls, model responses, and the complete execution flow. This is an example trace: View example trace →

How It Works

The agent is configured with:
  • Payment Tools: PayLink tools (like stk_push) are automatically available to the agent
  • Order Management: The get_orders tool allows the agent to retrieve orders that need payment
When a user asks the agent to pay for pending orders, the agent will:
  1. Retrieve pending orders using get_orders
  2. Calculate the total amount
  3. Initiate payment using the stk_push tool
  4. The user receives a payment prompt on their phone to complete the transaction

Full Code Example

For a complete, working example of an agent with PayLink payment integration, see the agent-with-payment repository on GitHub.

Next Steps

Once your agent can handle payments, learn how to implement Human in the Loop workflows for payment approvals.