-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathupgradelib.php
More file actions
137 lines (127 loc) · 4.92 KB
/
Copy pathupgradelib.php
File metadata and controls
137 lines (127 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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/>.
/**
* Upgrade functions for paygw_stripe.
*
* @package paygw_stripe
* @copyright 2021 Alex Morris <alex@navra.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../.extlib/stripe-php/init.php');
use core_payment\account;
use paygw_stripe\stripe_helper;
use Stripe\Stripe;
use Stripe\StripeClient;
/**
* Update all webhook events.
*
* @param array $events
* @return void
*/
function paygw_stripe_update_webhooks(array $events) {
global $DB;
$gateways = $DB->get_records('payment_gateways', ['gateway' => 'stripe']);
foreach ($gateways as $gatewayrecord) {
$account = new account($gatewayrecord->accountid);
$gateway = $account->get_gateways(false)['stripe'] ?? null;
if ($gateway != null) {
$config = $gateway->get_configuration();
try {
$stripe = new StripeClient([
'api_key' => $config['secretkey'],
'stripe_version' => stripe_helper::$apiversion,
]);
Stripe::setAppInfo(
'Moodle Stripe Payment Gateway',
get_config('paygw_stripe')->version,
'https://github.com/alexmorrisnz/moodle-paygw_stripe'
);
$webhooks = $DB->get_records('paygw_stripe_webhooks', ['paymentaccountid' => $account->get('id')]);
foreach ($webhooks as $webhookrecord) {
$stripe->webhookEndpoints->update($webhookrecord->webhookid, ['enabled_events' => $events]);
}
} catch (Exception $ignored) {
// Ignore errors, the api keys we are given may be wrong.
continue;
}
}
}
}
/**
* Delete webhooks, they will be recreated when used later.
*
* @return void
*/
function paygw_stripe_delete_webhooks() {
global $DB;
$gateways = $DB->get_records('payment_gateways', ['gateway' => 'stripe']);
foreach ($gateways as $gatewayrecord) {
$account = new account($gatewayrecord->accountid);
$gateway = $account->get_gateways(false)['stripe'] ?? null;
if ($gateway != null) {
$config = $gateway->get_configuration();
try {
$stripe = new StripeClient([
'api_key' => $config['secretkey'],
'stripe_version' => stripe_helper::$apiversion,
]);
Stripe::setAppInfo(
'Moodle Stripe Payment Gateway',
get_config('paygw_stripe')->version,
'https://github.com/alexmorrisnz/moodle-paygw_stripe'
);
$webhooks = $DB->get_records('paygw_stripe_webhooks', ['paymentaccountid' => $account->get('id')]);
foreach ($webhooks as $webhookrecord) {
$stripe->webhookEndpoints->delete($webhookrecord->webhookid);
$DB->delete_records('paygw_stripe_webhooks', ['id' => $webhookrecord->id]);
}
} catch (Exception $ignored) {
// Ignore errors, the api keys we are given may be wrong.
continue;
}
}
}
}
/**
* Recreate webhooks for API upgrades.
*
* @return void
* @throws coding_exception
* @throws dml_exception
*/
function paygw_stripe_recreate_webhooks() {
global $DB;
$gateways = $DB->get_records('payment_gateways', ['gateway' => 'stripe']);
foreach ($gateways as $gatewayrecord) {
$account = new account($gatewayrecord->accountid);
$gateway = $account->get_gateways(false)['stripe'] ?? null;
if ($gateway != null) {
$config = $gateway->get_configuration();
if (!is_string($config['apikey']) || !is_string($config['secretkey'])) {
continue;
}
try {
$stripehelper = new stripe_helper($config['apikey'], $config['secretkey']);
$stripehelper->delete_webhook($account->get('id'));
$stripehelper->create_webhook($account->get('id'));
} catch (Exception $ignored) {
// Ignore errors, the api keys we are given may be wrong.
continue;
}
}
}
}