MDL-69166 core_payment: Save general payment information
authorShamim Rezaie <shamim@moodle.com>
Wed, 8 Jan 2020 16:45:51 +0000 (03:45 +1100)
committerShamim Rezaie <shamim@moodle.com>
Tue, 27 Oct 2020 03:37:28 +0000 (14:37 +1100)
lib/db/install.xml [changed mode: 0644->0755]
lib/db/upgrade.php
payment/classes/helper.php
version.php

old mode 100644 (file)
new mode 100755 (executable)
index 4781461..6476490
@@ -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"
 >
         <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"/>
index b9c5d34..575ef24 100644 (file)
@@ -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;
 }
index 0c86875..e3b4f1a 100644 (file)
@@ -149,4 +149,34 @@ class helper {
 
         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;
+    }
 }
index 40a8acb..a1508dc 100644 (file)
@@ -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