Commit 75df25e3 authored by Omar Mohamad - El Hassan Lopesino's avatar Omar Mohamad - El Hassan Lopesino
Browse files

Issue #3266550: Allow adding a return URL when the user goes to its customer portal

parent 3b5a68ce
Loading
Loading
Loading
Loading
+26 −10
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\stripe_registration\StripeRegistrationService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

/**
@@ -38,17 +39,26 @@ class UserSubscriptionsController extends ControllerBase {
   */
  private $logger;

  /**
   * Used to get the current return URL, plus the query parameters.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * UserSubscriptionsController constructor.
   *
   * @param \Drupal\stripe_registration\StripeRegistrationService $stripe_registration
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   */
  public function __construct(StripeRegistrationService $stripe_registration, LoggerChannelInterface $logger, AccountProxyInterface $current_user) {
  public function __construct(StripeRegistrationService $stripe_registration, LoggerChannelInterface $logger, AccountProxyInterface $current_user, RequestStack $requestStack) {
    $this->stripeRegistration = $stripe_registration;
    $this->logger = $logger;
    $this->currentUser = $current_user;
    $this->currentRequest = $requestStack->getCurrentRequest();
  }

  /**
@@ -58,7 +68,8 @@ class UserSubscriptionsController extends ControllerBase {
    return new static(
      $container->get('stripe_registration.stripe_api'),
      $container->get('logger.channel.stripe_registration'),
      $container->get('current_user')
      $container->get('current_user'),
      $container->get('request_stack')
    );
  }

@@ -69,7 +80,7 @@ class UserSubscriptionsController extends ControllerBase {
   *   Return Hello string.
   */
  public function redirectToSubscriptions() {
    return $this->redirect('stripe_registration.manage_subscriptions', ['user' => $this->currentUser()->id()]);
    return $this->redirect('stripe_registration.manage_subscriptions', ['user' => $this->currentUser()->id()], ['query' => $this->currentRequest->query->all()]);
  }

  /**
@@ -94,7 +105,7 @@ class UserSubscriptionsController extends ControllerBase {
   * @throws \Stripe\Exception\ApiErrorException
   */
  public function subscribe(): array {
    $remote_plans = $this->stripeRegistration->loadRemotePlanMultiple();
    $remote_plans = $this->stripeRegistration->loadRemotePlanMultiple(['active' => TRUE]);
    $user = User::load($this->currentUser->id());
    $build = [
      '#theme' => 'stripe_subscribe_plans',
@@ -232,12 +243,17 @@ class UserSubscriptionsController extends ControllerBase {
  public function manageSubscriptions($user) {
    try {
      $customer_id = $this->stripeRegistration->getLocalUserCustomerId($user);
      if ($this->currentRequest->query->has('return_url')) {
        $return_url_string = Url::fromUri('internal:' . $this->currentRequest->query->get('return_url'), ['absolute' => TRUE])->toString(TRUE)->getGeneratedUrl();
      }
      else {
        $return_url = Url::fromRoute('<front>', [], ['absolute' => TRUE]);
        // This was not fun.
        // @see https://www.drupal.org/node/2630808
        // @see https://drupal.stackexchange.com/questions/225956/cache-controller-with-json-response
        // @see https://www.lullabot.com/articles/early-rendering-a-lesson-in-debugging-drupal-8
        $return_url_string = $return_url->toString(TRUE)->getGeneratedUrl();
      }
      $session = \Stripe\BillingPortal\Session::create([
        'customer' => $customer_id,
        'return_url' => $return_url_string,