braintree_cashier-8.x-4.x-dev/src/Controller/ThankYouController.php
src/Controller/ThankYouController.php
<?php
namespace Drupal\braintree_cashier\Controller;
use Drupal\braintree_cashier\Entity\BraintreeCashierSubscriptionInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class ThankYouController.
*/
class ThankYouController extends ControllerBase {
/**
* Drupal\Core\Logger\LoggerChannelInterface definition.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Braintree Cashier service.
*
* @var \Drupal\braintree_cashier\BraintreeCashierService
*/
protected $bcService;
/**
* Braintree Cashier Subscription service.
*
* @var \Drupal\braintree_cashier\SubscriptionService
*/
protected $subscriptionService;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->logger = $container->get('logger.channel.braintree_cashier');
$instance->bcService = $container->get('braintree_cashier.braintree_cashier_service');
$instance->subscriptionService = $container->get('braintree_cashier.subscription_service');
return $instance;
}
/**
* View the thank you page after completing checkout.
*
* @return array
* The render array.
*/
public function view(BraintreeCashierSubscriptionInterface $subscription) {
$billing_plan = $subscription->getBillingPlan();
$braintree_plan = $this->bcService->getBraintreeBillingPlan($billing_plan->getBraintreePlanId());
$braintree_subscription = $this->subscriptionService->asBraintreeSubscription($subscription);
$build = [
'#subscription' => $subscription,
'#braintree_subscription' => $braintree_subscription,
'#cache' => [
'max-age' => 0,
],
'#username' => $subscription->getSubscribedUser()->getAccountName(),
'#subscription_tab_url' => Url::fromRoute('braintree_cashier.my_subscription', ['user' => $subscription->getSubscribedUserId()]),
'#billing_frequency' => $braintree_plan->billingFrequency,
'#never_expires' => $braintree_subscription->neverExpires,
'#next_billing_date' => $this->subscriptionService->getFormattedPeriodEndDate($subscription),
'#next_billing_period_amount' => $this->bcService->getFormattedAmount($braintree_subscription->nextBillingPeriodAmount),
];
if ($subscription->isTrialing()) {
$build = array_merge($build, [
'#theme' => 'braintree_cashier_thank_you_with_trial',
]);
}
else {
$transaction = $braintree_subscription->transactions[0];
$build = array_merge($build, [
'#theme' => 'braintree_cashier_thank_you_no_trial',
'#amount_paid' => $this->bcService->getFormattedAmount($transaction->amount),
]);
}
return $build;
}
/**
* Access control handler for this route.
*
* @param \Drupal\Core\Session\AccountInterface $browsing_account
* The user account browsing.
* @param \Drupal\braintree_cashier\Entity\BraintreeCashierSubscriptionInterface $subscription
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function accessRoute(AccountInterface $browsing_account, BraintreeCashierSubscriptionInterface $subscription) {
$is_own_subscription = $browsing_account->id() == $subscription->getSubscribedUserId();
$is_recent_purchase = $subscription->getCreatedTime() > (time() - 60*30);
return AccessResult::allowedIf($is_own_subscription && $is_recent_purchase)->addCacheableDependency($subscription);
}
}
