Installation
composer require dwddevops/baofoo-pay-bundle
Publish the configuration file:
php artisan vendor:publish --provider="Dwddevops\BaofooPayBundle\BaofooPayServiceProvider" --tag="baofoo-pay-config"
Configuration
Edit config/baofoo-pay.php with your Baofoo merchant credentials:
'merchant_id' => env('BAOFOO_MERCHANT_ID'),
'merchant_key' => env('BAOFOO_MERCHANT_KEY'),
'base_url' => env('BAOFOO_BASE_URL', 'https://api.baofoo.com'),
First Use Case: Create a Payment Inject the service into a controller:
use Dwddevops\BaofooPayBundle\Services\BaofooPayService;
public function createPayment(BaofooPayService $baofooPay)
{
$result = $baofooPay->createPayment([
'order_id' => 'ORD-12345',
'amount' => 100.00,
'currency' => 'USD',
'subject' => 'Product Purchase',
'body' => 'Payment for product #123',
]);
return response()->json($result);
}
Payment Creation & Processing
// Create a payment request
$payment = $baofooPay->createPayment($paymentData);
// Verify payment status (e.g., after redirect)
$status = $baofooPay->verifyPayment($payment['payment_id']);
Webhook Handling
Configure routes in routes/web.php:
Route::post('/baofoo-webhook', [BaofooPayController::class, 'handleWebhook']);
Implement the controller:
public function handleWebhook(Request $request, BaofooPayService $baofooPay)
{
$baofooPay->handleWebhook($request->all());
}
Refunds
$refund = $baofooPay->createRefund([
'order_id' => 'ORD-12345',
'amount' => 50.00,
'reason' => 'Customer refund request',
]);
baofoo.payment.created or baofoo.payment.completed events.$baofooPay->validateWebhookSignature($request->all());
config/baofoo-pay.php for troubleshooting:
'debug' => env('BAOFOO_DEBUG', false),
Signature Validation
signature field in the payload:
$isValid = $baofooPay->validateWebhookSignature($request->all());
merchant_key is misconfigured, signatures will fail silently. Double-check the key in .env.Async Processing
payment_id in a database before processing).Currency & Amount
100.00 → 10000 for cents). The bundle may not auto-convert; check the API docs.Testing
BAOFOO_BASE_URL=https://sandbox.baofoo.com). Mock the service in tests:
$this->partialMock(BaofooPayService::class, ['createPayment']);
Enable Debug Mode:
'debug' => true,
Logs will appear in storage/logs/laravel.log.
Common Errors:
InvalidSignatureException: Verify merchant_key and payload order.PaymentNotFound: Ensure order_id or payment_id matches Baofoo’s records.Custom Responses Override the default response format by binding a custom formatter:
$baofooPay->setResponseFormatter(function ($data) {
return ['success' => true, 'data' => $data];
});
Database Storage Extend the bundle to auto-save payments to your database:
// In a service provider
$baofooPay->setPaymentSaver(function ($payment) {
Payment::create($payment);
});
Localization
Add language support by extending the BaofooPayService class and overriding error messages:
$baofooPay->setLanguage('zh_CN'); // If supported
How can I help you explore Laravel packages today?