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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pesa_gateway-0.0.13.tar.gz
  • Upload date:
  • Size: 8.5 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.13.tar.gz
Algorithm Hash digest
SHA256 1069e30cac6ecca0850b10f66a9dd7bb1427683f9fb1e9d82fbf0e6c2da73739
MD5 eccb31783d7a97b76afadad57eee69c3
BLAKE2b-256 e26388202f4da6fd3516cdff2448d00e522bc44e4fee063e86c12c6103f5c3f9

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pesa_gateway-0.0.13-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.13-py3-none-any.whl
Algorithm Hash digest
SHA256 342e1f30f6ac73a20f33617b758dec5fccf3c69fea7b321bab680db630091284
MD5 a2a9957e21c6a002dedc7c30808c8669
BLAKE2b-256 e759c7de33a71a394ea45247c1351186f746baeed647c5b687c9980a7b4c7db4

See more details on using hashes here.

Provenance

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