Merge branch 'MDL-70287-master-3' of git://github.com/rezaies/moodle

This commit is contained in:
Sara Arjona 2021-03-10 20:48:44 +01:00
commit 2c0038f7f9
10 changed files with 211 additions and 22 deletions

View File

@ -37,7 +37,7 @@ class service_provider implements \core_payment\local\callback\service_provider
* Callback function that returns the enrolment cost and the accountid
* for the course that $instanceid enrolment instance belongs to.
*
* @param string $paymentarea
* @param string $paymentarea Payment area
* @param int $instanceid The enrolment instance id
* @return \core_payment\local\entities\payable
*/
@ -49,6 +49,21 @@ class service_provider implements \core_payment\local\callback\service_provider
return new \core_payment\local\entities\payable($instance->cost, $instance->currency, $instance->customint1);
}
/**
* Callback function that returns the URL of the page the user should be redirected to in the case of a successful payment.
*
* @param string $paymentarea Payment area
* @param int $instanceid The enrolment instance id
* @return \moodle_url
*/
public static function get_success_url(string $paymentarea, int $instanceid): \moodle_url {
global $DB;
$courseid = $DB->get_field('enrol', 'courseid', ['enrol' => 'fee', 'id' => $instanceid], MUST_EXIST);
return new \moodle_url('/course/view.php', ['id' => $courseid]);
}
/**
* Callback function that delivers what the user paid for to them.
*

View File

@ -212,6 +212,7 @@ class enrol_fee_plugin extends enrol_plugin {
'instanceid' => $instance->id,
'description' => get_string('purchasedescription', 'enrol_fee',
format_string($course->fullname, true, ['context' => $context])),
'successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false),
];
echo $OUTPUT->render_from_template('enrol_fee/payment_region', $data);
}

View File

@ -28,17 +28,20 @@
* data-itemid
* data-cost
* data-description
* data-successurl
Context variables required for this template:
* cost - Human readable cost string including amount and currency
* instanceid - Id of the enrolment instance
* description - The description for this purchase
* successurl - The URL of the course
Example context (json):
{
"cost": "$108.50",
"instanceid": 11,
"description": "Enrolment in course Introduction to algorithms",
"successurl": "https://moodlesite/course/view.php?id=2",
"isguestuser": false
}
@ -63,6 +66,7 @@
data-paymentarea="fee"
data-itemid="{{instanceid}}"
data-cost="{{cost}}"
data-successurl="{{successurl}}"
data-description={{# quote }}{{description}}{{/ quote }}
>
{{# str }} sendpaymentbutton, enrol_fee {{/ str }}

View File

@ -0,0 +1,133 @@
<?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/>.
/**
* Unit tests for the enrol_fee's payment subsystem callback implementation.
*
* @package enrol_fee
* @category test
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_fee\payment;
/**
* Unit tests for the enrol_fee's payment subsystem callback implementation.
*
* @coversDefaultClass service_provider
*/
class service_provider_testcase extends \advanced_testcase {
/**
* Test for service_provider::get_payable().
*
* @covers ::get_payable
*/
public function test_get_payable() {
global $DB;
$this->resetAfterTest();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$feeplugin = enrol_get_plugin('fee');
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$course = $generator->create_course();
$data = [
'courseid' => $course->id,
'customint1' => $account->get('id'),
'cost' => 250,
'currency' => 'USD',
'roleid' => $studentrole->id,
];
$id = $feeplugin->add_instance($course, $data);
$payable = service_provider::get_payable('fee', $id);
$this->assertEquals($account->get('id'), $payable->get_account_id());
$this->assertEquals(250, $payable->get_amount());
$this->assertEquals('USD', $payable->get_currency());
}
/**
* Test for service_provider::get_success_url().
*
* @covers ::get_success_url
*/
public function test_get_success_url() {
global $CFG, $DB;
$this->resetAfterTest();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$feeplugin = enrol_get_plugin('fee');
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$course = $generator->create_course();
$data = [
'courseid' => $course->id,
'customint1' => $account->get('id'),
'cost' => 250,
'currency' => 'USD',
'roleid' => $studentrole->id,
];
$id = $feeplugin->add_instance($course, $data);
$successurl = service_provider::get_success_url('fee', $id);
$this->assertEquals(
$CFG->wwwroot . '/course/view.php?id=' . $course->id,
$successurl->out(false)
);
}
/**
* Test for service_provider::deliver_order().
*
* @covers ::deliver_order
*/
public function test_deliver_order() {
global $DB;
$this->resetAfterTest();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$feeplugin = enrol_get_plugin('fee');
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$course = $generator->create_course();
$context = \context_course::instance($course->id);
$user = $generator->create_user();
$data = [
'courseid' => $course->id,
'customint1' => $account->get('id'),
'cost' => 250,
'currency' => 'USD',
'roleid' => $studentrole->id,
];
$id = $feeplugin->add_instance($course, $data);
$paymentid = $generator->get_plugin_generator('core_payment')->create_payment([
'accountid' => $account->get('id'),
'amount' => 10,
'userid' => $user->id
]);
service_provider::deliver_order('fee', $id, $paymentid, $user->id);
$this->assertTrue(is_enrolled($context, $user));
$this->assertTrue(user_has_role_assignment($user->id, $studentrole->id, $context->id));
}
}

