autepos/discount-nke-laravel

Laravel 的 autepos/discount 封装

dev-main 2023-06-15 06:44 UTC

This package is auto-updated.

Last update: 2024-09-15 09:13:01 UTC


README

DiscountNkeLaravel 是 autepos/discount 在 Laravel 中的实现。它设计得与 Stripe 的折扣类似,由优惠券和促销码组成。它并不完全是 autepos/discount 的封装,因为它主要实现了 DiscountInstrument 接口,并提供必要的 Eloquent 模型。

要求

  • PHP 8.0+
  • Laravel 9.x+

安装

使用 composer 安装此包

composer require autepos/discount-nke-laravel

php artisan migrate

使用方法

use Autepos\DiscountNkeLaravel\Contracts\DiscountProcessorFactory;
use Autepos\Discount\Contracts\DiscountableDevice;
use Autepos\DiscountNkeLaravel\Models\PromotionCode;

class Order implements DiscountableDevice
{
    //...
}


$discountableDevice = new Order();
$discountInstrument = new PromotionCode::find(1);

$processor = app(DiscountProcessorFactory::class);
$processor->addDiscountableDevice($discountableDevice)
          ->addDiscountInstrument($discountInstrument);
$discountLineList = $processor->calculate();

// Get the discount amount
$discountAmount = $discountLineList->amount();

// Persist the discount
$discountLineList->redeem();