forked from Monetbil/monetbil-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monetbil.php
754 lines (666 loc) · 16.5 KB
/
monetbil.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
<?php
/*
This program 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 any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
define('__MONETBIL__', true);
abstract class Monetbil
{
const WIDGET_URL = 'https://www.monetbil.com/widget/';
const CHECK_PAYMENT_URL = 'https://api.monetbil.com/payment/v1/checkPayment';
// Monetbil Widget version
const MONETBIL_WIDGET_VERSION_V1 = 'v1';
const MONETBIL_WIDGET_VERSION_V2 = 'v2.1';
// Live mode
const STATUS_SUCCESS = 1;
const STATUS_FAILED = 0;
const STATUS_CANCELLED = -1;
// Test mode
const STATUS_SUCCESS_TESTMODE = 7;
const STATUS_FAILED_TESTMODE = 8;
const STATUS_CANCELLED_TESTMODE = 9;
public static $serviceKey;
public static $serviceSecret;
public static $widgetVersion = 'v2.1';
// Setup Monetbil arguments
public static $amount;
public static $currency;
public static $phone;
public static $country;
public static $item_ref;
public static $payment_ref;
public static $user;
public static $first_name;
public static $last_name;
public static $email;
public static $locale;
public static $return_url;
public static $notify_url;
public static $logo;
/**
* getServiceKey
*
* @return string
*/
public static function getServiceKey()
{
return Monetbil::$serviceKey;
}
/**
* setServiceKey
*
* @param string $serviceKey
* @return string
*/
public static function setServiceKey($serviceKey)
{
Monetbil::$serviceKey = $serviceKey;
}
/**
* getServiceSecret
*
* @return string
*/
public static function getServiceSecret()
{
return Monetbil::$serviceSecret;
}
/**
* setServiceSecret
*
* @param string $serviceSecret
* @return string
*/
public static function setServiceSecret($serviceSecret)
{
Monetbil::$serviceSecret = $serviceSecret;
}
/**
* getWidgetVersion
*
* @return string
*/
public static function getWidgetVersion()
{
return Monetbil::$widgetVersion;
}
/**
* getWidgetVersion
*
* @param string $widgetVersion
* @return string
*/
public static function setWidgetVersion($widgetVersion)
{
self::$widgetVersion = $widgetVersion;
}
/**
* getAmount
*
* @return string
*/
public static function getAmount()
{
return self::$amount;
}
/**
* setAmount
*
* @param string $amount
* @return string
*/
public static function setAmount($amount)
{
self::$amount = $amount;
}
/**
* getCurrency
*
* @return string
*/
public static function getCurrency()
{
return self::$currency;
}
/**
* setCurrency
*
* @param string $currency
* @return string
*/
public static function setCurrency($currency)
{
self::$currency = $currency;
}
/**
* getPhone
*
* @return string
*/
public static function getPhone()
{
return self::$phone;
}
/**
* setPhone
*
* @param string $phone
* @return string
*/
public static function setPhone($phone)
{
self::$phone = $phone;
}
/**
* getCountry
*
* @return string
*/
public static function getCountry()
{
return self::$country;
}
/**
* setCountry
*
* @param string $country
* @return string
*/
public static function setCountry($country)
{
self::$country = $country;
}
/**
* getItem_ref
*
* @return string
*/
public static function getItem_ref()
{
return self::$item_ref;
}
/**
* setItem_ref
*
* @param string $item_ref
* @return string
*/
public static function setItem_ref($item_ref)
{
self::$item_ref = $item_ref;
}
/**
* getPayment_ref
*
* @return string
*/
public static function getPayment_ref()
{
return self::$payment_ref;
}
/**
* setPayment_ref
*
* @param string $payment_ref
* @return string
*/
public static function setPayment_ref($payment_ref)
{
self::$payment_ref = $payment_ref;
}
/**
* getUser
*
* @return string
*/
public static function getUser()
{
return self::$user;
}
/**
* setUser
*
* @param string $user
* @return string
*/
public static function setUser($user)
{
self::$user = $user;
}
/**
* getFirst_name
*
* @return string
*/
public static function getFirst_name()
{
return self::$first_name;
}
/**
* setFirst_name
*
* @param string $first_name
* @return string
*/
public static function setFirst_name($first_name)
{
self::$first_name = $first_name;
}
/**
* getLast_name
*
* @return string
*/
public static function getLast_name()
{
return self::$last_name;
}
/**
* setLast_name
*
* @param string $last_name
* @return string
*/
public static function setLast_name($last_name)
{
self::$last_name = $last_name;
}
/**
* getEmail
*
* @return string
*/
public static function getEmail()
{
return self::$email;
}
/**
* setEmail
*
* @param string $email
* @return string
*/
public static function setEmail($email)
{
self::$email = $email;
}
/**
* getLocale
*
* @return string
*/
public static function getLocale()
{
return self::$locale;
}
/**
* setLocale
*
* @param string $locale
* @return string
*/
public static function setLocale($locale)
{
self::$locale = $locale;
}
/**
* getReturn_url
*
* @return string
*/
public static function getReturn_url()
{
return self::$return_url;
}
/**
* setReturn_url
*
* @param string $return_url
* @return string
*/
public static function setReturn_url($return_url)
{
self::$return_url = $return_url;
}
/**
* getNotify_url
*
* @return string
*/
public static function getNotify_url()
{
return self::$notify_url;
}
/**
* setNotify_url
*
* @param string $notify_url
* @return string
*/
public static function setNotify_url($notify_url)
{
self::$notify_url = $notify_url;
}
/**
* getLogo
*
* @return string
*/
public static function getLogo()
{
return self::$logo;
}
/**
* setLogo
*
* @param string $logo
* @return string
*/
public static function setLogo($logo)
{
self::$logo = $logo;
}
/**
* sign
*
* @param string $service_secret
* @param array $params
* @return string
*/
public static function sign($service_secret, $params)
{
ksort($params);
$signature = md5($service_secret . implode('', $params));
return $signature;
}
/**
* checkSign
*
* @param string $service_secret
* @param array $params
* @return boolean
*/
public static function checkSign($service_secret, $params)
{
if (!array_key_exists('sign', $params)) {
return false;
}
$sign = $params['sign'];
unset($params['sign']);
$signature = Monetbil::sign($service_secret, $params);
return ($sign == $signature);
}
/**
* checkPayment
*
* @param string $paymentId
* @return array ($payment_status, $testmode)
*/
public static function checkPayment($paymentId)
{
$postData = array(
'paymentId' => $paymentId
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Monetbil::CHECK_PAYMENT_URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData, '', '&'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$json = curl_exec($ch);
$result = json_decode($json, true);
$payment_status = 0;
$testmode = 0;
if (is_array($result) and array_key_exists('transaction', $result)) {
$transaction = $result['transaction'];
$payment_status = $transaction['status'];
$testmode = $transaction['testmode'];
}
return array($payment_status, $testmode);
}
/**
* getPost
*
* @param string $key
* @param string $default
* @return string | null
*/
public static function getPost($key = null, $default = null)
{
return $key == null ? $_POST : (isset($_POST[$key]) ? $_POST[$key] : $default);
}
/**
* getQuery
*
* @param string $key
* @param string $default
* @return string | null
*/
public static function getQuery($key = null, $default = null)
{
return $key == null ? $_GET : (isset($_GET[$key]) ? $_GET[$key] : $default);
}
/**
* getQueryParams
*
* @return array
*/
public static function getQueryParams()
{
$queryParams = array();
$parts = explode('?', Monetbil::getUrl());
if (isset($parts[1])) {
parse_str($parts[1], $queryParams);
}
return $queryParams;
}
/**
* mergeArguments
*
* @param array $monetbil_args
* @return array
*/
public static function mergeArguments($monetbil_args)
{
$sign = Monetbil::sign(Monetbil::getServiceSecret(), $monetbil_args);
return array_merge(array(
'amount' => Monetbil::getAmount(),
'phone' => Monetbil::getPhone(),
'country' => Monetbil::getCountry(),
'currency' => Monetbil::getCurrency(),
'item_ref' => Monetbil::getItem_ref(),
'payment_ref' => Monetbil::getPayment_ref(),
'user' => Monetbil::getUser(),
'first_name' => Monetbil::getFirst_name(),
'last_name' => Monetbil::getLast_name(),
'email' => Monetbil::getEmail(),
'return_url' => Monetbil::getReturn_url(),
'notify_url' => Monetbil::getNotify_url(),
'logo' => Monetbil::getLogo(),
'sign' => $sign
), $monetbil_args);
}
/**
* getServerUrl
*
* @return string | null
*/
public static function getServerUrl()
{
$server_name = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$scheme = 'http';
if ('443' === $port) {
$scheme = 'https';
}
$url = $scheme . '://' . $server_name;
return $url;
}
/**
* getUrl
*
* @return string | null
*/
public static function getUrl()
{
$url = Monetbil::getServerUrl() . Monetbil::getUri(true);
return $url;
}
/**
* getUri
*
* @param boolean $full
* @return string | null
*/
public static function getUri($full = false)
{
$requestUri = $_SERVER['REQUEST_URI'];
$scriptFilename = $_SERVER['SCRIPT_FILENAME'];
$uri1 = '/' . ltrim($requestUri, '/');
if ($full) {
return $uri1;
}
$filename = '';
if (is_file($scriptFilename)) {
$filename = basename($scriptFilename);
}
$uri = str_replace('/' . $filename, '', $uri1);
return $uri;
}
/**
* getPath
*
* @return stringgetPath
*/
public static function getPath()
{
$documentRoot = Monetbil::getDocumentRoot();
$current_dir = str_replace('\\', '/', __DIR__);
$path = str_replace($documentRoot, '', $current_dir);
return $path;
}
/**
* getDocumentRoot
*
* @return string
*/
public static function getDocumentRoot()
{
$current_script = dirname($_SERVER['SCRIPT_NAME']);
$current_path = dirname($_SERVER['SCRIPT_FILENAME']);
// work out how many folders we are away from document_root
// by working out how many folders deep we are from the url.
// this isn't fool proof
$adjust = explode('/', $current_script);
$adjustCount = count($adjust) - 1;
// move up the path with ../
$traverse = str_repeat('../', $adjustCount);
$adjusted_path = sprintf('%s/%s', $current_path, $traverse);
// real path expands the ../'s to the correct folder names
$realpath = realpath($adjusted_path);
return str_replace('\\', '/', $realpath);
}
/**
* getWidgetUrl
*
* @return string
*/
public static function getWidgetUrl()
{
$version = Monetbil::getWidgetVersion();
$service_key = Monetbil::getServiceKey();
$widget_url = Monetbil::WIDGET_URL . $version . '/' . $service_key;
return $widget_url;
}
/**
* getWidgetV1Url
*
* @return string
*/
public static function getWidgetV1Url($monetbil_args = array())
{
$query_data = Monetbil::mergeArguments($monetbil_args);
$monetbil_v1_redirect = Monetbil::getWidgetUrl() . '?' . http_build_query($query_data, '', '&');
return $monetbil_v1_redirect;
}
/**
* url
*
* @param array $monetbil_args
* @return string
*/
public static function url($monetbil_args = array())
{
$query_data = Monetbil::mergeArguments($monetbil_args);
$payment_url = '';
if (self::MONETBIL_WIDGET_VERSION_V2 == self::getWidgetVersion()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::getWidgetUrl());
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query_data, '', '&'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$body = curl_exec($ch);
$result = json_decode($body, true);
if (is_array($result) and array_key_exists('payment_url', $result)) {
$payment_url = $result['payment_url'];
}
} else {
$payment_url = Monetbil::getWidgetV1Url($query_data);
}
return $payment_url;
}
/**
* redirect
*
* @param array $monetbil_args
* @return string
*/
public static function redirect($monetbil_args = array())
{
$query_data = Monetbil::mergeArguments($monetbil_args);
$url = Monetbil::url($query_data);
header("Location: $url");
exit;
}
/**
* js
*
* @param bool $autoopen
* @return string
*/
public static function js($autoopen = false)
{
$auto = $autoopen ? '-auto' : '';
if (self::MONETBIL_WIDGET_VERSION_V2 == self::getWidgetVersion()) {
$js = '<script type="text/javascript" src="' . Monetbil::getServerUrl() . Monetbil::getPath() . '/assets/js/monetbil' . $auto . '.min.js?t=' . time() . '"></script>';
} else {
$js = '<script type="text/javascript" src="' . Monetbil::getServerUrl() . Monetbil::getPath() . '/assets/js/monetbil-mobile-payments' . $auto . '.js?t=' . time() . '"></script>';
}
return $js;
}
/**
* startPayment
*
* @param array $monetbil_args
* @return string
*/
public static function startPayment($monetbil_args = array())
{
Monetbil::redirect($monetbil_args);
}
}
require_once 'config.php';