Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove money helper from code to stop collision #119

Merged
merged 3 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Credit/Credit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Laravel\Cashier\Traits\HasOwner;
use Money\Currency;
use Money\Money;

class Credit extends Model
Expand Down Expand Up @@ -55,13 +56,13 @@ public static function maxOutForOwner(Model $owner, Money $amount)
$credit = static::whereOwner($owner)->whereCurrency($amount->getCurrency()->getCode())->firstOrCreate([]);

if ($credit->value == 0) {
return money(0, $amount->getCurrency()->getCode());
return new Money(0, new Currency($amount->getCurrency()->getCode()));
}

$use_credit = min([$credit->value, (int) $amount->getAmount()]);
$credit->decrement('value', $use_credit);

return money($use_credit, $amount->getCurrency()->getCode());
return new Money($use_credit, new Currency($amount->getCurrency()->getCode()));
});
}

Expand All @@ -72,6 +73,6 @@ public static function maxOutForOwner(Model $owner, Money $amount)
*/
public function money()
{
return money($this->value, $this->currency);
return new Money($this->value, new Currency($this->currency));
}
}
4 changes: 3 additions & 1 deletion src/FirstPayment/Actions/ActionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Exceptions\CurrencyMismatchException;
use Laravel\Cashier\Order\OrderItemCollection;
use Money\Currency;
use Money\Money;

