Skip to content

Commit

Permalink
MDL-69166 core_payment: add payment accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy authored and rezaies committed Oct 27, 2020
1 parent 3c87455 commit 6ff3087
Show file tree
Hide file tree
Showing 40 changed files with 1,049 additions and 58 deletions.
9 changes: 9 additions & 0 deletions admin/settings/payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

// This file defines settingpages and externalpages under the "Payment" category

$ADMIN->add('payment', new admin_externalpage(
'paymentaccounts',
new lang_string('paymentaccounts', 'payment'),
new moodle_url("/payment/accounts.php"),
['moodle/payment:manageaccounts', 'moodle/payment:viewpayments']));
1 change: 1 addition & 0 deletions admin/settings/top.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
$ADMIN->add('root', new admin_category('location', new lang_string('location','admin')));
$ADMIN->add('root', new admin_category('language', new lang_string('language')));
$ADMIN->add('root', new admin_category('messaging', new lang_string('messagingcategory', 'admin')));
$ADMIN->add('root', new admin_category('payment', new lang_string('payments', 'payment')));
$ADMIN->add('root', new admin_category('modules', new lang_string('plugins', 'admin')));
$ADMIN->add('root', new admin_category('security', new lang_string('security','admin')));
$ADMIN->add('root', new admin_category('appearance', new lang_string('appearance','admin')));
Expand Down
3 changes: 2 additions & 1 deletion enrol/fee/classes/payment/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class provider implements \core_payment\local\callback\provider {
* Callback function that returns the enrolment cost for the course that $instanceid enrolment instance belongs to.
*
* @param int $instanceid The enrolment instance id
* @return array['amount' => float, 'currency' => string]
* @return array['amount' => float, 'currency' => string, 'accountid' => int]
*/
public static function get_cost(int $instanceid): array {
global $DB;
Expand All @@ -47,6 +47,7 @@ public static function get_cost(int $instanceid): array {
return [
'amount' => (float) $instance->cost,
'currency' => $instance->currency,
'accountid' => $instance->customint1,
];
}

Expand Down
5 changes: 5 additions & 0 deletions enrol/fee/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function enrol_page_hook(stdClass $instance) {
'isguestuser' => isguestuser(),
'cost' => $localisedcost,
'currency' => $instance->currency,
'accountid' => $instance->customint1,
'amount' => $cost,
'instanceid' => $instance->id,
'description' => get_string('purchasedescription', 'enrol_fee',
Expand Down Expand Up @@ -309,6 +310,10 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $context)
$mform->addElement('select', 'status', get_string('status', 'enrol_fee'), $options);
$mform->setDefault('status', $this->get_config('status'));

$mform->addElement('select', 'customint1', get_string('paymentaccount', 'payment'),
['' => ''] + \core_payment\helper::get_payment_accounts_menu($context));
$mform->addRule('customint1', get_string('required'), 'required', null, 'client');

$mform->addElement('text', 'cost', get_string('cost', 'enrol_fee'), array('size' => 4));
$mform->setType('cost', PARAM_RAW);
$mform->setDefault('cost', format_float($this->get_config('cost'), 2, true));
Expand Down
2 changes: 2 additions & 0 deletions enrol/fee/templates/payment_region.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"cost": "$108.50",
"amount": 108.50,
"currency": "AUD",
"accountid": 1,
"instanceid": 11,
"description": "Enrolment in course Introduction to algorithms",
"isguestuser": false
Expand All @@ -64,6 +65,7 @@
id="gateways-modal-trigger-{{ uniqid }}"
data-amount="{{amount}}"
data-currency="{{currency}}"
data-accountid="{{accountid}}"
data-component="enrol_fee"
data-componentid="{{instanceid}}"
data-description={{# quote }}{{description}}{{/ quote }}
Expand Down
10 changes: 10 additions & 0 deletions lang/en/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['accountname'] = 'Account name';
$string['accountnotavailable'] = 'Not available';
$string['callbacknotimplemented'] = 'The callback is not implemented for component {$a}.';
$string['createaccount'] = 'Create payment account';
$string['feeincludesurcharge'] = '{$a->fee} (includes {$a->surcharge}% surcharge for using this payment type)';
$string['gatewaycannotbeenabled'] = 'The payment gateway cannot be enabled because the configuration is incomplete.';
$string['gatewaydisabled'] = 'Disabled';
$string['gatewayenabled'] = 'Enabled';
$string['gatewaynotfound'] = 'Gateway not found';
$string['nocurrencysupported'] = 'No payment in any currency is supported. Please make sure that at least one payment gateway is enabled.';
$string['nogateway'] = 'There is no payment gateway that can be used.';
$string['nogatewayselected'] = 'You first need to select a payment gateway.';
$string['payments'] = 'Payments';
$string['paymentaccount'] = 'Payment account';
$string['paymentaccounts'] = 'Payment accounts';
$string['selectpaymenttype'] = 'Select payment type';
$string['supportedcurrencies'] = 'Supported currencies';
$string['surcharge'] = 'Surcharge (percentage)';
Expand Down
1 change: 1 addition & 0 deletions lang/en/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
$string['type_mod'] = 'Activity module';
$string['type_mod_plural'] = 'Activity modules';
$string['type_pgmanage'] = 'Manage payment gateways';
$string['type_pg'] = 'Payment gateway';
$string['type_pg_plural'] = 'Payment gateways';
$string['type_plagiarism'] = 'Plagiarism plugin';
$string['type_plagiarism_plural'] = 'Plagiarism plugins';
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/form/persistent.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function get_data() {
/**
* Return the persistent object associated with this form instance.
*
* @return core\persistent
* @return \core\persistent
*/
final protected function get_persistent() {
return $this->persistent;
Expand Down
18 changes: 18 additions & 0 deletions lib/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -2576,4 +2576,22 @@
'editingteacher' => CAP_ALLOW,
]
],

// Allow to manage payment accounts.
'moodle/payment:manageaccounts' => [
'captype' => 'write',
'riskbitmask' => RISK_PERSONAL | RISK_CONFIG | RISK_DATALOSS,
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
]
],

// Allow to view payments.
'moodle/payment:viewpayments' => [
'captype' => 'read',
'riskbitmask' => RISK_PERSONAL,
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
]
],
);
31 changes: 31 additions & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4288,6 +4288,35 @@
<INDEX NAME="instance" UNIQUE="false" FIELDS="contextid, contenttype, instanceid"/>
</INDEXES>
</TABLE>
<TABLE NAME="payment_accounts" COMMENT="Payment accounts">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="idnumber" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="payment_gateways" COMMENT="Configuration for one gateway for one payment account">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="accountid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="gateway" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="config" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="accountid" TYPE="foreign" FIELDS="accountid" REFTABLE="payment_accounts" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="payments" COMMENT="Stores information about payments">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
Expand All @@ -4296,13 +4325,15 @@
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="amount" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="currency" TYPE="char" LENGTH="3" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="accountid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="gateway" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id"/>
<KEY NAME="accountid" TYPE="foreign" FIELDS="accountid" REFTABLE="payment_accounts" REFFIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="component" UNIQUE="false" FIELDS="component"/>
Expand Down
63 changes: 63 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2896,5 +2896,68 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021052500.32);
}

