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

[13.x] Multiple discounts on receipts #1147

Merged
merged 4 commits into from
May 17, 2021
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
28 changes: 16 additions & 12 deletions resources/views/receipt.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
vertical-align: bottom;
font-weight: bold;
padding: 8px;
line-height: 20px;
line-height: 14px;
text-align: left;
border-bottom: 1px solid #ddd;
}
Expand All @@ -41,7 +41,7 @@

.table td {
padding: 8px;
line-height: 20px;
line-height: 14px;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decreased the line-height here slightly to account for the extra items on the invoice.

text-align: left;
vertical-align: top;
}
Expand Down Expand Up @@ -248,17 +248,21 @@

<!-- Display The Discount -->
@if ($invoice->hasDiscount())
<tr>
<td colspan="{{ $invoice->hasTax() ? 3 : 2 }}" style="text-align: right;">
@if ($invoice->discountIsPercentage())
{{ $invoice->couponName() }} ({{ $invoice->percentOff() }}% Off)
@else
{{ $invoice->couponName() }} ({{ $invoice->amountOff() }} Off)
@endif
</td>
@foreach ($invoice->discounts() as $discount)
@php($coupon = $discount->coupon())

<td>-{{ $invoice->discount() }}</td>
</tr>
<tr>
<td colspan="{{ $invoice->hasTax() ? 3 : 2 }}" style="text-align: right;">
@if ($coupon->isPercentage())
{{ $coupon->name() }} ({{ $coupon->percentOff() }}% Off)
@else
{{ $coupon->name() }} ({{ $coupon->amountOff() }} Off)
@endif
</td>

<td>-{{ $invoice->discountFor($discount) }}</td>
</tr>
@endforeach
@endif

<!-- Display The Taxes -->
Expand Down
2 changes: 1 addition & 1 deletion src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Cashier
*/
public static function findBillable($stripeId)
{
if ($stripeId === null) {
if (is_null($stripeId)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function jsonSerialize()
}

/**
* Dynamically get values from the Stripe Checkout Session.
* Dynamically get values from the Stripe object.
*
* @param string $key
* @return mixed
Expand Down
10 changes: 3 additions & 7 deletions src/Concerns/ManagesInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public function upcomingInvoice()
}

try {
$stripeInvoice = StripeInvoice::upcoming(
['customer' => $this->stripe_id, 'expand' => ['account_tax_ids']], $this->stripeOptions()
);
$stripeInvoice = StripeInvoice::upcoming(['customer' => $this->stripe_id], $this->stripeOptions());

return new Invoice($this, $stripeInvoice);
} catch (StripeInvalidRequestException $exception) {
Expand All @@ -128,9 +126,7 @@ public function findInvoice($id)
$stripeInvoice = null;

try {
$stripeInvoice = StripeInvoice::retrieve(
['id' => $id, 'expand' => ['account_tax_ids']], $this->stripeOptions()
);
$stripeInvoice = StripeInvoice::retrieve($id, $this->stripeOptions());
} catch (StripeInvalidRequestException $exception) {
//
}
Expand Down Expand Up @@ -187,7 +183,7 @@ public function downloadInvoice($id, array $data, $filename = null)
public function invoices($includePending = false, $parameters = [])
{
if (! $this->hasStripeId()) {
return collect();
return new Collection();
}

$invoices = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/ManagesPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Cashier\Concerns;

use Exception;
use Illuminate\Support\Collection;
use Laravel\Cashier\PaymentMethod;
use Stripe\Customer as StripeCustomer;
use Stripe\PaymentMethod as StripePaymentMethod;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function hasPaymentMethod($type = 'card')
public function paymentMethods($type = 'card', $parameters = [])
{
if (! $this->hasStripeId()) {
return collect();
return new Collection();
}

$parameters = array_merge(['limit' => 24], $parameters);
Expand All @@ -65,7 +66,7 @@ public function paymentMethods($type = 'card', $parameters = [])
$this->stripeOptions()
);

return collect($paymentMethods->data)->map(function ($paymentMethod) {
return Collection::make($paymentMethods->data)->map(function ($paymentMethod) {
return new PaymentMethod($this, $paymentMethod);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/Concerns/PerformsCharges.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Cashier\Concerns;

use Illuminate\Support\Collection;
use Laravel\Cashier\Checkout;
use Laravel\Cashier\Payment;
use Stripe\PaymentIntent as StripePaymentIntent;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function refund($paymentIntent, array $options = [])
*/
public function checkout($items, array $sessionOptions = [], array $customerOptions = [])
{
$items = collect((array) $items)->map(function ($item, $key) {
$items = Collection::make((array) $items)->map(function ($item, $key) {
if (is_string($key)) {
return ['price' => $key, 'quantity' => $item];
}
Expand Down
144 changes: 144 additions & 0 deletions src/Coupon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Laravel\Cashier;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stripe\Coupon as StripeCoupon;

class Coupon implements Arrayable, Jsonable, JsonSerializable
{
/**
* The Stripe Coupon instance.
*
* @var \Stripe\Coupon
*/
protected $coupon;

/**
* Create a new Coupon instance.
*
* @param \Stripe\Coupon $coupon
* @return void
*/
public function __construct(StripeCoupon $coupon)
{
$this->coupon = $coupon;
}

/**
* Get the readable name for the Coupon.
*
* @return string
*/
public function name()
{
return $this->coupon->name ?: $this->coupon->id;
}

/**
* Determine if the coupon is a percentage.
*
* @return bool
*/
public function isPercentage()
{
return ! is_null($this->coupon->percent_off);
}

/**
* Get the discount percentage for the invoice.
*
* @return float|null
*/
public function percentOff()
{
return $this->coupon->percent_off;
}

/**
* Get the amount off for the coupon.
*
* @return string|null
*/
public function amountOff()
{
if (! is_null($this->coupon->amount_off)) {
return $this->formatAmount($this->rawAmountOff());
}
}

/**
* Get the raw amount off for the coupon.
*
* @return int|null
*/
public function rawAmountOff()
{
return $this->coupon->amount_off;
}

/**
* Format the given amount into a displayable currency.
*
* @param int $amount
* @return string
*/
protected function formatAmount($amount)
{
return Cashier::formatAmount($amount, $this->coupon->currency);
}

/**
* Get the Stripe Coupon instance.
*
* @return \Stripe\Coupon
*/
public function asStripeCoupon()
{
return $this->coupon;
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->asStripeCoupon()->toArray();
}

/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

/**
* Dynamically get values from the Stripe object.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->coupon->{$key};
}
}
91 changes: 91 additions & 0 deletions src/Discount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Laravel\Cashier;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stripe\Discount as StripeDiscount;

class Discount implements Arrayable, Jsonable, JsonSerializable
{
/**
* The Stripe Discount instance.
*
* @var \Stripe\Discount
*/
protected $discount;

/**
* Create a new Discount instance.
*
* @param \Stripe\Discount $discount
* @return void
*/
public function __construct(StripeDiscount $discount)
{
$this->discount = $discount;
}

/**
* Get the coupon applied to the discount.
*
* @return \Laravel\Cashier\Coupon
*/
public function coupon()
{
return new Coupon($this->discount->coupon);
}

/**
* Get the Stripe Discount instance.
*
* @return \Stripe\Discount
*/
public function asStripeDiscount()
{
return $this->discount;
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->asStripeDiscount()->toArray();
}

/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

/**
* Dynamically get values from the Stripe object.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->discount->{$key};
}
}
Loading