Skip to content

Commit

Permalink
Update for coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Nov 10, 2020
1 parent 6a98fa5 commit 4166903
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/SubscriptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class SubscriptionBuilder
*/
protected $coupon;

/**
* Determines if user redeemable promotion codes are available in Stripe Checkout.
*
* @var bool
*/
protected $allowPromotionCodes = false;

/**
* The metadata to apply to the subscription.
*
Expand Down Expand Up @@ -200,6 +207,18 @@ public function withCoupon($coupon)
return $this;
}

/**
* Enables user redeemable promotion codes.
*
* @return $this
*/
public function allowPromotionCodes()
{
$this->allowPromotionCodes = true;

return $this;
}

/**
* The metadata to apply to a new subscription.
*
Expand Down Expand Up @@ -312,9 +331,12 @@ public function checkout(array $sessionOptions = [], array $customerOptions = []
return Checkout::create($this->owner, array_merge([
'mode' => 'subscription',
'line_items' => collect($this->items)->values()->all(),
'allow_promotion_codes' => $this->allowPromotionCodes,
'discounts' => [
'coupon' => $this->coupon,
],
'default_tax_rates' => $this->getTaxRatesForPayload(),
'subscription_data' => [
'coupon' => $this->coupon,
'trial_end' => $trialEnd ? $trialEnd->getTimestamp() : null,
'metadata' => array_merge($this->metadata, ['name' => $this->name]),
],
Expand Down
13 changes: 8 additions & 5 deletions tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ public function test_customers_can_start_a_subscription_checkout_session()
'unit_amount' => 1500,
]);

$checkout = $user->newSubscription('default', $price->id)->checkout([
'success_url' => 'http://example.com',
'cancel_url' => 'http://example.com',
]);
$checkout = $user->newSubscription('default', $price->id)
->allowPromotionCodes()
->checkout([
'success_url' => 'http://example.com',
'cancel_url' => 'http://example.com',
]);

$this->assertInstanceOf(Checkout::class, $checkout);
$this->assertInstanceOf(StripeCheckoutSession::class, $checkout->asStripeCheckoutSession());
$this->assertInstanceOf(StripeCheckoutSession::class, $session = $checkout->asStripeCheckoutSession());
$this->assertTrue($session->allow_promotion_codes);
}
}

0 comments on commit 4166903

Please sign in to comment.