81 lines
3.5 KiB
PHP
81 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Core\Providers;
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Lunar\Facades\ModelManifest;
|
|
use Lunar\Models\Contracts\Transaction as TransactionContract;
|
|
use Lunar\Pipelines\Cart\ApplyShipping;
|
|
use Modules\Core\Command\SyncPaymentDriversCommand;
|
|
use Modules\Core\Payment\Drivers\BankTransferPaymentDriver;
|
|
use Modules\Core\Payment\Drivers\CashOnDeliveryPaymentDriver;
|
|
use Modules\Core\Payment\Drivers\OfflinePaymentDriver;
|
|
use Modules\Core\Payment\Drivers\StripePaymentDriver;
|
|
use Modules\Core\Payment\Events\PaymentMethodCreated;
|
|
use Modules\Core\Payment\Events\PaymentMethodDeleted;
|
|
use Modules\Core\Payment\Events\PaymentMethodUpdated;
|
|
use Modules\Core\Payment\Listeners\LogPaymentMethodActivity;
|
|
use Modules\Core\Payment\Models\CoreTransaction;
|
|
use Modules\Core\Payment\Services\PaymentDriverRegistry;
|
|
|
|
class PaymentServiceProvider extends ServiceProvider
|
|
{
|
|
public function register(): void
|
|
{
|
|
$this->mergeConfigFrom(__DIR__ . '/../../config/payment.php', 'payment');
|
|
|
|
$this->app->singleton(PaymentDriverRegistry::class);
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
$registry = $this->app->make(PaymentDriverRegistry::class);
|
|
$registry->register('offline', OfflinePaymentDriver::class, 'Offline / Manual');
|
|
$registry->register('stripe', StripePaymentDriver::class, 'Stripe');
|
|
$registry->register('bank-transfer', BankTransferPaymentDriver::class, 'Bank Transfer');
|
|
$registry->register('cash-on-delivery', CashOnDeliveryPaymentDriver::class, 'Cash on Delivery');
|
|
|
|
$cartPipeline = config('lunar.cart.pipelines.cart', []);
|
|
$insertAfter = array_search(ApplyShipping::class, $cartPipeline, true);
|
|
|
|
foreach (config('payment.cart_pipeline', []) as $pipe) {
|
|
if (in_array($pipe, $cartPipeline, true)) {
|
|
continue;
|
|
}
|
|
|
|
if ($insertAfter === false) {
|
|
$cartPipeline[] = $pipe;
|
|
} else {
|
|
array_splice($cartPipeline, $insertAfter + 1, 0, [$pipe]);
|
|
$insertAfter++;
|
|
}
|
|
}
|
|
|
|
config(['lunar.cart.pipelines.cart' => $cartPipeline]);
|
|
|
|
// Same contract-swap mechanism this codebase already uses for
|
|
// Customer/Staff (see e.g. consuming apps' own AppServiceProvider,
|
|
// ModelManifest::replace(Contracts\Customer::class, ...)) — every
|
|
// place Lunar's own code resolves a transaction via
|
|
// Transaction::modelClass() (Order::transactions()'s own relation
|
|
// included) gets Modules\Core\Payment\Models\CoreTransaction
|
|
// instead of the vendor's own Transaction. That subclass's
|
|
// driver() override is the ENTIRE fix for the admin panel's
|
|
// refund/capture actions silently landing on our real payment
|
|
// system — see CoreTransaction's own docblock. Lunar's own
|
|
// Payments facade/PaymentManager is never touched at all.
|
|
ModelManifest::replace(TransactionContract::class, CoreTransaction::class);
|
|
|
|
Event::listen(PaymentMethodCreated::class, [LogPaymentMethodActivity::class, 'handleCreated']);
|
|
Event::listen(PaymentMethodUpdated::class, [LogPaymentMethodActivity::class, 'handleUpdated']);
|
|
Event::listen(PaymentMethodDeleted::class, [LogPaymentMethodActivity::class, 'handleDeleted']);
|
|
|
|
$this->loadRoutesFrom(__DIR__ . '/../Payment/routes/webhooks.php');
|
|
|
|
if ($this->app->runningInConsole()) {
|
|
$this->commands([SyncPaymentDriversCommand::class]);
|
|
}
|
|
}
|
|
}
|