PHP code example of kahil-raghed / masarat-ly-php-sdk

1. Go to this page and download the library: Download kahil-raghed/masarat-ly-php-sdk 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/ */

    

kahil-raghed / masarat-ly-php-sdk example snippets


use KahilRaghed\MasaratLy\MasaratLy;
use KahilRaghed\MasaratLy\ApiResponse;

$masarat = new MasaratLy('https://api.masarat.ly');

// Sign in
$response = $masarat->signIn(
    userId: 'your_user_id',
    pin: 'your_pin',
    providerId: 'your_provider_id',
    authUserType: 'merchant'
);

if ($response->success()) {
    // Cache token and expiry
    $tokenValidTo = $response->content['validTo'];
    $token = $response->content['value'];
    $masarat->setToken($token);
} else {
    echo "Sign in failed: " . $response->message;
    return;
}

// Open payment session
$sessionResponse = $masarat->openSession(
    amount: 100.00,
    identityCard: '123456789',
    transactionId: 'ORDER-12345',
    onlineOperation: 1 // 1 = Sell, 2 = Refund
);

if ($sessionResponse->success()) {
    // Session token and expiry
    $sessionTokenValidTo = $sessionResponse->content['validTo'];
    $sessionToken = $sessionResponse->content['value'];
} else {
    echo "Session failed: " . $sessionResponse->message;
    return;
}

// Complete transaction with OTP
$result = $masarat->completeSession(
    sessionToken: $sessionToken,
    otp: '123456'
);

if ($result->success()) {
    echo "Payment successful!";
} else {
    echo "Payment failed: " . $result->message;
}