← All projects

python

  • Python 100%
git@gitlab.com:aice-lab/payment/python.git

Latest commit

3ea405d4 ·

README

aice-payment

Python client SDK for the AiCE-Lab payment service.

Installation

pip install aice-payment

Requires Python 3.12+ and httpx>=0.27.

Quick start

from aice_payment import PaymentClient, CheckoutRequest

client = PaymentClient(base_url="https://pay.example.com", api_key="your-api-key")

# Create a checkout
response = client.create_checkout(
    CheckoutRequest(
        order_ref="ORD-001",
        gateway="bkash",
        amount_minor=50000,  # 500.00 BDT
        currency="BDT",
        customer={"name": "Customer Name"},
        return_urls={"success": "https://your-app.example/success"},
    )
)
print(response.redirect_url)  # redirect the customer here

# Retrieve a transaction
tx = client.get_transaction(response.transaction_id)
if tx.is_succeeded():
    print("Payment successful")

# Refund
tx = client.refund(response.transaction_id)

Error handling

All exceptions inherit from PaymentError. Each has a code (machine-readable string from the API) and a message.

from aice_payment import (
    PaymentError,
    UnauthorizedError,
    NotFoundError,
    ConflictError,
    AmountMismatchError,
    BadRequestError,
    GatewayUnavailableError,
    TransportError,
)

try:
    tx = client.get_transaction("tx-id")
except GatewayUnavailableError as e:
    print(f"Retry after {e.retry_after_seconds}s")
except PaymentError as e:
    print(f"Error [{e.code}]: {e}")

Testing consumer code

Use MockPaymentClient or PaymentClientProtocol to stub the client in your tests:

from aice_payment import MockPaymentClient, PaymentClientProtocol

def charge(client: PaymentClientProtocol, order_ref: str) -> str:
    resp = client.create_checkout(...)
    return resp.transaction_id

def test_charge() -> None:
    mock = MockPaymentClient()
    txn_id = charge(mock, "ORD-1")
    assert txn_id.startswith("mock-tx-")
    assert mock.last_checkout is not None

License

FSL-1.1-Apache-2.0. See LICENSE.

This is a snapshot generated from GitLab. For the live README, see the project page.