https://github.com/alkhatibdev/laravel-zain
Zain DSP API integration with Laravel, was made to simplify the process and API calls and let developers focus on other integration parts and logic.
https://github.com/alkhatibdev/laravel-zain
laravel php
Last synced: 9 months ago
JSON representation
Zain DSP API integration with Laravel, was made to simplify the process and API calls and let developers focus on other integration parts and logic.
- Host: GitHub
- URL: https://github.com/alkhatibdev/laravel-zain
- Owner: alkhatibdev
- License: mit
- Created: 2022-07-13T14:36:49.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-12T07:58:56.000Z (almost 4 years ago)
- Last Synced: 2025-03-02T00:12:12.337Z (over 1 year ago)
- Topics: laravel, php
- Language: PHP
- Homepage: https://github.com/alkhatibdev/laravel-zain
- Size: 167 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Zain DSP API integration with Laravel
[](https://github.com/alkhatibdev/laravel-zain/releases)
[](LICENSE)
# Introduction
Laravel Zain is Zain DSP API integration with Laravel, made to simplify the process and API calls and let developers focus on other integration parts and logic. See also [Laravel Sudani](https://github.com/alkhatibdev/laravel-sudani).
# Installation
## Requirements
- PHP >= `7.4.x`
- Laravel >= `7.x`
## install via composer
```shell
composer require alkhatibdev/laravel-zain
```
## Publish Configs
```shell
php artisan vendor:publish --tag=laravel-zain-config
```
A `laravel-zain.php` config file will be published on your `configs` directory, with the following content:
```php
env('ZAIN_SERVER_BASE_API_URL'),
'product_code' => env('ZAIN_PRODUCT_CODE'),
'username' => env('ZAIN_USERNAME'),
'password' => env('ZAIN_PASSWORD'),
'remember_token' => env('ZAIN_REMEMBER_TOKEN', false),
'enable_logging' => false,
];
```
Don't forget to set all these variable on your `.env` file
```env
ZAIN_SERVER_BASE_API_URL=https://test.zaindsp.com:3030/api/v1/json/
ZAIN_PRODUCT_CODE=xxxxxx
ZAIN_USERNAME=xxxxx
ZAIN_PASSWORD=xxxxx
ZAIN_REMEMBER_TOKEN=false
```
# Usage
## Initial Payment/Subscription
```php
use AlkhatibDev\LaravelZain\Facades\Zain;
// Initiate payment request
$response = Zain::initiate($phone)
```
When `initiate` payment request successfully sent, a SMS with `OTP` code will be send to the `$phone` number, and `$response` will contain a `subscribe_request_id` and you should save it to the next step `verify`.
## Verify Payment/Subscription
```php
$response = Zain::verify($otp, $requestId)
```
## Check Subscription
```php
$response = Zain::checkSubscription($phone)
```
## Unsubscribe
```php
$response = Zain::unsubscribe($phone)
// cacheToken($response['token'])
```
## Login and Cache DSP token
Out of the box the package will login automatically and get the `token` and use it for each action `initiate`, `verify` ..etc per request.
If you want to cache the token and use it for furthor requests, you can request `token` like this:
```php
$token = Zain::token()
```
And you can cache it and use it for each request for the next 24 hours.
If you set `ZAIN_REMEMBER_TOKEN=true` token will be expired after 30 days.
Example of using cached token:
```php
// $token = getCachedToken()
$response = Zain::withToken($token)->initiate($phone)
$response = Zain::withToken($token)->verify($phone)
...
```
## Logging
You can enable logging from package config file
```
'enable_logging' => true,
```
# Other Packages
- ### [Laravel Sudani](https://github.com/alkhatibdev/laravel-sudani) SPay API Integration
# License
Laravel Zain is open-sourced software licensed under the [MIT license](LICENSE).