diff --git a/lib/db/install.xml b/lib/db/install.xml old mode 100644 new mode 100755 index 478146184cc25..647649055ac69 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -4288,6 +4288,28 @@ + + + + + + + + + + + + + + + + + + + + + +
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index b9c5d34235919..575ef24b81066 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2862,5 +2862,39 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021052500.30); } + if ($oldversion < 2021052500.32) { + + // Define table payments to be created. + $table = new xmldb_table('payments'); + + // Adding fields to table payments. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('componentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('amount', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null); + $table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, XMLDB_NOTNULL, null, null); + $table->add_field('gateway', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + + // Adding keys to table payments. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']); + + // Adding indexes to table payments. + $table->add_index('component', XMLDB_INDEX_NOTUNIQUE, ['component']); + $table->add_index('componentid', XMLDB_INDEX_NOTUNIQUE, ['componentid']); + $table->add_index('gateway', XMLDB_INDEX_NOTUNIQUE, ['gateway']); + + // Conditionally launch create table for payments. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021052500.32); + } + return true; } diff --git a/payment/classes/helper.php b/payment/classes/helper.php index 0c8687567d8ea..e3b4f1a874600 100644 --- a/payment/classes/helper.php +++ b/payment/classes/helper.php @@ -149,4 +149,34 @@ public static function deliver_order(string $component, int $componentid): bool return $result; } + + /** + * Stores essential information about the payment and returns the "id" field of the payment record in DB. + * Each payment gateway may then store the additional information their way. + * + * @param string $component Name of the component that the componentid belongs to + * @param int $componentid 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 + * @param string $currency Currency of payment + * @param string $gateway The gateway that is used for the payment + * @return int + */ + public static function save_payment(string $component, int $componentid, int $userid, float $amount, string $currency, + string $gateway): int { + global $DB; + + $record = new \stdClass(); + $record->component = $component; + $record->componentid = $componentid; + $record->userid = $userid; + $record->amount = $amount; + $record->currency = $currency; + $record->gateway = $gateway; + $record->timecreated = $record->timemodified = time(); + + $id = $DB->insert_record('payments', $record); + + return $id; + } } diff --git a/version.php b/version.php index 40a8acb9c7c78..a1508dc11ec2c 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.31; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.32; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20201023)'; // Human-friendly version name