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.14.tar.gz (8.4 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.14-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pesa_gateway-0.0.14.tar.gz
  • Upload date:
  • Size: 8.4 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.14.tar.gz
Algorithm Hash digest
SHA256 c1fb9253139693becb69447bac43e254569080d41dd698f392e4f5bd49eeec12
MD5 29ad3cbe0be48db6890c2b6d8727eaa5
BLAKE2b-256 2a6fff61438056f9c003bea7f671baa6d710882ab506d146ee2faa0513ed3d0d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pesa_gateway-0.0.14-py3-none-any.whl
  • Upload date:
  • Size: 9.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.14-py3-none-any.whl
Algorithm Hash digest
SHA256 21cc4cf663a7d5ff9c1de399c08990b8d10fac644282b301de390d8379fe3ff0
MD5 f9a0e9362172a906e81f8c5c79a3ae4d
BLAKE2b-256 932f91e92e320fe7905bfc16304fc0071c353d7ab66b63a8780c0b4b204f8ebb

See more details on using hashes here.

Provenance

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