Skip to main content

a Python package for integrating the ZarinPal payment gateway.

Project description

ZarinPal Python Payment Gateway Integration

This Python package provides a simple interface for integrating ZarinPal's payment gateway into your Python applications. It supports both sandbox and production environments, enabling you to test your payment functionality before going live.


Features

  • Payment Request: Send payment requests to ZarinPal's API.
  • Payment Verification: Verify payment transactions to ensure success.
  • Generate Payment URL: Generate a URL for users to complete their payment.
  • Sandbox & Production Modes: Switch between sandbox for testing and the production environment for live payments.

Installation

To install the package, run:

pip install zarinpal-payment

Usage

Here is how to use the package to handle payment requests, verification, and generate payment URLs.

1. Initialize ZarinPal Client

Create an instance of the ZarinPal class by passing your merchant ID, callback URL, and optionally, set the sandbox environment (True for testing, False for production):

from zarinpal_payment.zarinpal import ZarinPal

zarinpal = ZarinPal(merchant_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", callback_url="Your callback URL", sandbox=True)

2. Request a Payment

Use the payment_request method to initiate a payment request. Pass the amount and description (optional):

response = zarinpal.payment_request(amount=1000, description="Test payment")

3. Get the Authority

Extract the payment authority from the response to generate the payment URL:

authority = response.get("data", {}).get("authority")

4. Generate Payment URL

Use the generate_payment_url method to create a URL that will redirect users to the ZarinPal payment page:

payment_url = zarinpal.generate_payment_url(authority)

5. Verify the Payment

After the user completes the payment, verify the payment using the payment_verify method:

verify_response = zarinpal.payment_verify(amount=1000, authority=authority)

Example Code

from zarinpal_payment.zarinpal import ZarinPal

# Create an instance of ZarinPal
# you can use any string but must be greater than 36 characters
zarinpal = ZarinPal(merchant_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", callback_url="Your callback URL", sandbox=True)

# Step 1: Request a payment
response = zarinpal.payment_request(amount=1000, description="Test payment")

# Step 2: Get the authority from the response
authority = response.get("data", {}).get("authority")

# Step 3: Generate the payment URL
payment_url = zarinpal.generate_payment_url(authority)

# Step 4: Verify the payment
verify_response = zarinpal.payment_verify(amount=1000, authority=authority)

# Print the payment URL and verification response
print(payment_url)
print(verify_response)

Configuration

  • merchant_id: Your ZarinPal merchant ID.
  • callback_url: The URL to which ZarinPal will redirect after the payment attempt.
  • sandbox: Set this to True for testing in the sandbox environment or False for the live environment.

Logging

The package uses Python's built-in logging module to log requests and responses. You can adjust the logging level to suit your needs.


References

For more details about ZarinPal's API, please refer to the official documentation:


License

This project is licensed under the MIT License - see the LICENSE file for details.

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

zarinpal_payment-0.0.7.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

zarinpal_payment-0.0.7-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file zarinpal_payment-0.0.7.tar.gz.

File metadata

  • Download URL: zarinpal_payment-0.0.7.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for zarinpal_payment-0.0.7.tar.gz
Algorithm Hash digest
SHA256 9e2698078c314652583c85891ebbfafc47ffb93b3aab613d2d96e8b96aa42339
MD5 f2a9e326394da4977074a3fcb7ed5854
BLAKE2b-256 001264644b2c236d1d49f38ad82d4f9eb914d18521bb1c241f91c8eaaef80706

See more details on using hashes here.

File details

Details for the file zarinpal_payment-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for zarinpal_payment-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c5f4276c149fcea959f8721aa4bcc7be366a3d06e90e5863fa7d37eeeee102df
MD5 1e1619e47434be45b8b088e5d152caf4
BLAKE2b-256 90e0bd9b14b3c248374e21de95d52ed22e387900dee3cb9f028c0515cb04efc1

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