承接 ht3aa/zain-cash 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ht3aa/zain-cash

Composer 安装命令:

composer require ht3aa/zain-cash

包简介

This is a laravel package that provide all the functionality that you need to integrate your app with zain cash gateway api

README 文档

README

Latest Version on Packagist Total Downloads

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.

ZainCash Delivery Integration For Laravel

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:

composer require ht3aa/zain-cash

Publish the configuration file:

php artisan vendor:publish --tag="zain-cash-config"

Publish and run the migrations:

php artisan vendor:publish --tag="zain-cash-migrations"
php artisan migrate

Configuration

Add the following environment variables to your .env file:

ZAIN_CASH_IS_PRODUCTION=false
ZAIN_CASH_MERCHANT_ID=your_merchant_id
ZAIN_CASH_MERCHANT_SECRET=your_merchant_secret
ZAIN_CASH_MSISDN=your_msisdn
ZAIN_CASH_WEBHOOK_URL="${APP_URL}/api/zain-cash/webhook"
ZAIN_CASH_CUSTOM_WEBHOOK_URL=

Configuration Options

  • ZAIN_CASH_IS_PRODUCTION: Set to true for production environment, false for testing
  • ZAIN_CASH_MERCHANT_ID: Your ZainCash merchant ID
  • ZAIN_CASH_MERCHANT_SECRET: Your ZainCash merchant secret key
  • ZAIN_CASH_MSISDN: Your registered mobile number with ZainCash
  • ZAIN_CASH_WEBHOOK_URL: The URL where ZainCash will send payment status updates
  • ZAIN_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)

use Ht3aa\ZainCash\Models\ZainCashTransaction;
use Ht3aa\ZainCash\Facades\ZainCash;

// Create a new transaction with simple order ID
$transaction = ZainCashTransaction::create([
    'amount' => 10000, // Amount in IQD (e.g., 10000 IQD)
    'service_type' => 'Product Purchase',
    'order_id' => 'ORDER-' . uniqid(),
    'redirect_url' => route('payment.callback'),
]);

// Initiate the transaction with ZainCash
$transaction = ZainCash::initiateTransaction($transaction);
$transaction->save();

// Redirect user to payment page
return redirect($transaction->payment_redirect_url);

Advanced Usage (Polymorphic Relationship)

Link the transaction directly to your Order model using polymorphic relationships:

use App\Models\Order;
use Ht3aa\ZainCash\Models\ZainCashTransaction;
use Ht3aa\ZainCash\Facades\ZainCash;

// Create an order first
$order = Order::create([
    'user_id' => auth()->id(),
    'total' => 10000,
    // ... other order fields
]);

// Create a transaction linked to the order
$transaction = ZainCashTransaction::create([
    'amount' => $order->total,
    'service_type' => 'Product Purchase',
    'order_id' => $order->id,
    'order_type' => Order::class,
    'redirect_url' => route('payment.callback'),
]);

// Initiate the transaction with ZainCash
$transaction = ZainCash::initiateTransaction($transaction);
$transaction->save();

// Redirect user to payment page
return redirect($transaction->payment_redirect_url);

Now you can access the order from the transaction:

$transaction = ZainCashTransaction::find(1);
$order = $transaction->order; // Returns the Order model instance

Transaction Model

The ZainCashTransaction model includes the following attributes:

  • amount: Payment amount in IQD
  • service_type: Description of the service/product
  • order_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 payment
  • token: JWT token for the transaction
  • iat: Token issued at timestamp
  • exp: Token expiration timestamp
  • zain_cash_response: Full response from ZainCash API
  • status: Transaction status (pending, completed, failed, etc.)
  • payment_redirect_url: URL to redirect user for payment
  • transaction_id: ZainCash transaction ID

Polymorphic Relationship

The model includes a polymorphic order() relationship that allows you to link transactions to any order model:

// Access the related order
$transaction = ZainCashTransaction::find(1);
$order = $transaction->order;

