Skip to content

Commit

Permalink
Merge branch 'release/4.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Sep 28, 2023
2 parents 5cda87d + 7060879 commit b5a745f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes for Stripe for Craft Commerce

## 4.0.1 - 2023-09-28
- Fixed a PHP error that occurred when switching a subscription’s plan.

## 4.0.0 - 2023-09-13

- Added support for all of Stripe’s payment methods, including Apple Pay and Google Wallet. ([#223](https://github.com/craftcms/commerce-stripe/issues/223), [#222](https://github.com/craftcms/commerce-stripe/issues/222),[#212](https://github.com/craftcms/commerce-stripe/issues/212))
Expand Down
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,7 @@ Once the gateway has been saved (and it has an ID), revisiting its edit screen w
We recommend enabling _all_ available events for the webhook, in Stripe. Events that the plugin has no use for will be ignored.

Remember that the webhook URL will be different for each of your environments! The gateway itself may have a different ID in production than in development, due to [the way Project Config works](https://craftcms.com/docs/4.x/project-config.html#ids-uuids-and-handles)). Run one of these Commerce console commands to view your gateway’s configuration:

```bash
# Outputs a table with information about each gateway:
php craft commerce/gateways/list

# Outputs a single gateway’s webhook URL:
php craft commerce/gateways/webhook-url myGatewayHandle
```
Remember that the webhook URL will be different for each of your environments! The gateway itself may have a different ID in production than in development, due to [the way Project Config works](https://craftcms.com/docs/4.x/project-config.html#ids-uuids-and-handles)).

#### Local Development

Expand Down
39 changes: 17 additions & 22 deletions src/base/SubscriptionGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,43 +401,38 @@ public function switchSubscriptionPlan(Subscription $subscription, BasePlan $pla
$stripeSubscription = $this->getStripeClient()->subscriptions->retrieve($subscription->reference);
/** @var SubscriptionItem $item */
$item = $stripeSubscription->items->data[0];
$stripeSubscription->items = [

$request = [];

$request['items'] = [
[
'id' => $item->id,
'plan' => $plan->reference,
'quantity' => $parameters->quantity ?: $item->quantity,
],
];

/** @phpstan-ignore-next-line */
$stripeSubscription->prorate = (bool)$parameters->prorate;

if ($parameters->billingCycleAnchor) {
$stripeSubscription->billing_cycle_anchor = $parameters->billingCycleAnchor;
}

if ($parameters->quantity) {
$stripeSubscription->items[0]['quantity'] = $parameters->quantity;
$request['billing_cycle_anchor'] = $parameters->billingCycleAnchor;
}

if ($parameters->prorationDate) {
/** @phpstan-ignore-next-line */
$stripeSubscription->proration_date = $parameters->prorationDate;
$request['proration_date'] = $parameters->prorationDate;
}

$response = $this->createSubscriptionResponse($stripeSubscription->save());

// Bill immediately only for non-trials
if (!$subscription->getIsOnTrial() && $parameters->billImmediately) {
try {
$this->getStripeClient()->invoices->create([
'customer' => $stripeSubscription->customer,
'subscription' => $stripeSubscription->id,
]);
} catch (Throwable $exception) {
// Or, maybe, Stripe already invoiced them because reasons.
if (!$parameters->prorate) {
$request['proration_behavior'] = 'none';
} else {
if ($parameters->billImmediately) {
$request['proration_behavior'] = 'always_invoice';
} else {
$request['proration_behavior'] = 'create_prorations';
}
}

$stripeSubscription = $this->getStripeClient()->subscriptions->update($stripeSubscription->id, $request);
$response = $this->createSubscriptionResponse($stripeSubscription);

return $response;
}

Expand Down

0 comments on commit b5a745f

Please sign in to comment.