recurly-8.x-1.x-dev/modules/recurly_hosted/recurly_hosted.module
modules/recurly_hosted/recurly_hosted.module
<?php
/**
* @file
* Integrates with Recurly to provide hosted links for Drupal user accounts.
*
* Enabling this module is not recommended if your site is using Commerce
* Recurly or other modules that provide local management of Recurly accounts.
*/
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
/**
* Implements hook_recurly_url_info().
*/
function recurly_hosted_recurly_url_info($operation, $context) {
if (\Drupal::currentUser()->isAnonymous()) {
return Url::fromRoute('recurly.redirect_to_registration');
}
$entity = $context['entity'];
switch ($operation) {
case 'update_billing':
$recurly_account = recurly_account_load(['entity_id' => $entity->id()]);
return recurly_hosted_account_manage_url($recurly_account, TRUE);
case 'subscribe':
$entity_type = $context['entity_type'];
$account_code = $entity_type . '-' . $entity->id();
// Fetch tokens for the user's name and e-mail address. Replace them with
// proper values, and then sanitize as plain text.
$settings_raw = \Drupal::service('recurly.entity_type')->getUserInfoMappings();
foreach ($settings_raw as &$value) {
$value = \Drupal::token()->replace($value, ['user' => \Drupal::currentUser()]);
}
$settings = array_map(['Drupal\Component\Utility\Html', 'escape'], $settings_raw);
// Return the requested plan-purchase URL.
return recurly_hosted_subscription_plan_purchase_url($context['plan_code'], $account_code, $entity->label(), $settings);
}
}
/**
* Implements hook_entity_type_alter().
*/
function recurly_hosted_entity_type_alter(array &$entity_types) {
\Drupal::service('recurly_hosted.entity_type')->entityTypeAlter($entity_types);
}
/**
* Returns an edit Url for a Recurly account.
*
* @param \Recurly_Account $recurly_account
* The recurly account values array returned by recurly_account_load.
*
* @return \Drupal\Core\Url
* A Drupal Url object for the plan's edit page at Recurly.
*/
function recurly_hosted_account_edit_url(\Recurly_Account $recurly_account) {
$recurly_url_manager = \Drupal::service('recurly.url_manager');
return $recurly_url_manager->hostedUrl('accounts/' . $recurly_account->account_code)->getUri();
}
/**
* Returns a front end management Url for a Recurly account.
*
* @param \Recurly_Account $recurly_account
* The recurly account object returned by recurly_account_load.
* @param bool $hosted_login_token
* Boolean indicating whether or not to use the hosted login token in the Url
* if it's available in the account object; defaults to FALSE.
*
* @return \Drupal\Core\Url
* A Drupal Url object for the account's front end management page at Recurly;
* uses the hosted login token if specified and it's available.
*/
function recurly_hosted_account_manage_url(\Recurly_Account $recurly_account, $hosted_login_token = FALSE) {
$recurly_url_manager = \Drupal::service('recurly.url_manager');
// Return a Url using the hosted login token if available.
if ($hosted_login_token && !empty($recurly_account->hosted_login_token)) {
return $recurly_url_manager->hostedUrl('account/billing_info/edit?ht=' . $recurly_account->hosted_login_token);
}
return $recurly_url_manager->hostedUrl('account');
}
/**
* Returns a Hosted Payment Page Url for the given subscription plan.
*
* @param int $plan_code
* The subscription plan code.
* @param string $account_code
* Optional. The account code to assign to this new subscriber. This unique
* identifier is permanent and is used to bind the account to Drupal IDs.
* @param string $username
* Optional. The username that will be stored at Recurly for this subscriber.
* Usually just used for reference.
* @param array $settings
* An optional array of settings used to pre-populate the payment form.
* - first_name: the customer's first name.
* - last_name: the customer's last name.
* - email: the customer's e-mail address.
*
* @return \Drupal\Core\Url
* A Drupal Url object for the plan's Hosted Payment Page at Recurly.
*/
function recurly_hosted_subscription_plan_purchase_url($plan_code, $account_code = NULL, $username = NULL, array $settings = NULL) {
$url = 'subscribe/' . $plan_code;
if ($account_code) {
$url .= '/' . $account_code;
if ($username) {
$url .= '/' . $username;
}
}
if (!empty($settings)) {
$url .= '?' . UrlHelper::buildQuery($settings);
}
$recurly_url_manager = \Drupal::service('recurly.url_manager');
return $recurly_url_manager->hostedUrl($url);
}
