diff --git a/composer.json b/composer.json index 84768af..edf23ff 100644 --- a/composer.json +++ b/composer.json @@ -33,13 +33,15 @@ "providers": [ "Modules\\Core\\Providers\\CoreServiceProvider", "Modules\\Core\\Providers\\AuthServiceProvider", - "Modules\\Core\\Providers\\CustomerServiceProvider" + "Modules\\Core\\Providers\\CustomerServiceProvider", + "Modules\\Core\\Providers\\PaymentServiceProvider" ] } }, "config": { "allow-plugins": { - "pestphp/pest-plugin": true + "pestphp/pest-plugin": true, + "php-http/discovery": true } } } diff --git a/config/payment.php b/config/payment.php new file mode 100644 index 0000000..c82088f --- /dev/null +++ b/config/payment.php @@ -0,0 +1,37 @@ + [ + 'cash-on-delivery' => [ + 'driver' => 'offline', + 'authorized' => 'awaiting-payment', + 'fee' => 0, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Lunar cart pipeline additions + |-------------------------------------------------------------------------- + | + | Appended to config('lunar.cart.pipelines.cart') after ApplyShipping so + | the cash-on-delivery fee is added to the shipping total before the + | final Calculate step sums everything up. + | + */ + 'cart_pipeline' => [ + ApplyCashOnDeliveryFee::class, + ], +]; diff --git a/src/Payment/Pipelines/Cart/ApplyCashOnDeliveryFee.php b/src/Payment/Pipelines/Cart/ApplyCashOnDeliveryFee.php new file mode 100644 index 0000000..573b161 --- /dev/null +++ b/src/Payment/Pipelines/Cart/ApplyCashOnDeliveryFee.php @@ -0,0 +1,30 @@ +meta['payment_method'] ?? null) === 'cash-on-delivery') { + $fee = (int) config('lunar.payments.types.cash-on-delivery.fee', 0); + + $cart->shippingTotal = new Price( + ($cart->shippingTotal?->value ?? 0) + $fee, + $cart->currency, + 1 + ); + } + + return $next($cart); + } +} diff --git a/src/Providers/PaymentServiceProvider.php b/src/Providers/PaymentServiceProvider.php new file mode 100644 index 0000000..9e9e6fe --- /dev/null +++ b/src/Providers/PaymentServiceProvider.php @@ -0,0 +1,42 @@ +mergeConfigFrom(__DIR__ . '/../../config/payment.php', 'payment'); + } + + public function boot(): void + { + config([ + 'lunar.payments.types' => array_merge( + config('lunar.payments.types', []), + config('payment.types', []) + ), + ]); + + $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]); + } +}