From e5a10a4c4db93109eebb00f7d49ad176620b0816 Mon Sep 17 00:00:00 2001 From: Divyajose <75604843+divya-intelli@users.noreply.github.com> Date: Wed, 21 Jul 2021 19:05:55 +0530 Subject: [PATCH] Checks if ApigeeX and gives option to install m10 module (#495) --- .../ApigeeDevportalKickstartMonetization.php | 8 ++- .../ApigeeMonetizationConfigurationForm.php | 55 +++++++++++++------ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Installer/ApigeeDevportalKickstartMonetization.php b/src/Installer/ApigeeDevportalKickstartMonetization.php index 6c6c2685..92f0d2c9 100644 --- a/src/Installer/ApigeeDevportalKickstartMonetization.php +++ b/src/Installer/ApigeeDevportalKickstartMonetization.php @@ -38,7 +38,13 @@ public static function isMonetizable() { $sdk_connector = \Drupal::service('apigee_edge.sdk_connector'); $organization_controller = new OrganizationController($sdk_connector->getClient()); $organization = $organization_controller->load($sdk_connector->getOrganization()); - return ($organization->getPropertyValue('features.isMonetizationEnabled') === 'true'); + // Check if org is Hybrid or ApigeeX. + if ($organization && ('CLOUD' === $organization->getRuntimeType() || 'HYBRID' === $organization->getRuntimeType()) && $organization->getAddonsConfig()) { + return (TRUE === $organization->getAddonsConfig()->getMonetizationConfig()->getEnabled()); + } + else { + return ($organization->getPropertyValue('features.isMonetizationEnabled') === 'true'); + } } catch (\Exception $exception) { // Do not log the exception here. This litters the logs since this is run diff --git a/src/Installer/Form/ApigeeMonetizationConfigurationForm.php b/src/Installer/Form/ApigeeMonetizationConfigurationForm.php index 798e8547..d7c798e3 100644 --- a/src/Installer/Form/ApigeeMonetizationConfigurationForm.php +++ b/src/Installer/Form/ApigeeMonetizationConfigurationForm.php @@ -96,6 +96,13 @@ class ApigeeMonetizationConfigurationForm extends FormBase { */ protected $missingDependencies; + /** + * TRUE if organization is monetizable ApigeeX. + * + * @var bool + */ + protected $isOrgApigeeX; + /** * ApigeeM10nConfigurationForm constructor. * @@ -121,8 +128,16 @@ public function __construct(SDKConnectorInterface $sdk_connector, LanguageManage // cache these values? $organization_controller = new OrganizationController($client); $organization = $organization_controller->load($organization_id); + if ($organization && ('CLOUD' === $organization->getRuntimeType() || 'HYBRID' === $organization->getRuntimeType())) { + if ($this->isMonetizable = $organization->getAddonsConfig()->getMonetizationConfig()->getEnabled() === TRUE) { + // Set the organization. + $this->organization = $organization; + + $this->isOrgApigeeX = TRUE; + } + } /** @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface $organization */ - if ($this->isMonetizable = $organization->getPropertyValue('features.isMonetizationEnabled') === 'true') { + elseif ($this->isMonetizable = $organization->getPropertyValue('features.isMonetizationEnabled') === 'true') { // Set the organization. $organization_profile_controller = new OrganizationProfileController($organization_id, $client); $this->organization = $organization_profile_controller->load($organization_id); @@ -245,16 +260,20 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#description' => $this->t('Enable monetization for your Apigee Edge organization.'), ]; - $form['modules']['apigee_m10n_add_credit'] = [ - '#title' => $this->t('Enable Add Credit module'), - '#type' => 'checkbox', - '#description' => $this->t('Allow users to add credit to their prepaid balances.'), - '#states' => [ - 'visible' => [ - 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], + // Don't show add credit checkbox as for now ApigeeX does not support prepaid functionality. + // TODO: remove this restriction when ApigeeX supports prepaid functionality. + if (!$this->isOrgApigeeX) { + $form['modules']['apigee_m10n_add_credit'] = [ + '#title' => $this->t('Enable Add Credit module'), + '#type' => 'checkbox', + '#description' => $this->t('Allow users to add credit to their prepaid balances.'), + '#states' => [ + 'visible' => [ + 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], + ], ], - ], - ]; + ]; + } $form['store'] = [ '#type' => 'details', @@ -296,10 +315,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; - $form['store']['default_currency'] = [ - '#type' => 'value', - '#value' => $this->organization->getCurrencyCode(), - ]; + // Hide for now as ApigeeX does not support add credit. + // TODO: remove this restriction when ApigeeX supports prepaid functionality. + if (!$this->isOrgApigeeX) { + $form['store']['default_currency'] = [ + '#type' => 'value', + '#value' => $this->organization->getCurrencyCode(), + ]; + } $form['store']['type'] = [ '#type' => 'value', @@ -351,8 +374,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; } - // Add default address from organization. - if ($addresses = $this->organization->getAddresses()) { + // Check if ApigeeX and add default address from organization for org other than ApigeeX. + if (!$this->isOrgApigeeX && $addresses = $this->organization->getAddresses()) { /** @var \Apigee\Edge\Api\Monetization\Structure\Address $address */ $address = reset($addresses);