Skip to main content

Official Sunny Payments Python SDK - Payment processing made simple

Project description

Sunny Payments Python SDK

The official Python SDK for Sunny Payments - Payment processing made simple.

PyPI version Python versions License: MIT

Installation

pip install sunny-payments

Quick Start

from sunny import Sunny

# Initialize the client with your API key
sunny = Sunny("sk_live_your_api_key")

# Create a payment
payment = sunny.payments.create(
    amount=1000,
    currency="KES",
    source="mpesa",
    description="Order #12345"
)

print(payment["id"])  # pay_xxxxx

Features

  • Payments - Create, capture, refund, and manage payments
  • Customers - Create and manage customer profiles
  • Refunds - Process full and partial refunds
  • Webhooks - Register endpoints and verify signatures
  • Bills - Airtime, data, electricity, and utility payments
  • Type Hints - Full type annotations included

Usage Examples

Payments

# Create a payment
payment = sunny.payments.create(
    amount=5000,
    currency="KES",
    source="mpesa",
    metadata={"order_id": "12345"}
)

# Retrieve a payment
payment = sunny.payments.retrieve("pay_123")

# List payments
result = sunny.payments.list(limit=10)
for payment in result["data"]:
    print(payment["id"])

# Refund a payment
sunny.payments.refund("pay_123", amount=2500, reason="Customer request")

Bill Payments

# Purchase airtime
airtime = sunny.bills.purchase_airtime(
    phone_number="+254712345678",
    amount=100,
    network="safaricom"
)

# Get data bundles
bundles = sunny.bills.get_data_bundles("safaricom")

# Buy electricity tokens
electricity = sunny.bills.purchase_electricity(
    meter_number="12345678",
    amount=500,
    phone_number="+254712345678"
)
print(electricity["token"])  # KPLC token

Webhooks

from flask import Flask, request
from sunny.resources.webhooks import WebhooksResource

app = Flask(__name__)
WEBHOOK_SECRET = "whsec_xxx"

@app.route("/webhooks/sunny", methods=["POST"])
def handle_webhook():
    payload = request.data
    signature = request.headers.get("x-sunny-signature")
    
    if not WebhooksResource.verify_signature(payload, signature, WEBHOOK_SECRET):
        return "Invalid signature", 400
    
    event = request.json
    
    if event["type"] == "payment.succeeded":
        # Handle successful payment
        pass
    
    return "OK", 200

Error Handling

from sunny import Sunny, AuthenticationError, ValidationError, RateLimitError

try:
    payment = sunny.payments.create(...)
except AuthenticationError:
    print("Invalid API key")
except ValidationError as e:
    print(f"Validation error: {e.message}, field: {e.field}")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")

License

MIT License - see LICENSE for details.

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

sunny_payments-1.0.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

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

sunny_payments-1.0.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sunny_payments-1.0.0.tar.gz
Algorithm Hash digest
SHA256 88839adffc2191f9ebf76b76bd3feb3bc4717333661f9965a07be13df700fdad
MD5 539ee136a608497b9f507f71ed34e397
BLAKE2b-256 4603cd22929ae8266c6bcf6717bb4e5ce1ceed4e8657fc857033ce760eca5f5c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sunny_payments-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a8b3ad72fec214879b2cbcd8307d6d7e101a350e7a53788d303d33d34fdb54f
MD5 705f38077fa24f7660c5e1692b974ec7
BLAKE2b-256 ee9886cf985661a856911354e6d805372849364ced57a3cb78785221f341cc78

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