}
*/
-const processPayment = async(gateway, amount, currency, component, componentid, description, callback) => {
+const processPayment = async(gateway, {value, currency, surcharge = 0}, component, componentid, description, callback) => {
const paymentMethod = await import(`pg_${gateway}/gateways_modal`);
- paymentMethod.process(amount, currency, component, componentid, description, callback);
+ value += value * surcharge / 100;
+ paymentMethod.process(value, currency, component, componentid, description, callback);
};
/**
diff --git a/payment/amd/src/selectors.js b/payment/amd/src/selectors.js
index 1a571e8b458..3fea06c9cc4 100644
--- a/payment/amd/src/selectors.js
+++ b/payment/amd/src/selectors.js
@@ -23,6 +23,9 @@
*/
export default {
+ elements: {
+ gateways: '[data-region="gateways-container"] input[type="radio"]',
+ },
regions: {
gatewaysContainer: '[data-region="gateways-container"]',
costContainer: '[data-region="fee-breakdown-container"]',
diff --git a/payment/classes/external/get_gateways_for_currency.php b/payment/classes/external/get_gateways_for_currency.php
index 4028e638fe7..42fe3f02211 100644
--- a/payment/classes/external/get_gateways_for_currency.php
+++ b/payment/classes/external/get_gateways_for_currency.php
@@ -67,6 +67,7 @@ class get_gateways_for_currency extends external_api {
'shortname' => $gateway,
'name' => get_string('gatewayname', 'pg_' . $gateway),
'description' => get_string('gatewaydescription', 'pg_' . $gateway),
+ 'surcharge' => \core_payment\helper::get_gateway_surcharge($gateway),
];
}
@@ -84,6 +85,7 @@ class get_gateways_for_currency extends external_api {
'shortname' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
'name' => new external_value(PARAM_TEXT, 'Human readable name of the gateway'),
'description' => new external_value(PARAM_TEXT, 'description of the gateway'),
+ 'surcharge' => new external_value(PARAM_INT, 'percentage of surcharge when using the gateway'),
])
);
}
diff --git a/payment/classes/helper.php b/payment/classes/helper.php
index 58c8925ed76..2adf0978570 100644
--- a/payment/classes/helper.php
+++ b/payment/classes/helper.php
@@ -76,6 +76,16 @@ class helper {
return $gateways;
}
+ /**
+ * Returns the percentage of surcharge that is applied when using a gateway
+ *
+ * @param string $gateway Name of the gateway
+ * @return int
+ */
+ public static function get_gateway_surcharge(string $gateway): int {
+ return get_config('pg_' . $gateway, 'surcharge') ?: 0;
+ }
+
/**
* Returns the attributes to place on a pay button.
*
@@ -164,4 +174,16 @@ class helper {
return $id;
}
+
+ /**
+ * This functions adds the settings that are common for all payment gateways.
+ *
+ * @param \admin_settingpage $settings The settings object
+ * @param string $gateway The gateway name prefic with pg_
+ */
+ public static function add_common_gateway_settings(\admin_settingpage $settings, string $gateway): void {
+ $settings->add(new \admin_setting_configtext($gateway . '/surcharge', get_string('surcharge', 'core_payment'),
+ get_string('surcharge_desc', 'core_payment'), 0, PARAM_INT));
+
+ }
}
diff --git a/payment/gateway/paypal/classes/external/transaction_complete.php b/payment/gateway/paypal/classes/external/transaction_complete.php
index 2b95d2ae72e..889f13a99c1 100644
--- a/payment/gateway/paypal/classes/external/transaction_complete.php
+++ b/payment/gateway/paypal/classes/external/transaction_complete.php
@@ -77,6 +77,11 @@ class transaction_complete extends external_api {
'currency' => $currency
] = payment_helper::get_cost($component, $componentid);
+ // Add surcharge if there is any.
+ if ($config->surcharge) {
+ $amount += $amount * $config->surcharge / 100;
+ }
+
$paypalhelper = new paypal_helper($config->clientid, $config->secret, $sandbox);
$orderdetails = $paypalhelper->get_order_details($orderid);
diff --git a/payment/gateway/paypal/settings.php b/payment/gateway/paypal/settings.php
index cdd850af9ed..2463cfaf215 100644
--- a/payment/gateway/paypal/settings.php
+++ b/payment/gateway/paypal/settings.php
@@ -41,4 +41,6 @@ if ($ADMIN->fulltree) {
];
$settings->add(new admin_setting_configselect('pg_paypal/environment', get_string('environment', 'pg_paypal'),
get_string('environment_desc', 'pg_paypal'), 'live', $options));
+
+ \core_payment\helper::add_common_gateway_settings($settings, 'pg_paypal');
}
diff --git a/payment/templates/fee_breakdown.mustache b/payment/templates/fee_breakdown.mustache
index 2e74fd031b6..1483461515f 100644
--- a/payment/templates/fee_breakdown.mustache
+++ b/payment/templates/fee_breakdown.mustache
@@ -31,8 +31,19 @@
}}
- {{# str }} labelvalue, core, {
- "label": {{# quote }}{{# str }} cost {{/ str }}{{/ quote }},
- "value": "{{fee}}"
- } {{/ str }}
+ {{#surcharge}}
+ {{# str }} labelvalue, core, {
+ "label": {{# quote }}{{# str }} cost {{/ str }}{{/ quote }},
+ "value": {{# quote }}{{# str }} feeincludesurcharge, core_payment, {
+ "fee": "{{fee}}",
+ "surcharge": {{surcharge}}
+ } {{/ str }}{{/ quote }}
+ } {{/ str }}
+ {{/surcharge}}
+ {{^surcharge}}
+ {{# str }} labelvalue, core, {
+ "label": {{# quote }}{{# str }} cost {{/ str }}{{/ quote }},
+ "value": "{{fee}}"
+ } {{/ str }}
+ {{/surcharge}}
diff --git a/payment/templates/gateway.mustache b/payment/templates/gateway.mustache
index e93fbadc836..9e2c41aedfa 100644
--- a/payment/templates/gateway.mustache
+++ b/payment/templates/gateway.mustache
@@ -29,6 +29,7 @@
* shortname
* name
* description
+ * surcharge
* image
Example context (json):
@@ -36,11 +37,12 @@
"shortname": "paypal",
"name": "PayPal",
"description": "A description for PayPal.",
+ "surcharge": "3"
}
}}