MDL-69166 core_payment: add payment accounts
[moodle.git] / payment / classes / account_gateway.php
CommitLineData
895f38cc
MG
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Class account_gateway
19 *
20 * @package core_payment
21 * @copyright 2020 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_payment;
26
27use core\persistent;
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Class account_gateway
33 *
34 * @package core_payment
35 * @copyright 2020 Marina Glancy
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class account_gateway extends persistent {
39 /**
40 * Database table.
41 */
42 const TABLE = 'payment_gateways';
43
44 /**
45 * Return the definition of the properties of this model.
46 *
47 * @return array
48 */
49 protected static function define_properties() : array {
50 return array(
51 'accountid' => [
52 'type' => PARAM_INT,
53 ],
54 'gateway' => [
55 'type' => PARAM_COMPONENT,
56 // TODO select with options?
57 ],
58 'enabled' => [
59 'type' => PARAM_BOOL,
60 'default' => true
61 ],
62 'config' => [
63 'type' => PARAM_RAW,
64 'optional' => true,
65 'null' => NULL_ALLOWED,
66 'default' => null
67 ],
68 );
69 }
70
71 /**
72 * Return the gateway name ready for display
73 *
74 * @return string
75 * @throws \coding_exception
76 */
77 public function get_display_name(): string {
78 return get_string('pluginname', 'pg_' . $this->get('gateway'));
79 }
80
81 /**
82 * Gateway management url
83 *
84 * @return \moodle_url
85 * @throws \coding_exception
86 * @throws \moodle_exception
87 */
88 public function get_edit_url(): \moodle_url {
89 $params = $this->get('id') ? ['id' => $this->get('id')] :
90 ['accountid' => $this->get('accountid'), 'gateway' => $this->get('gateway')];
91 return new \moodle_url('/payment/manage_gateway.php', $params);
92 }
93
94 /**
95 * Get corresponding account
96 *
97 * @return account
98 * @throws \coding_exception
99 */
100 public function get_account(): account {
101 return new account($this->get('accountid'));
102 }
103
104 /**
105 * Parse configuration from the json-encoded stored value
106 *
107 * @return array
108 * @throws \coding_exception
109 */
110 public function get_configuration(): array {
111 $config = @json_decode($this->get('config'), true);
112 return ($config && is_array($config)) ? $config : [];
113 }
114}