Skip to main content

A Python library for integrating with various payment gateways in Africa, with initial support for M-Pesa

Project description

Usage Examples

1. M-Pesa Integration & Configuration

from pesa_gateway import MPesaClient

client = MPesaClient(
    consumer_key="your_consumer_key",  # or use env MPESA_CONSUMER_KEY
    consumer_secret="your_consumer_secret",  # or use env MPESA_CONSUMER_SECRET
    shortcode="your_shortcode",  # or use env MPESA_SHORTCODE
    passkey="your_passkey",  # or use env MPESA_PASSKEY
    environment="sandbox"  # or use env MPESA_ENVIRONMENT ("sandbox" or "production")
)

2. STK Push (Lipa Na M-Pesa Online)

response = client.stk_push_request(
    data={
        "amount": 100,
        "phone_number": "254713164545",
        "account_reference": "test",
        "transaction_description": "test",
        "callback_url": "https://example.com/callback",
    }
)

3. C2B (Customer to Business)

# Register C2B URLs
client.customer_to_business_register_url(
    data={
        "shortcode": "your_shortcode",
        "response_type": "Completed",
        "confirmation_url": "https://yourdomain.com/confirmation",
        "validation_url": "https://yourdomain.com/validation"
    }
)

# Simulate C2B payment (for sandbox/testing)
client.simulate_c2b_payment(
    data={
        "shortcode": "your_shortcode",
        "command_id": "CustomerPayBillOnline",
        "amount": 100,
        "msisdn": "2547XXXXXXXX",
        "bill_ref_number": "INV123"
    }
)

4. B2B (Business to Business)

response = client.business_to_business_payment(
    data={
        "amount": 1000,
        "party_a": "123456",
        "party_b": "000000",
        "account_reference": "B2BREF",
        "initiator": "test",
        "requester": "254700000000",
        "remarks": "Payment to supplier",
        "occassion": "test",
        "queue_timeout_url": "https://yourdomain.com/timeout",
        "result_url": "https://yourdomain.com/result"
    }
)

5. B2C (Business to Customer)

response = client.business_to_customer_payment(
    data={
        "initiator": "test",
        "security_credential": "credential",
        "amount": 500,
        "party_a": "shortcode",
        "party_b": "2547XXXXXXXX",
        "remarks": "Salary payment",
        "queue_timeout_url": "https://yourdomain.com/timeout",
        "result_url": "https://yourdomain.com/result",
        "occasion": "test"
    }
)

6. Transaction Status Query

status = client.query_transaction_status(
    data={
        "initiator": "test",
        "transaction_id": "LKXXXXXX",
        "remarks": "test",
        "occassion": "test",
        "result_url": "https://yourdomain.com/result",
        "queue_timeout_url": "https://yourdomain.com/timeout"
    }
)

7. Account Balance Query

balance = client.account_balance(
    data={
        "remarks": "test",
        "initiator": "test",
        "queue_timeout_url": "https://yourdomain.com/timeout",
        "result_url": "https://yourdomain.com/result"
    }
)

8. Transaction Reversal

reversal = client.reverse_transaction(
    data={
        "initiator": "test",
        "transaction_id": "LKXXXXXX",
        "amount": 100,
        "remarks": "Erroneous payment",
        "occassion": "test",
        "result_url": "https://yourdomain.com/result",
        "queue_timeout_url": "https://yourdomain.com/timeout"
    }
)

9. Dynamic QR Code Generation

qr_code = client.generate_dynamic_qr_code(
    data={
        "merchant_name": "test",
        "ref_no": "test",
        "amount": 100,
        "trx_code": "PB",
        "cpi": "789621",
        "size": "300"
    }
)

10. Phone Number Validation

from pesa_gateway.utils import validate_phone_number

is_valid = validate_phone_number("2547XXXXXXXX")

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

pesa_gateway-0.0.15.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

pesa_gateway-0.0.15-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pesa_gateway-0.0.15.tar.gz.

File metadata

  • Download URL: pesa_gateway-0.0.15.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pesa_gateway-0.0.15.tar.gz
Algorithm Hash digest
SHA256 63d8a705f7b1a887a4aa9189a2235c4054ba0406510c69f7f060fb2a8a6fc604
MD5 ccfb1f26ab2cbe9a843f542c4f1591db
BLAKE2b-256 ccd49d7209c99bfee2a118814c46cebfd8754e00cae9cfb0e226dd63243bdb1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pesa_gateway-0.0.15.tar.gz:

Publisher: release.yaml on thaka-creations/pesa-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pesa_gateway-0.0.15-py3-none-any.whl.

File metadata

  • Download URL: pesa_gateway-0.0.15-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pesa_gateway-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 77e7c1a837b395cd675988ff19d0783c894c52fd64262b0af90802863c21889b
MD5 96fadb4724d67d0dcc7bd7f334413c80
BLAKE2b-256 d3439647c2962eb64b1280f28280b337e102b0605533f99c53be89238a55feaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pesa_gateway-0.0.15-py3-none-any.whl:

Publisher: release.yaml on thaka-creations/pesa-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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