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

Commit

Permalink
Merge pull request #2 from webong/webhook
Browse files Browse the repository at this point in the history
Added Webhook Support
  • Loading branch information
webong authored Aug 28, 2019
2 parents a38e993 + e42c33c commit 990b4f1
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 357 deletions.
157 changes: 157 additions & 0 deletions classes/Paystack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?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/>.
/**
* Paystack API Class
*
* @package enrol_paystack
* @copyright 2019 Paystack
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace enrol_paystack;

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

class paystack {
public $plugin_name;
public $public_key;
public $secret_key;

public function __construct($plugin, $pk, $sk){
//configure plugin name
//configure public key
$this->base_url = "https://api.paystack.co/";
$this->plugin_name = $plugin;
$this->public_key = $pk;
$this->secret_key = $sk;
}

/**
* Verify Payment Transaction
*
* @param string $reference
* @param array $data
* @return void
*/
public function initialize_transaction($data)
{
$paystackUrl = $this->base_url . "transaction/initialize";

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $paystackUrl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"authorization: Bearer " . $this->secret_key,
"content-type: application/json",
"cache-control: no-cache"
],
]);

$request = curl_exec($curl);
$res = json_decode($request, true);

if (curl_errno($curl)) {
throw new moodle_exception(
'errpaystackconnect',
'enrol_paystack',
'',
array('url' => $paystackUrl, 'response' => $res),
json_encode($data)
);
}

curl_close($curl);

return $res;
}

/**
* Verify Payment Transaction
*
* @param string $reference
* @return void
*/
public function verify_transaction($reference)
{
$paystackUrl = $this->base_url . "transaction/verify/" . $reference;

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $paystackUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"authorization: Bearer " . $this->secret_key,
"content-type: application/json",
"cache-control: no-cache"
],
]);

$request = curl_exec($curl);
$res = json_decode($request, true);
// $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if (curl_errno($curl)) {
throw new moodle_exception(
'errpaystackconnect',
'enrol_paystack',
'',
array('url' => $paystackUrl, 'response' => $res),
''
);
}

curl_close($curl);

return $res;
}

/**
* Track Payment Transactions from this Plugin
*
* @param string $trx_ref
* @return void
*/
public function log_transaction_success($reference)
{
//send reference to logger along with plugin name and public key
$url = "https://plugin-tracker.paystackintegrations.com/log/charge_success";
$params = [
'plugin_name' => $this->plugin_name,
'transaction_reference' => $reference,
'public_key' => $this->public_key
];
$params_string = http_build_query($params);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
curl_exec($ch);
}

/**
* Validate Webhook Signature
*
* @param $input
* @return boolean
*/
public function validate_webhook($input)
{
return $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, $this->secret_key);
}
}
53 changes: 0 additions & 53 deletions classes/paystack_plugin_tracker.php

This file was deleted.

54 changes: 28 additions & 26 deletions enrol.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,38 @@ <h3><?php echo $instancename; ?></h3>
<p><?php echo $message; ?></p>
<p><b><?php echo get_string("cost").": {$instance->currency} {$localisedcost}"; ?></b></p>
<div>

<?php
$data = [
"cmd" => "_xclick",
"charset" => "utf-8",
"item_name" => s($coursefullname),
"item_name" => s($coursefullname),
"item_number" => s($courseshortname),
"quantity" => "1",
"on0" => get_string("user"),
"os0" => s($userfullname),
"custom" => "{$USER->id}-{$course->id}-{$instance->id}",
"currency_code" => $instance->currency,
"amount" => $cost,
"for_auction" => "false",
"no_note" => "1",
"no_shipping" => "1" ,
"rm" => "2",
"first_name" => s($userfirstname),
"last_name" => s($userlastname),
"email" => s($useremail),
"reference" => $reference,
];
?>
<form method="post" action="<?php echo "$CFG->wwwroot/enrol/paystack/verify.php"?>">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="charset" value="utf-8" />
<input type="hidden" name="item_name" value="<?php p($coursefullname) ?>" />
<input type="hidden" name="item_number" value="<?php p($courseshortname) ?>" />
<input type="hidden" name="quantity" value="1" />
<input type="hidden" name="on0" value="<?php print_string("user") ?>" />
<input type="hidden" name="os0" value="<?php p($userfullname) ?>" />
<input type="hidden" name="custom" value="<?php echo "{$USER->id}-{$course->id}-{$instance->id}" ?>" />
<input type="hidden" name="currency_code" value="<?php p($instance->currency) ?>" />
<input type="hidden" name="amount" value="<?php p($cost) ?>" />
<input type="hidden" name="for_auction" value="false" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="no_shipping" value="1" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="cbt" value="<?php print_string("continuetocourse") ?>" />
<input type="hidden" name="first_name" value="<?php p($userfirstname) ?>" />
<input type="hidden" name="last_name" value="<?php p($userlastname) ?>" />
<input type="hidden" name="email" value="<?php p($USER->email) ?>" />
<script
src="https://js.paystack.co/v1/inline.js"
data-label="<?php echo get_string('btntext','enrol_paystack') ?>"
data-key="<?php echo $publickey; ?>"
data-ref="<?php echo $reference; ?>"
data-email="<?php p($USER->email) ?>"
data-description="Enrolment Cost (<?php p($instance->currency) ?><?php p($cost) ?>)"
data-currency="<?php p($instance->currency) ?>"
data-amount="<?php p($cost * 100) ?>"
data-locale="<?php echo current_language(); ?>">

