dbp/relay-mono-connector-payunity-bundle
Installation Add the bundle to your Laravel project via Composer:
composer require dbp/relay-mono-connector-payunity-bundle
Register the bundle in config/app.php under providers:
DigitalBlueprint\RelayMonoConnectorPayUnityBundle\PayUnityBundle::class,
Configuration Publish the default config:
php artisan vendor:publish --provider="DigitalBlueprint\RelayMonoConnectorPayUnityBundle\PayUnityBundle" --tag="config"
Update config/payunity.php with your PayUnity API credentials (e.g., api_key, merchant_id).
First Use Case: Payment Webhook
Define a Relay route in config/relay.php to handle PayUnity webhooks:
'routes' => [
'payunity_webhook' => [
'path' => '/payunity/webhook',
'method' => 'POST',
'handler' => \App\Http\Controllers\PayUnityWebhookController::class,
],
],
Create a controller to process the webhook payload:
namespace App\Http\Controllers;
use DigitalBlueprint\RelayMonoConnectorPayUnityBundle\Services\PayUnityService;
class PayUnityWebhookController extends Controller {
public function __invoke(PayUnityService $payUnityService, Request $request) {
$payload = $request->json()->all();
return $payUnityService->handleWebhook($payload);
}
}
Initiate Payment
Use the PayUnityService to create a payment request:
$payment = $payUnityService->createPayment([
'amount' => 100.00,
'currency' => 'USD',
'description' => 'Order #12345',
'customer_email' => 'user@example.com',
]);
Return the payment->id to redirect the user to PayUnity’s checkout.
Webhook Handling
Implement a Relay route (as above) to listen for PayUnity’s payment.succeeded or payment.failed events. Validate the payload using PayUnity’s signature:
public function handleWebhook(PayUnityService $payUnityService, Request $request) {
$payload = $request->json()->all();
if (!$payUnityService->validateWebhook($payload)) {
abort(403, 'Invalid webhook signature');
}
// Process the event (e.g., update order status)
return response()->json(['status' => 'success']);
}
Refunds/Voids Use the service to manage post-payment actions:
$payUnityService->refundPayment($paymentId, ['amount' => 50.00]);
// or
$payUnityService->voidPayment($paymentId);
// In a Relay middleware or service
$relay = app(\DigitalBlueprint\RelayBundle\Relay::class);
$relay->route('payunity_webhook', $request);
PaymentSucceeded) after processing webhooks:
event(new PaymentSucceeded($payload['payment_id'], $payload['amount']));
\Log::info('PayUnity payment created', ['payment_id' => $payment->id]);
Deprecated Service
Webhook Validation
validateWebhook() method may break.RELAY_DEBUG=true) to inspect raw webhook payloads:
if (config('relay.debug')) {
\Log::debug('Raw PayUnity payload', $payload);
}
Configuration Overrides
config('payunity') for settings. Overrides in config/payunity.php may conflict with Relay’s mono-connector defaults.api_key, endpoint) to avoid silent failures.Error Handling
400 Bad Request). Wrap service calls in try-catch:
try {
$payment = $payUnityService->createPayment($data);
} catch (\DigitalBlueprint\RelayMonoConnectorPayUnityBundle\Exceptions\PayUnityException $e) {
\Log::error('PayUnity error: ' . $e->getMessage());
abort(500, 'Payment processing failed');
}
Custom Webhook Events
Extend the PayUnityService to support additional PayUnity events (e.g., subscription.updated):
// In a service provider
$payUnityService->extendWebhookHandler('subscription.updated', function ($payload) {
// Custom logic
});
Relay Middleware Add middleware to Relay’s pipeline to pre-process PayUnity requests:
// In a Relay service provider
$relay->addMiddleware(\App\Middleware\ValidatePayUnityRequest::class);
Testing
Mock the PayUnityService in tests using Laravel’s HTTP testing:
$response = $this->post('/payunity/webhook', ['payload' => $data]);
$response->assertStatus(200);
storage/logs/laravel.log for Relay/PayUnity-related entries.dd() or dump() to inspect $request->json()->all() during webhook testing.
```markdown
## Configuration Quirks
1. **Endpoint Overrides**
The bundle defaults to PayUnity’s production endpoint. For testing, override in `config/payunity.php`:
```php
'endpoint' => 'https://sandbox.payunity.com/api/v1',
Signature Secret
Ensure the signature_secret in config/payunity.php matches PayUnity’s webhook settings. Mismatches will cause 403 errors.
Relay Route Conflicts
Avoid naming Relay routes that conflict with Laravel’s built-in routes (e.g., /payunity/webhook vs. /payments/webhook). Use unique paths.
How can I help you explore Laravel packages today?