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] Discount improvements #1354

Merged
merged 4 commits into from
May 2, 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
62 changes: 62 additions & 0 deletions src/Concerns/ManagesCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Illuminate\Support\Collection;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\CustomerBalanceTransaction;
use Laravel\Cashier\Discount;
use Laravel\Cashier\Exceptions\CustomerAlreadyCreated;
use Laravel\Cashier\Exceptions\InvalidCustomer;
use Laravel\Cashier\PromotionCode;
use Stripe\Customer as StripeCustomer;
use Stripe\Exception\InvalidRequestException as StripeInvalidRequestException;

Expand Down Expand Up @@ -194,6 +196,20 @@ public function syncStripeCustomerDetails()
]);
}

/**
* The discount that applies to the customer, if applicable.
*
* @return \Laravel\Cashier\Discount|null
*/
public function discount()
{
$customer = $this->asStripeCustomer(['discount.promotion_code']);

return $customer->discount
? new Discount($customer->discount)
: null;
}

/**
* Apply a coupon to the customer.
*
Expand All @@ -209,6 +225,52 @@ public function applyCoupon($coupon)
]);
}

/**
* Apply a promotion code to the customer.
*
* @param string $promotionCodeId
* @return void
*/
public function applyPromotionCode($promotionCodeId)
{
$this->assertCustomerExists();

$this->updateStripeCustomer([
'promotion_code' => $promotionCodeId,
]);
}

/**
* Retrieve a promotion code by its code.
*
* @param string $code
* @param array $options
* @return \Laravel\Cashier\PromotionCode|null
*/
public function findPromotionCode($code, array $options = [])
{
$codes = $this->stripe()->promotionCodes->all(array_merge([
'code' => $code,
'limit' => 1,
], $options));

if ($codes && $promotionCode = $codes->first()) {
return new PromotionCode($promotionCode);
}
}

/**
* Retrieve a promotion code by its code.
*
* @param string $code
* @param array $options
* @return \Laravel\Cashier\PromotionCode|null
*/
public function findActivePromotionCode($code, array $options = [])
{
return $this->findPromotionCode($code, array_merge($options, ['active' => true]));
}

/**
* Get the total balance of the customer.
*
Expand Down
35 changes: 35 additions & 0 deletions src/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Cashier;

use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
Expand Down Expand Up @@ -37,6 +38,40 @@ public function coupon()
return new Coupon($this->discount->coupon);
}

/**
* Get the promotion code applied to create this discount.
*
* @return \Laravel\Cashier\PromotionCode|null
*/
public function promotionCode()
{
if (! is_null($this->discount->promotion_code) && ! is_string($this->discount->promotion_code)) {
return new PromotionCode($this->discount->promotion_code);
}
}

/**
* Get the date that the coupon was applied.
*
* @return \Carbon\Carbon
*/
public function start()
{
return Carbon::createFromTimestamp($this->discount->start);
}

/**
* Get the date that this discount will end.
*
* @return \Carbon\Carbon|null
*/
public function end()
{
if (! is_null($this->discount->end)) {
return Carbon::createFromTimestamp($this->discount->end);
}
}

/**
* Get the Stripe Discount instance.
*
Expand Down
92 changes: 92 additions & 0 deletions src/PromotionCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Laravel\Cashier;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stripe\PromotionCode as StripePromotionCode;

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

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

/**
* Get the coupon that belongs to the promotion code.
*
* @return \Laravel\Cashier\Coupon
*/
public function coupon()
{
return new Coupon($this->promotionCode->coupon);
}

/**
* Get the Stripe PromotionCode instance.
*
* @return \Stripe\PromotionCode
*/
public function asStripePromotionCode()
{
return $this->promotionCode;
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->asStripePromotionCode()->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
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}

/**
* Dynamically get values from the Stripe object.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->promotionCode->{$key};
}
}
40 changes: 40 additions & 0 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,46 @@ public function latestPayment()
}
}

/**
* The discount that applies to the subscription, if applicable.
*
* @return \Laravel\Cashier\Discount|null
*/
public function discount()
{
$subscription = $this->asStripeSubscription(['discount.promotion_code']);

return $subscription->discount
? new Discount($subscription->discount)
: null;
}

/**
* Apply a coupon to the subscription.
*
* @param string $coupon
* @return void
*/
public function applyCoupon($coupon)
{
$this->updateStripeSubscription([
'coupon' => $coupon,
]);
}

/**
* Apply a promotion code to the subscription.
*
* @param string $promotionCodeId
* @return void
*/
public function applyPromotionCode($promotionCodeId)
{
$this->updateStripeSubscription([
'promotion_code' => $promotionCodeId,
]);
}

/**
* Make sure a subscription is not incomplete when performing changes.
*
Expand Down
Loading