Skip to content

Commit

Permalink
Update plan data when plan updates in Stripe
Browse files Browse the repository at this point in the history
Fixed #240
  • Loading branch information
lukeholder committed May 23, 2023
1 parent 6b73878 commit e0325e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Stripe for Craft Commerce

## Unreleased

- Plans’ data is now updated when the associated plan is updated in Stripe. ([#240](https://github.com/craftcms/commerce-stripe/issues/240))

## 3.1.1 - 2023-05-10

- Stripe customers’ default payment methods are now kept in sync with Craft users’ primary payment sources. ([#235](https://github.com/craftcms/commerce-stripe/issues/235))
Expand Down
22 changes: 14 additions & 8 deletions src/base/SubscriptionGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,10 @@ public function handleWebhook(array $data): void
switch ($data['type']) {
case 'payment_method.detached':
$this->handlePaymentMethodDetached($data);
// no break
// no break
case 'charge.refund.updated':
$this->handleRefundUpdated($data);
// no break
// no break
case 'plan.deleted':
case 'plan.updated':
$this->handlePlanEvent($data);
Expand Down Expand Up @@ -684,14 +684,20 @@ protected function handlePlanEvent(array $data): void
$this->configureStripeClient();
$planService = Commerce::getInstance()->getPlans();

if ($data['type'] == 'plan.deleted') {
$plan = $planService->getPlanByReference($data['data']['object']['id']);
$plan = $planService->getPlanByReference($data['data']['object']['id']);

if (!$plan) {
throw new InvalidConfigException('Plan with the reference “' . $data['data']['object']['id'] . '” not found when processing webhook ' . $data['id']);
}

if ($plan) {
$planService->archivePlanById($plan->id);
Craft::warning($plan->name . ' was archived because the corresponding plan was deleted on Stripe. (event "' . $data['id'] . '")', 'stripe');
}
if ($data['type'] == 'plan.deleted') {
$planService->archivePlanById($plan->id);
Craft::warning($plan->name . ' was archived because the corresponding plan was deleted on Stripe. (event "' . $data['id'] . '")', 'stripe');
} else {
$plan->planData = Json::encode($data['data']['object']);
$planService->savePlan($plan);
}

}

/**
Expand Down

0 comments on commit e0325e9

Please sign in to comment.