mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-69166 core_payment: Addressing various integration points
- Add help for 'payment account' field in the enrol instance form - Remove MOODLE_INTERNALs when not necessary - Add $userid to deliver_order - Check if provider classes implement the provider interface - Rename get_cost to get_payable - get_payable returns payable object - Improve registerEventListeners and added init - Rename payment\provider to payment\service_provider
This commit is contained in:
parent
d5a9d6e520
commit
f5d94d6305
@ -31,25 +31,22 @@ namespace enrol_fee\payment;
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,10 +55,11 @@ class provider implements \core_payment\local\callback\provider {
|
||||
* @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);
|
||||
|
||||
@ -75,7 +73,7 @@ class provider implements \core_payment\local\callback\provider {
|
||||
$timeend = 0;
|
||||
}
|
||||
|
||||
$plugin->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
|
||||
$plugin->enrol_user($instance, $userid, $instance->roleid, $timestart, $timeend);
|
||||
|
||||
return true;
|
||||
}
|
@ -313,6 +313,7 @@ class enrol_fee_plugin extends enrol_plugin {
|
||||
$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);
|
||||
|
@ -43,6 +43,8 @@ $string['fee:manage'] = 'Manage enrolled users';
|
||||
$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}';
|
||||
|
@ -58,6 +58,7 @@
|
||||
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}}"
|
||||
@ -70,6 +71,6 @@
|
||||
</div>
|
||||
{{#js}}
|
||||
require(['core_payment/gateways_modal'], function(modal) {
|
||||
modal.registerEventListeners(document.querySelector('#gateways-modal-trigger-{{ uniqid }}'));
|
||||
modal.init();
|
||||
});
|
||||
{{/js}}
|
||||
|
@ -31,7 +31,6 @@ $string['accountname'] = 'Account name';
|
||||
$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';
|
||||
|
2
payment/amd/build/gateways_modal.min.js
vendored
2
payment/amd/build/gateways_modal.min.js
vendored
@ -1,2 +1,2 @@
|
||||
define ("core_payment/gateways_modal",["exports","core/modal_factory","core/templates","core/str","./repository","./selectors","core/modal_events","core_payment/events","core/toast","core/notification","./modal_gateways"],function(a,b,c,d,e,f,g,h,i,j,k){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.registerEventListeners=a.registerEventListenersBySelector=void 0;b=l(b);c=l(c);f=l(f);g=l(g);h=l(h);j=l(j);k=l(k);var o="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function l(a){return a&&a.__esModule?a:{default:a}}function m(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function n(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var h=a.apply(b,c);function f(a){m(h,d,e,f,g,"next",a)}function g(a){m(h,d,e,f,g,"throw",a)}f(void 0)})}}a.registerEventListenersBySelector=function registerEventListenersBySelector(a){document.querySelectorAll(a).forEach(function(a){p(a)})};var p=function(a){a.addEventListener("click",function(b){b.preventDefault();q(a,{focusOnClose:b.target})})};a.registerEventListeners=p;var q=function(){var a=n(regeneratorRuntime.mark(function a(l){var m,n,o,p,q,u,v,w,x,y,z=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:m=1<z.length&&z[1]!==void 0?z[1]:{},n=m.focusOnClose,o=void 0===n?null:n;a.t0=b.default;a.t1=k.default.TYPE;a.next=5;return(0,d.get_string)("selectpaymenttype","core_payment");case 5:a.t2=a.sent;a.next=8;return c.default.render("core_payment/gateways_modal",{});case 8:a.t3=a.sent;a.t4={type:a.t1,title:a.t2,body:a.t3};a.next=12;return a.t0.create.call(a.t0,a.t4);case 12:p=a.sent;q=p.getRoot()[0];(0,i.addToastRegion)(q);p.show();p.getRoot().on(g.default.hidden,function(){p.destroy();try{o.focus()}catch(a){}});p.getRoot().on(h.default.proceed,function(a){var b=(q.querySelector(f.default.values.gateway)||{value:""}).value;if(b){t(b,l.dataset.component,l.dataset.paymentarea,l.dataset.itemid,l.dataset.description,function(a){var b=a.success,c=a.message,d=void 0===c?"":c;p.hide();if(b){j.default.addNotification({message:d,type:"success"});location.reload()}else{j.default.alert("",d)}})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,i.add)(a)})}a.preventDefault()});q.addEventListener("change",function(a){if(a.target.matches(f.default.elements.gateways)){s(q,l.dataset.cost)}});a.next=21;return(0,e.getAvailableGateways)(l.dataset.component,l.dataset.paymentarea,l.dataset.itemid);case 21:u=a.sent;v={gateways:u};a.next=25;return c.default.renderForPromise("core_payment/gateways",v);case 25:w=a.sent;x=w.html;y=w.js;c.default.replaceNodeContents(q.querySelector(f.default.regions.gatewaysContainer),x,y);r(q);a.next=32;return s(q,l.dataset.cost);case 32:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),r=function(a){var b=a.querySelectorAll(f.default.elements.gateways);if(1==b.length){b[0].checked=!0}},s=function(){var a=n(regeneratorRuntime.mark(function a(b){var d,e,g,h,i,j,k,l=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d=1<l.length&&l[1]!==void 0?l[1]:"";e=b.querySelector(f.default.values.gateway);g=parseInt((e||{dataset:{surcharge:0}}).dataset.surcharge);h=(e||{dataset:{cost:d}}).dataset.cost;a.next=6;return c.default.renderForPromise("core_payment/fee_breakdown",{fee:h,surcharge:g});case 6:i=a.sent;j=i.html;k=i.js;c.default.replaceNodeContents(b.querySelector(f.default.regions.costContainer),j,k);case 10:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),t=function(){var a=n(regeneratorRuntime.mark(function a(b,c,d,e,f,g){var h;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return"function"==typeof o.define&&o.define.amd?new Promise(function(a,c){o.require(["paygw_".concat(b,"/gateways_modal")],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&o.require&&"component"===o.require.loader?Promise.resolve(require(("paygw_".concat(b,"/gateways_modal")))):Promise.resolve(o["paygw_".concat(b,"/gateways_modal")]);case 2:h=a.sent;h.process(c,d,e,f,g);case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}()});
|
||||
define ("core_payment/gateways_modal",["exports","core/modal_factory","core/templates","core/str","./repository","./selectors","core/modal_events","core_payment/events","core/toast","core/notification","./modal_gateways"],function(a,b,c,d,e,f,g,h,i,j,k){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=l(b);c=l(c);f=l(f);g=l(g);h=l(h);j=l(j);k=l(k);var o="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function l(a){return a&&a.__esModule?a:{default:a}}function m(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function n(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var h=a.apply(b,c);function f(a){m(h,d,e,f,g,"next",a)}function g(a){m(h,d,e,f,g,"throw",a)}f(void 0)})}}var p=function(){document.addEventListener("click",function(a){var b=a.target.closest("[data-action=\"core_payment/triggerPayment\"]");if(b){a.preventDefault();q(b,{focusOnClose:a.target})}})},q=function(){var a=n(regeneratorRuntime.mark(function a(l){var m,n,o,p,q,u,v,w,x,y,z=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:m=1<z.length&&z[1]!==void 0?z[1]:{},n=m.focusOnClose,o=void 0===n?null:n;a.t0=b.default;a.t1=k.default.TYPE;a.next=5;return(0,d.get_string)("selectpaymenttype","core_payment");case 5:a.t2=a.sent;a.next=8;return c.default.render("core_payment/gateways_modal",{});case 8:a.t3=a.sent;a.t4={type:a.t1,title:a.t2,body:a.t3};a.next=12;return a.t0.create.call(a.t0,a.t4);case 12:p=a.sent;q=p.getRoot()[0];(0,i.addToastRegion)(q);p.show();p.getRoot().on(g.default.hidden,function(){p.destroy();try{o.focus()}catch(a){}});p.getRoot().on(h.default.proceed,function(a){var b=(q.querySelector(f.default.values.gateway)||{value:""}).value;if(b){t(b,l.dataset.component,l.dataset.paymentarea,l.dataset.itemid,l.dataset.description,function(a){var b=a.success,c=a.message,d=void 0===c?"":c;p.hide();if(b){j.default.addNotification({message:d,type:"success"});location.reload()}else{j.default.alert("",d)}})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,i.add)(a)})}a.preventDefault()});q.addEventListener("change",function(a){if(a.target.matches(f.default.elements.gateways)){s(q,l.dataset.cost)}});a.next=21;return(0,e.getAvailableGateways)(l.dataset.component,l.dataset.paymentarea,l.dataset.itemid);case 21:u=a.sent;v={gateways:u};a.next=25;return c.default.renderForPromise("core_payment/gateways",v);case 25:w=a.sent;x=w.html;y=w.js;c.default.replaceNodeContents(q.querySelector(f.default.regions.gatewaysContainer),x,y);r(q);a.next=32;return s(q,l.dataset.cost);case 32:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),r=function(a){var b=a.querySelectorAll(f.default.elements.gateways);if(1==b.length){b[0].checked=!0}},s=function(){var a=n(regeneratorRuntime.mark(function a(b){var d,e,g,h,i,j,k,l=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d=1<l.length&&l[1]!==void 0?l[1]:"";e=b.querySelector(f.default.values.gateway);g=parseInt((e||{dataset:{surcharge:0}}).dataset.surcharge);h=(e||{dataset:{cost:d}}).dataset.cost;a.next=6;return c.default.renderForPromise("core_payment/fee_breakdown",{fee:h,surcharge:g});case 6:i=a.sent;j=i.html;k=i.js;c.default.replaceNodeContents(b.querySelector(f.default.regions.costContainer),j,k);case 10:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),t=function(){var a=n(regeneratorRuntime.mark(function a(b,c,d,e,f,g){var h;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return"function"==typeof o.define&&o.define.amd?new Promise(function(a,c){o.require(["paygw_".concat(b,"/gateways_modal")],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&o.require&&"component"===o.require.loader?Promise.resolve(require(("paygw_".concat(b,"/gateways_modal")))):Promise.resolve(o["paygw_".concat(b,"/gateways_modal")]);case 2:h=a.sent;h.process(c,d,e,f,g);case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),u=function(){if(!u.initialised){u.initialised=!0;p()}};a.init=u;u.initialised=!1});
|
||||
//# sourceMappingURL=gateways_modal.min.js.map
|
||||
|
File diff suppressed because one or more lines are too long
@ -35,24 +35,15 @@ import ModalGateways from './modal_gateways';
|
||||
|
||||
/**
|
||||
* 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});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -190,3 +181,22 @@ const processPayment = async(gateway, component, paymentArea, itemId, descriptio
|
||||
* @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;
|
||||
|
@ -68,10 +68,9 @@ class get_available_gateways extends external_api {
|
||||
|
||||
$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);
|
||||
|
@ -68,16 +68,14 @@ class helper {
|
||||
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;
|
||||
@ -149,41 +147,53 @@ class helper {
|
||||
* @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]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,9 +208,9 @@ class helper {
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@ -213,21 +223,18 @@ class helper {
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
// 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.
|
||||
*
|
||||
@ -26,28 +26,28 @@
|
||||
|
||||
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;
|
||||
}
|
70
payment/classes/local/entities/payable.php
Normal file
70
payment/classes/local/entities/payable.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
@ -67,14 +67,14 @@ class get_config_for_js extends external_api {
|
||||
]);
|
||||
|
||||
$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(),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -76,15 +76,12 @@ class transaction_complete extends external_api {
|
||||
$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);
|
||||
@ -102,8 +99,8 @@ class transaction_complete extends external_api {
|
||||
$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();
|
||||
@ -112,7 +109,7 @@ class transaction_complete extends external_api {
|
||||
|
||||
$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;
|
||||
|
@ -24,8 +24,6 @@
|
||||
|
||||
namespace paygw_paypal;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The gateway class for PayPal payment gateway.
|
||||
*
|
||||
|
@ -147,7 +147,7 @@ class accounts_testcase extends advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Provier for test_get_cost_as_string
|
||||
* Provider for test_get_cost_as_string
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user