laravel-bale-wallet maintained by medicoassassino
Laravel Bale E-Wallet (medicoassassino/laravel-bale-wallet)
A modern Laravel 12+ (PHP 8.3+) Composer package for integrating Bale Messenger E-Wallet & In-App Payments (sendInvoice, createInvoiceLink, answerPreCheckoutQuery, inquireTransaction, and Webhooks) based on the official Bale Bot API documentation.
English Documentation
Features
- PHP 8.3+ & Laravel 12+: Built with
readonlyDTOs, backed Enums, and Laravel's HTTP Client. - Full Bale Wallet Payment Flow:
sendInvoice,createInvoiceLink(Mini Apps),answerPreCheckoutQuery, andinquireTransaction. - Rial & Toman Helpers: Native
IRRsupport withLabeledPrice::fromRial()andLabeledPrice::fromToman(). - Multi-Bot Runtime Support: Switch bot/provider tokens dynamically via
BaleWallet::forBot($botToken, $providerToken). - Sandbox Ready: Built-in support for Bale's official test token (
WALLET-TEST-1111111111111111). - Webhook & Events: Optional webhook route with secret verification, automatic
inquireTransactionverification, andPreCheckoutReceived/PaymentSucceededevents. - Optional Database Persistence: Stateless by default, with an optional publishable migration and
BaleWalletTransactionmodel for logging and webhook idempotency. - Testing Friendly: Includes
BaleWallet::fake()with expressive assertions.
Installation
composer require medicoassassino/laravel-bale-wallet
Publish the configuration file:
php artisan vendor:publish --tag="bale-wallet-config"
(Optional) Publish the migration if you want database logging and webhook idempotency:
php artisan vendor:publish --tag="bale-wallet-migrations"
php artisan migrate
Configuration
Add the following variables to your .env file:
BALE_BOT_TOKEN=your-bot-token-from-botfather
BALE_WALLET_PROVIDER_TOKEN=your-wallet-provider-token
BALE_WALLET_SANDBOX=false
BALE_WALLET_WEBHOOK_ENABLED=true
BALE_WALLET_WEBHOOK_SECRET=your-optional-webhook-secret
BALE_WALLET_AUTO_INQUIRE=true
BALE_WALLET_DB_ENABLED=false
Usage
1. Send an Invoice to a Chat
use Bale\Wallet\Data\InvoiceRequest;
use Bale\Wallet\Data\LabeledPrice;
use Bale\Wallet\Facades\BaleWallet;
$invoice = new InvoiceRequest(
title: 'Pro Subscription',
description: '1-month access to all features',
payload: json_encode(['order_id' => 1042]),
prices: [
LabeledPrice::fromToman('Monthly Plan', 50_000), // automatically converted to 500,000 IRR
],
);
$response = BaleWallet::sendInvoice(chatId: 123456789, invoice: $invoice);
2. Create an Invoice Link (Mini Apps / WebApps)
$link = BaleWallet::createInvoiceLink($invoice);
3. Dynamic Multi-Bot Usage
BaleWallet::forBot($tenantBotToken, $tenantProviderToken)
->sendInvoice($chatId, $invoice);
4. Inquire Transaction Status
$transaction = BaleWallet::inquireTransaction($paymentChargeId);
if ($transaction->isPaid()) {
// Fulfill order
}
5. Handling Webhooks with Events
When BALE_WALLET_WEBHOOK_ENABLED=true, the package registers a POST /bale-wallet/webhook route. Listen to the dispatched events in your AppServiceProvider or dedicated listeners:
use Bale\Wallet\Events\PreCheckoutReceived;
use Bale\Wallet\Events\PaymentSucceeded;
use Bale\Wallet\Facades\BaleWallet;
use Illuminate\Support\Facades\Event;
Event::listen(function (PreCheckoutReceived $event): void {
// Validate order availability/price using $event->query->invoicePayload
BaleWallet::answerPreCheckoutQuery($event->query->id, ok: true);
});
Event::listen(function (PaymentSucceeded $event): void {
$traceNo = $event->payment->traceNo;
$isVerifiedPaid = $event->transaction?->isPaid() ?? false;
// Mark order as paid
});
Testing
Use BaleWallet::fake() in your Pest or PHPUnit tests to avoid hitting Bale's API:
use Bale\Wallet\Facades\BaleWallet;
$fake = BaleWallet::fake();
// Execute your application code...
$fake->assertInvoiceSent();
$fake->assertTransactionInquired('txn_123');
مستندات فارسی
پکیج مدرن لاراول ۱۲+ (PHP 8.3+) برای اتصال به کیف پول الکترونیکی و پرداخت درونبرنامهای پیامرسان بله بر اساس مستندات رسمی بازوی بله.
امکانات کلیدی
- سازگار با PHP 8.3+ و Laravel 12+: پیادهسازیشده با DTOهای
readonly، Enumهای تایپشده و کلاینت HTTP لاراول. - پشتیبانی کامل از متدهای پرداخت بله:
sendInvoice،createInvoiceLink(مینیاپها)،answerPreCheckoutQueryوinquireTransaction. - مدیریت ریال و تومان: واحد پیشفرض
IRR(ریال) به همراه متدهای کمکیLabeledPrice::fromRial()وLabeledPrice::fromToman(). - پشتیبانی از چند ربات (Multi-Bot): تغییر پویا در زمان اجرا با
BaleWallet::forBot($botToken, $providerToken). - محیط تست (Sandbox): پشتیبانی از توکن رسمی تست بله (
WALLET-TEST-1111111111111111). - وبهوک و رویدادها (Events): روت اختیاری وبهوک با بررسی توکن امنیتی، استعلام خودکار تراکنش و انتشار رویدادهای
PreCheckoutReceivedوPaymentSucceeded. - ذخیرهسازی اختیاری در دیتابیس: به صورت پیشفرض Stateless؛ همراه با Migration و مدل
BaleWalletTransactionقابل انتشار برای ثبت تراکنشها و جلوگیری از پردازش تکراری (Idempotency). - ابزار تست داخلی: ارائه
BaleWallet::fake()برای تست بدون نیاز به شبکه.
نصب
composer require medicoassassino/laravel-bale-wallet
انتشار فایل تنظیمات:
php artisan vendor:publish --tag="bale-wallet-config"
(اختیاری) در صورت نیاز به ذخیره تراکنشها در دیتابیس و جلوگیری از وبهوک تکراری:
php artisan vendor:publish --tag="bale-wallet-migrations"
php artisan migrate
پیکربندی
متغیرهای زیر را در فایل .env پروژه خود تنظیم کنید:
BALE_BOT_TOKEN=توکن-ربات-دریافتی-از-botfather
BALE_WALLET_PROVIDER_TOKEN=توکن-کیف-پول-دریافتی-از-botfather
BALE_WALLET_SANDBOX=false
BALE_WALLET_WEBHOOK_ENABLED=true
BALE_WALLET_WEBHOOK_SECRET=کلید-امنیتی-دلخواه-وبهوک
BALE_WALLET_AUTO_INQUIRE=true
BALE_WALLET_DB_ENABLED=false
نحوه استفاده
۱. ارسال مستقیم فاکتور در چت (sendInvoice)
use Bale\Wallet\Data\InvoiceRequest;
use Bale\Wallet\Data\LabeledPrice;
use Bale\Wallet\Facades\BaleWallet;
$invoice = new InvoiceRequest(
title: 'اشتراک ویژه',
description: 'دسترسی یکماهه به تمام امکانات',
payload: json_encode(['order_id' => 1042]),
prices: [
LabeledPrice::fromToman('اشتراک ماهانه', 50_000), // تبدیل خودکار به ۵۰۰,۰۰۰ ریال
],
);
$response = BaleWallet::sendInvoice(chatId: 123456789, invoice: $invoice);
۲. ساخت لینک پرداخت برای مینیاپ (createInvoiceLink)
$link = BaleWallet::createInvoiceLink($invoice);
۳. استفاده با چند ربات بهصورت پویا (forBot)
BaleWallet::forBot($tenantBotToken, $tenantProviderToken)
->sendInvoice($chatId, $invoice);
۴. استعلام وضعیت تراکنش (inquireTransaction)
$transaction = BaleWallet::inquireTransaction($paymentChargeId);
if ($transaction->isPaid()) {
// سفارش را نهایی کنید
}
۵. مدیریت وبهوک و رویدادهای پرداخت
زمانی که BALE_WALLET_WEBHOOK_ENABLED=true باشد، مسیر POST /bale-wallet/webhook به صورت خودکار ثبت میشود. کافی است به رویدادهای زیر گوش دهید:
use Bale\Wallet\Events\PreCheckoutReceived;
use Bale\Wallet\Events\PaymentSucceeded;
use Bale\Wallet\Facades\BaleWallet;
use Illuminate\Support\Facades\Event;
Event::listen(function (PreCheckoutReceived $event): void {
// بررسی موجودی یا اعتبار سفارش با $event->query->invoicePayload
BaleWallet::answerPreCheckoutQuery($event->query->id, ok: true);
});
Event::listen(function (PaymentSucceeded $event): void {
$traceNo = $event->payment->traceNo;
$isVerifiedPaid = $event->transaction?->isPaid() ?? false;
// ثبت پرداخت موفق و تحویل سرویس
});
تستنویسی
برای نوشتن تست در پروژه خود بدون ارسال درخواست واقعی به سرورهای بله، از BaleWallet::fake() استفاده کنید:
use Bale\Wallet\Facades\BaleWallet;
$fake = BaleWallet::fake();
// اجرای کدهای برنامه...
$fake->assertInvoiceSent();
$fake->assertTransactionInquired('txn_123');
License
The MIT License (MIT). Please see LICENSE for more information.