class ActionCollection extends Collection
{
Expand All @@ -32,7 +34,7 @@ protected function validate()
*/
public function total()
{
$total = money(0, $this->getCurrency());
$total = new Money(0, new Currency($this->getCurrency()));

$this->each(function (BaseAction $item) use (&$total) {
$total = $total->add($item->getTotal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Database\Eloquent\Model;
use Laravel\Cashier\Coupon\Coupon;
use Laravel\Cashier\Order\OrderItemCollection;
use Money\Currency;
use Money\Money;

class ApplySubscriptionCouponToPayment extends BaseNullAction
{
Expand Down Expand Up @@ -57,6 +59,6 @@ public function getTax()
*/
protected function toMoney($value = 0)
{
return money($value, $this->getCurrency());
return new Money($value, new Currency($this->getCurrency()));
}
}
6 changes: 4 additions & 2 deletions src/FirstPayment/Actions/StartSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Laravel\Cashier\Plan\Contracts\PlanRepository;
use Laravel\Cashier\SubscriptionBuilder\Contracts\SubscriptionConfigurator;
use Laravel\Cashier\SubscriptionBuilder\MandatedSubscriptionBuilder;
use Money\Currency;
use Money\Money;

class StartSubscription extends BaseAction implements SubscriptionConfigurator
{
Expand Down Expand Up @@ -215,7 +217,7 @@ public function trialDays(int $trialDays)
{
$this->trialDays = $trialDays;
$this->builder()->trialDays($trialDays);
$this->unitPrice = money(0, $this->getCurrency());
$this->unitPrice = new Money(0, new Currency($this->getCurrency()));

return $this;
}
Expand All @@ -230,7 +232,7 @@ public function trialUntil(Carbon $trialUntil)
{
$this->trialUntil = $trialUntil;
$this->builder()->trialUntil($trialUntil);
$this->unitPrice = money(0, $this->getCurrency());
$this->unitPrice = new Money(0, new Currency($this->getCurrency()));

return $this;
}
Expand Down
14 changes: 0 additions & 14 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ function object_to_array_recursive($object)
}
}

if (! function_exists('money')) {
/**
* Create a Money object from a Mollie Amount array.
*
* @param int|string $value
* @param string $currency
* @return \Money\Money
*/
function money($value, string $currency)
{
return new Money($value, new Currency($currency));
}
}

if (! function_exists('decimal_to_money')) {
/**
* Create a Money object from a decimal string / currency pair.
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Controllers/AftercareWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Mollie\Api\Resources\Payment as MolliePayment;
use Mollie\Api\Resources\Refund as MollieRefund;
use Mollie\Api\Types\RefundStatus;
use Money\Currency;
use Money\Money;
use Symfony\Component\HttpFoundation\Response;

class AftercareWebhookController extends BaseWebhookController
Expand Down Expand Up @@ -80,7 +82,7 @@ protected function handleRefunds(Order $order, MolliePayment $molliePayment)
// Update the locally known refunded amount
$amountRefunded = $molliePayment->amountRefunded
? mollie_object_to_money($molliePayment->amountRefunded)
: money(0, $molliePayment->amount->currency);
: new Money(0, new Currency($molliePayment->amount->currency));

$localPayment = Cashier::$paymentModel::findByPaymentId($molliePayment->id);
$localPayment->update(['amount_refunded' => (int) $amountRefunded->getAmount()]);
Expand Down
5 changes: 4 additions & 1 deletion src/Order/ConvertsToMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Laravel\Cashier\Order;

use Money\Currency;
use Money\Money;

trait ConvertsToMoney
{
/**
Expand All @@ -10,6 +13,6 @@ trait ConvertsToMoney
*/
protected function toMoney($value = 0)
{
return money(round($value), $this->getCurrency());
return new Money(round($value), new Currency($this->getCurrency()));
}
}
15 changes: 8 additions & 7 deletions src/Order/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Str;
use Laravel\Cashier\Order\Contracts\InvoicableItem;
use Laravel\Cashier\Traits\FormatsAmount;
use Money\Currency;
use Money\Money;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -201,7 +202,7 @@ public function subtotal()
*/
public function rawSubtotal()
{
$subtotal = money(0, $this->currency);
$subtotal = new Money(0, new Currency($this->currency));

$this->items->each(function (InvoicableItem $item) use (&$subtotal) {
$subtotal = $subtotal->add($item->getSubtotal());
Expand All @@ -227,7 +228,7 @@ public function total()
*/
public function rawTotal()
{
$subtotal = money(0, $this->currency);
$subtotal = new Money(0, new Currency($this->currency));

$this->items->each(function (InvoicableItem $item) use (&$subtotal) {
$subtotal = $subtotal->add($item->getTotal());
Expand Down Expand Up @@ -279,9 +280,9 @@ public function taxDetails()
return [
'tax_percentage' => (float) $percentage,
'raw_over_subtotal' => $raw_over_subtotal,
'over_subtotal' => $this->formatAmount(money($raw_over_subtotal, $this->currency)),
'over_subtotal' => $this->formatAmount(new Money($raw_over_subtotal, new Currency($this->currency))),
'raw_total' => $raw_total,
'total' => $this->formatAmount(money($raw_total, $this->currency)),
'total' => $this->formatAmount(new Money($raw_total, new Currency($this->currency))),
];
};

Expand Down Expand Up @@ -330,7 +331,7 @@ public function startingBalance()
*/
public function rawStartingBalance()
{
return $this->startingBalance ?: money(0, $this->currency);
return $this->startingBalance ?: new Money(0, new Currency($this->currency));
}

/**
Expand Down Expand Up @@ -361,7 +362,7 @@ public function usedBalance()
*/
public function rawUsedBalance()
{
return $this->usedBalance ?: money(0, $this->currency);
return $this->usedBalance ?: new Money(0, new Currency($this->currency));
}

/**
Expand Down Expand Up @@ -392,7 +393,7 @@ public function completedBalance()
*/
public function rawCompletedBalance()
{
return $this->completedBalance ?: money(0, $this->currency);
return $this->completedBalance ?: new Money(0, new Currency($this->currency));
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use Mollie\Api\Resources\Mandate;
use Mollie\Api\Resources\Payment as MolliePayment;
use Mollie\Api\Types\PaymentStatus;
use Money\Currency;
use Money\Money;

/**
* @property int id
Expand Down Expand Up @@ -208,7 +210,7 @@ public function processPayment()
return $this->handlePaymentFailedDueToInvalidMandate();
}

$totalDue = money($this->total_due, $this->currency);
$totalDue = new Money($this->total_due, new Currency($this->currency));

if ($maximumPaymentAmount && $totalDue->greaterThan($maximumPaymentAmount)) {
$this->items->each(function (OrderItem $item) {
Expand All @@ -231,7 +233,7 @@ public function processPayment()
$this->mollie_payment_id = null;

// Add credit to the owner's balance
$credit = Cashier::$creditModel::addAmountForOwner($owner, money(-($this->total_due), $this->currency));
$credit = Cashier::$creditModel::addAmountForOwner($owner, new Money(-($this->total_due), new Currency($this->currency)));

if (! $owner->hasActiveSubscriptionWithCurrency($this->currency)) {
Event::dispatch(new BalanceTurnedStale($credit));
Expand Down Expand Up @@ -696,7 +698,7 @@ private function ensureValidMandateAndMinimumPaymentAmountWhenTotalDuePositive()
$this->guardMandate($mandate);
$minimumPaymentAmount = app(MinimumPayment::class)::forMollieMandate($mandate, $this->getCurrency());
} else {
$minimumPaymentAmount = money(0, $this->getCurrency());
$minimumPaymentAmount = new Money(0, new Currency($this->getCurrency()));
}

return $minimumPaymentAmount;
Expand All @@ -715,7 +717,7 @@ private function ensureValidMandateAndMaximumPaymentAmountWhenTotalDuePositive()

$maximumPaymentAmount = app(MaximumPayment::class)::forMollieMandate($mandate, $this->getCurrency());
} else {
$maximumPaymentAmount = money(0, $this->getCurrency());
$maximumPaymentAmount = new Money(0, new Currency($this->getCurrency()));
}

return $maximumPaymentAmount;
Expand Down
3 changes: 2 additions & 1 deletion src/Order/OrderItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection as BaseCollection;
use Laravel\Cashier\Cashier;
use LogicException;
use Money\Currency;
use Money\Money;

class OrderItemCollection extends Collection
Expand Down Expand Up @@ -169,7 +170,7 @@ public function getTotal(): Money
throw new LogicException('Calculating the total requires items to be of the same currency.');
}

return money($this->sum('total'), $this->currency());
return new Money($this->sum('total'), new Currency($this->currency()));
}

public function currency(): string
Expand Down
5 changes: 3 additions & 2 deletions src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laravel\Cashier\Traits\HasOwner;
use Mollie\Api\Resources\Payment as MolliePayment;
use Mollie\Api\Types\PaymentStatus;
use Money\Currency;
use Money\Money;

/**
Expand Down Expand Up @@ -72,11 +73,11 @@ public static function makeFromMolliePayment(MolliePayment $payment, Model $owne
{
$amountChargedBack = $payment->amountChargedBack
? mollie_object_to_money($payment->amountChargedBack)
: money(0, $payment->amount->currency);
: new Money(0, new Currency($payment->amount->currency));

$amountRefunded = $payment->amountRefunded
? mollie_object_to_money($payment->amountRefunded)
: money(0, $payment->amount->currency);
: new Money(0, new Currency($payment->amount->currency));

$localActions = ! empty($actions) ? $actions : $payment->metadata->actions ?? null;

Expand Down
4 changes: 3 additions & 1 deletion src/Refunds/RefundItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Order\OrderItem;
use Laravel\Cashier\Order\OrderItemCollection;
use Money\Currency;
use Money\Money;

class RefundItemCollection extends Collection
Expand All @@ -23,7 +24,8 @@ public static function makeFromOrderItemCollection(OrderItemCollection $orderIte

public function getTotal(): Money
{
return money($this->sum('total'), $this->getCurrency());
return new Money($this->sum('total'), new Currency($this->getCurrency()));

}

public function getCurrency(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Laravel\Cashier\Traits\HasOwner;
use Laravel\Cashier\Types\SubscriptionCancellationReason;
use LogicException;
use Money\Currency;
use Money\Money;

/**
Expand Down Expand Up @@ -698,7 +699,7 @@ protected function reimburse(Money $amount, array $overrides = [])
*/
protected function reimbursableAmount()
{
$zeroAmount = \money('0.00', $this->currency);
$zeroAmount = new Money('0.00', new Currency($this->currency));

// Determine base amount eligible to reimburse
$latestProcessedOrderItem = $this->latestProcessedOrderItem();
Expand Down
3 changes: 2 additions & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Mockery;
use Mollie\Api\MollieApiClient;
use Mollie\Laravel\Wrappers\MollieApiWrapper;
use Money\Currency;
use Money\Money;
use Orchestra\Testbench\TestCase;

Expand Down Expand Up @@ -374,7 +375,7 @@ protected function assertMoney(int $value, string $currency, Money $money)
{
$this->assertEquals($currency, $money->getCurrency()->getCode());
$this->assertEquals($money->getAmount(), $value);
$this->assertTrue(money($value, $currency)->equals($money));
$this->assertTrue((new Money($value, new Currency($currency)))->equals($money));
}

/**
Expand Down
10 changes: 6 additions & 4 deletions tests/CashierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use Mollie\Api\Resources\Customer;
use Mollie\Api\Resources\Mandate;
use Mollie\Api\Resources\Payment;
use Money\Currency;
use Money\Money;

class CashierTest extends BaseTestCase
{
Expand Down Expand Up @@ -302,8 +304,8 @@ public function canSwapSubscriptionPlanAndReimburseUnusedTime()
/** @test */
public function testFormatAmount()
{
$this->assertEquals('1.000,00 €', Cashier::formatAmount(money(100000, 'EUR')));
$this->assertEquals('-9.123,45 €', Cashier::formatAmount(money(-912345, 'EUR')));
$this->assertEquals('1.000,00 €', Cashier::formatAmount(new Money(100000, new Currency('EUR'))));
$this->assertEquals('-9.123,45 €', Cashier::formatAmount(new Money(-912345, new Currency('EUR'))));
}

/**
Expand Down Expand Up @@ -415,14 +417,14 @@ protected function withMockedGetMollieMandate($attributes = [[
protected function withMockedGetMollieMethodMinimumAmount($times = 1): void
{
$this->mock(GetMollieMethodMinimumAmount::class, function ($mock) use ($times) {
return $mock->shouldReceive('execute')->with('directdebit', 'EUR')->times($times)->andReturn(money(100, 'EUR'));
return $mock->shouldReceive('execute')->with('directdebit', 'EUR')->times($times)->andReturn(new Money(100, new Currency('EUR')));
});
}

protected function withMockedGetMollieMethodMaximumAmount($times = 1): void
{
$this->mock(GetMollieMethodMaximumAmount::class, function ($mock) use ($times) {
return $mock->shouldReceive('execute')->with('directdebit', 'EUR')->times($times)->andReturn(money(30000, 'EUR'));
return $mock->shouldReceive('execute')->with('directdebit', 'EUR')->times($times)->andReturn(new Money(30000, new Currency('EUR')));
});
}

Expand Down
Loading