A flexible Payment Module for the Juzaweb CMS. This module provides a unified interface for handling multiple payment gateways (PayPal, Stripe) and allows other modules to integrate payment functionalities easily.
Install the module:
php artisan module:install juzaweb/payment
Publish the configuration and assets (optional):
php artisan vendor:publish --tag="payment-config"
php artisan vendor:publish --tag="payment-module-views"
Payment methods are configured directly in the Juzaweb Admin Panel. Navigate to Settings > Payment Methods to enable and configure gateways like PayPal (Client ID, Secret) and Stripe (Publishable Key, Secret Key).
Paymentable ContractYour order model (the entity being paid for) must implement the Juzaweb\Modules\Payment\Contracts\Paymentable interface.
use Juzaweb\Modules\Payment\Contracts\Paymentable;
use Illuminate\Database\Eloquent\Model;
class Order extends Model implements Paymentable
{
public function getTotalAmount(): float
{
return $this->total_price;
}
public function getCurrency(): string
{
return 'USD';
}
public function getPaymentDescription(): string
{
return "Payment for Order #{$this->code}";
}
public function getCode(): string
{
return $this->code;
}
}
Create a class that implements Juzaweb\Modules\Payment\Contracts\ModuleHandlerInterface. This handler manages the business logic after a payment transaction.
use Juzaweb\Modules\Payment\Contracts\ModuleHandlerInterface;
use Juzaweb\Modules\Payment\Contracts\Paymentable;
class MyShopModuleHandler implements ModuleHandlerInterface
{
public function createOrder(array $params): Paymentable
{
// Logic to create an order if needed, or return an existing one
return Order::find($params['order_id']);
}
public function success(Paymentable $paymentable, array $params): void
{
// Handle successful payment (e.g., update order status to 'completed', send email)
$paymentable->update(['status' => 'completed']);
}
public function fail(Paymentable $paymentable, array $params): void
{
// Handle failed payment
$paymentable->update(['status' => 'failed']);
}
public function cancel(Paymentable $paymentable, array $params): void
{
// Handle cancelled payment
$paymentable->update(['status' => 'cancelled']);
}
public function getReturnUrl(): string
{
return url('/shop/checkout/completed');
}
}
Register your module handler in your module's ServiceProvider using the PaymentManager.
use Juzaweb\Modules\Payment\Contracts\PaymentManager;
public function boot()
{
$this->app[PaymentManager::class]->registerModule(
'my_shop_module',
new MyShopModuleHandler()
);
}
Use the PaymentManager to create a payment transaction.
use Juzaweb\Modules\Payment\Contracts\PaymentManager;
public function checkout(Request $request)
{
$user = $request->user();
$paymentMethod = $request->input('payment_method'); // e.g., 'paypal'
$orderId = $request->input('order_id');
// Additional params passed to the gateway and handler
$params = [
'return_url' => route('payment.return'),
'cancel_url' => route('payment.cancel'),
];
try {
$response = app(PaymentManager::class)->create(
$user,
'my_shop_module',
$paymentMethod,
$orderId,
$params
);
// Redirect user to the payment gateway
if ($response->isRedirect()) {
return $response->redirect();
}
return $response->getMessage();
} catch (\Exception $e) {
return redirect()->back()->withErrors($e->getMessage());
}
}
You can register a custom payment driver in your ServiceProvider:
use Juzaweb\Modules\Payment\Contracts\PaymentManager;
use Juzaweb\Modules\Payment\Services\PaymentDriverAdapter;
$this->app[PaymentManager::class]->registerDriver(
'MyGateway',
fn() => new PaymentDriverAdapter(
MyGatewayImplementation::class,
['api_key' => 'Config Label']
)
);
The module fires the following events during the payment process:
Juzaweb\Modules\Payment\Events\PaymentSuccessJuzaweb\Modules\Payment\Events\PaymentFailJuzaweb\Modules\Payment\Events\PaymentCancelListen to these events in your EventServiceProvider to perform additional actions.
No reviews yet.
0 Comments