Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
wip currency
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed May 13, 2021
1 parent 6c71e81 commit b346a37
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ class CashierRegisterServiceProvider extends BaseServiceProvider
{
parent::boot();

Saas::currency('EUR');

Saas::plan('Gold Plan', 'price_from_stripe_mo...', 'yearly_price_id')
->monthly(30)
->yearly(300)
->currency('EUR')
->features([
Saas::feature('Build Minutes', 'build.minutes', 3000),
]);
Expand Down Expand Up @@ -172,9 +173,10 @@ class CashierRegisterServiceProvider extends BaseServiceProvider
{
parent::boot();

Saas::currency('EUR');

Saas::plan('Gold Plan', 'price_...')
->monthly(30)
->currency('EUR')
->features([
Saas::feature('Seats', 'seats', 5)->notResettable(),
]);
Expand Down
4 changes: 3 additions & 1 deletion src/Concerns/HasPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace RenokiCo\CashierRegister\Concerns;

use RenokiCo\CashierRegister\Saas;

trait HasPrice
{
/**
Expand Down Expand Up @@ -41,7 +43,7 @@ public function price(float $price, string $currency = null)
*/
public function currency(string $currency = null)
{
$this->currency = $currency ?: $this->currency;
$this->currency = $currency ?: Saas::getCurrency($this->currency);

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $name, $id, float $price = 0.00, string $curr
$this->id($id);
$this->price($price);

$this->currency = $currency;
$this->currency = Saas::getCurrency($currency);
$this->subitems = collect([]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function yearlyId($id)
*/
public function monthly(float $price, $currency = null)
{
return $this->price($price, $currency);
return $this->price($price, Saas::getCurrency($currency));
}

/**
Expand Down
68 changes: 67 additions & 1 deletion src/Saas.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace RenokiCo\CashierRegister;

use Closure;
use Illuminate\Database\Eloquent\Model;

class Saas
{
/**
Expand All @@ -18,6 +21,20 @@ class Saas
*/
protected static $items = [];

/**
* The callback to call when syncing the current usage.
*
* @var array[Closure]
*/
protected static $syncUsageCallbacks = [];

/**
* Specify the global currency.
*
* @var string|null
*/
public static $currency;

/**
* Start creating a new plan.
*
Expand Down Expand Up @@ -72,13 +89,62 @@ public static function meteredFeature(string $name, $id, int $value = 0)
*/
public static function item($id, string $name, float $price = 0.00, string $currency = 'EUR')
{
$item = new Item($id, $name, $price, $currency);
$item = new Item($id, $name, $price, Saas::getCurrency($currency));

static::$items[] = $item;

return $item;
}

/**
* Add a callback to sync the feature usage.
*
* @param string|int $id
* @param Closure $callback
* @return void
*/
public static function syncFeatureUsage($id, Closure $callback)
{
static::$syncUsageCallbacks[$id] = $callback;
}

/**
* Apply the feature usage sync via callback.
*
* @param \Illuminate\Database\Eloquent\Model $subscription
* @param \RenokiCo\CashierRegister\Feature $feature
* @return int|float|null
*/
public static function applyFeatureUsageSync(Model $subscription, Feature $feature)
{
if ($callback = static::$syncUsageCallbacks[$feature->getId()] ?? null) {
return call_user_func($callback, $subscription, $feature);
}
}

/**
* Set the global currency.
*
* @param string $currency
* @return void
*/
public static function currency(string $currency)
{
static::$currency = $currency;
}

/**
* Get the global currency if set.
* Returns a default value if currency is not set.
*
* @param string|null $default
* @return string|null
*/
public static function getCurrency(string $default = null)
{
return static::$currency ?: $default;
}

/**
* Get the list of plans.
*
Expand Down
4 changes: 3 additions & 1 deletion stubs/CashierRegisterServiceProvider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class CashierRegisterServiceProvider extends BaseServiceProvider
{
parent::boot();

Saas::currency('EUR');

Saas::plan('Gold Plan', 'gold-plan')
->description('The gold plan is 10 EUR.')
->monthly(10, 'EUR')
->monthly(10)
->features([
Saas::feature('Build Minutes', 'build.minutes')->value(3000),
Saas::feature('Seats', 'seats')->unlimited(),
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function setUp(): void
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

$this->withFactories(__DIR__.'/database/factories');

Saas::currency('EUR');
}

/**
Expand Down

0 comments on commit b346a37

Please sign in to comment.