-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpaystack.php
227 lines (211 loc) · 9.31 KB
/
paystack.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
<?php
/**
* ****************************************************************** **\
* *
* Paystack Payment Gateway *
* Version: 2.3.0 *
* Build Date: April 25, 2024 *
* *
* **********************************************************************
* *
* Email: [email protected] *
* Website: https://www.paystack.com *
* *
\
************************************************************************/
if (!defined("WHMCS")) {
die("<!-- Silence. SHHHHH!!!! -->");
}
/**
* Define Paystack gateway configuration options.
*
* @return array
*/
function paystack_config()
{
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
$callbackUrl = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
//substr($_SERVER['SERVER_NAME'], 0, strrpos($_SERVER['SERVER_NAME'], '/')) . '/'
substr(str_replace('/admin/', '/', $_SERVER['REQUEST_URI']), 0, strrpos($_SERVER['REQUEST_URI'], '/')) .
'/modules/gateways/callback/paystack.php';
return array(
'FriendlyName' => array(
'Type' => 'System',
'Value' => 'Paystack (Debit/Credit Cards)'
),
'webhook' => array(
'FriendlyName' => 'Webhook URL',
'Type' => 'yesno',
'Description' => 'Copy and paste this URL on your Webhook URL settings <code>' . $callbackUrl . '</code>',
'Default' => "'" . $callbackUrl . "'",
),
'gatewayLogs' => array(
'FriendlyName' => 'Gateway logs',
'Type' => 'yesno',
'Description' => 'Tick to enable gateway logs',
'Default' => '0'
),
'testMode' => array(
'FriendlyName' => 'Test Mode',
'Type' => 'yesno',
'Description' => 'Tick to enable test mode',
'Default' => '0'
),
'liveSecretKey' => array(
'FriendlyName' => 'Live Secret Key',
'Type' => 'text',
'Size' => '32',
'Default' => 'sk_live_xxx'
),
'livePublicKey' => array(
'FriendlyName' => 'Live Public Key',
'Type' => 'text',
'Size' => '32',
'Default' => 'pk_live_xxx'
),
'testSecretKey' => array(
'FriendlyName' => 'Test Secrect Key',
'Type' => 'text',
'Size' => '32',
'Default' => 'sk_test_xxx'
),
'testPublicKey' => array(
'FriendlyName' => 'Test Public Key',
'Type' => 'text',
'Size' => '32',
'Default' => 'pk_test_xxx'
)
);
}
/**
* Payment link.
*
* @param array $params Payment Gateway Module Parameters
*
* @return string
*/
function paystack_link($params)
{
// Client
$email = $params['clientdetails']['email'];
$phone = $params['clientdetails']['phonenumber'];
$params['langpaynow']
= array_key_exists('langpaynow', $params) ?
$params['langpaynow'] : 'Pay with Paystack';
// Config Options
if ($params['testMode'] == 'on') {
$publicKey = $params['testPublicKey'];
$secretKey = $params['testSecretKey'];
} else {
$publicKey = $params['livePublicKey'];
$secretKey = $params['liveSecretKey'];
}
// check if there is an id in the GET meaning the invoice was loaded directly
$paynowload = (!array_key_exists('id', $_GET));
// Invoice
$invoiceId = $params['invoiceid'];
$amountinkobo = round(floatval($params['amount']) * 100);
$currency = $params['currency'];
///Transaction_reference
$txnref = $invoiceId . '_' . time();
if (!in_array(strtoupper($currency), ['NGN', 'USD', 'GHS', 'ZAR', 'EGP', 'XOF', 'KES', 'RWF'])) {
return ("<b style='color:red;margin:2px;padding:2px;border:1px dotted;display: block;border-radius: 10px;font-size: 13px;'>Sorry, this version of the Paystack WHMCS plugin only accepts NGN, USD, GHS, ZAR, EGP, XOF, KES, and RWF payments. <i>$currency</i> not yet supported.</b>");
}
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
$fallbackUrl = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/')) .
'/modules/gateways/callback/paystack.php?' .
http_build_query(
array(
'invoiceid' => $invoiceId,
'email' => $email,
'phone' => $phone,
'reference' => $txnref,
'amountinkobo' => $amountinkobo,
'go' => 'standard'
)
);
$callbackUrl = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/')) .
'/modules/gateways/callback/paystack.php?' .
http_build_query(
array(
'invoiceid' => $invoiceId
)
);
$code = '
<form target="hiddenIFrame" action="about:blank">
<script src="https://js.paystack.co/v2/inline.js"></script>
<div class="payment-btn-container2"></div>
<script>
// load jQuery 1.12.3 if not loaded
(typeof $ === \'undefined\') && document.write("<scr" + "ipt type=\"text\/javascript\" ' .
'src=\"https:\/\/code.jquery.com\/jquery-1.12.3.min.js\"><\/scr" + "ipt>");
</script>
<script>
$(function() {
var paymentMethod = $(\'select[name="gateway"]\').val();
if (paymentMethod === \'paystack\') {
$(\'.payment-btn-container2\').hide();
var toAppend = \'<button type="button"' .
' onclick="payWithPaystack()"' .
' style="padding: 10px 25px; margin: 10px;border-radius: 5px;background: #021C32; color:#fff">' .
addslashes($params['langpaynow']) . '</button>' .
'<img style="width: 150px; display: block; margin: 0 auto;"' .
' src="https://cdn-assets-cloud.frontify.com/s3/frontify-cloud-files-us/eyJwYXRoIjoiZnJvbnRpZnlcL2FjY291bnRzXC8yYVwvMTQxNzczXC9wcm9qZWN0c1wvMTc4NjE0XC9hc3NldHNcLzdmXC8yNzQ2ODcxXC83NDY5OGViODMzMzhlMWJiNjVhMDk4MTYwNjkzY2FlOC0xNTQwMDM5NjA0LnBuZyJ9:cloud:YYqwtVK3Tb8KMGeFiXCl_w9flKcsEY9D022GMOK9oFc"/>\';
$(\'.payment-btn-container\').append(toAppend);
if($(\'.payment-btn-container\').length===0){
$(\'select[name="gateway"]\').after(toAppend);
}
}
});
</script>
</form>
<div class="hidden" style="display:none"><iframe name="hiddenIFrame"></iframe></div>
<script>
var button_created = false;
var paystackPop = new PaystackPop()
function payWithPaystack(){
paystackPop.checkout({
key: \'' . addslashes(trim($publicKey)) . '\',
email: \'' . addslashes(trim($email)) . '\',
phone: \'' . addslashes(trim($phone)) . '\',
amount: ' . $amountinkobo . ',
currency: \'' . addslashes(trim($currency)) . '\',
ref:\'' . $txnref . '\',
metadata:{
"custom_fields":[
{
"display_name":"Plugin",
"variable_name":"plugin",
"value":"whmcs"
}
]
},
onSuccess: function(response){
$(\'div.alert.alert-info.text-center\').hide();
$(\'.payment-btn-container2\').hide();
window.location.href = \'' . addslashes($callbackUrl) . '&trxref=\' + response.trxref;
},
onCancel: function(){
}
});
$(\'img[alt="Loading"]\').hide();
$(\'div.alert.alert-info.text-center\').html(\'Click the button below to retry payment...\');
create_button();
}
function create_button(){
if(!button_created){
button_created = true;
$(\'.payment-btn-container2\').append(\'<button type="button"' .
' onClick="window.location.reload()"' .
' style="padding: 10px 25px; margin: 10px;border-radius: 5px;background: #021C32; color:#fff">' .
addslashes($params['langpaynow']) . '</button>' .
'<img style="width: 150px; display: block; margin: 0 auto;"' .
' src="https://cdn-assets-cloud.frontify.com/s3/frontify-cloud-files-us/eyJwYXRoIjoiZnJvbnRpZnlcL2FjY291bnRzXC8yYVwvMTQxNzczXC9wcm9qZWN0c1wvMTc4NjE0XC9hc3NldHNcLzdmXC8yNzQ2ODcxXC83NDY5OGViODMzMzhlMWJiNjVhMDk4MTYwNjkzY2FlOC0xNTQwMDM5NjA0LnBuZyJ9:cloud:YYqwtVK3Tb8KMGeFiXCl_w9flKcsEY9D022GMOK9oFc"/>\');
}
}
' . ($paynowload ? 'setTimeout("payWithPaystack()", 5100);' : '') . '
</script>';
return $code;
}