Skip to content

Commit 532c85a

Browse files
committed
Completed requested changes
1 parent c8a5c02 commit 532c85a

File tree

7 files changed

+63
-22
lines changed

7 files changed

+63
-22
lines changed

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ matrix:
3131
env: DB=mysqli MOODLE_BRANCH=MOODLE_37_STABLE
3232
- php: 7.2
3333
env: DB=pgsql MOODLE_BRANCH=MOODLE_37_STABLE
34+
- php: 7.2
35+
env: DB=mysqli MOODLE_BRANCH=MOODLE_38_STABLE
36+
- php: 7.2
37+
env: DB=pgsql MOODLE_BRANCH=MOODLE_38_STABLE
38+
- php: 7.2
39+
env: DB=mysqli MOODLE_BRANCH=MOODLE_39_STABLE
40+
- php: 7.2
41+
env: DB=pgsql MOODLE_BRANCH=MOODLE_39_STABLE
3442
- php: 7.2
3543
env: DB=mysqli MOODLE_BRANCH=master
3644
- php: 7.2

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ The admin tool plugin comes with a PayPal subplugin pre-installed. This subplugi
7575
## Support
7676

7777
This plugin was developed by QUT MAHQ Developers as a Capstone project.
78-
Haruki Nakagawa - [email protected], Quyen Nguyen - [email protected]
78+
Haruki Nakagawa - [email protected]
79+
Quyen Nguyen - [email protected]
7980
Aaron Dang - [email protected]
8081
// add your name here
8182

classes/payment_manager.php

+30-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626

2727
namespace tool_paymentplugin;
2828

29+
use dml_exception;
30+
use moodle_exception;
31+
32+
use function PHPSTORM_META\type;
33+
2934
defined ('MOODLE_INTERNAL') || die();
3035