View File

@ -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.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).then(function(a){p.hide();j.default.addNotification({message:a,type:"success"});location.reload();return a}).catch(function(a){return j.default.alert("",a)})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,i.add)(a)}).catch()}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){var g;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:g=a.sent;return a.abrupt("return",g.process(c,d,e,f));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});
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).then(function(a){p.hide();j.default.addNotification({message:a,type:"success"});location.href=l.dataset.successurl;return a}).catch(function(a){return j.default.alert("",a)})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,i.add)(a)}).catch()}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){var g;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:g=a.sent;return a.abrupt("return",g.process(c,d,e,f));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

View File

@ -95,7 +95,7 @@ const show = async(rootNode, {
message: message,
type: 'success',
});
location.reload();
location.href = rootNode.dataset.successurl;
// The following return statement is never reached. It is put here just to make eslint happy.
return message;

View File

@ -60,9 +60,9 @@ class helper {
/**
* Returns the list of gateways that can process payments in the given currency.
*
* @param string $component
* @param string $paymentarea
* @param int $itemid
* @param string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @param int $itemid An identifier that is known to the component
* @return string[]
*/
public static function get_available_gateways(string $component, string $paymentarea, int $itemid): array {
@ -141,8 +141,8 @@ class helper {
/**
* Returns the attributes to place on a pay button.
*
* @param string $component Name of the component that the itemid belongs to
* @param string $paymentarea
* @param string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @param int $itemid An internal identifier that is used by the component
* @param string $description Description of the payment
* @return array
@ -151,6 +151,7 @@ class helper {
string $description): array {
$payable = static::get_payable($component, $paymentarea, $itemid);
$successurl = static::get_success_url($component, $paymentarea, $itemid);
return [
'id' => 'gateways-modal-trigger',
@ -161,11 +162,14 @@ class helper {
'data-itemid' => $itemid,
'data-cost' => static::get_cost_as_string($payable->get_amount(), $payable->get_currency()),
'data-description' => $description,
'data-successurl' => $successurl->out(false),
];
}
/**
* @param string $component
* Get the name of the service provider class
*
* @param string $component The component
* @return string
* @throws \coding_exception
*/
@ -185,8 +189,8 @@ class helper {
/**
* Asks the payable from the related component.
*
* @param string $component Name of the component that the itemid belongs to
* @param string $paymentarea
* @param string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @param int $itemid An internal identifier that is used by the component
* @return local\entities\payable
*/
@ -196,13 +200,26 @@ class helper {
return component_class_callback($providerclass, 'get_payable', [$paymentarea, $itemid]);
}
/**
* Fetches the URL of the page the user should be redirected to from the related component
*
* @param string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @param int $itemid An identifier that is known to the component
* @return \moodle_url
*/
public static function get_success_url(string $component, string $paymentarea, int $itemid): \moodle_url {
$providerclass = static::get_service_provider_classname($component);
return component_class_callback($providerclass, 'get_success_url', [$paymentarea, $itemid]);
}
/**
* Returns the gateway configuration for given component and gateway
*
* @param string $component
* @param string $paymentarea
* @param int $itemid
* @param string $gatewayname
* @param string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @param int $itemid An identifier that is known to the component
* @param string $gatewayname The gateway name
* @return array
* @throws \moodle_exception
*/
@ -225,8 +242,8 @@ class helper {
*
* @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 string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @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
@ -244,8 +261,8 @@ class helper {
* Each payment gateway may then store the additional information their way.
*
* @param int $accountid Account id
* @param string $component Name of the component that the itemid belongs to
* @param string $paymentarea
* @param string $component Name of the component that the paymentarea and itemid belong to
* @param string $paymentarea Payment area
* @param int $itemid An internal identifier that is used by the component
* @param int $userid Id of the user who is paying
* @param float $amount Amount of payment

View File

@ -35,14 +35,28 @@ namespace core_payment\local\callback;
interface service_provider {
/**
* @param string $paymentarea
* Callback function that returns the cost of the given item in the specified payment area,
* along with the accountid that payments are paid to.
*
* @param string $paymentarea Payment area
* @param int $itemid An identifier that is known to the plugin
* @return \core_payment\local\entities\payable
*/
public static function get_payable(string $paymentarea, int $itemid): \core_payment\local\entities\payable;
/**
* @param string $paymentarea
* Callback function that returns the URL of the page the user should be redirected to in the case of a successful payment.
*
* @param string $paymentarea Payment area
* @param int $itemid An identifier that is known to the plugin
* @return \moodle_url
*/
public static function get_success_url(string $paymentarea, int $itemid): \moodle_url;
/**
* Callback function that delivers what the user paid for to them.
*
* @param string $paymentarea Payment area
* @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

5
payment/upgrade.txt Normal file
View File

@ -0,0 +1,5 @@
This files describes API changes in /payment/*,
information provided here is intended especially for developers.
=== 3.11 ===
* Service provider plugins using the payment subsystem are now required to implement the get_success_url method.