Download the PHP package ht3aa/zain-cash without Composer
On this page you can find all versions of the php package ht3aa/zain-cash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ht3aa/zain-cash
More information about ht3aa/zain-cash
Files in ht3aa/zain-cash
Package zain-cash
Short Description This is a laravel package that provide all the functionality that you need to integrate your app with zain cash gateway api
License MIT
Homepage https://github.com/ht3aa/zain-cash
Informations about the package zain-cash
ZainCash Integration For Laravel
A comprehensive Laravel package for integrating ZainCash payment gateway into your application. This package provides a simple and elegant way to handle payment transactions, webhooks, and transaction tracking with ZainCash.
Features
- ✅ Easy integration with ZainCash payment gateway
- ✅ Automatic transaction tracking and management
- ✅ Polymorphic relationships - link transactions to any order model
- ✅ Webhook handling for payment status updates
- ✅ Check transaction status with ZainCash API
- ✅ Support for both production and test environments
- ✅ JWT token generation and validation
- ✅ Database transaction logging
- ✅ Custom webhook URL support
- ✅ Built-in error handling and logging
Installation
You can install the package via composer:
Publish the configuration file:
Publish and run the migrations:
Configuration
Add the following environment variables to your .env file:
Configuration Options
ZAIN_CASH_IS_PRODUCTION: Set totruefor production environment,falsefor testingZAIN_CASH_MERCHANT_ID: Your ZainCash merchant IDZAIN_CASH_MERCHANT_SECRET: Your ZainCash merchant secret keyZAIN_CASH_MSISDN: Your registered mobile number with ZainCashZAIN_CASH_WEBHOOK_URL: The URL where ZainCash will send payment status updatesZAIN_CASH_CUSTOM_WEBHOOK_URL: (Optional) Custom URL to receive payment notifications in your app
Usage
Creating a Payment Transaction
To initiate a payment transaction, create a new ZainCashTransaction instance and pass it to the initiateTransaction method.
Basic Usage (Simple Order ID)
Advanced Usage (Polymorphic Relationship)
Link the transaction directly to your Order model using polymorphic relationships:
Now you can access the order from the transaction:
Transaction Model
The ZainCashTransaction model includes the following attributes:
amount: Payment amount in IQDservice_type: Description of the service/productorder_id: Unique order identifier (can be string or model ID for polymorphic relationship)order_type: Model class name for polymorphic relationship (e.g.,App\Models\Order)redirect_url: URL to redirect after paymenttoken: JWT token for the transactioniat: Token issued at timestampexp: Token expiration timestampzain_cash_response: Full response from ZainCash APIstatus: Transaction status (pending, completed, failed, etc.)payment_redirect_url: URL to redirect user for paymenttransaction_id: ZainCash transaction ID
Polymorphic Relationship
The model includes a polymorphic order() relationship that allows you to link transactions to any order model:
Webhook Handling
The package automatically registers a webhook route at /api/zain-cash/webhook to handle payment status updates from ZainCash.
When a payment is completed, ZainCash will send a callback to this URL with the transaction status. The webhook controller will:
- Decode the JWT token from ZainCash
- Update the transaction status in your database
- (Optional) Forward the notification to your custom webhook URL
Custom Webhook Notifications
If you want to receive notifications in your own application when a payment status is updated, set the ZAIN_CASH_CUSTOM_WEBHOOK_URL in your .env file:
The package will POST to this URL with the following payload:
You can then create your own controller to handle this:
Checking Transaction Status
From Local Database
You can check the status of a transaction from your local database:
From ZainCash API
You can also verify the transaction status directly from ZainCash API:
This is useful for:
- Verifying payment status before fulfilling orders
- Reconciling payments with ZainCash
- Handling edge cases where webhook might have failed
Using the Facade
The package provides a facade for easy access to all methods:
Complete Example Flow
Here's a complete example of a payment flow with polymorphic relationships:
Setting Up Your Order Model
To use polymorphic relationships, add the inverse relationship to your Order model:
Now you can easily access transactions from orders:
Testing
The package includes test credentials for the ZainCash sandbox environment. Make sure ZAIN_CASH_IS_PRODUCTION is set to false when testing.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Hasan Tahseen
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of zain-cash with dependencies
firebase/php-jwt Version ^6.11
illuminate/contracts Version ^11.0||^12.0
spatie/laravel-package-tools Version ^1.16