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; +}