1. Go to this page and download the library: Download unipay/unipay-bd 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/ */
unipay / unipay-bd example snippets
use Unipay\BD\Facades\Payment;
use Unipay\BD\DTOs\PaymentRequest;
$paymentRequest = PaymentRequest::builder()
->amount(1500.00, 'BDT')
->invoice('INV-' . time())
->customer(
mobile: '01712345678',
email: '[email protected]',
name: 'Rahim Uddin'
)
->coupon('PROMO20', 100.00)
->build();
// Initiate with default or specific gateway
$response = Payment::driver('bkash')->createPayment($paymentRequest);
if ($response->isSuccessful() || $response->isPending()) {
// Redirect customer to payment portal
return redirect()->away($response->redirectUrl);
}
return back()->withErrors(['message' => $response->message]);
use Unipay\BD\Events\PaymentSucceeded;
use Unipay\BD\Events\PaymentFailed;
use Unipay\BD\Events\PaymentRefunded;
protected $listen = [
PaymentSucceeded::class => [
\App\Listeners\MarkOrderAsPaid::class,
],
PaymentFailed::class => [
\App\Listeners\HandleFailedPayment::class,
],
];
namespace App\Listeners;
use Unipay\BD\Events\PaymentSucceeded;
use App\Models\Order;
class MarkOrderAsPaid
{
public function handle(PaymentSucceeded $event)
{
$response = $event->response; // PaymentResponse DTO
$order = Order::where('invoice_id', $response->invoiceId)->first();
if ($order) {
$order->update([
'status' => 'paid',
'transaction_id' => $response->transactionId,
'gateway' => $response->gatewayName,
]);
}
}
}
use Unipay\BD\Facades\Payment;
use Unipay\BD\DTOs\RefundRequest;
$refundRequest = new RefundRequest(
paymentId: 'PAY_12345678',
transactionId: 'TRX_98765432',
amount: 500.00,
reason: 'Customer return request'
);
$response = Payment::driver('bkash')->refund($refundRequest);
if ($response->isSuccessful()) {
// Refund complete
$refundTrxId = $response->refundTransactionId;
}
use Unipay\BD\Facades\Payment;
$response = Payment::driver('nagad')->queryPayment('NAGAD_REF_1001');
if ($response->isSuccessful()) {
$trxId = $response->transactionId;
$amount = $response->amount;
}
use Unipay\BD\Facades\Payment;
public function boot()
{
Payment::resolveTenantConfigUsing(function ($tenant, string $driver) {
// Retrieve tenant's credentials from your database or encrypted settings
return [
'app_key' => $tenant->bkash_app_key,
'app_secret' => $tenant->bkash_app_secret,
'username' => $tenant->bkash_username,
'password' => $tenant->bkash_password,
];
});
}
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Unipay\BD\Traits\Billable;
class User extends Authenticatable
{
use Billable;
}
$user = auth()->user();
// Create a monthly Pro subscription with a 7-day trial
$subscription = $user->newSubscription('pro-plan', 999.00)
->monthly()
->trialDays(7)
->create();
// Check subscription status
if ($user->subscribed('pro-plan')) {
// User has an active or trialing subscription
}
// Check if user is currently in free trial
if ($subscription->onTrial()) {
$daysLeft = $subscription->trial_ends_at->diffInDays(now());
}
// Cancel or pause subscription
$subscription->cancel();
$subscription->pause();
$subscription->resume();
use Illuminate\Support\Facades\Schedule;
Schedule::command('unipay:billing:process')->dailyAt('00:01');