How to Integrate LangChain for payments with Slack for production AI
Combining LangChain for payments with Slack gives you a clean production pattern: let the agent decide when a payment action is needed, then push the execution and approval trail into Slack where humans already work. That’s useful for invoice approvals, subscription changes, refunds, and internal chargebacks where you want an AI agent to draft the action but keep control in a chat-based workflow.
Prerequisites
- •Python 3.10+
- •A Slack app with:
- •Bot token
- •
chat:writescope - •A target channel ID
- •LangChain installed with your payments integration package
- •Payment provider credentials configured in environment variables
- •A running webhook endpoint if you want Slack interactive approvals
- •Basic familiarity with:
- •
langchain_core.tools - •
langchain_openaior your model provider of choice - •Slack Web API via
slack_sdk
- •
Integration Steps
- •Install the dependencies.
pip install langchain langchain-core langchain-openai slack-sdk python-dotenv
If your payments integration is exposed as a LangChain tool or custom wrapper, install that package too. In production, keep all secrets in environment variables.
- •Create a payment tool that LangChain can call.
This pattern uses a BaseTool wrapper around your payment provider SDK. If your payments backend already exposes a LangChain tool, plug that in directly.
import os
from typing import Optional
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
class CreatePaymentInput(BaseModel):
amount_cents: int = Field(..., gt=0)
currency: str = Field(default="usd")
customer_id: str
description: Optional[str] = None
class CreatePaymentTool(BaseTool):
name = "create_payment"
description = "Create a payment intent for an approved customer action."
args_schema = CreatePaymentInput
def _run(self, amount_cents: int, currency: str, customer_id: str, description: Optional[str] = None):
# Replace this with your real payments SDK call.
# Example shape for Stripe-like APIs:
# payment_intent = stripe.PaymentIntent.create(...)
return {
"payment_id": "pay_12345",
"status": "requires_confirmation",
"amount_cents": amount_cents,
"currency": currency,
"customer_id": customer_id,
"description": description,
}
payment_tool = CreatePaymentTool()
- •Wire the tool into a LangChain agent.
Use an LLM-backed agent so the model can decide when to call the payment tool. For production, keep the tool narrow and deterministic.
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent = initialize_agent(
tools=[payment_tool],
llm=llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
)
result = agent.invoke({
"input": "Create a $49.99 payment for customer cus_001 for monthly subscription renewal."
})
print(result)
- •Send the result to Slack for human review or notification.
Use slack_sdk.WebClient.chat_postMessage to publish the payment proposal or status into a channel. In production AI systems, this is where you add approval workflows and auditability.
import os
from slack_sdk import WebClient
slack_client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])
def notify_slack(channel_id: str, payload: dict):
text = (
f"*Payment action requested*\n"
f"- Payment ID: `{payload['payment_id']}`\n"
f"- Status: `{payload['status']}`\n"
f"- Amount: `{payload['amount_cents']}` {payload['currency']}\n"
f"- Customer: `{payload['customer_id']}`\n"
f"- Description: {payload.get('description', '-')}"
)
return slack_client.chat_postMessage(
channel=channel_id,
text=text,
)
notify_slack(
channel_id=os.environ["SLACK_CHANNEL_ID"],
payload={
"payment_id": "pay_12345",
"status": "requires_confirmation",
"amount_cents": 4999,
"currency": "usd",
"customer_id": "cus_001",
"description": "monthly subscription renewal",
},
)
- •Add an approval callback before finalizing the payment.
For production AI, don’t auto-capture money from an LLM decision alone. Post the request in Slack, wait for approval, then call your real payment finalization method.
def finalize_payment(payment_id: str):
# Replace with real SDK call.
# Example:
# stripe.PaymentIntent.confirm(payment_id)
return {"payment_id": payment_id, "status": "succeeded"}
def handle_approval(approved: bool, payment_payload: dict):
if not approved:
slack_client.chat_postMessage(
channel=os.environ["SLACK_CHANNEL_ID"],
text=f"Payment `{payment_payload['payment_id']}` was rejected by reviewer.",
)
return {"status": "rejected"}
final_result = finalize_payment(payment_payload["payment_id"])
slack_client.chat_postMessage(
channel=os.environ["SLACK_CHANNEL_ID"],
text=f"Payment `{final_result['payment_id']}` finalized with status `{final_result['status']}`.",
)
return final_result
Testing the Integration
Run a smoke test that exercises both paths: create the payment proposal and post it to Slack.
test_payload = {
"payment_id": "pay_test_001",
"status": "requires_confirmation",
"amount_cents": 2500,
"currency": "usd",
"customer_id": "cus_test_42",
"description": "test charge",
}
slack_response = notify_slack(os.environ["SLACK_CHANNEL_ID"], test_payload)
print("Slack message sent:", slack_response["ok"])
approval_result = handle_approval(True, test_payload)
print("Final result:", approval_result)
Expected output:
Slack message sent: True
Final result: {'payment_id': 'pay_test_001', 'status': 'succeeded'}
Real-World Use Cases
- •
Invoice approvals
- •The agent drafts a payment action from ERP or CRM data.
- •Slack posts the request to finance for review and approval.
- •
Refund workflows
- •The agent identifies eligible refunds from support tickets.
- •Slack notifies an ops channel before funds are returned.
- •
Subscription lifecycle management
- •The agent detects failed renewals or plan changes.
- •Slack captures human review before charging or downgrading accounts.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit