Skip to main content

Python SDK for MonCashConnect — independent MonCash payment integration platform for Haiti.

Project description

moncashconnect

Official Python SDK for MonCashConnect — the easiest way to integrate MonCash payments in your Python application.

Zero dependencies — uses only the Python standard library.

Note: MonCashConnect is not affiliated with Digicel or the official MonCash service.

Requirements

  • Python 3.9+

Installation

pip install moncashconnect

Quick Start

import os
from moncashconnect import MonCashClient

client = MonCashClient(os.environ["MCC_SECRET_KEY"])

payment = client.create_payment(
    1500,
    "order-001",
    return_url="https://yoursite.com/payment/success",
    customer_name="Jean Dupont",
)

# Redirect the customer
print(payment["paymentUrl"])

Your secret key starts with sk_proj_ — get it from Developer → Projects in your dashboard.

Check Payment Status

tx = client.get_payment_status("order-001")

print(tx["status"])    # "pending" | "completed" | "failed"
print(tx["netAmount"]) # Amount after commission deduction

Get Account Balance

balance = client.get_balance()

print(balance["balanceHtg"])      # Total available
print(balance["withdrawableHtg"]) # Can withdraw now

Webhooks

Configure a webhook URL in your project. MonCashConnect sends a signed POST when a payment is finalized.

Always read the raw body before any json.loads().

from moncashconnect import construct_event, MonCashError

# Django
def webhook(request):
    raw_body  = request.body          # bytes — read before any parsing
    signature = request.META.get("HTTP_X_MCC_SIGNATURE", "")
    timestamp = request.META.get("HTTP_X_MCC_TIMESTAMP", "")

    try:
        event = construct_event(
            raw_body, signature, timestamp,
            os.environ["MCC_WEBHOOK_SECRET"],
        )
    except MonCashError as exc:
        return HttpResponse(str(exc), status=exc.status_code)

    if event["event"] == "payment.completed":
        Order.objects.filter(reference=event["reference"]).update(status="paid")
    elif event["event"] == "payment.failed":
        Order.objects.filter(reference=event["reference"]).update(status="failed")

    return HttpResponse("OK")

Flask

from flask import Flask, request, abort
from moncashconnect import construct_event, MonCashError

app = Flask(__name__)

@app.route("/webhooks/moncash", methods=["POST"])
def moncash_webhook():
    raw_body  = request.get_data()   # bytes
    signature = request.headers.get("X-MCC-Signature", "")
    timestamp = request.headers.get("X-MCC-Timestamp", "")

    try:
        event = construct_event(
            raw_body, signature, timestamp,
            os.environ["MCC_WEBHOOK_SECRET"],
        )
    except MonCashError as exc:
        abort(exc.status_code, description=str(exc))

    if event["event"] == "payment.completed":
        mark_order_paid(event["reference"])

    return "OK", 200

FastAPI

from fastapi import FastAPI, Header, HTTPException, Request
from moncashconnect import construct_event, MonCashError

app = FastAPI()

@app.post("/webhooks/moncash")
async def moncash_webhook(
    request: Request,
    x_mcc_signature: str = Header(...),
    x_mcc_timestamp: str = Header(...),
):
    raw_body = await request.body()
    try:
        event = construct_event(
            raw_body, x_mcc_signature, x_mcc_timestamp,
            os.environ["MCC_WEBHOOK_SECRET"],
        )
    except MonCashError as exc:
        raise HTTPException(status_code=exc.status_code, detail=str(exc))

    if event["event"] == "payment.completed":
        await mark_order_paid(event["reference"])

    return {"ok": True}

Error Handling

from moncashconnect import MonCashClient, MonCashError

try:
    payment = client.create_payment(500, "order-42")
except MonCashError as exc:
    print(exc)             # "referenceId already exists for this project"
    print(exc.status_code) # 409
    print(exc.context)     # Full API response dict, or None

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

moncashconnect-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

moncashconnect-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file moncashconnect-1.0.0.tar.gz.

File metadata

  • Download URL: moncashconnect-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for moncashconnect-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d63dd5da0ab9234c20f506b546d1b5bb3320a04c6447761898009c4f87f8c5a8
MD5 80bdd0c3e052a7dc678afb6f3dc79cf8
BLAKE2b-256 0b21e728698d911418ca5c7f04bc36906629e08ccf00d3351c3855d822e26c21

See more details on using hashes here.

File details

Details for the file moncashconnect-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: moncashconnect-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for moncashconnect-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e49b811edfe450a4e0b5149dcb9d8bfaffa04df29cf190ffbb8a97dfe0196719
MD5 acdbdf0c6abd9bae1d8486faea88309d
BLAKE2b-256 bbcfa71af9a37f45e546dafdf801509c314d492dba55e50dccbf61ec7205034d

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