<script src="https://js.paystack.co/v1/inline.js" data-key="<?php echo $publickey; ?>"
data-ref="<?php echo $reference; ?>" data-email="<?php p($useremail) ?>"
data-currency="<?php p($instance->currency) ?>" data-amount="<?php p($cost * 100) ?>"
data-metadata='<?php echo json_encode($data) ?>' data-locale="<?php echo current_language(); ?>">
</script>
</form>

Expand Down
6 changes: 4 additions & 2 deletions lang/en/enrol_paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
$string['paystack:unenrol'] = 'Unenrol users from course';
$string['paystack:unenrolself'] = 'Unenrol self from the course';
$string['paystackaccepted'] = 'Paystack payments accepted';
$string['pluginname'] = 'Paystack Payment';
$string['pluginname_desc'] = 'The Paystack module allows you to set up paid courses. If the cost for any course is zero, then students are not asked to pay for entry. There is a site-wide cost that you set here as a default for the whole site and then a course setting that you can set for each course individually. The course cost overrides the site cost.';
$string['pluginname'] = 'Paystack';
$string['pluginname_desc'] = 'The Paystack module allows you to set up paid courses. If the cost for any course is zero, then students are not asked to pay for entry. There is a site-wide cost that you set here as a default for the whole site and then a course setting that you can set for each course individually. The course cost overrides the site cost.';
$string['sendpaymentbutton'] = 'Send payment via Paystack';
$string['status'] = 'Allow Paystack enrolments';
$string['status_desc'] = 'Allow users to use Paystack to enrol into a course by default.';
Expand All @@ -94,3 +94,5 @@
$string['charge_description1'] = "Create customer for email receipt";
$string['charge_description2'] = 'Charge for Course Enrolment Cost.';
$string['paystack_sorry'] = "Sorry, you can not use the script that way.";
$string['webhook'] = 'Paystack Webhook Url';
$string['webhook_desc'] = 'Add this Webhook Url "{$a->webhook}" to your paystack account developer settings page <a href="{$a->url}">here</a>';
2 changes: 2 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,12 @@ public function enrol_page_hook(stdClass $instance)
$userfullname = fullname($USER);
$userfirstname = $USER->firstname;
$userlastname = $USER->lastname;
$useremail = $USER->email;
$instancename = $this->get_instance_name($instance);

$publickey = $this->get_publickey();
$reference = $this->getHashedToken();

include($CFG->dirroot.'/enrol/paystack/enrol.html');
}
}
Expand Down
15 changes: 10 additions & 5 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@
/** Paystack live mode enabled.*/
define('LIVE_MODE_ENABLED', 1);

global $CFG;

if ($ADMIN->fulltree) {
$webhook = "$CFG->wwwroot/enrol/paystack/webhook.php";
$url = "https://dashboard.paystack.com/#/settings/developer";
$text = '<p>Add this Webhook Url <span style="color:blue; text-decoration:underline;">' . $webhook . '</span> to your paystack account developer settings page <a href="' . $url .'" target="_blank">here</a></p>';
$settings->add(new admin_setting_heading(
'enrol_paystack_enrolname_short',
'',
get_string('pluginname_desc', 'enrol_paystack')
get_string('pluginname_desc', 'enrol_paystack') . " " . $text
));

$options = array(
Expand Down Expand Up @@ -111,10 +116,10 @@
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
);
$settings->add(new admin_setting_configselect(
'enrol_paystack/expiredaction',
get_string('expiredaction', 'enrol_paystack'),
get_string('expiredaction_help', 'enrol_paystack'),
ENROL_EXT_REMOVED_SUSPENDNOROLES,
'enrol_paystack/expiredaction',
get_string('expiredaction', 'enrol_paystack'),
get_string('expiredaction_help', 'enrol_paystack'),
ENROL_EXT_REMOVED_SUSPENDNOROLES,
$options
));

Expand Down
Loading

0 comments on commit 990b4f1

Please sign in to comment.