birkof/netopia-mobilpay-bundle
Installation:
composer require birkof/netopia-mobilpay-bundle
For Laravel, manually create a service wrapper (since the bundle is Symfony-specific).
Configuration:
Add to .env:
NETOPIA_MOBILPAY_PAYMENT_URL=https://api.mobilpay.ro
NETOPIA_MOBILPAY_PUBLIC_CERT=file://path/to/cert.pem
NETOPIA_MOBILPAY_PRIVATE_KEY=file://path/to/key.pem
NETOPIA_MOBILPAY_SIGNATURE=your_signature_key
First Use Case:
Create a Laravel service to wrap the bundle’s core logic (e.g., app/Services/MobilPayService.php):
use birkof\NetopiaMobilPay\Client;
use birkof\NetopiaMobilPay\Config;
class MobilPayService {
protected $client;
public function __construct() {
$config = new Config([
'payment_url' => env('NETOPIA_MOBILPAY_PAYMENT_URL'),
'public_cert' => env('NETOPIA_MOBILPAY_PUBLIC_CERT'),
'private_key' => env('NETOPIA_MOBILPAY_PRIVATE_KEY'),
'signature' => env('NETOPIA_MOBILPAY_SIGNATURE'),
]);
$this->client = new Client($config);
}
public function createPayment(array $data) {
return $this->client->createPayment($data);
}
}
Register the Service:
Bind the service in AppServiceProvider:
public function register() {
$this->app->singleton(MobilPayService::class, function ($app) {
return new MobilPayService();
});
}
Usage in Controller:
use App\Services\MobilPayService;
public function checkout(MobilPayService $mobilPay) {
$payment = $mobilPay->createPayment([
'amount' => 100,
'currency' => 'RON',
'description' => 'Order #123',
]);
return redirect($payment['redirect_url']);
}
Synchronous Payments:
// 1. Create payment
$payment = $mobilPay->createPayment($data);
// 2. Redirect to MobilPay
return redirect($payment['redirect_url']);
// 3. Handle callback (after payment)
public function callback(MobilPayService $mobilPay) {
$response = $mobilPay->verifyPayment($_POST);
if ($response['status'] === 'success') {
// Update order status
}
}
Webhook Handling:
public function webhook(MobilPayService $mobilPay) {
$isValid = $mobilPay->verifySignature($_POST);
if ($isValid) {
// Process payment update
}
}
Recurring Payments:
$profile = $mobilPay->createRecurringProfile([
'amount' => 50,
'currency' => 'RON',
'start_date' => now()->addDay(),
]);
Configuration:
Vault or encrypted .env.$config = new Config([
'public_cert' => storage_path('certs/mobilpay_cert.pem'),
'private_key' => storage_path('certs/mobilpay_key.pem'),
]);
Error Handling:
try {
$payment = $mobilPay->createPayment($data);
} catch (\Exception $e) {
Log::error("MobilPay error: " . $e->getMessage());
return back()->with('error', 'Payment failed');
}
Testing:
Http facade to mock MobilPay API responses in tests:
$response = Http::fake([
'api.mobilpay.ro' => Http::response(['status' => 'success'], 200),
]);
Logging:
Log::info('MobilPay payment request', ['data' => $data]);
Log::info('MobilPay payment response', ['response' => $payment]);
Middleware for Webhooks:
public function handleWebhook(Request $request, MobilPayService $mobilPay) {
if (!$mobilPay->verifySignature($request->all())) {
abort(403, 'Invalid signature');
}
// Process webhook
}
Symfony Dependencies:
HttpFoundation and DependencyInjection. Replace these with Laravel equivalents:
Symfony\Component\HttpFoundation\Request → Illuminate\Http\Request.Symfony\Component\DependencyInjection → Laravel’s bind() or AppServiceProvider.Certificate Paths:
storage/app/certs/ and set proper permissions).Signature Validation:
// Wrong: Missing sorting or incorrect data
$mobilPay->verifySignature($_POST);
Fix: Sort and stringify data as per MobilPay’s docs.Webhook Retries:
PHP Version:
API Errors:
$client = new Client($config, [
'http_client' => Http::withOptions(['debug' => true]),
]);
Signature Mismatches:
Webhook Failures:
200 OK status.Use Facades:
// app/Facades/MobilPay.php
public static function createPayment(array $data) {
return app(MobilPayService::class)->createPayment($data);
}
$payment = MobilPay::createPayment($data);
Environment-Specific Config:
config() helper to manage different environments (e.g., sandbox vs. live):
$config = new Config([
'payment_url' => config('services.mobilpay.payment_url'),
]);
Queue Webhook Processing:
public function webhook(Request $request) {
ProcessWebhook::dispatch($request->all());
}
Sandbox Testing:
NETOPIA_MOBILPAY_PAYMENT_URL=https://sandbox.mobilpay.ro
PCI Compliance:
Custom Exceptions:
class MobilPayException extends \Exception {}
Documentation:
README.md in your project for Laravel-specific usage (e.g., service setup, webhookHow can I help you explore Laravel packages today?