Skip to main content

SDK for AI agents to make payments on merchant websites, make transfters and other kinds of bill payments.

Project description

AI Payment Agent SDK

A Python SDK for AI agents to make payments on merchant websites that support the AI payment protocol.

Installation

pip install ai-payment-agent

Requirements

  • Python 3.7+
  • cryptography
  • aiohttp
  • playwright

Quick Start

The SDK supports two integration methods:

  1. Direct payment flow
  2. Webhook-based payment flow

Direct Payment Flow

import asyncio
from ai_payment_agent import AgentPaymentClient

# Replace with your RSA private key
PRIVATE_KEY = """-----BEGIN PRIVATE KEY-----
YOUR_PRIVATE_KEY_HERE
-----END PRIVATE KEY-----"""

async def main():
    # Initialize the payment client
    client = AgentPaymentClient(
        agent_id="YOUR_AGENT_ID",
        private_key_pem=PRIVATE_KEY,
        payment_gateway_url="https://payment-gateway.example.com/api"
    )
    
    # Check if a website supports AI payments
    compatibility = await client.check_website_compatibility("https://merchant-example.com")
    
    if compatibility["compatible"]:
        merchant_id = compatibility["merchantId"]
        
        # Initiate a payment
        payment_response = await client.initiate_payment(
            merchant_id=merchant_id,
            amount=19.99,
            currency="USD",
            description="AI-assisted purchase",
            agent_payment_reference="order-12345"
        )
        
        print(f"Payment initiated: {payment_response}")
        
        # Later, complete the payment
        # (usually after merchant website confirmation, i.e. initialization)
        if payment_response:
            success = await client.complete_payment(
                payment_id=payment_response["payment_id"],
                merchant_id=merchant_id,
                encrypted_advice=payment_response["encrypted_advice"],
                secret=payment_response["secret"]
            )
            
            print(f"Payment completed: {success}")

if __name__ == "__main__":
    asyncio.run(main())

Webhook-based Payment Flow

Part 1: Check Compatibility and Initiate Payment

import asyncio
from ai_payment_agent import AgentPaymentClient

# Replace with your RSA private key
PRIVATE_KEY = """-----BEGIN PRIVATE KEY-----
YOUR_PRIVATE_KEY_HERE
-----END PRIVATE KEY-----"""

async def start_payment():
    # Initialize the payment client
    client = AgentPaymentClient(
        agent_id="YOUR_AGENT_ID",
        private_key_pem=PRIVATE_KEY,
        payment_gateway_url="https://payment-gateway.example.com/api"
    )
    
    # Check if a website supports AI payments
    compatibility = await client.check_website_compatibility("https://merchant-example.com")
    
    if compatibility["compatible"]:
        merchant_id = compatibility["merchantId"]
        
        # Initiate a payment
        payment_response = await client.initiate_payment(
            merchant_id=merchant_id,
            amount=19.99,
            currency="USD",
            description="AI-assisted purchase",
            agent_payment_reference="order-12345"
        )
        
        print(f"Payment initiated: {payment_response}")
        # Payment completion will be handled by webhook

if __name__ == "__main__":
    asyncio.run(start_payment())

Part 2: Set Up Webhook for Payment Completion

Implement a webhook endpoint in your server application (e.g., using Flask or FastAPI):

from flask import Flask, request, jsonify
from ai_payment_agent import AgentPaymentClient

app = Flask(__name__)
client = AgentPaymentClient(
    agent_id="YOUR_AGENT_ID",
    private_key_pem=PRIVATE_KEY,
    payment_gateway_url="https://payment-gateway.example.com/api"
)

@app.route('/webhook/payment', methods=['POST'])
async def payment_webhook():
    data = request.json
    
    if data['status'] == 'initialized':
        # Complete the payment
        success = await client.complete_payment(
            merchant_id=data['merchant_id'],
            payment_id=data['payment_id'],
            encrypted_advice=data['encrypted_advice'],
            secret=data['secret']
        )
        return jsonify({"success": success})
    
    return jsonify({"success": True})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Note: Your webhook URL must be registered with the payment processing backend.

Bill Payments and Bank Transfers

The SDK also supports bill payments and bank transfers:

# Make a bill payment
bill_result = await client.make_bill_payment(
    merchant_id="MERCHANT_ID",
    parameters={
        "bill_type": "electricity",
        "provider": "NationalGrid",
        "account_number": "1234567890",
        "amount": 50.00,
        "reference": "March2025"
    }
)

# Make a bank transfer
transfer_result = await client.make_transfer(
    merchant_id="MERCHANT_ID",
    parameters={
        "amount": 100.00,
        "recipient": "Jane Doe",
        "bank_name": "Example Bank",
        "account_number": "0987654321",
        "reference": "Invoice #123"
    }
)

API Reference

AgentPaymentClient

The main class for interacting with the payment gateway.

Constructor

client = AgentPaymentClient(
    agent_id: str,
    private_key_pem: str,
    payment_gateway_url: str
)

Methods

  • get_public_key_pem() - Get the agent's public key in PEM format
  • check_website_compatibility(url: str) - Check if a website supports AI payments
  • initiate_payment(merchant_id, amount, currency, description, agent_payment_reference) - Start a payment
  • complete_payment(payment_id, merchant_id, encrypted_advice, secret) - Complete a payment
  • make_bill_payment(merchant_id, parameters) - Make a bill payment (utilities, etc.)
  • make_transfer(merchant_id, parameters) - Make a bank transfer
  • check_initialization_status(agent_payment_reference) - Check payment status

Security

This SDK uses RSA public-key cryptography for secure communication with the payment gateway. Ensure that your private key is kept secure and never exposed in client-side code.

Testing

You can learn how to set up and test this SDK alongside the other 2 components (payment gateway backend and merchant frontend SDK) here.

Community

Join our community on Discord and follow us on LinkedIn and X. Feel free to contribute and raise issues.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_payment_client-0.1.10.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_payment_client-0.1.10-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file agent_payment_client-0.1.10.tar.gz.

File metadata

  • Download URL: agent_payment_client-0.1.10.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for agent_payment_client-0.1.10.tar.gz
Algorithm Hash digest
SHA256 b9ce0cd325c4efca1640d81e0fab4097c297e7cc400d704e9da68516a6990423
MD5 90a3bfc6bce1d6690b41ab264b423401
BLAKE2b-256 ab7f2d0792f103fd9b7f50fdf32d3a9894ab0639bee9bcf43198be858c1b2bae

See more details on using hashes here.

File details

Details for the file agent_payment_client-0.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_payment_client-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 0ef36703ab86488e29898f2148571784af39dba82c7f547b4e5bf5710b38b9db
MD5 fdd8f966713e842d137a3b1fc8c69efe
BLAKE2b-256 18711e0911899ea480dcc92fbb7b928d123baf0cd9df1f647315d5562cccce55

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page