-
Notifications
You must be signed in to change notification settings - Fork 3
/
paystackpay.php
executable file
·528 lines (449 loc) · 17.2 KB
/
paystackpay.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
/**
* @plugin Paystack Payment Plugin for the Pay per Download component
* @author Adedayo Adeniyi
* @copyright (C) Adedayo Adeniyi
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/
defined( '_JEXEC' ) or die;
//Review PayPal format at Payperdownload component path/site/models/pay.php
require_once (JPATH_ADMINISTRATOR . "/components/com_payperdownload/classes/debug.php");
// import the JPlugin class
jimport('joomla.event.plugin');
// Add a prefix to this class name to avoid conflict with other plugins
class joomla_pp_paystack_plugin_tracker {
var $public_key;
var $plugin_name;
function __construct($plugin, $pk){
//configure plugin name
//configure public key
$this->plugin_name = $plugin;
$this->public_key = $pk;
}
function log_transaction_success($trx_ref){
//send reference to logger along with plugin name and public key
$url = "https://plugin-tracker.paystackintegrations.com/log/charge_success";
$fields = [
'plugin_name' => $this->plugin_name,
'transaction_reference' => $trx_ref,
'public_key' => $this->public_key
];
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
// echo $result;
}
}
class plgPayperDownloadPlusPaystackPay extends JPlugin
{
protected static $tax_percentage;
public function __construct(&$subject, $config = array())
{
parent::__construct($subject, $config);
$this->loadLanguage();
}
public function onRenderPaymentForm($user, $license, $resource, $returnUrl, $thankyouUrl)
{
$pmntinfo = $this->getPaystackPaymentInfo();
$public_key = $pmntinfo->public_key;
if (empty($public_key)) {
echo JText::_('PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_MISSINGPUBLICKEY');
return;
}
$secret_key = $pmntinfo->secret_key;
if (empty($secret_key)) {
echo JText::_('PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_MISSINGSECRETKEY');
return;
}
if (!isset($user)) {
echo JText::_('PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_USERMUSTBELOGGEDIN');
return;
}
$user_id = $user->id;
$customer_email = $user->email;
// $thename = explode(" ",$user->name);
// $firstname = $thename[0]; // that's a leap of faith
// if (count($thename) > 1) {
// $lastname = $thename[1];
// }
if($license || $resource)
{
$item_id = 0; $name = ""; $damount = 0; $currency = ""; $description = "";
$type = ""; $task = ""; $download_id = ''; $amount = 0;
if($resource)
{
$damount = $resource->resource_price;
$currency = $resource->resource_price_currency;
$item_id = $resource->resource_license_id;
$name = $resource->resource_name;
$download_id = $resource->download_id;
if($resource->alternate_resource_description)
$description = $resource->alternate_resource_description;
else
$description = $resource->resource_description;
$task = "confirmres";
$type = "resource";
}
else
{
$damount = $license->price;
$currency = $license->currency_code;
$item_id = $license->license_id;
$name = $license->license_name;
$description = $license->description;
$task = "confirm";
$type = "license";
}
$tax = 0;
$tax_percentage = $this->_getTaxPercentage();
if ($tax_percentage > 0) {
$tax = round($damount * $tax_percentage / 100, 2);
$damount = $damount + $tax;
}
// apply the tax before or after the additonnal fee ?
$package_price = $this->calcFinalAmount($damount, $pmntinfo->ps_extra, $pmntinfo->ps_extratype, $pmntinfo->ps_extraval);
$amount = $package_price[0] * 100;
$amount = (int)$amount;
$callbackurl = JURI::root() . 'index.php?option=com_payperdownload&gateway=paystack&task=' . $task;
$the_reference = 'ppdp'.floor(mt_rand()*10 + 1);//''+Math.floor((Math.random() * 1000000000) + 1);
//put the reference in a session
$session = JFactory::getSession();
$session->set('session_transaction_id',$the_reference); //dump($the_reference,"the created reference");
//construct the variable containing the form
JFactory::getDocument()->addScript('https://js.paystack.co/v1/inline.js');
$js_declaration = <<< JS
function payWithPaystack() {
var handler = PaystackPop.setup({
key: "$public_key",
email: "$customer_email",
amount: "$amount",
ref: "$the_reference",
currency: "$currency",
metadata: {
"ecommerce_platform":"Joomla 3",
"payment_plugin": "Paystackpay for PayPerDownload",
"author": "Daydah",
custom_fields: [
{
display_name: "Plugin",
variable_name: "plugin",
value: "Paystackpay for PayPerDownload"
}
]
},
callback: function(response){
document.getElementById("paystack-payment_form").submit();
},
onClose: function(){
/* cancelling by the buyer */
}
});
handler.openIframe();
}
window.addEventListener('load', function() {
document.querySelector('#paystack-pay-btn').addEventListener('click', function () {
payWithPaystack();
});
}, false);
JS;
JFactory::getDocument()->addScriptDeclaration($js_declaration);
echo '<div class="gateway">';
echo '<form method="post" action="'.$callbackurl.'" name="paystack-payment_form" id="paystack-payment_form">';
$variables = array('additional_fee' => $package_price[1], 'currency' => $currency);
echo $this->_loadTemplate('default.php', $variables);
//add the hidden form fields too
echo '<input type="hidden" name="item_id" value="'.htmlentities($item_id).'"/>';
echo '<input type="hidden" name="item_type" value="'.htmlentities($type).'"/>';
echo '<input type="hidden" name="user_id" value="'.htmlentities($user_id).'"/>';
echo '<input type="hidden" name="amount" value="'.htmlentities($package_price[0]).'"/>'; // damount + possible reversal fee before x100
echo '<input type="hidden" name="tax" value="'.htmlentities($tax).'"/>';
echo '<input type="hidden" name="currency" value="'.htmlentities($currency).'"/>';
if ($type == 'resource') {
echo '<input type="hidden" name="r" value="'.htmlentities(base64_encode($returnUrl)).'"/>';
}
if ($thankyouUrl) {
$returnParams = '';
if ($type == 'license') {
if (strstr($thankyouUrl, "?") === false) {
$returnParams = '?lid=' . (int)$license->license_id;
} else {
$returnParams = '&lid=' . (int)$license->license_id;
}
}
echo '<input type="hidden" name="redirect" value="'.htmlentities(base64_encode($thankyouUrl . $returnParams)).'"/>';
}
//echo '<input type="hidden" name="the-fee" value="'.htmlentities($package_price[1]).'"/>';
if ($download_id) {
echo '<input type="hidden" name="download_id" value="'.(int)$download_id.'/>';
}
echo '</form>';
echo '</div>';
}
}
public function onPaymentReceived($gateway, &$dealt, &$payed,
&$user_id, &$license_id, &$resource_id, &$transactionId,
&$response, &$validate_response, &$status, &$amount,
&$tax, &$fee, &$currency)
{
if($gateway == "paystack")
{
$jinput = JFactory::getApplication()->input;
$dealt = true;
$payed = false;
$amount = $jinput->getFloat('amount');
$currency = $jinput->getString('currency');
$user_id = $jinput->getInt("user_id");
$item_type = $jinput->getString("item_type");
$item_id = $jinput->getInt("item_id");
$download_id = $jinput->getInt("download_id");
$license_id = 0;
$resource_id = 0;
$tax = $jinput->getFloat('tax', 0);
$fee = 0; // $jinput->getFloat('the-fee', 0);
if ($item_type == 'resource') {
$resource_id = $item_id;
} else {
$license_id = $item_id;
}
$session = JFactory::getSession();
$thetransactionid = $session->get('session_transaction_id');
$pmntinfo = $this->getPaystackPaymentInfo();
$secret_key = $pmntinfo->secret_key;
$public_key = $pmntinfo->public_key;
if($thetransactionid)
{
try
{
$transData = $this->verifyPaystackTransaction($thetransactionid, $secret_key);
$sentreference = '';
if(property_exists($transData, 'reference'))
{
$sentreference = $transData->reference;
}
else if(property_exists($transData, 'paystack-reference'))
{
$sentreference = $transData->paystack-reference;
}
if (
(!property_exists($transData, 'error')) &&
property_exists($transData, 'status') &&
($transData->status === 'success') &&
(strpos($transData->reference, $thetransactionid) === 0)
)
{
//pstk-logger
$pstk_logger = new joomla_pp_paystack_plugin_tracker('Paystackpay for PayPerDownload', $public_key);
$pstk_logger->log_transaction_success($sentreference);
//-------------
// Update order status - From pending to complete
$payed = true;
$transactionId = $sentreference;
$fee = $transData->fees / 100.0;
$status = "COMPLETED";
$validate_response = "VERIFIED";
$response = json_encode($transData);
$payerEmail = $transData->customer->email;
}
else if (property_exists($transData, 'error') || (property_exists($transData, 'status') && $transData->status === 'failed'))
{
$status = "FAILED";
$response = $transData->gateway_response;
}
if($download_id)
{
$session = JFactory::getSession();
$transactions = $session->get("trans", array());
$transactions["$transactionId"] =
array("download_id" => $download_id,
"payeremail" => $payerEmail
);
$session->set("trans", $transactions);
}
}
catch (Exception $e)
{
$status = "FAILED";
$response = $e->getMessage();
}
}
}
}
public function onGetPayerEmail($transactionId, &$payer_email)
{
$session = JFactory::getSession();
$transactions = $session->get("trans", array());
if(isset($transactions[$transactionId]))
{
$payer_email = $transactions[$transactionId]["payeremail"];
unset($transactions[$transactionId]);
$session->set("trans", $transactions);
}
}
public function onGetDownloadLinkId($transactionId, &$download_id)
{
$session = JFactory::getSession();
$transactions = $session->get("trans", array());
if(isset($transactions[$transactionId]))
{
$download_id = $transactions[$transactionId]["download_id"];
}
}
public function calcFinalAmount($initialval, $extra1, $extratype, $extraval)
{
$finvalue = 0; $calcpercent = 0; $finvsend = array(); $dextra = 0;
//check if there extra switch is on or off
if($extra1 == 0){
//then extra switch is off.
$finvalue = $initialval;
}
else{ //the extra switch is on, so we need to calculate final value
//check type
if($extratype == 0){
//then the type is a percentage
//calculate percentage value
$dextra = round(($extraval * $initialval)/100, 2);
$finvalue = $initialval + $dextra;
}
else{//then the type is a fixed amount
$dextra = $extraval;
$finvalue = $initialval + $extraval;
}
}
array_push($finvsend,$finvalue,$dextra);
return $finvsend;
}
/*
* @param string $secret_key is either the demo or live secret key from your dashboard. $transactionid is the transaction reference code sent to the API previously
*/
public function verifyPaystackTransaction($transactionid, $secret_key)
{
$transactionStatus = new stdClass();
$transactionStatus->error = "";
// try a file_get verification
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Authorization: Bearer " . $secret_key
)
);
$context = stream_context_create($opts);
$url = "https://api.paystack.co/transaction/verify/" . $transactionid;
$response = file_get_contents($url, false, $context);
// if file_get didn't work, try curl
if (!$response) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.paystack.co/transaction/verify/" . $transactionid);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $secret_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
// Make sure CURL_SSLVERSION_TLSv1_2 is defined as 6
// cURL must be able to use TLSv1.2 to connect
// to Paystack servers
if (!defined('CURL_SSLVERSION_TLSv1_2')) {
define('CURL_SSLVERSION_TLSv1_2', 6);
}
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
// exec the cURL
$response = curl_exec($ch);
// should be 0
if (curl_errno($ch)) {
// curl ended with an error
$transactionStatus->error = "cURL said:" . curl_error($ch);
}
//close connection
curl_close($ch);
}
if ($response)
{
$body = json_decode($response);
if (!$body->status)
{
// paystack has an error message for us
$transactionStatus->error = "Paystack API said: " . $body->message;
} else {
// get body returned by Paystack API
$transactionStatus = $body->data;
}
} else {
// no response
$transactionStatus->error = $transactionStatus->error . " : No response";
}
return $transactionStatus;
}
public function getPaystackPaymentInfo()
{
$pddpinfo = new StdClass();
$pddpinfo->pmode = $this->params->get('ppdp_test_mode');//get test mode and use to get keys
switch($pddpinfo->pmode)
{
case 0: //its in test mode
$pddpinfo->secret_key = trim($this->params->get('ppdp_test_secret_key'));
$pddpinfo->public_key = trim($this->params->get('ppdp_test_public_key')); break;
case 1://its live
$pddpinfo->secret_key = trim($this->params->get('ppdp_live_secret_key'));
$pddpinfo->public_key = trim($this->params->get('ppdp_live_public_key')); break;
default: //its in test mode
$pddpinfo->secret_key = trim($this->params->get('ppdp_test_secret_key'));
$pddpinfo->public_key = trim($this->params->get('ppdp_test_public_key')); break;
}
//$pddpinfo->notify_email = $this->params->get('notify_email', '');
$pddpinfo->ps_extra = $this->params->get('paystack_extra_yes_no');
$pddpinfo->ps_extratype = $this->params->get('paystack_extra_type');
$pddpinfo->ps_extraval = $this->params->get('paystack_extra_charges_value');
//$pddpinfo->currency = $this->params['paystack_currcode'];
return $pddpinfo;
}
protected function _getTaxPercentage()
{
if (!isset(self::$tax_percentage)) {
$db = JFactory::getDBO();
$db->setQuery('SELECT tax_rate FROM #__payperdownloadplus_config');
self::$tax_percentage = 0;
try {
$tax_percentage = $db->loadResult();
if (!is_null($tax_percentage)) {
self::$tax_percentage = $tax_percentage;
}
} catch (RuntimeException $e) {
self::$tax_percentage = 0;
}
}
return self::$tax_percentage;
}
protected function _loadTemplate($file = null, $variables = array())
{
$template = JFactory::getApplication()->getTemplate();
$overridePath = JPATH_THEMES.'/'.$template.'/html/plg_payperdownloadplus_paystackpay';
if (is_file($overridePath.'/'.$file)) {
$file = $overridePath.'/'.$file;
} else {
$file = __DIR__.'/tmpl/'.$file;
}
unset($template);
unset($overridePath);
if (!empty($variables)) {
foreach ($variables as $name => $value) {
$$name = $value;
}
}
unset($variables);
unset($name);
unset($value);
if (isset($this->this)) {
unset($this->this);
}
@ob_start();
include $file;
$html = ob_get_contents();
@ob_end_clean();
return $html;
}
}
?>