MDL-69166 core_payment: Show cost on the modal when no gateway is chosen
authorShamim Rezaie <shamim@moodle.com>
Thu, 1 Oct 2020 14:53:54 +0000 (00:53 +1000)
committerShamim Rezaie <shamim@moodle.com>
Tue, 27 Oct 2020 03:40:49 +0000 (14:40 +1100)
enrol/fee/classes/plugin.php
enrol/fee/templates/payment_region.mustache
payment/amd/build/gateways_modal.min.js
payment/amd/build/gateways_modal.min.js.map
payment/amd/src/gateways_modal.js
payment/classes/helper.php

index aa5ed3c..1408139 100644 (file)
@@ -204,7 +204,7 @@ class enrol_fee_plugin extends enrol_plugin {
 
             $data = [
                 'isguestuser' => isguestuser(),
-                'cost' => \core_payment\helper::get_cost_as_string($instance->cost, $instance->currency),
+                'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency),
                 'instanceid' => $instance->id,
                 'description' => get_string('purchasedescription', 'enrol_fee',
                     format_string($course->fullname, true, ['context' => $context])),
index 649ab5a..b1aa574 100644 (file)
     * none
 
     Data attributes required for JS:
-    * data-amount
-    * data-currency
     * data-component
     * data-componentid
+    * data-cost
     * data-description
 
     Context variables required for this template:
     * cost - Human readable cost string including amount and currency
-    * amount - amount of the cost
-    * currency - currency of the cost
     * instanceid - Id of the enrolment instance
     * description - The description for this purchase
 
     Example context (json):
     {
         "cost": "$108.50",
-        "amount": 108.50,
-        "currency": "AUD",
         "instanceid": 11,
         "description": "Enrolment in course Introduction to algorithms",
         "isguestuser": false
@@ -64,6 +59,7 @@
             id="gateways-modal-trigger-{{ uniqid }}"
             data-component="enrol_fee"
             data-componentid="{{instanceid}}"
+            data-cost="{{cost}}"
             data-description={{# quote }}{{description}}{{/ quote }}
         >
             {{# str }} sendpaymentbutton, enrol_fee {{/ str }}
index 4f1e815..2779809 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js and b/payment/amd/build/gateways_modal.min.js differ
index ede802b..1a72198 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js.map and b/payment/amd/build/gateways_modal.min.js.map differ
index 643371a..8cb88ae 100644 (file)
@@ -122,7 +122,7 @@ const show = async(rootNode, {
     // Re-calculate the cost when gateway is changed.
     rootElement.addEventListener('change', e => {
         if (e.target.matches(Selectors.elements.gateways)) {
-            updateCostRegion(rootElement);
+            updateCostRegion(rootElement, rootNode.dataset.cost);
         }
     });
 
@@ -134,7 +134,7 @@ const show = async(rootNode, {
     const {html, js} = await Templates.renderForPromise('core_payment/gateways', context);
     Templates.replaceNodeContents(rootElement.querySelector(Selectors.regions.gatewaysContainer), html, js);
     selectSingleGateway(rootElement);
-    await updateCostRegion(rootElement);
+    await updateCostRegion(rootElement, rootNode.dataset.cost);
 };
 
 /**
@@ -154,18 +154,17 @@ const selectSingleGateway = root => {
  * Shows the cost of the item the user is purchasing in the cost region.
  *
  * @param {HTMLElement} root An HTMLElement that contains the cost region
- * @param {number} amount The amount part of cost
- * @param {string} currency The currency part of cost in the 3-letter ISO-4217 format
+ * @param {string} defaultCost The default cost that is going to be displayed if no gateway is selected
  * @returns {Promise<void>}
  */
-const updateCostRegion = async(root) => {
-    const surcharge = parseInt((root.querySelector(Selectors.values.gateway) || {dataset: {surcharge: 0}}).dataset.surcharge);
-    const cost = root.querySelector(Selectors.values.gateway).dataset.cost;
+const updateCostRegion = async(root, defaultCost = '') => {
+    const gatewayElement = root.querySelector(Selectors.values.gateway);
+    const surcharge = parseInt((gatewayElement || {dataset: {surcharge: 0}}).dataset.surcharge);
+    const cost = (gatewayElement || {dataset: {cost: defaultCost}}).dataset.cost;
 
     const {html, js} = await Templates.renderForPromise('core_payment/fee_breakdown', {fee: cost, surcharge});
     Templates.replaceNodeContents(root.querySelector(Selectors.regions.costContainer), html, js);
 };
-updateCostRegion.locale = getString("localecldr", "langconfig");
 
 /**
  * Process payment using the selected gateway.
index 9e21115..9c4b0cb 100644 (file)
@@ -142,11 +142,17 @@ class helper {
      * @return array
      */
     public static function gateways_modal_link_params(string $component, int $componentid, string $description): array {
+        [
+            'amount' => $amount,
+            'currency' => $currency
+        ] = self::get_cost($component, $componentid);
+
         return [
             'id' => 'gateways-modal-trigger',
             'role' => 'button',
             'data-component' => $component,
             'data-componentid' => $componentid,
+            'data-cost' => self::get_cost_as_string($amount, $currency),
             'data-description' => $description,
         ];
     }