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.16.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.16-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pesa_gateway-0.0.16.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.16.tar.gz
Algorithm Hash digest
SHA256 3bd5120a7356c67d52d9745b971a6c85c2bfb187a8d917079455e90245a11a51
MD5 37f12916d335abcbcb305b8678473a17
BLAKE2b-256 3a7f1cbc0d59e8fded88424ce8e379fd884a84272d69b35757e99aac1a1dd55c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pesa_gateway-0.0.16.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.16-py3-none-any.whl.

File metadata

  • Download URL: pesa_gateway-0.0.16-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.16-py3-none-any.whl
Algorithm Hash digest
SHA256 545f8801b409e3a7c4229ec5ea086765261730a1aaf100ffabc0a3f50cabfdb2
MD5 5971d745aa78c01f89c776b2196785bd
BLAKE2b-256 cdda90df40c8b56ec99aa350da39fe3472990f56cee2b268118d3399879d48c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pesa_gateway-0.0.16-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