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
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();
}