1. Go to this page and download the library: Download hamoda-dev/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/ */
hamoda-dev / moyasar-php example snippets
use HamodaDev\Moyasar\Moyasar;
use HamodaDev\Moyasar\Invoice\DTO\CreateInvoiceDTO;
$moyasar = new Moyasar(
baseUrl: 'https://api.moyasar.com/v1',
apiKey: getenv('MOYASAR_SECRET_KEY'),
);
// Create an invoice and send the customer to the hosted payment page
$invoice = $moyasar->invoice()->create(new CreateInvoiceDTO(
amount: 2500, // 25.00 SAR — always in the smallest unit
currency: 'SAR',
description: 'Order #1234',
callbackUrl: 'https://example.com/webhooks/moyasar',
));
header("Location: {$invoice->url}");
use HamodaDev\Moyasar\Invoice\DTO\CreateInvoiceDTO;
$invoice = $moyasar->invoice()->create(new CreateInvoiceDTO(
amount: 2500,
currency: 'SAR',
description: 'Order #1234',
callbackUrl: 'https://example.com/webhooks/moyasar',
successUrl: 'https://example.com/payment/success',
backUrl: 'https://example.com/payment/cancel',
metadata: ['order_id' => '1234'],
));
echo $invoice->url; // Hosted payment page — redirect your user here
echo $invoice->id; // Store this alongside your order
echo $invoice->status; // "initiated" for a fresh invoice
$payment = $moyasar->payment()->refund('payment_12345', amount: 2500); // Refund 25.00 SAR
// Full capture
$payment = $moyasar->payment()->capture('payment_12345');
// Partial capture (e.g. only ship part of an order)
$payment = $moyasar->payment()->capture('payment_12345', amount: 5000);