PHP code example of skrskr / paymob

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

    

skrskr / paymob example snippets


composer 

php artisan vendor:publish --tag=config --provider="Skrskr\Paymob\PaymobServiceProvider"




use Illuminate\Support\Facades\Route;
use Skrskr\Paymob\Facades\Paymob;


Route::get('/test', function () {

    $orderData = [
        "amount_cents"=> "100", // / 
            "last_name" => "Sakr", // "street" => "NA", //optional
            "building" => "NA",  // optional
            "shipping_method" => "NA", // optional
            "postal_code" => "NA",  //optional
            "city" => "NA",  // optional
            "country" => "NA", // optional
            "state" => "NA" // optional
        ],
    ];

    // Get payment iframe URL
    $iframeUrl = Paymob::pay($orderData);
    return $iframeUrl;
    //redirect to ifram url
    // return redirect()->to($iframeUrl);
});


 
# Events:
- Skrskr\Paymob\Events\TransactionSuccessedEvent::class
- Skrskr\Paymob\Events\TransactionFailedEvent::class

# Create two listeners for each event 
# run two commands
php artisan make:listener PaymobTransactionSuccessedListener
php artisan make:listener PaymobTransactionFailedListener




namespace App\Listeners;

use Skrskr\Paymob\Events\TransactionSuccessedEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class PaymobTransactionSuccessedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Skrskr\Paymob\Events\TransactionSuccessedEvent  $event
     * @return void
     */
    public function handle(TransactionSuccessedEvent $event)
    {
        \Log::info($event->payload);
    }
}




namespace App\Listeners;

use Skrskr\Paymob\Events\TransactionFailedEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class PaymobTransactionFailedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Skrskr\Paymob\Events\TransactionFailedEvent  $event
     * @return void
     */
    public function handle(TransactionFailedEvent $event)
    {
        \Log::info($event->payload);
    }
}




namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Skrskr\Paymob\Events\TransactionFailedEvent;
use Skrskr\Paymob\Events\TransactionSuccessedEvent;

use App\Listeners\PaymobTransactionSuccessedListener;
use App\Listeners\PaymobTransactionFailedListener;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [        
        TransactionSuccessedEvent::class => [
            PaymobTransactionSuccessedListener::class
        ],
        
        TransactionFailedEvent::class => [
            PaymobTransactionFailedListener::class
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}