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
PaymentGatewayInterface
and use theHelperGateway
trait for shared functionality.
- All gateways must implement the
-
Register the Gateway
- Add the gateway class to the
GatewayPack
registry in theModules\\GatewayPack\\Entities\\GatewayPack
class.
- 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
Gateway
model instance based on its endpoint. - Return Type:
Gateway
- Usage:
$gateway = self::getGatewayByEndpoint();
- Redirects the user to the dashboard with an error message.
- Usage:
return self::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
HelperGateway
trait for common functionality and implementPaymentGatewayInterface
.
namespace Modules\GatewayPack\Gateways\Once; use App\Models\Gateways\Gateway; use App\Models\Gateways\PaymentGatewayInterface; use App\Models\Payment; use Illuminate\Http\Request; use Modules\GatewayPack\Traits\HelperGateway; class ExampleGateway implements PaymentGatewayInterface { use HelperGateway; public static function endpoint(): string { return 'example-gateway'; } public static function getConfigMerge(): array { return [ 'api_key' => '', 'test_mode' => true, ]; } public static function drivers(): array { return [ 'ExampleGateway' => [ 'driver' => 'ExampleGateway', 'type' => 'once', 'class' => self::class, 'endpoint' => self::endpoint(), 'refund_support' => false, ], ]; } public static function processGateway(Gateway $gateway, Payment $payment) { // Implementation for payment processing } public static function returnGateway(Request $request) { // Implementation for handling gateway return } }
- Use the
-
Register the Gateway
- Add the new gateway to the
GatewayPack
registry.
namespace Modules\GatewayPack\Entities; use Modules\GatewayPack\Gateways\Once\ExampleGateway; class GatewayPack { protected static array $gateways = [ ExampleGateway::class, ]; public static function drivers(): array { $drivers = []; foreach (self::$gateways as $class) { if (method_exists($class, 'drivers')) { $drivers = array_merge($drivers, $class::drivers()); } } return $drivers; } }
- Add the new gateway to the
Happy coding! 😊