Add cash-on-delivery payment type with cart fee pipeline
This commit is contained in:
+4
-2
@@ -33,13 +33,15 @@
|
|||||||
"providers": [
|
"providers": [
|
||||||
"Modules\\Core\\Providers\\CoreServiceProvider",
|
"Modules\\Core\\Providers\\CoreServiceProvider",
|
||||||
"Modules\\Core\\Providers\\AuthServiceProvider",
|
"Modules\\Core\\Providers\\AuthServiceProvider",
|
||||||
"Modules\\Core\\Providers\\CustomerServiceProvider"
|
"Modules\\Core\\Providers\\CustomerServiceProvider",
|
||||||
|
"Modules\\Core\\Providers\\PaymentServiceProvider"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"allow-plugins": {
|
"allow-plugins": {
|
||||||
"pestphp/pest-plugin": true
|
"pestphp/pest-plugin": true,
|
||||||
|
"php-http/discovery": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Modules\Core\Payment\Pipelines\Cart\ApplyCashOnDeliveryFee;
|
||||||
|
|
||||||
|
return [
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Lunar payment types merged in by Boboko Core
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| These are merged into config('lunar.payments.types') so every app using
|
||||||
|
| boboko-core gets cash-on-delivery out of the box, without publishing
|
||||||
|
| Lunar's own config.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'types' => [
|
||||||
|
'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,
|
||||||
|
],
|
||||||
|
];
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Core\Payment\Pipelines\Cart;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Lunar\DataTypes\Price;
|
||||||
|
use Lunar\Models\Contracts\Cart as CartContract;
|
||||||
|
|
||||||
|
final class ApplyCashOnDeliveryFee
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Called just before cart totals are calculated.
|
||||||
|
*
|
||||||
|
* @param Closure(CartContract): mixed $next
|
||||||
|
*/
|
||||||
|
public function handle(CartContract $cart, Closure $next): mixed
|
||||||
|
{
|
||||||
|
if (($cart->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Core\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Lunar\Pipelines\Cart\ApplyShipping;
|
||||||
|
|
||||||
|
class PaymentServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
$this->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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user