PHP code example of iabduul7 / laravel-moyasar
1. Go to this page and download the library: Download iabduul7/laravel-moyasar 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/ */
iabduul7 / laravel-moyasar example snippets
use Iabduul7\Moyasar\Facades\Moyasar;
$payment = Moyasar::payments()->create([
'amount' => 10000, // halalas = 100.00 SAR
'currency' => 'SAR',
'description' => 'Order #123',
'callback_url' => route('checkout.callback'),
'source' => [
'type' => 'creditcard',
'name' => 'John Doe',
'number' => '4111111111111111',
'cvc' => '123',
'month' => 12,
'year' => 2030,
],
]);
// $payment is an Iabduul7\Moyasar\DataObjects\PaymentData (readonly DTO)
$payment->id;
$payment->status; // Iabduul7\Moyasar\Enums\PaymentStatus
$payment->source->company; // 'visa'
use Iabduul7\Moyasar\Enums\PaymentStatus;
use Iabduul7\Moyasar\Facades\Moyasar;
Moyasar::payments()->find('pay_123');
Moyasar::payments()->list(
page: 1,
perPage: 50,
filters: ['status' => PaymentStatus::Paid, 'metadata[order_id]' => '123'],
);
Moyasar::payments()->refund('pay_123'); // full refund
Moyasar::payments()->refund('pay_123', amount: 5000); // 50.00 SAR partial
Moyasar::payments()->capture('pay_123', amount: 10000); // capture an authorized payment
Moyasar::payments()->void('pay_123'); // void an authorized payment
use Illuminate\Support\Str;
Moyasar::payments()->create([
'given_id' => (string) Str::uuid(),
'amount' => 10000,
'currency' => 'SAR',
// ...
]);
use Iabduul7\Moyasar\Exceptions\ApiException;
try {
Moyasar::payments()->create([...]);
} catch (ApiException $e) {
$e->statusCode; // 400
$e->type; // 'invalid_request_error'
$e->errors; // ['amount' => ['must be at least 1']]
}
use Iabduul7\Moyasar\Exceptions\{
AuthenticationException, NotFoundException, RateLimitException, ServerErrorException,
ValidationException, CardDeclinedException, InsufficientFundsException,
ExpiredCardException, InvalidCardException,
};
try {
Moyasar::payments()->create([...]);
} catch (InsufficientFundsException $e) {
// ISO 8583 response code 51 — prompt the customer to try a different card
} catch (CardDeclinedException $e) {
// Any other declined card (response codes 04, 05, 41, 43, 62, 65)
} catch (AuthenticationException $e) {
// 401 — API key invalid or revoked
} catch (RateLimitException $e) {
// 429 — back off and retry
} catch (ApiException $e) {
// Catch-all
}
use Iabduul7\Moyasar\Money;
Money::sar(100.00)->amount; // 10000 (halalas)
Money::halalas(10000)->format(); // "100.00 SAR"
Money::fromMajor(10.500, 'KWD'); // 10500 minor (3-decimal currency)
$total = Money::sar(99.99)->plus(Money::sar(0.01)); // 100.00 SAR
$total->amount; // 10000
use Iabduul7\Moyasar\Facades\Moyasar;
$invoice = Moyasar::invoices()->create([
'amount' => 10000,
'currency' => 'SAR',
'description' => 'Invoice INV-001',
'callback_url' => route('checkout.callback'),
]);
$invoice->url; // hosted checkout page
Moyasar::invoices()->find($invoice->id);
Moyasar::invoices()->list(page: 1, perPage: 50);
Moyasar::invoices()->update($invoice->id, ['description' => 'Updated']);
Moyasar::invoices()->cancel($invoice->id);
$token = Moyasar::tokens()->create([
'name' => 'John Doe',
'number' => '4111111111111111',
'cvc' => '123',
'month' => '12',
'year' => '30',
'callback_url' => route('checkout.token-callback'),
]);
$token->id; // 'token_...'
$token->status; // TokenStatus enum
$token->lastFour; // '1111'
$token->verificationUrl; // 3DS URL when status === Initiated
// Use the token in a subsequent payment:
Moyasar::payments()->create([
'amount' => 10000,
'currency' => 'SAR',
'source' => ['type' => 'token', 'token' => $token->id],
]);
Moyasar::tokens()->find('token_...');
use Iabduul7\Moyasar\Enums\PayoutStatus;
$payout = Moyasar::payouts()->create([
'amount' => 100000, // 1000.00 SAR
'currency' => 'SAR',
'description' => 'Vendor settlement',
'beneficiary' => [
'name' => 'Vendor Co.',
'iban' => 'SA0380000000608010167519',
'type' => 'iban',
],
]);
Moyasar::payouts()->find($payout->id);
Moyasar::payouts()->list(page: 1, perPage: 50, filters: ['status' => PayoutStatus::Paid]);
use Iabduul7\Moyasar\Testing\MoyasarTestCards;
// Named constants instead of magic 16-digit literals:
MoyasarTestCards::VISA_SUCCESS;
MoyasarTestCards::VISA_DECLINE;
MoyasarTestCards::MADA_SUCCESS;
MoyasarTestCards::MASTERCARD_SUCCESS;
MoyasarTestCards::AMEX_SUCCESS;
// Ready-to-spread source arrays:
$source = MoyasarTestCards::source(
number: MoyasarTestCards::MADA_SUCCESS,
threeDs: true,
manual: true,
);
$stcPay = MoyasarTestCards::stcPaySource('0512345678');
use Iabduul7\Moyasar\Events\PaymentPaid;
use Illuminate\Support\Facades\Event;
Event::listen(function (PaymentPaid $event) {
$event->payment; // PaymentData DTO
$event->rawPayload; // raw webhook body, array
});
use Iabduul7\Moyasar\Concerns\HasMoyasarPayments;
class Order extends Model
{
use HasMoyasarPayments;
}
$order = Order::create([...]);
$record = $order->createMoyasarPayment([
'amount' => 10000,
'currency' => 'SAR',
'description' => "Order #{$order->id}",
'source' => [...],
]);
$record->moyasar_id;
$record->status; // PaymentStatus enum
$order->moyasarPayments(); // MorphMany<MoyasarPayment>
$order->latestMoyasarPayment();
$order->hasSuccessfulPayment(); // true if any payment is paid, captured, or authorized
use Iabduul7\Moyasar\Casts\MoyasarPaymentCast;
protected function casts(): array
{
return ['moyasar_payment_id' => MoyasarPaymentCast::class];
}
use Iabduul7\Moyasar\Facades\Moyasar;
use Illuminate\Support\Facades\Http;
Http::fake([
'api.moyasar.com/v1/payments' => Http::response([
'id' => 'pay_test',
'status' => 'paid',
'amount' => 10000,
'currency' => 'SAR',
// ...
], 201),
]);
$payment = Moyasar::payments()->create([...]);
expect($payment->isPaid())->toBeTrue();
return [
'secret_key' => env('MOYASAR_SECRET_KEY'),
'publishable_key' => env('MOYASAR_PUBLISHABLE_KEY'),
'webhook_secret' => env('MOYASAR_WEBHOOK_SECRET'),
'base_url' => env('MOYASAR_BASE_URL', 'https://api.moyasar.com/v1'),
'http' => [
'timeout' => env('MOYASAR_HTTP_TIMEOUT', 30),
'retry_times' => env('MOYASAR_HTTP_RETRY_TIMES', 3),
'retry_delay' => env('MOYASAR_HTTP_RETRY_DELAY', 200),
],
'webhooks' => [
'enabled' => env('MOYASAR_WEBHOOKS_ENABLED', true),
'route' => env('MOYASAR_WEBHOOK_ROUTE', 'moyasar/webhook'),
'middleware' => ['api'],
'replay_ttl' => env('MOYASAR_WEBHOOK_REPLAY_TTL', 86400),
'sync_eloquent' => env('MOYASAR_WEBHOOK_SYNC_ELOQUENT', true),
],
'default_currency' => env('MOYASAR_DEFAULT_CURRENCY', 'SAR'),
'logging' => [
'enabled' => env('MOYASAR_LOGGING_ENABLED', false),
'channel' => env('MOYASAR_LOG_CHANNEL', 'stack'),
],
];
bash
php artisan vendor:publish --tag=moyasar-config
php artisan vendor:publish --tag=moyasar-migrations
php artisan migrate