Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit a38e993

Browse files
authored
Merge pull request #1 from webong/apikeys
Enable Paystack Connection Config in Plugin Settings
2 parents 4d917e4 + d7cae5d commit a38e993

5 files changed

Lines changed: 100 additions & 18 deletions

File tree

lang/en/enrol_paystack.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
$string['btntext']= 'Pay Now';
3232
$string['billingaddress'] = 'Require users to enter their billing address';
3333
$string['billingaddress_desc'] = 'This sets the Paystack payment option for whether the user should be asked to input their billing address. It is off by default, but it is a good idea to turn it on.';
34-
$string['secretkey'] = 'Paystack Secret Key';
35-
$string['publickey'] = 'Paystack Public Key';
36-
$string['secretkey_desc'] = 'The API Secret Key of Paystack account';
37-
$string['publickey_desc'] = 'The API Public Key of Paystack account';
3834
$string['cost'] = 'Enrol cost';
3935
$string['costerror'] = 'The enrolment cost is not numeric';
4036
$string['costorkey'] = 'Please choose one of the following methods of enrolment.';
@@ -55,10 +51,18 @@
5551
$string['enrolstartdate_help'] = 'If enabled, users can be enrolled from this date onward only.';
5652
$string['expiredaction'] = 'Enrolment expiration action';
5753
$string['expiredaction_help'] = 'Select action to carry out when user enrolment expires. Please note that some user data and settings are purged from course during course unenrolment.';
54+
$string['live_secretkey'] = 'Paystack Live Secret Key';
55+
$string['live_publickey'] = 'Paystack Live Public Key';
56+
$string['live_secretkey_desc'] = 'The Live API Secret Key of Paystack account';
57+
$string['live_publickey_desc'] = 'The Live API Public Key of Paystack account';
5858
$string['mailadmins'] = 'Notify admin';
5959
$string['mailstudents'] = 'Notify students';
6060
$string['mailteachers'] = 'Notify teachers';
6161
$string['messageprovider:paystack_enrolment'] = 'Paystack enrolment messages';
62+
$string['mode'] = 'Paystack Connection Mode';
63+
$string['mode_desc'] = 'Set api configuration to use when Paystack charges for enrolment into a course by default.';
64+
$string['mode_live'] = 'Live Mode';
65+
$string['mode_test'] = 'Test Mode';
6266
$string['nocost'] = 'There is no cost associated with enrolling in this course!';
6367
$string['paymentthanks']='Thanks for your payment';
6468
$string['paystack:config'] = 'Configure Paystack enrol instances';
@@ -71,6 +75,10 @@
7175
$string['sendpaymentbutton'] = 'Send payment via Paystack';
7276
$string['status'] = 'Allow Paystack enrolments';
7377
$string['status_desc'] = 'Allow users to use Paystack to enrol into a course by default.';
78+
$string['test_secretkey'] = 'Paystack Test Secret Key';
79+
$string['test_publickey'] = 'Paystack Test Public Key';
80+
$string['test_secretkey_desc'] = 'The Test API Secret Key of Paystack account';
81+
$string['test_publickey_desc'] = 'The Test API Public Key of Paystack account';
7482
$string['unenrolselfconfirm'] = 'Do you really want to unenrol yourself from course "{$a}"?';
7583
$string['messageprovider:paystackpayment_enrolment'] = 'Message Provider';
7684
$string['validatezipcode'] = 'Validate the billing postal code';
@@ -83,6 +91,6 @@
8391
$string['paystackpayment:manage'] = 'Manage paystackpayment';
8492
$string['paystackpayment:unenrol'] = 'Unenrol paystackpayment';
8593
$string['paystackpayment:unenrolself'] = 'Unenrolself paystackpayment';
86-
$string['charge_description1'] = "create customer for email receipt";
94+
$string['charge_description1'] = "Create customer for email receipt";
8795
$string['charge_description2'] = 'Charge for Course Enrolment Cost.';
8896
$string['paystack_sorry'] = "Sorry, you can not use the script that way.";

lib.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,40 @@ public function get_name()
4646
return $words[1];
4747
}
4848