// Define inverse relationship in your Order model
class Order extends Model
{
    public function zainCashTransactions()
    {
        return $this->morphMany(ZainCashTransaction::class, 'order');
    }
}

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:

  1. Decode the JWT token from ZainCash
  2. Update the transaction status in your database
  3. (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:

ZAIN_CASH_CUSTOM_WEBHOOK_URL=https://yourapp.com/api/payment-notification

The package will POST to this URL with the following payload:

{
    "zain_cash_transaction_id": 123
}

You can then create your own controller to handle this:

use Ht3aa\ZainCash\Models\ZainCashTransaction;

public function handlePaymentNotification(Request $request)
{
    $transaction = ZainCashTransaction::find($request->zain_cash_transaction_id);
    
    if ($transaction->status === 'success') {
        // Payment successful - update your order, send confirmation email, etc.
    } else {
        // Payment failed - handle accordingly
    }
}

Checking Transaction Status

From Local Database

You can check the status of a transaction from your local database:

use Ht3aa\ZainCash\Models\ZainCashTransaction;

$transaction = ZainCashTransaction::where('transaction_id', 'TRANS-123')->first();

if ($transaction->status === 'success') {
    // Payment completed
} elseif ($transaction->status === 'pending') {
    // Payment still pending
} else {
    // Payment failed or cancelled
}

From ZainCash API

You can also verify the transaction status directly from ZainCash API:

use Ht3aa\ZainCash\Facades\ZainCash;

// Check transaction status from ZainCash
$response = ZainCash::checkTransaction('TRANS-123');

// Response includes:
// - id: Transaction ID
// - status: Current status
// - amount: Transaction amount
// - orderId: Your order ID
// ... other transaction details

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:

use Ht3aa\ZainCash\Facades\ZainCash;

// Initiate a transaction
$transaction = ZainCash::initiateTransaction($zainCashTransaction);

// Check transaction status
$response = ZainCash::checkTransaction('transaction_id');

Complete Example Flow

Here's a complete example of a payment flow with polymorphic relationships:

use App\Models\Order;
use Ht3aa\ZainCash\Models\ZainCashTransaction;
use Ht3aa\ZainCash\Facades\ZainCash;
use Illuminate\Support\Facades\Mail;

// 1. Create order and initiate payment
public function initiatePayment(Request $request)
{
    // Create the order
    $order = Order::create([
        'user_id' => auth()->id(),
        'total' => $request->amount,
        'status' => 'pending',
    ]);

    // Create transaction linked to order
    $transaction = ZainCashTransaction::create([
        'amount' => $order->total,
        'service_type' => 'Product Purchase',
        'order_id' => $order->id,
        'order_type' => Order::class,
        'redirect_url' => route('payment.callback'),
    ]);

    // Initiate with ZainCash
    $transaction = ZainCash::initiateTransaction($transaction);
    $transaction->save();

    // Redirect user to payment page
    return redirect($transaction->payment_redirect_url);
}

// 2. Handle callback (optional - webhook handles status updates automatically)
public function paymentCallback(Request $request)
{
    return view('payment.processing', [
        'message' => 'Processing your payment...'
    ]);
}

// 3. Handle custom webhook notification
public function handlePaymentNotification(Request $request)
{
    $transaction = ZainCashTransaction::with('order')
        ->find($request->zain_cash_transaction_id);
    
    if ($transaction->status === 'success') {
        // Update order status using the polymorphic relationship
        $order = $transaction->order;
        $order->update([
            'status' => 'paid',
            'payment_status' => 'completed'
        ]);
            
        // Send confirmation email
        Mail::to($order->user)->send(new PaymentConfirmation($transaction));
        
        // Optional: Verify with ZainCash API
        $verification = ZainCash::checkTransaction($transaction->transaction_id);
        
        if ($verification['status'] === 'success') {
            // Process order fulfillment
        }
    } else {
        // Handle failed payment
        $transaction->order->update(['status' => 'payment_failed']);
    }
    
    return response()->json(['message' => 'Notification received']);
}

// 4. Check payment status (e.g., from admin panel)
public function checkPaymentStatus($transactionId)
{
    $transaction = ZainCashTransaction::where('transaction_id', $transactionId)->first();
    
    // Verify with ZainCash API
    $response = ZainCash::checkTransaction($transactionId);
    
    // Update local status if different
    if ($response['status'] !== $transaction->status) {
        $transaction->update(['status' => $response['status']]);
    }
    
    return response()->json([
        'local_status' => $transaction->status,
        'zaincash_status' => $response['status'],
        'order' => $transaction->order
    ]);
}

Setting Up Your Order Model

To use polymorphic relationships, add the inverse relationship to your Order model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ht3aa\ZainCash\Models\ZainCashTransaction;

class Order extends Model
{
    protected $fillable = ['user_id', 'total', 'status', 'payment_status'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function zainCashTransactions()
    {
        return $this->morphMany(ZainCashTransaction::class, 'order');
    }
    
    public function latestZainCashTransaction()
    {
        return $this->morphOne(ZainCashTransaction::class, 'order')->latestOfMany();
    }
}

Now you can easily access transactions from orders:

$order = Order::find(1);

// Get all ZainCash transactions for this order
$transactions = $order->zainCashTransactions;

// Get the latest transaction
$latestTransaction = $order->latestZainCashTransaction;

// Check payment status
if ($latestTransaction->status === 'success') {
    // Order is paid
}

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

License

The MIT License (MIT). Please see License File for more information.

ht3aa/zain-cash 适用场景与选型建议

ht3aa/zain-cash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「ht3aa」 「zain-cash」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ht3aa/zain-cash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ht3aa/zain-cash 我们能提供哪些服务?
定制开发 / 二次开发

基于 ht3aa/zain-cash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-02