3136
class payment_manager {
@@ -45,11 +50,13 @@ class payment_manager {
4550
*/
4651
public static function paymentplugin_enrol(int $courseid, int $userid) {
4752
global $DB;
48-
if (!$DB->record_exists('course', array('id' => $courseid))) {
53+
try {
54+
get_course($courseid);
55+
} catch (dml_exception $e) {
4956
throw new \moodle_exception('errorinvalidcourse', 'tool_paymentplugin', '', $courseid);
5057
}
5158

52-
if (!$DB->record_exists('user', array('id' => $userid))) {
59+
if (!\core_user::get_user($userid)) {
5360
throw new \moodle_exception('errorinvaliduser', 'tool_paymentplugin', '', $userid);
5461
}
5562

@@ -62,13 +69,26 @@ public static function paymentplugin_enrol(int $courseid, int $userid) {
6269
$enrol->enrol_user($enrolinstance, $userid);
6370
}
6471

65-
public static function filter_underscores($data) : \stdClass {
66-
$newdata = new \stdclass();
72+
/**
73+
* Strips underscores from all keys from an array, or all property names in an object.
74+
*
75+
* @param array|object $data An array or object to be operated on.
76+
* @return void
77+
*/
78+
public static function filter_underscores(&$data) {
79+
$datatype = gettype($data);
6780
foreach ($data as $var => $value) {
68-
$var = str_replace('_', '', $var);
69-
$newdata->$var = $value;
81+
$count = 0;
82+
$newvar = str_replace('_', '', $var, $count);
83+
// Only replace and unset if the key actually changed as indicated by $count.
84+
if ($datatype == 'array' && $count) {
85+
$data[$newvar] = $value;
86+
unset($data[$var]);
87+
} else if ($datatype == 'object' && $count) {
88+
$data->$newvar = $value;
89+
unset($data->$var);
90+
}
7091
}
71-
return $newdata;
7292
}
7393

7494
/**
@@ -101,9 +121,10 @@ public static function submit_transaction(\tool_paymentplugin\paymentgateway\obj
101121
'userid' => $userid, 'amount' => $amount, 'paymentdate' => $date, 'courseid' => $courseid, 'success' => $paymentstatus]
102122
);
103123

104-
if (!is_null($additionaldata)) {
124+
if (!empty($additionaldata)) {
105125
$additionaldata['purchaseid'] = $id; // NOTE, all subplugin tables will need purchase_id.
106-
$DB->insert_record($gatewaytablename, self::filter_underscores($additionaldata));
126+
self::filter_underscores($additionaldata);
127+
$DB->insert_record($gatewaytablename, $additionaldata);
107128
}
108129

109130
if ($paymentstatus == self::PAYMENT_COMPLETE) {

classes/paymentgateway/object_paymentgateway.php

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public function set_tablename(string $tablename) {
9898
*/
9999
public function is_enabled() : bool {
100100
$config = $this->config;
101-
// Explicitly convert to bool instead of using int to avoid type conversion errors.
102101
if (!empty($config->enabled) && $config->enabled) {
103102
$pluginenabled = true;
104103
} else {

paymentgateway/paypal/classes/ipn.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
namespace paymentgateway_paypal;
2929

30+
use dml_exception;
31+
3032
defined ('MOODLE_INTERNAL') || die();
3133

3234
/**
@@ -138,7 +140,9 @@ private function error_check(\stdClass &$data) {
138140

139141
// Check that course exists.
140142
$errorinfo = "";
141-
if (!$DB->record_exists('course', array('id' => $data->courseid))) {
143+
try {
144+
get_course($data->courseid);
145+
} catch (dml_exception $e) {
142146
$noerror = false;
143147
$errorinfo .= get_string('erroripncourseid', 'paymentgateway_paypal') . " ";
144148
}
@@ -154,7 +158,7 @@ private function error_check(\stdClass &$data) {
154158
}
155159

156160
// Check that userid is valid.
157-
if (!$DB->record_exists('user', array('id' => $data->userid))) {
161+
if (!\core_user::get_user($data->userid)) {
158162
$noerror = false;
159163
$errorinfo .= get_string('erroripnuserid', 'paymentgateway_paypal') . " ";
160164
}

paymentgateway/paypal/tests/paymentgateway_paypal_ipn_test.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ public function test_successful_ipn() {
208208

209209
// Check transaction details were recorded correctly.
210210
$ex = $this->generate_expected_table_data($course, $user);
211-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
211+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
212+
$this->assertEquals($ex, $details);
212213

213214
// Check enrolment happened correctly.
214215
$this->assertEquals($user->id, $enrolment->userid);
@@ -250,7 +251,8 @@ public function test_incorrect_currency() {
250251
$ex = $this->generate_expected_table_data($course, $user);
251252
$ex->success = '0';
252253
$ex->errorinfo = get_string('erroripncurrency', 'paymentgateway_paypal');
253-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
254+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
255+
$this->assertEquals($ex, $details);
254256

255257
// Check enrolment failed.
256258
$this->assertEquals(false, $enrolment);
@@ -290,7 +292,8 @@ public function test_incorrect_cost() {
290292
$ex = $this->generate_expected_table_data($course, $user);
291293
$ex->success = '0';
292294
$ex->errorinfo = get_string('erroripncost', 'paymentgateway_paypal');
293-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
295+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
296+
$this->assertEquals($ex, $details);
294297

295298
// Check enrolment failed.
296299
$this->assertEquals(false, $enrolment);
@@ -332,7 +335,8 @@ public function test_incorrect_userid() {
332335
$ex->userid = (string) $incorrectid;
333336
$ex->success = '0';
334337
$ex->errorinfo = get_string('erroripnuserid', 'paymentgateway_paypal');
335-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
338+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
339+
$this->assertEquals($ex, $details);
336340

337341
// Check enrolment failed.
338342
$this->assertEquals(false, $enrolment);
@@ -374,7 +378,8 @@ public function test_incorrect_courseid() {
374378
$ex->courseid = (string) $incorrectid;
375379
$ex->success = '0';
376380
$ex->errorinfo = get_string('erroripncourseid', 'paymentgateway_paypal');
377-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
381+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
382+
$this->assertEquals($ex, $details);
378383

379384
// Check enrolment failed.
380385
$this->assertEquals(false, $enrolment);
@@ -420,7 +425,8 @@ public function test_incorrect_multiple() {
420425
$ex->errorinfo = get_string('erroripncurrency', 'paymentgateway_paypal') . " " .
421426
get_string('erroripncost', 'paymentgateway_paypal') . " " .
422427
get_string('erroripnuserid', 'paymentgateway_paypal');
423-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
428+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
429+
$this->assertEquals($ex, $details);
424430

425431
// Check enrolment failed.
426432
$this->assertEquals(false, $enrolment);
@@ -495,7 +501,8 @@ public function test_pending_ipn() {
495501
$ex->payment_status = 'Pending';
496502
$ex->pending_reason = 'echeck';
497503
$ex->success = '2';
498-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
504+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
505+
$this->assertEquals($ex, $details);
499506

500507
// Check enrolment did not happen.
501508
$this->assertEquals(false, $enrolment);
@@ -534,7 +541,8 @@ public function test_failed_ipn() {
534541
$ex = $this->generate_expected_table_data($course, $user);
535542
$ex->payment_status = 'Failed';
536543
$ex->success = '0';
537-
$this->assertEquals(\tool_paymentplugin\payment_manager::filter_underscores($ex), $details);
544+
\tool_paymentplugin\payment_manager::filter_underscores($ex);
545+
$this->assertEquals($ex, $details);
538546

539547
// Check enrolment did not happen.
540548
$this->assertEquals(false, $enrolment);

paymentgateway/paypal/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030

3131
$plugin->dependencies = array(
3232
'tool_paymentplugin' => 2020101200
33-
);
33+
);

0 commit comments

Comments
 (0)