1. Go to this page and download the library: Download ht3aa/zain-cash library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ht3aa / zain-cash example snippets
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);
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);
$transaction = ZainCashTransaction::find(1);
$order = $transaction->order; // Returns the Order model instance
// 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');
}
}
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
}
}
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
}
use Ht3aa\ZainCash\Facades\ZainCash;
// Check transaction status from ZainCash
$response = ZainCash::checkTransaction('TRANS-123');
// Response
use Ht3aa\ZainCash\Facades\ZainCash;
// Initiate a transaction
$transaction = ZainCash::initiateTransaction($zainCashTransaction);
// Check transaction status
$response = ZainCash::checkTransaction('transaction_id');
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
]);
}
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();
}
}
$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
}