This document provides details about adding and managing payment gateways in the GatewayPack module. It also describes
available methods in the system and their usage.
GatewayPack is designed for seamless integration and management of payment gateways. The system provides a trait (
HelperGateway) for shared functionality and a structured way to implement new gateways.
Create a Gateway Class
- All gateways must implement the
PaymentGatewayInterfaceand use theHelperGatewaytrait for shared functionality.
- All gateways must implement the
Register the Gateway
- Add the gateway class to the
GatewayPackregistry in theModules\\GatewayPack\\Entities\\GatewayPackclass.
- Add the gateway class to the
Define Drivers and Configuration
- Implement methods like
getConfigMerge()anddrivers()to define specific behaviors for your gateway.
- Implement methods like
- Retrieves the
Gatewaymodel instance based on its endpoint. - Return Type:
Gateway - Usage:
$gateway = self::getGatewayByEndpoint();
- Redirects the user to the dashboard with an error message.
- Usage:
returnself::errorRedirect('Payment failed');
- Generates the return URL for the payment gateway.
- Return Type:
string - Usage:
$url = self::getReturnUrl();
- Generates the cancel URL for the payment gateway.
- Return Type:
string - Usage:
$url = self::getCancelUrl($payment);
- Generates the success URL for the payment gateway.
- Return Type:
string - Usage:
$url = self::getSucceedUrl($payment);
- Sends an HTTP request (GET or POST) to the specified URL.
- Parameters:
$method: HTTP method (GET or POST).$url: URL to send the request.$data: Request payload.$token: Optional authorization token.
- Return Type:
HttpResponse - Usage:
$response = self::sendHttpRequest('POST', $url, $data, $token);
- Logs a message with the specified level.
- Parameters:
$message: Message to log.$level: Log level (info, warning, error).
- Usage:
self::log('Payment failed', 'error');
Here is a step-by-step example for adding a new payment gateway:
Create a New Gateway Class
- Use the
HelperGatewaytrait for common functionality and implementPaymentGatewayInterface.
namespaceModules\GatewayPack\Gateways\Once; useApp\Models\Gateways\Gateway; useApp\Models\Gateways\PaymentGatewayInterface; useApp\Models\Payment; useIlluminate\Http\Request; useModules\GatewayPack\Traits\HelperGateway; class ExampleGateway implements PaymentGatewayInterface { use HelperGateway; publicstaticfunctionendpoint(): string { return'example-gateway'; } publicstaticfunctiongetConfigMerge(): array { return [ 'api_key' => '', 'test_mode' => true, ]; } publicstaticfunctiondrivers(): array { return [ 'ExampleGateway' => [ 'driver' => 'ExampleGateway', 'type' => 'once', 'class' => self::class, 'endpoint' => self::endpoint(), 'refund_support' => false, ], ]; } publicstaticfunctionprocessGateway(Gateway$gateway, Payment$payment) { // Implementation for payment processing } publicstaticfunctionreturnGateway(Request$request) { // Implementation for handling gateway return } }
- Use the
Register the Gateway
- Add the new gateway to the
GatewayPackregistry.
namespaceModules\GatewayPack\Entities; useModules\GatewayPack\Gateways\Once\ExampleGateway; class GatewayPack { protectedstaticarray$gateways = [ ExampleGateway::class, ]; publicstaticfunctiondrivers(): array { $drivers = []; foreach (self::$gatewaysas$class) { if (method_exists($class, 'drivers')) { $drivers = array_merge($drivers, $class::drivers()); } } return$drivers; } }
- Add the new gateway to the
Happy coding! 😊