From 5337ca485cc6f06bb278fc4543ab9e29bb4bfc23 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Thu, 9 Jan 2020 01:41:12 +1100 Subject: [PATCH] MDL-69166 core_payment: helper methods to get cost and to deliver order Also create the infrastructure to let components know when they have to deliver what they sold. We are going to use namespace functions instead of traditional callbacks. --- lang/en/payment.php | 1 + payment/classes/helper.php | 36 +++++++++++++++ payment/classes/local/callback/provider.php | 50 +++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 payment/classes/local/callback/provider.php diff --git a/lang/en/payment.php b/lang/en/payment.php index 9d0e615ab69..632f3be2769 100644 --- a/lang/en/payment.php +++ b/lang/en/payment.php @@ -22,6 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['callbacknotimplemented'] = 'The callback is not implemented for component {$a}.'; $string['nogateway'] = 'There is no payment gateway that can be used.'; $string['nogatewayselected'] = 'You first need to select a payment gateway.'; $string['selectpaymenttype'] = 'Select payment type'; diff --git a/payment/classes/helper.php b/payment/classes/helper.php index 6c4d631e6d8..0c8687567d8 100644 --- a/payment/classes/helper.php +++ b/payment/classes/helper.php @@ -113,4 +113,40 @@ class helper { 'data-description' => $description, ]; } + + /** + * Asks the cost from the related component. + * + * @param string $component Name of the component that the componentid belongs to + * @param int $componentid An internal identifier that is used by the component + * @return array['amount' => float, 'currency' => string] + * @throws \moodle_exception + */ + public static function get_cost(string $component, int $componentid): array { + $cost = component_class_callback("$component\\payment\\provider", 'get_cost', [$componentid]); + + if ($cost === null) { + throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component); + } + + return $cost; + } + + /** + * Delivers what the user paid for. + * + * @param string $component Name of the component that the componentid belongs to + * @param int $componentid An internal identifier that is used by the component + * @return bool Whether successful or not + * @throws \moodle_exception + */ + public static function deliver_order(string $component, int $componentid): bool { + $result = component_class_callback("$component\\payment\\provider", 'deliver_order', [$componentid]); + + if ($result === null) { + throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component); + } + + return $result; + } } diff --git a/payment/classes/local/callback/provider.php b/payment/classes/local/callback/provider.php new file mode 100644 index 00000000000..5d9db19eaab --- /dev/null +++ b/payment/classes/local/callback/provider.php @@ -0,0 +1,50 @@ +. + +/** + * This file contains the \core_payment\local\local\callback\provider interface. + * + * Plugins should implement this if they use payment subsystem. + * + * @package core_payment + * @copyright 2020 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_payment\local\callback; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The provider interface for plugins to provide callbacks which are needed by the payment subsystem. + * + * @copyright 2020 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +interface provider { + + /** + * @param int $identifier An identifier that is known to the plugin + * @return array['amount' => float, 'currency' => string] + */ + public static function get_cost(int $identifier): array; + + /** + * @param int $identifier An identifier that is known to the plugin + * @return bool Whether successful or not + */ + public static function deliver_order(int $identifier): bool; +} -- 2.17.1