PHP code example of fateel-tech / moyasar-php
1. Go to this page and download the library: Download fateel-tech/moyasar-php 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/ */
fateel-tech / moyasar-php example snippets
use Moyasar\Providers\PaymentService;
use Moyasar\Providers\InvoiceService;
use Moyasar\Facades\Moyasar;
// Fetch payment
$paymentService = new PaymentService();
$payment = $paymentService->fetch('payment_id');
// Create invoice
$invoiceService = new InvoiceService();
$invoice = $invoiceService->create(['amount' => 1000, 'currency' => 'SAR']);
// Basic Laravel facade
$payment = \Moyasar\Facades\Payment::fetch('id');
// Limited search capabilities
$search = \Moyasar\Search::query()->status('paid')->page(2);
$paginationResult = $paymentService->all($search);
// No tokenization support
// No webhook management
// No metadata search
use FateelTech\Moyasar\Laravel\Facades\Moyasar;
use FateelTech\Moyasar\Enums\Currency;
use FateelTech\Moyasar\Enums\WebhookEvent;
// Fetch payment
$payment = Moyasar::payments()->find('payment_id');
// Create invoice with enums
$invoice = Moyasar::invoices()->create(
amount: 1000,
currency: Currency::SAR
);
// NEW: Tokenization support
$payment = Moyasar::payments()->createWithToken(
token: 'token_abc123',
amount: 5000,
description: 'Payment with saved card'
);
// NEW: Webhook management
$webhook = Moyasar::webhooks()->create(
url: 'https://yourapp.com/webhooks',
events: [WebhookEvent::PAYMENT_PAID]
);
// NEW: Enhanced search with metadata filtering
$payments = Moyasar::payments()->list(
page: 1,
status: PaymentStatus::PAID,
metadata: ['order_id' => '12345'],
cardLastDigits: '1234',
createdGt: '2024-01-01'
);
// NEW: Artisan commands
php artisan moyasar:make-webhook
php artisan moyasar:webhook list
use FateelTech\Moyasar\MoyasarClient;
$moyasar = new MoyasarClient('sk_test_your_key');
// Create payment
$payment = $moyasar->payments()->createWithToken(
token: 'token_abc123',
amount: 5000,
description: 'Test payment'
);
// Find payment
$payment = $moyasar->payments()->find('pay_abc123');
// List payments
$payments = $moyasar->payments()->list();
use FateelTech\Moyasar\MoyasarClient;
class PaymentController
{
public function __construct(private MoyasarClient $moyasar) {}
public function showPayment(Request $request)
{
$payment = $this->moyasar->payments()->find($request->payment_id);
if ($payment->isCompleted()) {
// Process successful payment
}
}
}
use FateelTech\Moyasar\Laravel\Facades\Moyasar;
$payment = Moyasar::payments()->createWithToken(
token: 'token_abc123',
amount: 5000,
description: 'Order #1234'
);
use FateelTech\Moyasar\Enums\WebhookEvent;
$webhook = $moyasar->webhooks()->create(
httpMethod: 'post',
url: 'https://yourapp.com/api/webhooks/payment',
sharedSecret: 'your_webhook_secret',
events: [WebhookEvent::PAYMENT_PAID, WebhookEvent::PAYMENT_FAILED]
);
WebhookEvent::PAYMENT_PAID // ✅ Payment completed successfully
WebhookEvent::PAYMENT_FAILED // ❌ Payment failed
WebhookEvent::PAYMENT_AUTHORIZED // 🔒 Payment authorized (not captured)
WebhookEvent::PAYMENT_CAPTURED // 💰 Payment captured
WebhookEvent::PAYMENT_REFUNDED // 💸 Payment refunded
WebhookEvent::PAYMENT_VOIDED // ⚫ Payment voided
WebhookEvent::PAYMENT_ABANDONED // 🚪 Payment abandoned by customer
WebhookEvent::PAYMENT_CANCELED // ❌ Payment canceled
WebhookEvent::PAYMENT_EXPIRED // ⏰ Payment expired
WebhookEvent::PAYMENT_VERIFIED // ✔️ Payment verified
WebhookEvent::PAYOUT_PAID // 💳 Payout completed
WebhookEvent::PAYOUT_FAILED // ❌ Payout failed
WebhookEvent::BALANCE_TRANSFERRED // 🏦 Balance transferred to bank
$event = WebhookEvent::PAYMENT_PAID;
$event->isSuccessEvent(); // true
$event->isFailureEvent(); // false
$event->isPostPaymentEvent(); // false
$event->getDescription(); // "Payment completed successfully"
use FateelTech\Moyasar\Laravel\Facades\Moyasar;
// List all settlements
$settlements = Moyasar::settlements()->list();
// List with pagination and filters
$settlements = Moyasar::settlements()->list(
page: 2,
id: 'settlement_abc123',
createdGt: '2024-01-01',
createdLt: '2024-12-31'
);
// Access settlement data
foreach ($settlements->items as $settlement) {
echo "Settlement ID: " . $settlement->id . "\n";
echo "Amount: " . $settlement->amount . " " . $settlement->currency->value . "\n";
echo "Fee: " . $settlement->fee . "\n";
echo "Net Amount: " . $settlement->getNetAmount() . "\n";
if ($settlement->hasInvoice()) {
echo "Invoice URL: " . $settlement->invoiceUrl . "\n";
}
}
// Find specific settlement
$settlement = Moyasar::settlements()->find('settlement_abc123');
echo "Recipient Type: " . $settlement->recipientType . "\n";
echo "Settlement Count: " . $settlement->settlementCount . "\n";
echo "Created: " . $settlement->createdAt->format('Y-m-d H:i:s') . "\n";
// Find settlement and get its lines in one fluent chain
$settlement = Moyasar::settlements()->find('settlement_abc123');
$lines = $settlement->getLines();
// Or with pagination
$lines = $settlement->getLines(page: 2);
// Access line data
foreach ($lines->items as $line) {
echo "Payment ID: " . $line->paymentId . "\n";
echo "Type: " . $line->type . "\n";
echo "Amount: " . $line->amount . " " . $line->currency->value . "\n";
echo "Fee: " . $line->fee . "\n";
echo "Net Amount: " . $line->getNetAmount() . "\n";
if ($line->hasMetadata()) {
echo "Metadata: " . json_encode($line->metadata) . "\n";
}
if ($line->source) {
echo "Payment Source: " . $line->source->type->value . "\n";
}
}
// Alternative: Direct access to settlement lines
$lines = Moyasar::settlements()->listLines('settlement_abc123');
// With pagination
$lines = Moyasar::settlements()->listLines('settlement_abc123', page: 2);
$settlement = Moyasar::settlements()->find('settlement_abc123');
// Check available resources
if ($settlement->hasInvoice()) {
$invoiceUrl = $settlement->invoiceUrl;
}
if ($settlement->hasCsvList()) {
$csvUrl = $settlement->csvListUrl;
}
if ($settlement->hasPdfList()) {
$pdfUrl = $settlement->pdfListUrl;
}
// Calculate net amount after fees and taxes
$netAmount = $settlement->getNetAmount();
$payment = $moyasar->payments()->createWithToken(
token: $request->payment_token,
amount: $order->total_in_halalas,
description: "Order #{$order->id}",
metadata: ['order_id' => $order->id]
);
class ProcessSubscriptionBilling implements ShouldQueue
{
public function handle(MoyasarClient $moyasar)
{
Subscription::due()->chunk(100, function ($subscriptions) use ($moyasar) {
foreach ($subscriptions as $subscription) {
$moyasar->payments()->createWithToken(
token: $subscription->payment_token,
amount: $subscription->plan->price,
metadata: ['subscription_id' => $subscription->id]
);
}
});
}
}
bash
composer bash
# Publish webhook request classes for custom validation
php artisan vendor:publish --tag=moyasar-requests