PHP code example of php-monsters / laravel-online-payment

1. Go to this page and download the library: Download php-monsters/laravel-online-payment 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/ */

    

php-monsters / laravel-online-payment example snippets


 $transaction = $order->createTransaction(Bank::MELLAT);
 $form = $transaction->generateForm();

 $transaction = Larapay::verifyTransaction($request);
 //if the gateway supports reverse method
 $transaction->reverseTransaction();
 $order = $transaction->model;

 $allTransactions = $order->transations;
 $accomplishedTransactions = $order->accomplishedTransactions;
 $isPaid = $order->isPaid();
 $paidAmount = $order->paidAmount();

PhpMonsters\Larapay\LarapayServiceProvider::class,
PhpMonsters\Log\XLogServiceProvider::class,

'Larapay' => PhpMonsters\Larapay\Facades\Larapay::class,
'XLog'    => PhpMonsters\Log\Facades\XLog::class,

Route::post('payment/callback', 'YourController@handleCallback')->name('payment.callback');

use PhpMonsters\Larapay\Payable;

class Order extends Model 
{
    use Payable;

    public function getAmount(){
        return intval($this->amount) * 10;
    }   

}

use PhpMonsters\Larapay\Models\Enum\Bank;

class BankController extends Controller
{
    public function index()
    {
        //your logic and prepare your order
        // ...

        //if you implement getAmount() method you can set amount to null
        $amount = 1200000;  //Rial at least 1000 
        //order or user description
        $description = 'I pay my order with Larapay <3';
        //some additional data that you need store on transaction
        $additionalData = [];
        //create transaction 
        $transaction = $order->createTransaction(Bank::MELLAT, $amount, $description, $additionalData);
        
        //auto submit bank form and transfer user to gateway
        $autoSubmit = true;
        //callback route name. if you set it on your .env file you can set this to null
        $callbackRouteName = 'payment.callback';
        //adapter config
        $adapterConfig = [];
        //generate bank form
        $form = $transaction->generateForm($autoSubmit, $callbackRouteName, $adapterConfig);
        
        return view('go-to-bank',[
            'form' => $form,
        ]);
    }
}

<div>
    {!! $form !!}
</div>

use Illuminate\Http\Request;
use PhpMonsters\Larapay\Facades\Larapay;

class YourController extends Controller
{
    public function handleCallback(Request $request)
    {
         try{
            $adapterConfig = [];
            $transaction = Larapay::verifyTransaction($request, $adapterConfig);
            $order = $transaction->model;
            //transaction done. payment is successful         
         } catch (\Exception $e){
            // transaction not complete!!!
            // show error to your user
         }
    }
}

$transaction->reverseTransaction();

use PhpMonsters\Larapay\Models\LarapayTransaction;

public function getTransaction($transactionId){

    //find single transaction by transaction id
    $transaction = LarapayTransaction::find($transactionId);
    
    //get all accomplished transaction
    $accomplishedTransactions = LarapayTransaction::where('accomplished',true)->get();
    
    //get all reversed transaction
    $reversedTransactions = LarapayTransaction::where('reversed',true)->get();
}
bash
php artisan vendor:publish --provider="PhpMonsters\Larapay\LarapayServiceProvider"
bash
php artisan migrate
ini
LARAPAY_MODE=development

SAMAN_MERCHANT_ID=bmcf****
SAMAN_MERCHANT_PASS=98221***

MELLAT_USERNAME=user***
MELLAT_PASSWORD=80714***
MELLAT_TERMINAL_ID=747
ini
LARAPAY_PAYMENT_CALLBACK=payment.callback