* @copyright 2020 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class provider implements \core_payment\local\callback\provider {
+class service_provider implements \core_payment\local\callback\service_provider {
/**
- * Callback function that returns the enrolment cost for the course that $instanceid enrolment instance belongs to.
+ * Callback function that returns the enrolment cost and the accountid
+ * for the course that $instanceid enrolment instance belongs to.
*
* @param string $paymentarea
* @param int $instanceid The enrolment instance id
- * @return array['amount' => float, 'currency' => string, 'accountid' => int]
+ * @return \core_payment\local\entities\payable
*/
- public static function get_cost(string $paymentarea, int $instanceid): array {
+ public static function get_payable(string $paymentarea, int $instanceid): \core_payment\local\entities\payable {
global $DB;
$instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST);
- return [
- 'amount' => (float) $instance->cost,
- 'currency' => $instance->currency,
- 'accountid' => $instance->customint1,
- ];
+ return new \core_payment\local\entities\payable($instance->cost, $instance->currency, $instance->customint1);
}
/**
* @param string $paymentarea
* @param int $instanceid The enrolment instance id
* @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference
+ * @param int $userid The userid the order is going to deliver to
* @return bool Whether successful or not
*/
- public static function deliver_order(string $paymentarea, int $instanceid, int $paymentid): bool {
- global $DB, $USER;
+ public static function deliver_order(string $paymentarea, int $instanceid, int $paymentid, int $userid): bool {
+ global $DB;
$instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST);
$timeend = 0;
}
- $plugin->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
+ $plugin->enrol_user($instance, $userid, $instance->roleid, $timestart, $timeend);
return true;
}
$mform->addElement('hidden', 'customint1');
$mform->setType('customint1', PARAM_INT);
}
+ $mform->addHelpButton('customint1', 'paymentaccount', 'enrol_fee');
$mform->addElement('text', 'cost', get_string('cost', 'enrol_fee'), array('size' => 4));
$mform->setType('cost', PARAM_RAW);
$string['fee:unenrol'] = 'Unenrol users from course';
$string['fee:unenrolself'] = 'Unenrol self from course';
$string['nocost'] = 'There is no cost to enrol in this course!';
+$string['paymentaccount'] = 'Payment account';
+$string['paymentaccount_help'] = 'Enrolment fees will be paid to this account.';
$string['pluginname'] = 'Enrolment on payment';
$string['pluginname_desc'] = 'The enrolment on payment enrolment method allows you to set up courses requiring a payment. If the fee for any course is set to zero, then students are not asked to pay for entry. There is a site-wide fee that you set here as a default for the whole site and then a course setting that you can set for each course individually. The course fee overrides the site fee.';
$string['purchasedescription'] = 'Enrolment in course {$a}';
class="btn btn-secondary"
type="button"
id="gateways-modal-trigger-{{ uniqid }}"
+ data-action="core_payment/triggerPayment"
data-component="enrol_fee"
data-paymentarea="fee"
data-itemid="{{instanceid}}"
</div>
{{#js}}
require(['core_payment/gateways_modal'], function(modal) {
- modal.registerEventListeners(document.querySelector('#gateways-modal-trigger-{{ uniqid }}'));
+ modal.init();
});
{{/js}}
$string['accountname_help'] = 'How this account will be identified for teachers or managers who set up payments (for example in the course enrolment plugin)';
$string['accountnotavailable'] = 'Not available';
$string['paymentaccountsexplained'] = 'Create one or multiple payment accounts for this site. Each account includes configuration for available payment gateways. The person who configures payments on the site (for example, payment for the course enrolment) will be able to chose from the available accounts.';
-$string['callbacknotimplemented'] = 'The callback is not implemented for component {$a}.';
$string['createaccount'] = 'Create payment account';
$string['deleteorarchive'] = 'Delete or archive';
$string['eventaccountcreated'] = 'Payment account created';
/**
* Register event listeners for the module.
- *
- * @param {string} nodeSelector The root to listen to.
*/
-export const registerEventListenersBySelector = (nodeSelector) => {
- document.querySelectorAll(nodeSelector).forEach((element) => {
- registerEventListeners(element);
- });
-};
+const registerEventListeners = () => {
+ document.addEventListener('click', e => {
+ const gatewayTrigger = e.target.closest('[data-action="core_payment/triggerPayment"]');
+ if (gatewayTrigger) {
+ e.preventDefault();
-/**
- * Register event listeners for the module.
- *
- * @param {HTMLElement} rootNode The root to listen to.
- */
-export const registerEventListeners = (rootNode) => {
- rootNode.addEventListener('click', (e) => {
- e.preventDefault();
- show(rootNode, {focusOnClose: e.target});
+ show(gatewayTrigger, {focusOnClose: e.target});
+ }
});
};
* @param {bool} success
* @param {string} message
*/
+
+/**
+ * Set up the payment actions.
+ */
+export const init = () => {
+ if (!init.initialised) {
+ // Event listeners should only be registered once.
+ init.initialised = true;
+ registerEventListeners();
+ }
+};
+
+/**
+ * Whether the init function was called before.
+ *
+ * @static
+ * @type {boolean}
+ */
+init.initialised = false;
$list = [];
$gateways = helper::get_available_gateways($params['component'], $params['paymentarea'], $params['itemid']);
- [
- 'amount' => $amount,
- 'currency' => $currency
- ] = helper::get_cost($params['component'], $params['paymentarea'], $params['itemid']);
+ $payable = helper::get_payable($params['component'], $params['paymentarea'], $params['itemid']);
+ $amount = $payable->get_amount();
+ $currency = $payable->get_currency();
foreach ($gateways as $gateway) {
$surcharge = helper::get_gateway_surcharge($gateway);
public static function get_available_gateways(string $component, string $paymentarea, int $itemid): array {
$gateways = [];
- [
- 'amount' => $amount,
- 'currency' => $currency,
- 'accountid' => $accountid,
- ] = self::get_cost($component, $paymentarea, $itemid);
- $account = new account($accountid);
+ $payable = static::get_payable($component, $paymentarea, $itemid);
+ $account = new account($payable->get_account_id());
+
if (!$account->get('id') || !$account->get('enabled')) {
return $gateways;
}
+ $currency = $payable->get_currency();
foreach ($account->get_gateways() as $plugin => $gateway) {
if (!$gateway->get('enabled')) {
continue;
* @param string $description Description of the payment
* @return array
*/
- public static function gateways_modal_link_params(string $component, string $paymentarea, int $itemid, string $description): array {
- [
- 'amount' => $amount,
- 'currency' => $currency
- ] = self::get_cost($component, $paymentarea, $itemid);
+ public static function gateways_modal_link_params(string $component, string $paymentarea, int $itemid,
+ string $description): array {
+
+ $payable = static::get_payable($component, $paymentarea, $itemid);
return [
'id' => 'gateways-modal-trigger',
'role' => 'button',
+ 'data-action' => 'core_payment/triggerPayment',
'data-component' => $component,
'data-paymentarea' => $paymentarea,
'data-itemid' => $itemid,
- 'data-cost' => self::get_cost_as_string($amount, $currency),
+ 'data-cost' => static::get_cost_as_string($payable->get_amount(), $payable->get_currency()),
'data-description' => $description,
];
}
/**
- * Asks the cost from the related component.
+ * @param string $component
+ * @return string
+ * @throws \coding_exception
+ */
+ private static function get_service_provider_classname(string $component) {
+ $providerclass = "$component\\payment\\service_provider";
+
+ if (class_exists($providerclass)) {
+ $rc = new \ReflectionClass($providerclass);
+ if ($rc->implementsInterface(local\callback\service_provider::class)) {
+ return $providerclass;
+ }
+ }
+
+ throw new \coding_exception("$component does not have an eligible implementation of payment service_provider.");
+ }
+
+ /**
+ * Asks the payable from the related component.
*
* @param string $component Name of the component that the itemid belongs to
* @param string $paymentarea
* @param int $itemid An internal identifier that is used by the component
- * @return array['amount' => float, 'currency' => string, 'accountid' => int]
- * @throws \moodle_exception
+ * @return local\entities\payable
*/
- public static function get_cost(string $component, string $paymentarea, int $itemid): array {
- $cost = component_class_callback("$component\\payment\\provider", 'get_cost', [$paymentarea, $itemid]);
+ public static function get_payable(string $component, string $paymentarea, int $itemid): local\entities\payable {
+ $providerclass = static::get_service_provider_classname($component);
- if ($cost === null || !is_array($cost) || !array_key_exists('amount', $cost)
- || !array_key_exists('currency', $cost) || !array_key_exists('accountid', $cost) ) {
- throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component);
- }
-
- return $cost;
+ return component_class_callback($providerclass, 'get_payable', [$paymentarea, $itemid]);
}
/**
*/
public static function get_gateway_configuration(string $component, string $paymentarea, int $itemid,
string $gatewayname): array {
- $x = self::get_cost($component, $paymentarea, $itemid);
+ $payable = self::get_payable($component, $paymentarea, $itemid);
$gateway = null;
- $account = new account($x['accountid']);
+ $account = new account($payable->get_account_id());
if ($account && $account->get('enabled')) {
$gateway = $account->get_gateways()[$gatewayname] ?? null;
}
/**
* Delivers what the user paid for.
*
- * @uses \core_payment\local\callback\provider::deliver_order()
+ * @uses \core_payment\local\callback\service_provider::deliver_order()
*
* @param string $component Name of the component that the itemid belongs to
* @param string $paymentarea
* @param int $itemid An internal identifier that is used by the component
* @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference
+ * @param int $userid The userid the order is going to deliver to
* @return bool Whether successful or not
*/
- public static function deliver_order(string $component, string $paymentarea, int $itemid, int $paymentid): bool {
- $result = component_class_callback("$component\\payment\\provider", 'deliver_order',
- [$paymentarea, $itemid, $paymentid]);
-
- if ($result === null) {
- throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component);
- }
+ public static function deliver_order(string $component, string $paymentarea, int $itemid, int $paymentid, int $userid): bool {
+ $providerclass = static::get_service_provider_classname($component);
+ $result = component_class_callback($providerclass, 'deliver_order', [$paymentarea, $itemid, $paymentid, $userid]);
return $result;
}
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
- * This file contains the \core_payment\local\local\callback\provider interface.
+ * This file contains the \core_payment\local\local\callback\service_provider interface.
*
* Plugins should implement this if they use payment subsystem.
*
namespace core_payment\local\callback;
-defined('MOODLE_INTERNAL') || die();
-
/**
- * The provider interface for plugins to provide callbacks which are needed by the payment subsystem.
+ * The service_provider interface for plugins to provide callbacks which are needed by the payment subsystem.
*
* @copyright 2020 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-interface provider {
+interface service_provider {
/**
* @param string $paymentarea
* @param int $itemid An identifier that is known to the plugin
- * @return array['amount' => float, 'currency' => string, 'accountid' => int]
+ * @return \core_payment\local\entities\payable
*/
- public static function get_cost(string $paymentarea, int $itemid): array;
+ public static function get_payable(string $paymentarea, int $itemid): \core_payment\local\entities\payable;
/**
* @param string $paymentarea
* @param int $itemid An identifier that is known to the plugin
* @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference
+ * @param int $userid The userid the order is going to deliver to
+ *
* @return bool Whether successful or not
*/
- public static function deliver_order(string $paymentarea, int $itemid, int $paymentid): bool;
+ public static function deliver_order(string $paymentarea, int $itemid, int $paymentid, int $userid): bool;
}
--- /dev/null
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The payable class.
+ *
+ * @package core_payment
+ * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core_payment\local\entities;
+
+/**
+ * The payable class.
+ *
+ * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class payable {
+ private $amount;
+ private $currency;
+ private $accountid;
+
+ public function __construct(float $amount, string $currency, int $accountid) {
+ $this->amount = $amount;
+ $this->currency = $currency;
+ $this->accountid = $accountid;
+ }
+
+ /**
+ * Get the amount of the payable cost.
+ *
+ * @return float
+ */
+ public function get_amount(): float {
+ return $this->amount;
+ }
+
+ /**
+ * Get the currency of the payable cost.
+ *
+ * @return string
+ */
+ public function get_currency(): string {
+ return $this->currency;
+ }
+
+ /**
+ * Get the id of the payment account the cost is payable to.
+ *
+ * @return int
+ */
+ public function get_account_id(): int {
+ return $this->accountid;
+ }
+}
]);
$config = helper::get_gateway_configuration($component, $paymentarea, $itemid, 'paypal');
- $cost = helper::get_cost($component, $paymentarea, $itemid);
+ $payable = helper::get_payable($component, $paymentarea, $itemid);
$surcharge = helper::get_gateway_surcharge('paypal');
return [
'clientid' => $config['clientid'],
'brandname' => $config['brandname'],
- 'cost' => helper::get_rounded_cost($cost['amount'], $cost['currency'], $surcharge),
- 'currency' => $cost['currency'],
+ 'cost' => helper::get_rounded_cost($payable->get_amount(), $payable->get_currency(), $surcharge),
+ 'currency' => $payable->get_currency(),
];
}
$config = (object)helper::get_gateway_configuration($component, $paymentarea, $itemid, 'paypal');
$sandbox = $config->environment == 'sandbox';
- [
- 'amount' => $amount,
- 'currency' => $currency,
- 'accountid' => $accountid,
- ] = payment_helper::get_cost($component, $paymentarea, $itemid);
+ $payable = payment_helper::get_payable($component, $paymentarea, $itemid);
+ $currency = $payable->get_currency();
// Add surcharge if there is any.
$surcharge = helper::get_gateway_surcharge('paypal');
- $amount = helper::get_rounded_cost($amount, $currency, $surcharge);
+ $amount = helper::get_rounded_cost($payable->get_amount(), $currency, $surcharge);
$paypalhelper = new paypal_helper($config->clientid, $config->secret, $sandbox);
$orderdetails = $paypalhelper->get_order_details($orderid);
$success = true;
// Everything is correct. Let's give them what they paid for.
try {
- $paymentid = payment_helper::save_payment((int) $accountid, $component, $paymentarea, $itemid,
- (int) $USER->id, $amount, $currency, 'paypal');
+ $paymentid = payment_helper::save_payment($payable->get_account_id(), $component, $paymentarea,
+ $itemid, (int) $USER->id, $amount, $currency, 'paypal');
// Store PayPal extra information.
$record = new \stdClass();
$DB->insert_record('paygw_paypal', $record);
- payment_helper::deliver_order($component, $paymentarea, $itemid, $paymentid);
+ payment_helper::deliver_order($component, $paymentarea, $itemid, $paymentid, (int) $USER->id);
} catch (\Exception $e) {
debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER);
$success = false;
namespace paygw_paypal;
-defined('MOODLE_INTERNAL') || die();
-
/**
* The gateway class for PayPal payment gateway.
*
}
/**
- * Provier for test_get_cost_as_string
+ * Provider for test_get_cost_as_string
*
* @return array[]
*/