Skip to main content

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")

Metadata

Release files for pesa-gateway 0.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pesa-gateway 0.0.16
File Size Uploaded Provenance Actions
pesa_gateway-0.0.16.tar.gz 9.1 kB With provenance Details for pesa_gateway-0.0.16.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pesa-gateway 0.0.16
File Interpreter ABI Platform Actions
pesa_gateway-0.0.16-py3-none-any.whl Python 3 none any Details for pesa_gateway-0.0.16-py3-none-any.whl

Total release size: 19.3 kB

Release files / pesa_gateway-0.0.16.tar.gz

Download URL pesa_gateway-0.0.16.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3bd5120a7356c67d52d9745b971a6c85c2bfb187a8d917079455e90245a11a51
BLAKE2b-256 checksum
How to use checksums
3a7f1cbc0d59e8fded88424ce8e379fd884a84272d69b35757e99aac1a1dd55c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2025.

Transparency log

Release files / pesa_gateway-0.0.16-py3-none-any.whl

Download URL pesa_gateway-0.0.16-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
545f8801b409e3a7c4229ec5ea086765261730a1aaf100ffabc0a3f50cabfdb2
BLAKE2b-256 checksum
How to use checksums
cdda90df40c8b56ec99aa350da39fe3472990f56cee2b268118d3399879d48c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2025.

Transparency log

Security for pesa-gateway 0.0.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pesa_gateway-0.0.16.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 12, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page