Skip to content

Commit

Permalink
MDL-69166 core_payment: Save general payment information
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Oct 27, 2020
1 parent fc37fb1 commit 52ed139
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
24 changes: 23 additions & 1 deletion lib/db/install.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20201007" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20201016" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -4288,6 +4288,28 @@
<INDEX NAME="instance" UNIQUE="false" FIELDS="contextid, contenttype, instanceid"/>
</INDEXES>
</TABLE>
<TABLE NAME="payments" COMMENT="Stores information about payments">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="component" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="componentid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<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="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"/>
</KEYS>
<INDEXES>
<INDEX NAME="component" UNIQUE="false" FIELDS="component"/>
<INDEX NAME="componentid" UNIQUE="false" FIELDS="componentid"/>
<INDEX NAME="gateway" UNIQUE="false" FIELDS="gateway"/>
</INDEXES>
</TABLE>
<TABLE NAME="infected_files" COMMENT="Table to store infected file details.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
Expand Down
34 changes: 34 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
30 changes: 30 additions & 0 deletions payment/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 52ed139

Please sign in to comment.