if ($oldversion < 2021052500.33) {

// Define table payment_accounts to be created.
$table = new xmldb_table('payment_accounts');

// Adding fields to table payment_accounts.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);

// Adding keys to table payment_accounts.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Conditionally launch create table for payment_accounts.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Define table payment_gateways to be created.
$table = new xmldb_table('payment_gateways');

// Adding fields to table payment_gateways.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('accountid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('gateway', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
$table->add_field('config', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);

// Adding keys to table payment_gateways.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('accountid', XMLDB_KEY_FOREIGN, ['accountid'], 'payment_accounts', ['id']);

// Conditionally launch create table for payment_gateways.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Define field accountid to be added to payments.
$table = new xmldb_table('payments');
$field = new xmldb_field('accountid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'currency');

// Conditionally launch add field accountid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define key accountid (foreign) to be added to payments.
$table = new xmldb_table('payments');
$key = new xmldb_key('accountid', XMLDB_KEY_FOREIGN, ['accountid'], 'payment_accounts', ['id']);

// Launch add key accountid.
$dbman->add_key($table, $key);

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.33);
}

return true;
}
71 changes: 71 additions & 0 deletions payment/accounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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/>.

/**
* Management of payment accounts
*
* @package core_payment
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once(__DIR__ . '/../config.php');
require_once($CFG->libdir . '/adminlib.php');

admin_externalpage_setup('paymentaccounts');
$PAGE->set_heading(get_string('paymentaccounts', 'payment'));

$enabledplugins = \core\plugininfo\pg::get_enabled_plugins();

echo $OUTPUT->header();

$accounts = \core_payment\helper::get_payment_accounts_to_manage(context_system::instance());
$table = new html_table();
$table->head = [get_string('accountname', 'payment'), get_string('type_pg', 'plugin'), ''];
$table->colclasses = ['', '', 'mdl-right'];
$table->data = [];
foreach ($accounts as $account) {
$gateways = [];
$canmanage = has_capability('moodle/payment:manageaccounts', $account->get_context());
foreach ($account->get_gateways() as $gateway) {
$status = $gateway->get('enabled') ? $OUTPUT->pix_icon('i/valid', get_string('gatewayenabled', 'payment')) :
$OUTPUT->pix_icon('i/invalid', get_string('gatewaydisabled', 'payment'));
$gateways[] = $status .
($canmanage ? html_writer::link($gateway->get_edit_url(), $gateway->get_display_name()) : $gateway->get_display_name());
}
$name = $account->get_formatted_name();
if (!$account->is_available()) {
$name .= ' ' . html_writer::span(get_string('accountnotavailable', 'payment'), 'badge badge-warning');
}

$menu = new action_menu();
$menu->set_alignment(action_menu::TL, action_menu::BL);
$menu->set_menu_trigger(get_string('edit'));
if ($canmanage) {
$menu->add(new action_menu_link_secondary($account->get_edit_url(), null, get_string('edit')));
$deleteurl = $account->get_edit_url(['delete' => 1, 'sesskey' => sesskey()]);
$deleteaction = new confirm_action(get_string('deleteconfirm', 'tool_recyclebin'));
$menu->add(new action_menu_link_secondary($deleteurl, null, get_string('delete')));
}

$table->data[] = [$name, join(', ', $gateways), $OUTPUT->render($menu)];
}

echo html_writer::table($table);

echo $OUTPUT->single_button(new moodle_url('/payment/manage_account.php'), get_string('createaccount', 'payment'), 'get');

echo $OUTPUT->footer();
Loading

0 comments on commit 6ff3087

Please sign in to comment.