49+
/**
50+
* Return connection mode of this enrol plugin.
51+
*
52+
* @return boolean
53+
*/
54+
public function get_mode()
55+
{
56+
return $this->get_config('mode') == "1" ? true : false;
57+
}
58+
59+
/**
60+
* Return public key of this enrol plugin.
61+
*
62+
* @return string
63+
*/
64+
public function get_publickey()
65+
{
66+
return $this->get_mode() ?
67+
$this->get_config('live_publickey') :
68+
$this->get_config('test_publickey') ;
69+
}
70+
71+
/**
72+
* Return secret key of this enrol plugin.
73+
*
74+
* @return string
75+
*/
76+
public function get_secretkey()
77+
{
78+
return $this->get_mode() ?
79+
$this->get_config('live_secretkey') :
80+
$this->get_config('test_secretkey') ;
81+
}
82+
4983
/**
5084
* Defines if user can be managed from admin.
5185
*
@@ -365,7 +399,7 @@ public function enrol_page_hook(stdClass $instance)
365399
$userlastname = $USER->lastname;
366400
$instancename = $this->get_instance_name($instance);
367401

368-
$publickey = $this->get_config('publickey');
402+
$publickey = $this->get_publickey();
369403
$reference = $this->getHashedToken();
370404
include($CFG->dirroot.'/enrol/paystack/enrol.html');
371405
}

settings.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,59 @@
2525

2626
defined('MOODLE_INTERNAL') || die();
2727

28+
/** Paystack live mode disabled. */
29+
define('LIVE_MODE_DISABLED', 0);
30+
31+
/** Paystack live mode enabled.*/
32+
define('LIVE_MODE_ENABLED', 1);
33+
2834
if ($ADMIN->fulltree) {
2935
$settings->add(new admin_setting_heading(
3036
'enrol_paystack_enrolname_short',
3137
'',
3238
get_string('pluginname_desc', 'enrol_paystack')
3339
));
3440

41+
$options = array(
42+
LIVE_MODE_ENABLED => get_string('mode_live', 'enrol_paystack'),
43+
LIVE_MODE_DISABLED => get_string('mode_test', 'enrol_paystack')
44+
);
45+
$settings->add(new admin_setting_configselect(
46+
'enrol_paystack/mode',
47+
get_string('mode', 'enrol_paystack'),
48+
get_string('mode_desc', 'enrol_paystack'),
49+
LIVE_MODE_DISABLED,
50+
$options
51+
));
52+
53+
$settings->add(new admin_setting_configtext(
54+
'enrol_paystack/live_secretkey',
55+
get_string('live_secretkey', 'enrol_paystack'),
56+
get_string('live_secretkey_desc', 'enrol_paystack'),
57+
'',
58+
PARAM_TEXT
59+
));
60+
3561
$settings->add(new admin_setting_configtext(
36-
'enrol_paystack/secretkey',
37-
get_string('secretkey', 'enrol_paystack'),
38-
get_string('secretkey_desc', 'enrol_paystack'),
62+
'enrol_paystack/live_publickey',
63+
get_string('live_publickey', 'enrol_paystack'),
64+
get_string('live_publickey_desc', 'enrol_paystack'),
3965
'',
4066
PARAM_TEXT
4167
));
4268

4369
$settings->add(new admin_setting_configtext(
44-
'enrol_paystack/publickey',
45-
get_string('publickey', 'enrol_paystack'),
46-
get_string('publickey_desc', 'enrol_paystack'),
70+
'enrol_paystack/test_secretkey',
71+
get_string('test_secretkey', 'enrol_paystack'),
72+
get_string('test_secretkey_desc', 'enrol_paystack'),
73+
'',
74+
PARAM_TEXT
75+
));
76+
77+
$settings->add(new admin_setting_configtext(
78+
'enrol_paystack/test_publickey',
79+
get_string('test_publickey', 'enrol_paystack'),
80+
get_string('test_publickey_desc', 'enrol_paystack'),
4781
'',
4882
PARAM_TEXT
4983
));
@@ -76,8 +110,13 @@
76110
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'),
77111
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
78112
);
79-
$settings->add(new admin_setting_configselect('enrol_paystack/expiredaction', get_string('expiredaction', 'enrol_paystack'), get_string('expiredaction_help', 'enrol_paystack'), ENROL_EXT_REMOVED_SUSPENDNOROLES, $options));
80-
113+
$settings->add(new admin_setting_configselect(
114+
'enrol_paystack/expiredaction',
115+
get_string('expiredaction', 'enrol_paystack'),
116+
get_string('expiredaction_help', 'enrol_paystack'),
117+
ENROL_EXT_REMOVED_SUSPENDNOROLES,
118+
$options
119+
));
81120

82121
// --- enrol instance defaults ----------------------------------------------------------------------------
83122
$settings->add(new admin_setting_heading(

verify.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
require('../../config.php');
3030
require_once('lib.php');
31+
3132
if ($CFG->version < 2018101900) {
3233
require_once($CFG->libdir . '/eventslib.php');
3334
}
@@ -92,7 +93,7 @@
9293

9394
$plugin_instance = $DB->get_record("enrol", array("id" => $data->instanceid, "enrol" => "paystack", "status" => 0), "*", MUST_EXIST);
9495
$plugin = enrol_get_plugin('paystack');
95-
$plugin_logger = new \enrol_paystack\paystack_plugin_tracker('moodle-enrol', $plugin->get_config('publickey'));
96+
$plugin_logger = new \enrol_paystack\paystack_plugin_tracker('moodle-enrol', $plugin->get_publickey());
9697

9798
// Set Course and Paystack Url
9899
$courseUrl = "$CFG->wwwroot/course/view.php?id=$course->id";
@@ -104,7 +105,7 @@
104105
CURLOPT_RETURNTRANSFER => true,
105106
CURLOPT_CUSTOMREQUEST => "GET",
106107
CURLOPT_HTTPHEADER => [
107-
"authorization: Bearer " . $plugin->get_config('secretkey'), //replace this with your own test key
108+
"authorization: Bearer " . $plugin->get_secretkey(), //replace this with your own test key
108109
"content-type: application/json",
109110
"cache-control: no-cache"
110111
],

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'enrol_paystack';
28-
$plugin->release = '1.0';
29-
$plugin->version = 2019081900;
28+
$plugin->release = '1.1.0';
29+
$plugin->version = 2019082700;
3030
$plugin->requires = 2018120300;
3131
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)