Skip to main content

SSLCommerz payment gateway wrapper for Python web applications.

Project description

SSLCOMMERZ Payment Gateway Python API

Downloads PyPI - Version

Python wrapper for the SSLCommerz payment gateway. Requires Python 3.10+.

Installation

pip install sslcommerz-python-api

or from git

pip install git+https://github.com/dreygur/SSLCommerz-Python.git

Usage

Initiate Payment

from decimal import Decimal
from uuid import uuid4

from sslcommerz_python_api import SSLCommerzService
from sslcommerz_python_api.models import CustomerInfo, PaymentRequest, ShippingInfo

service = SSLCommerzService.create(
    store_id='your_store_id',
    store_pass='your_store_pass',
    is_sandbox=True,
)

request = PaymentRequest(
    store_id='your_store_id',
    store_pass='your_store_pass',
    tran_id=str(uuid4()),
    total_amount=Decimal('20.20'),
    currency='BDT',
    success_url='https://example.com/success',
    fail_url='https://example.com/failed',
    cancel_url='https://example.com/cancel',
    ipn_url='https://example.com/ipn',
    product_name='demo-product',
    product_category='clothing',
    num_of_item=2,
    shipping_method='YES',
    customer=CustomerInfo(
        name='John Doe',
        email='johndoe@email.com',
        address1='demo address',
        address2='demo address 2',
        city='Dhaka',
        postcode='1207',
        country='Bangladesh',
        phone='01711111111',
    ),
    shipping=ShippingInfo(
        ship_name='demo customer',
        address='demo address',
        city='Dhaka',
        postcode='1209',
        country='Bangladesh',
    ),
    value_a='extra-a',
    value_b='extra-b',
)

response = service.initiate_payment(request)
print(response.gateway_url)   # redirect user here
print(response.session_key)
print(response.is_success)    # True / False

Response

On success, initiate_payment returns a PaymentResponse dataclass:

Field Type Description
status str "SUCCESS" or "FAILED"
session_key str SSLCommerz session key
gateway_url str URL to redirect the user to
is_success bool convenience property

On failure, raises SSLCommerzAPIError.

Validate Transaction

from sslcommerz_python_api.exceptions import SSLCommerzValidationError

try:
    result = service.validate_transaction(val_id='VAL_ID_FROM_IPN')
    print(result.status)   # "VALIDATED"
    print(result.data)     # full raw response dict
except SSLCommerzValidationError as e:
    print(f"Validation failed: {e}")

Verify IPN Signature

# ipn_data = POST body received from SSLCommerz webhook
try:
    service.verify_ipn(ipn_data)
    # signature valid — process the order
except SSLCommerzValidationError:
    # signature mismatch — reject
    pass

Error Handling

from sslcommerz_python_api.exceptions import (
    SSLCommerzAPIError,
    SSLCommerzValidationError,
    SSLCommerzError,
)

try:
    response = service.initiate_payment(request)
except SSLCommerzAPIError as e:
    print(e.reason)        # SSLCommerz failure reason
    print(e.status_code)   # HTTP status code if available
except SSLCommerzError:
    # catch-all for any library error
    pass

Logging

The library logs via Python's standard logging module under the sslcommerz_python_api namespace.

import logging

# enable for all loggers
logging.basicConfig(level=logging.DEBUG)

# or target just this library
logging.getLogger("sslcommerz_python_api").setLevel(logging.DEBUG)
Level Events
DEBUG HTTP request URL + transaction/val ID before each call
DEBUG Response status after each call
WARNING Payment initiation failed (FAILED status from API)
WARNING Transaction not validated

Migration from v1

SSLCSession still works but emits a DeprecationWarning. Switch to SSLCommerzService at your convenience — the old builder API is not planned for removal in the near term.

Acknowledgements

Fork of Shahed Mehbub's sslcommerz-python.

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

sslcommerz_python_api-2.0.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

sslcommerz_python_api-2.0.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file sslcommerz_python_api-2.0.0.tar.gz.

File metadata

  • Download URL: sslcommerz_python_api-2.0.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sslcommerz_python_api-2.0.0.tar.gz
Algorithm Hash digest
SHA256 76227b06ef96f3dc6f59b9f8b8fe194627575311680166c8b71dd49580c77c5f
MD5 0e6feb41d73762a09bde98feb6e761e2
BLAKE2b-256 bc6c0b0adf5a0371471cdf9ac6ea155d8952a6d09dc22e35a9d2b7645a5e548d

See more details on using hashes here.

File details

Details for the file sslcommerz_python_api-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sslcommerz_python_api-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9d1f3b40b733d687f52a2ae5b113af37a6241a8b45320d0b5fe7302403b122e
MD5 339975f5ed293a10ce2f47e418fbab16
BLAKE2b-256 63b71aa2f5eb5e434ce27c6cdc3ebcbdc9f1204d7b77c701bd2a9b7fc179fdd5

See more details on using hashes here.

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