PHP code example of ahmedlaggoun / moyasar

1. Go to this page and download the library: Download ahmedlaggoun/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/ */

    

ahmedlaggoun / moyasar example snippets


use AhmedLaggoun\Moyasar\Application\Moyasar;
use AhmedLaggoun\Moyasar\Domain\Exceptions\PaymentVerificationException;

public function return(Request $request, Moyasar $moyasar)
{
    $order = Order::findOrFail($request->session()->get('order_id'));

    try {
        $payment = $moyasar->verify($request->query('id'), $order->total());
    } catch (PaymentVerificationException $e) {
        report($e);

        return redirect()->route('checkout.failed');
    }

    $order->markPaid($payment->id);

    return redirect()->route('orders.show', $order);
}

use AhmedLaggoun\Moyasar\Domain\Data\Money;

Money::sar(10000);                        // SAR 100.00
Money::fromDecimalString('19.99');        // 1999 halalas
Money::fromDecimalString('1.500', 'KWD'); // KWD uses 3 decimals

use AhmedLaggoun\Moyasar\Application\Events\MoyasarPaymentUpdated;

Event::listen(MoyasarPaymentUpdated::class, function (MoyasarPaymentUpdated $event) {
    if (! $event->verified) {
        return;
    }

    $order = Order::where('payment_id', $event->payment->id)->first();

    $order?->markPaid();
});

$moyasar->capture($paymentId);                   // full
$moyasar->capture($paymentId, Money::sar(3000)); // partial
$moyasar->void($paymentId);
$moyasar->refund($paymentId, Money::sar(2000));

$moyasar->charge(
    tokenId: $user->card_token,
    amount: Money::sar(4900),
    idempotencyKey: "sub-{$subscription->id}-{$period->format('Y-m')}",
    description: 'Monthly subscription',
);

$moyasar->forgetToken($user->card_token);
blade
<script>
  Moyasar.init({
    element: '.mysr-form',
    amount: {{ $order->total()->minor }},
    currency: '{{ $moyasar->currency() }}',
    publishable_api_key: '{{ $moyasar->publishableKey() }}',
    callback_url: '{{ route('checkout.return') }}',
  });
</script>