|
19 | 19 | use craft\commerce\stripe\gateways\PaymentIntents; |
20 | 20 | use craft\commerce\web\assets\commercecp\CommerceCpAsset; |
21 | 21 | use craft\helpers\App; |
| 22 | +use craft\helpers\Cp; |
| 23 | +use craft\helpers\Html; |
22 | 24 | use craft\helpers\StringHelper; |
23 | 25 | use craft\helpers\UrlHelper; |
24 | 26 | use Throwable; |
@@ -236,7 +238,7 @@ public function actionSubscribe(): ?Response |
236 | 238 | } catch (SubscriptionException $exception) { |
237 | 239 | $error = $exception->getMessage(); |
238 | 240 | } |
239 | | - |
| 241 | + |
240 | 242 | if ($subscription && $returnUrl) { |
241 | 243 | $returnUrl = $this->getView()->renderObjectTemplate($returnUrl, $subscription); |
242 | 244 | $subscriptionRecord = SubscriptionRecord::findOne($subscription->id); |
@@ -468,6 +470,160 @@ public function actionCompleteSubscription(): ?Response |
468 | 470 | } |
469 | 471 |
|
470 | 472 |
|
| 473 | + /** |
| 474 | + * @since 5.7.0 |
| 475 | + */ |
| 476 | + public function actionDeleteSubscriptionsModal(): Response |
| 477 | + { |
| 478 | + $this->requireCpRequest(); |
| 479 | + $this->requireAcceptsJson(); |
| 480 | + $this->requirePermission('deleteUsers'); |
| 481 | + |
| 482 | + $numSubscriptions = count($this->request->getRequiredParam('subscriptionIds')); |
| 483 | + |
| 484 | + return $this->_renderGatewayCancelModal('commerce/subscriptions/delete-subscriptions') |
| 485 | + ->submitButtonLabel(Craft::t('app', 'Delete {type}', [ |
| 486 | + 'type' => $numSubscriptions === 1 ? Subscription::lowerDisplayName() : Subscription::pluralLowerDisplayName(), |
| 487 | + ])); |
| 488 | + } |
| 489 | + |
| 490 | + /** |
| 491 | + * @since 5.7.0 |
| 492 | + */ |
| 493 | + public function actionDeleteSubscriptions(): Response |
| 494 | + { |
| 495 | + $this->requireCpRequest(); |
| 496 | + $this->requireAcceptsJson(); |
| 497 | + $this->requirePermission('deleteUsers'); |
| 498 | + |
| 499 | + $subscriptions = $this->_subscriptionsFromRequest(); |
| 500 | + $this->_cancelSubscriptionsAtGateway($subscriptions); |
| 501 | + |
| 502 | + foreach ($subscriptions as $subscription) { |
| 503 | + if (!Craft::$app->getElements()->deleteElement($subscription)) { |
| 504 | + Craft::warning('Failed to delete subscription ' . $subscription->id . ' (' . $subscription->reference . ')', __METHOD__); |
| 505 | + } |
| 506 | + } |
| 507 | + |
| 508 | + $numSubscriptions = count($subscriptions); |
| 509 | + |
| 510 | + return $this->asSuccess(Craft::t('app', '{type} deleted.', [ |
| 511 | + 'type' => $numSubscriptions === 1 ? Subscription::displayName() : Subscription::pluralDisplayName(), |
| 512 | + ])); |
| 513 | + } |
| 514 | + |
| 515 | + /** |
| 516 | + * Returns the gateway cancel modal response, with an action URL for the submit endpoint. |
| 517 | + */ |
| 518 | + private function _renderGatewayCancelModal(string $actionUrl): \craft\web\Response |
| 519 | + { |
| 520 | + $subscriptionIds = collect($this->request->getRequiredParam('subscriptionIds'))->filter()->map(fn($id) => (int)$id)->all(); |
| 521 | + $gatewayId = (int)$this->request->getRequiredParam('gatewayId'); |
| 522 | + |
| 523 | + $gateway = Plugin::getInstance()->getGateways()->getGatewayById($gatewayId); |
| 524 | + $subscription = Subscription::find()->id($subscriptionIds)->status(null)->one(); |
| 525 | + |
| 526 | + $cancelFormHtml = ''; |
| 527 | + if ($gateway instanceof SubscriptionGateway && $subscription) { |
| 528 | + $cancelFormHtml = $gateway->getCancelSubscriptionFormHtml($subscription); |
| 529 | + } |
| 530 | + |
| 531 | + return $this->asCpModal() |
| 532 | + ->action($actionUrl) |
| 533 | + ->contentHtml(function() use ($cancelFormHtml, $subscriptionIds, $gatewayId) { |
| 534 | + $view = Craft::$app->getView(); |
| 535 | + |
| 536 | + if ($cancelFormHtml) { |
| 537 | + $view->registerJsWithVars( |
| 538 | + fn($formId, $inputName) => <<<JS |
| 539 | + (function() { |
| 540 | + var form = document.getElementById($formId); |
| 541 | + var radios = document.querySelectorAll(`input[name=$inputName]`); |
| 542 | + function toggle() { |
| 543 | + var checked = document.querySelector(`input[name=$inputName]:checked`); |
| 544 | + form.style.display = (checked && checked.value === '1') ? '' : 'none'; |
| 545 | + } |
| 546 | + radios.forEach(function(r) { r.addEventListener('change', toggle); }); |
| 547 | + })(); |
| 548 | + JS, |
| 549 | + [ |
| 550 | + $view->namespaceInputId('cancel-form'), |
| 551 | + $view->namespaceInputName('cancelWithGateway'), |
| 552 | + ] |
| 553 | + ); |
| 554 | + } |
| 555 | + |
| 556 | + return Cp::fieldHtml('template:_includes/forms/radioGroup.twig', [ |
| 557 | + 'label' => Craft::t('commerce', 'Gateway'), |
| 558 | + 'name' => 'cancelWithGateway', |
| 559 | + 'value' => '1', |
| 560 | + 'options' => [ |
| 561 | + ['label' => Craft::t('commerce', 'Cancel with gateway now'), 'value' => '1'], |
| 562 | + ['label' => Craft::t('commerce', 'Leave gateway subscription as-is'), 'value' => '0'], |
| 563 | + ], |
| 564 | + ]) . |
| 565 | + ($cancelFormHtml ? Html::tag('div', $cancelFormHtml, ['id' => 'cancel-form']) : '') . |
| 566 | + implode('', array_map(fn($id) => Html::hiddenInput('subscriptionIds[]', (string)$id), $subscriptionIds)) . |
| 567 | + Html::hiddenInput('gatewayId', (string)$gatewayId); |
| 568 | + }); |
| 569 | + } |
| 570 | + |
| 571 | + /** |
| 572 | + * @return Subscription[] |
| 573 | + */ |
| 574 | + private function _subscriptionsFromRequest(): array |
| 575 | + { |
| 576 | + $subscriptionIds = collect($this->request->getRequiredParam('subscriptionIds'))->filter()->map(fn($id) => (int)$id)->all(); |
| 577 | + |
| 578 | + return Subscription::find() |
| 579 | + ->id($subscriptionIds) |
| 580 | + ->status(null) |
| 581 | + ->all(); |
| 582 | + } |
| 583 | + |
| 584 | + /** |
| 585 | + * Cancels the given subscriptions at the gateway if the request opted in. Returns whether anything was cancelled. |
| 586 | + * |
| 587 | + * @param Subscription[] $subscriptions |
| 588 | + */ |
| 589 | + private function _cancelSubscriptionsAtGateway(array $subscriptions): bool |
| 590 | + { |
| 591 | + $cancelWithGateway = (bool)$this->request->getBodyParam('cancelWithGateway', false); |
| 592 | + if (!$cancelWithGateway) { |
| 593 | + return false; |
| 594 | + } |
| 595 | + |
| 596 | + $gatewayId = (int)$this->request->getRequiredParam('gatewayId'); |
| 597 | + $gateway = Plugin::getInstance()->getGateways()->getGatewayById($gatewayId); |
| 598 | + if (!$gateway instanceof SubscriptionGateway) { |
| 599 | + return false; |
| 600 | + } |
| 601 | + |
| 602 | + $parameters = $gateway->getCancelSubscriptionFormModel(); |
| 603 | + foreach ($parameters->attributes() as $attribute) { |
| 604 | + $value = $this->request->getBodyParam($attribute); |
| 605 | + if ($value !== null) { |
| 606 | + $parameters->$attribute = $value; |
| 607 | + } |
| 608 | + } |
| 609 | + |
| 610 | + $subscriptionsService = Plugin::getInstance()->getSubscriptions(); |
| 611 | + $cancelled = false; |
| 612 | + |
| 613 | + foreach ($subscriptions as $subscription) { |
| 614 | + if (!$subscription->isExpired) { |
| 615 | + try { |
| 616 | + $subscriptionsService->cancelSubscription($subscription, $parameters); |
| 617 | + $cancelled = true; |
| 618 | + } catch (Throwable $e) { |
| 619 | + Craft::warning('Failed to cancel subscription ' . $subscription->reference . ' with gateway: ' . $e->getMessage(), __METHOD__); |
| 620 | + } |
| 621 | + } |
| 622 | + } |
| 623 | + |
| 624 | + return $cancelled; |
| 625 | + } |
| 626 | + |
471 | 627 | /** |
472 | 628 | * @param Subscription $subscription |
473 | 629 | * @throws ForbiddenHttpException |
|
0 commit comments