$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])),
* 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
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 }}
// 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);
}
});
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);
};
/**
* 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.
* @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,
];
}