-
Notifications
You must be signed in to change notification settings - Fork 0
/
JustapAlipayMini.php
1367 lines (1175 loc) · 52.4 KB
/
JustapAlipayMini.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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace payment;
// -----------------------------------------------------------------
// 以下代码所有开源聚合支付插件相同
// -----------------------------------------------------------------
// -----------------------------
// class CaBundleJustapAlipayMini
// -----------------------------
if (!class_exists('CaBundleJustapAlipayMini')) {
/**
* @author Chris Smith <chris@cs278.org>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class CaBundleJustapAlipayMini
{
/** @var string|null */
private static $caPath;
/** @var array<string, bool> */
private static $caFileValidity = array();
/** @var bool|null */
private static $useOpensslParse;
/**
* Returns the system CA bundle path, or a path to the bundled one
*
* This method was adapted from Sslurp.
* https://github.com/EvanDotPro/Sslurp
*
* (c) Evan Coury <me@evancoury.com>
*
* For the full copyright and license information, please see below:
*
* Copyright (c) 2013, Evan Coury
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @return string path to a CA bundle file or directory
*/
public static function getSystemCaRootBundlePath($logger = null)
{
if (self::$caPath !== null) {
return self::$caPath;
}
$CaBundleJustapAlipayMiniPaths = array();
// If SSL_CERT_FILE env variable points to a valid certificate/bundle, use that.
// This mimics how OpenSSL uses the SSL_CERT_FILE env variable.
$CaBundleJustapAlipayMiniPaths[] = self::getEnvVariable('SSL_CERT_FILE');
// If SSL_CERT_DIR env variable points to a valid certificate/bundle, use that.
// This mimics how OpenSSL uses the SSL_CERT_FILE env variable.
$CaBundleJustapAlipayMiniPaths[] = self::getEnvVariable('SSL_CERT_DIR');
$CaBundleJustapAlipayMiniPaths[] = ini_get('openssl.cafile');
$CaBundleJustapAlipayMiniPaths[] = ini_get('openssl.capath');
$otherLocations = array(
'/etc/pki/tls/certs/ca-bundle.crt', // Fedora, RHEL, CentOS (ca-certificates package)
'/etc/ssl/certs/ca-certificates.crt', // Debian, Ubuntu, Gentoo, Arch Linux (ca-certificates package)
'/etc/ssl/ca-bundle.pem', // SUSE, openSUSE (ca-certificates package)
'/usr/local/share/certs/ca-root-nss.crt', // FreeBSD (ca_root_nss_package)
'/usr/ssl/certs/ca-bundle.crt', // Cygwin
'/opt/local/share/curl/curl-ca-bundle.crt', // OS X macports, curl-ca-bundle package
'/usr/local/share/curl/curl-ca-bundle.crt', // Default cURL CA bunde path (without --with-ca-bundle option)
'/usr/share/ssl/certs/ca-bundle.crt', // Really old RedHat?
'/etc/ssl/cert.pem', // OpenBSD
'/usr/local/etc/ssl/cert.pem', // FreeBSD 10.x
'/usr/local/etc/openssl/cert.pem', // OS X homebrew, openssl package
'/usr/local/etc/openssl@1.1/cert.pem', // OS X homebrew, openssl@1.1 package
);
foreach($otherLocations as $location) {
$otherLocations[] = dirname($location);
}
$CaBundleJustapAlipayMiniPaths = array_merge($CaBundleJustapAlipayMiniPaths, $otherLocations);
foreach ($CaBundleJustapAlipayMiniPaths as $CaBundleJustapAlipayMini) {
if ($CaBundleJustapAlipayMini && self::caFileUsable($CaBundleJustapAlipayMini, $logger)) {
return self::$caPath = $CaBundleJustapAlipayMini;
}
if ($CaBundleJustapAlipayMini && self::caDirUsable($CaBundleJustapAlipayMini, $logger)) {
return self::$caPath = $CaBundleJustapAlipayMini;
}
}
return self::$caPath = static::getBundledCaBundleJustapAlipayMiniPath(); // Bundled CA file, last resort
}
/**
* Returns the path to the bundled CA file
*
* In case you don't want to trust the user or the system, you can use this directly
*
* @return string path to a CA bundle file
*/
public static function getBundledCaBundleJustapAlipayMiniPath()
{
$CaBundleJustapAlipayMiniFile = __DIR__.'/../res/cacert.pem';
// cURL does not understand 'phar://' paths
// see https://github.com/composer/ca-bundle/issues/10
if (0 === strpos($CaBundleJustapAlipayMiniFile, 'phar://')) {
$tempCaBundleJustapAlipayMiniFile = tempnam(sys_get_temp_dir(), 'openssl-ca-bundle-');
if (false === $tempCaBundleJustapAlipayMiniFile) {
throw new \RuntimeException('Could not create a temporary file to store the bundled CA file');
}
file_put_contents(
$tempCaBundleJustapAlipayMiniFile,
file_get_contents($CaBundleJustapAlipayMiniFile)
);
register_shutdown_function(function() use ($tempCaBundleJustapAlipayMiniFile) {
@unlink($tempCaBundleJustapAlipayMiniFile);
});
$CaBundleJustapAlipayMiniFile = $tempCaBundleJustapAlipayMiniFile;
}
return $CaBundleJustapAlipayMiniFile;
}
/**
* Validates a CA file using opensl_x509_parse only if it is safe to use
*
* @param string $filename
*
* @return bool
*/
public static function validateCaFile($filename, $logger = null)
{
static $warned = false;
if (isset(self::$caFileValidity[$filename])) {
return self::$caFileValidity[$filename];
}
$contents = file_get_contents($filename);
// assume the CA is valid if php is vulnerable to
// https://www.sektioneins.de/advisories/advisory-012013-php-openssl_x509_parse-memory-corruption-vulnerability.html
if (!static::isOpensslParseSafe()) {
if (!$warned && $logger) {
$logger->warning(sprintf(
'Your version of PHP, %s, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.',
PHP_VERSION
));
$warned = true;
}
$isValid = !empty($contents);
} elseif (is_string($contents) && strlen($contents) > 0) {
$contents = preg_replace("/^(\\-+(?:BEGIN|END))\\s+TRUSTED\\s+(CERTIFICATE\\-+)\$/m", '$1 $2', $contents);
if (null === $contents) {
// regex extraction failed
$isValid = false;
} else {
$isValid = (bool) openssl_x509_parse($contents);
}
} else {
$isValid = false;
}
if ($logger) {
$logger->debug('Checked CA file '.realpath($filename).': '.($isValid ? 'valid' : 'invalid'));
}
return self::$caFileValidity[$filename] = $isValid;
}
/**
* Test if it is safe to use the PHP function openssl_x509_parse().
*
* This checks if OpenSSL extensions is vulnerable to remote code execution
* via the exploit documented as CVE-2013-6420.
*
* @return bool
*/
public static function isOpensslParseSafe()
{
if (null !== self::$useOpensslParse) {
return self::$useOpensslParse;
}
if (PHP_VERSION_ID >= 50600) {
return self::$useOpensslParse = true;
}
// Vulnerable:
// PHP 5.3.0 - PHP 5.3.27
// PHP 5.4.0 - PHP 5.4.22
// PHP 5.5.0 - PHP 5.5.6
if (
(PHP_VERSION_ID < 50400 && PHP_VERSION_ID >= 50328)
|| (PHP_VERSION_ID < 50500 && PHP_VERSION_ID >= 50423)
|| PHP_VERSION_ID >= 50507
) {
// This version of PHP has the fix for CVE-2013-6420 applied.
return self::$useOpensslParse = true;
}
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
// Windows is probably insecure in this case.
return self::$useOpensslParse = false;
}
$compareDistroVersionPrefix = function ($prefix, $fixedVersion) {
$regex = '{^'.preg_quote($prefix).'([0-9]+)$}';
if (preg_match($regex, PHP_VERSION, $m)) {
return ((int) $m[1]) >= $fixedVersion;
}
return false;
};
// Hard coded list of PHP distributions with the fix backported.
if (
$compareDistroVersionPrefix('5.3.3-7+squeeze', 18) // Debian 6 (Squeeze)
|| $compareDistroVersionPrefix('5.4.4-14+deb7u', 7) // Debian 7 (Wheezy)
|| $compareDistroVersionPrefix('5.3.10-1ubuntu3.', 9) // Ubuntu 12.04 (Precise)
) {
return self::$useOpensslParse = true;
}
// Symfony Process component is missing so we assume it is unsafe at this point
return self::$useOpensslParse = false;
// This is where things get crazy, because distros backport security
// fixes the chances are on NIX systems the fix has been applied but
// it's not possible to verify that from the PHP version.
//
// To verify exec a new PHP process and run the issue testcase with
// known safe input that replicates the bug.
// Based on testcase in https://github.com/php/php-src/commit/c1224573c773b6845e83505f717fbf820fc18415
// changes in https://github.com/php/php-src/commit/76a7fd893b7d6101300cc656058704a73254d593
$cert = 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVwRENDQTR5Z0F3SUJBZ0lKQUp6dThyNnU2ZUJjTUEwR0NTcUdTSWIzRFFFQkJRVUFNSUhETVFzd0NRWUQKVlFRR0V3SkVSVEVjTUJvR0ExVUVDQXdUVG05eVpISm9aV2x1TFZkbGMzUm1ZV3hsYmpFUU1BNEdBMVVFQnd3SApTOE9Ed3Jac2JqRVVNQklHQTFVRUNnd0xVMlZyZEdsdmJrVnBibk14SHpBZEJnTlZCQXNNRmsxaGJHbGphVzkxCmN5QkRaWEowSUZObFkzUnBiMjR4SVRBZkJnTlZCQU1NR0cxaGJHbGphVzkxY3k1elpXdDBhVzl1WldsdWN5NWsKWlRFcU1DZ0dDU3FHU0liM0RRRUpBUlliYzNSbFptRnVMbVZ6YzJWeVFITmxhM1JwYjI1bGFXNXpMbVJsTUhVWQpaREU1TnpBd01UQXhNREF3TURBd1dnQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBCkFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUEKQUFBQUFBQVhEVEUwTVRFeU9ERXhNemt6TlZvd2djTXhDekFKQmdOVkJBWVRBa1JGTVJ3d0dnWURWUVFJREJOTwpiM0prY21obGFXNHRWMlZ6ZEdaaGJHVnVNUkF3RGdZRFZRUUhEQWRMdzRQQ3RteHVNUlF3RWdZRFZRUUtEQXRUClpXdDBhVzl1UldsdWN6RWZNQjBHQTFVRUN3d1dUV0ZzYVdOcGIzVnpJRU5sY25RZ1UyVmpkR2x2YmpFaE1COEcKQTFVRUF3d1liV0ZzYVdOcGIzVnpMbk5sYTNScGIyNWxhVzV6TG1SbE1Tb3dLQVlKS29aSWh2Y05BUWtCRmh0egpkR1ZtWVc0dVpYTnpaWEpBYzJWcmRHbHZibVZwYm5NdVpHVXdnZ0VpTUEwR0NTcUdTSWIzRFFFQkFRVUFBNElCCkR3QXdnZ0VLQW9JQkFRRERBZjNobDdKWTBYY0ZuaXlFSnBTU0RxbjBPcUJyNlFQNjV1c0pQUnQvOFBhRG9xQnUKd0VZVC9OYSs2ZnNnUGpDMHVLOURaZ1dnMnRIV1dvYW5TYmxBTW96NVBINlorUzRTSFJaN2UyZERJalBqZGhqaAowbUxnMlVNTzV5cDBWNzk3R2dzOWxOdDZKUmZIODFNTjJvYlhXczROdHp0TE11RDZlZ3FwcjhkRGJyMzRhT3M4CnBrZHVpNVVhd1Raa3N5NXBMUEhxNWNNaEZHbTA2djY1Q0xvMFYyUGQ5K0tBb2tQclBjTjVLTEtlYno3bUxwazYKU01lRVhPS1A0aWRFcXh5UTdPN2ZCdUhNZWRzUWh1K3ByWTNzaTNCVXlLZlF0UDVDWm5YMmJwMHdLSHhYMTJEWAoxbmZGSXQ5RGJHdkhUY3lPdU4rblpMUEJtM3ZXeG50eUlJdlZBZ01CQUFHalFqQkFNQWtHQTFVZEV3UUNNQUF3CkVRWUpZSVpJQVliNFFnRUJCQVFEQWdlQU1Bc0dBMVVkRHdRRUF3SUZvREFUQmdOVkhTVUVEREFLQmdnckJnRUYKQlFjREFqQU5CZ2txaGtpRzl3MEJBUVVGQUFPQ0FRRUFHMGZaWVlDVGJkajFYWWMrMVNub2FQUit2SThDOENhRAo4KzBVWWhkbnlVNGdnYTBCQWNEclk5ZTk0ZUVBdTZacXljRjZGakxxWFhkQWJvcHBXb2NyNlQ2R0QxeDMzQ2tsClZBcnpHL0t4UW9oR0QySmVxa2hJTWxEb214SE83a2EzOStPYThpMnZXTFZ5alU4QVp2V01BcnVIYTRFRU55RzcKbFcyQWFnYUZLRkNyOVRuWFRmcmR4R1ZFYnY3S1ZRNmJkaGc1cDVTanBXSDErTXEwM3VSM1pYUEJZZHlWODMxOQpvMGxWajFLRkkyRENML2xpV2lzSlJvb2YrMWNSMzVDdGQwd1lCY3BCNlRac2xNY09QbDc2ZHdLd0pnZUpvMlFnClpzZm1jMnZDMS9xT2xOdU5xLzBUenprVkd2OEVUVDNDZ2FVK1VYZTRYT1Z2a2NjZWJKbjJkZz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K';
$script = <<<'EOT'
error_reporting(-1);
$info = openssl_x509_parse(base64_decode('%s'));
var_dump(PHP_VERSION, $info['issuer']['emailAddress'], $info['validFrom_time_t']);
EOT;
$script = '<'."?php\n".sprintf($script, $cert);
return self::$useOpensslParse = false;
$output = preg_split('{\r?\n}', trim($process->getOutput()));
$errorOutput = trim($process->getErrorOutput());
if (
is_array($output)
&& count($output) === 3
&& $output[0] === sprintf('string(%d) "%s"', strlen(PHP_VERSION), PHP_VERSION)
&& $output[1] === 'string(27) "stefan.esser@sektioneins.de"'
&& $output[2] === 'int(-1)'
&& preg_match('{openssl_x509_parse\(\): illegal (?:ASN1 data type for|length in) timestamp in - on line \d+}', $errorOutput)
) {
// This PHP has the fix backported probably by a distro security team.
return self::$useOpensslParse = true;
}
return self::$useOpensslParse = false;
}
/**
* Resets the static caches
* @return void
*/
public static function reset()
{
self::$caFileValidity = array();
self::$caPath = null;
self::$useOpensslParse = null;
}
/**
* @param string $name
* @return string|false
*/
private static function getEnvVariable($name)
{
if (isset($_SERVER[$name])) {
return (string) $_SERVER[$name];
}
if (PHP_SAPI === 'cli' && ($value = getenv($name)) !== false && $value !== null) {
return (string) $value;
}
return false;
}
/**
* @param string|false $certFile
* @return bool
*/
private static function caFileUsable($certFile, $logger = null)
{
return $certFile
&& static::isFile($certFile, $logger)
&& static::isReadable($certFile, $logger)
&& static::validateCaFile($certFile, $logger);
}
/**
* @param string|false $certDir
* @return bool
*/
private static function caDirUsable($certDir, $logger = null)
{
return $certDir
&& static::isDir($certDir, $logger)
&& static::isReadable($certDir, $logger)
&& static::glob($certDir . '/*', $logger);
}
/**
* @param string $certFile
* @return bool
*/
private static function isFile($certFile, $logger = null)
{
$isFile = @is_file($certFile);
if (!$isFile && $logger) {
$logger->debug(sprintf('Checked CA file %s does not exist or it is not a file.', $certFile));
}
return $isFile;
}
/**
* @param string $certDir
* @return bool
*/
private static function isDir($certDir, $logger = null)
{
$isDir = @is_dir($certDir);
if (!$isDir && $logger) {
$logger->debug(sprintf('Checked directory %s does not exist or it is not a directory.', $certDir));
}
return $isDir;
}
/**
* @param string $certFileOrDir
* @return bool
*/
private static function isReadable($certFileOrDir, $logger = null)
{
$isReadable = @is_readable($certFileOrDir);
if (!$isReadable && $logger) {
$logger->debug(sprintf('Checked file or directory %s is not readable.', $certFileOrDir));
}
return $isReadable;
}
/**
* @param string $pattern
* @return bool
*/
private static function glob($pattern, $logger = null)
{
$certs = glob($pattern);
if ($certs === false) {
if ($logger) {
$logger->debug(sprintf("An error occurred while trying to find certificates for pattern: %s", $pattern));
}
return false;
}
if (count($certs) === 0) {
if ($logger) {
$logger->debug(sprintf("No CA files found for pattern: %s", $pattern));
}
return false;
}
return true;
}
}
}
// -----------------------------
// class CurlHttpClientJustapAlipayMini
// -----------------------------
if (!class_exists('CurlHttpClientJustapAlipayMini')) {
class CurlHttpClientJustapAlipayMini {
public function send($uri, $headerOptions, $method, $body, array $options): array
{
$ch = curl_init();
$options = $this->buildOptions($uri, $headerOptions, $method, $body, $options);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
/** @var string|false $body */
$body = $this->exec($ch);
if ($body === false) {
$errorCode = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
$message = "cURL Error ({$errorCode}) {$error}";
$errorNumbers = [
CURLE_FAILED_INIT,
CURLE_URL_MALFORMAT,
CURLE_URL_MALFORMAT_USER,
];
if (in_array($errorCode, $errorNumbers, true)) {
throw new \Exception($message);
}
throw new \Exception($message);
}
$responses = $this->createResponse($ch, $body);
curl_close($ch);
return $responses;
}
public function get($uri, $headers) {
$headerOptions = [];
foreach ($headers as $key => $values) {
if (is_array($values)) {
$headerOptions[] = $key . ': ' . implode(', ', $values);
} else {
$headerOptions[] = $key . ': ' . $values;
}
}
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $uri,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => $headerOptions,
));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$body = curl_exec($ch);
if ($body === false) {
$errorCode = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
$message = "cURL Error ({$errorCode}) {$error}";
$errorNumbers = [
CURLE_FAILED_INIT,
CURLE_URL_MALFORMAT,
CURLE_URL_MALFORMAT_USER,
];
if (in_array($errorCode, $errorNumbers, true)) {
throw new \Exception($message);
}
throw new \Exception($message);
}
$response = $this->createResponse($ch, $body);
curl_close($ch);
return $response;
}
public function buildOptions($uri, $headerOptions, $method, $body, array $options): array
{
$headers = [];
foreach ($headerOptions as $key => $values) {
if (is_array($values)) {
$headers[] = $key . ': ' . implode(', ', $values);
} else {
$headers[] = $key . ': ' . $values;
}
}
$out = [
CURLOPT_URL => (string)$uri,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $headers,
];
switch (strtoupper($method)) {
case 'GET':
$out[CURLOPT_HTTPGET] = true;
break;
case 'POST':
$out[CURLOPT_POST] = true;
break;
case 'HEAD':
$out[CURLOPT_NOBODY] = true;
break;
default:
$out[CURLOPT_POST] = true;
$out[CURLOPT_CUSTOMREQUEST] = $method;
break;
}
$out[CURLOPT_POSTFIELDS] = $body;
// GET requests with bodies require custom request to be used.
if ($out[CURLOPT_POSTFIELDS] !== '' && isset($out[CURLOPT_HTTPGET])) {
$out[CURLOPT_CUSTOMREQUEST] = 'get';
}
if ($out[CURLOPT_POSTFIELDS] === '') {
unset($out[CURLOPT_POSTFIELDS]);
}
$out[CURLOPT_SSL_VERIFYHOST] = false;
return $out;
}
protected function createResponse($handle, $responseData): array
{
/** @psalm-suppress PossiblyInvalidArgument */
$headerSize = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$headers = trim(substr($responseData, 0, $headerSize));
$body = substr($responseData, $headerSize);
return [
'headers' => explode("\r\n", $headers),
'body' => $body
];
}
/**
* Execute the curl handle.
*
* @param resource|\CurlHandle $ch Curl Resource handle
* @return string|bool
* @psalm-suppress UndefinedDocblockClass
*/
protected function exec($ch)
{
/** @psalm-suppress PossiblyInvalidArgument */
return curl_exec($ch);
}
}
}
// ------------------------------
// class JustapBaseJustapAlipayMini
// ------------------------------
if (!class_exists('JustapBaseJustapAlipayMini')) {
class JustapBaseJustapAlipayMini {
//请求渠道
// const CHANNEL_ALIPAY_SCAN = 'AlipayScan';//: 支付宝条码支付
// const CHANNEL_ALIPAY_FACE = 'AlipayFace';//: 支付宝刷脸支付
const CHANNEL_ALIPAY_QR = 'AlipayQR';//: 支付宝扫码支付
const CHANNEL_ALIPAY_APP = 'AlipayApp'; //: 支付宝 App 支付
const CHANNEL_ALIPAY_WAP = 'AlipayWap'; //: 支付宝手机网站支付
const CHANNEL_ALIPAY_PAGE = 'AlipayPage'; //: 支付宝电脑网站支付
const CHANNEL_ALIPAY_LITE = 'AlipayLite';//: 支付宝小程序支付
const CHANNEL_WECHATPAY_APP = 'WechatpayApp'; //: 微信 App 支付
const CHANNEL_WECHATPAY_H5 = 'WechatpayH5'; //: 微信 H5 支付
const CHANNEL_WECHATPAY_NATIVE = 'WechatpayNative';//: 微信 Native 支付
const CHANNEL_WECHATPAY_LITE = 'WechatpayLite';//: 微信小程序支付
const CHANNEL_WECHATPAY_JSAPI = 'WechatpayJSAPI';//: 微信 JSAPI 支付
// const CHANNEL_WECHATPAY_FACE = 'WechatpayFace';//: 刷脸支付
// const CHANNEL_WECHATPAY_SCAN = 'WechatpayScan';//: 微信付款码支付
protected $config = [];
protected $client;
public function __construct($params = [])
{
$this->config = $params;
}
function initSdk() {
$server = JustapConfigurationJustapAlipayMini::getDefaultConfiguration();
$server->setApiKey($this->config['justap_secret_key']);
$server->setHost('https://trade.justap.cn');
$server->setPrivateKey($this->config['justap_merchant_private_key']);
$server->setUserAgent('justap-php-sdk/shopxo');
$sdk = new JustapSdkJustapAlipayMini();
$sdk->setConfig($server);
$sdk->setClient(new CurlHttpClientJustapAlipayMini());
$this->client = $sdk;
}
public function doPay($channel = '', $params = []) {
$this->initSdk();
$allKeys = array_keys($this->config);
$requiredItems = ['justap_app_id', 'justap_secret_key', 'justap_public_key', 'justap_merchant_private_key'];
// 配置信息
if (empty($this->config) || count(array_intersect($requiredItems, $allKeys)) != count($requiredItems)) {
return DataReturn('支付缺少配置', -1);
}
$creatChargeParams = [
'app_id' => $this->config['justap_app_id'],
'merchant_trade_id' => $params['order_no'], //交易单号
'channel' => $channel,
'amount' => floatval($params['total_price']),
'client_ip' => GetClientIP(),
'currency' => 'CNY',
'subject' => $params['name'],
'body' => $params['name'],
'extra' => '',
// 'metadata' => [
// 'user_id' => $params['user']['id'],
// 'username' => $params['user']['username'],
// 'nickname' => $params['user']['nickname'],
// ],
'ttl' => $this->orderAutoCloseTime(),
'description' => $params['name'],
'notify_url' => $params['notify_url'],
'callback_url' => $params['call_back_url'],
'notification_area' => 'CN',
];
$resp = [];
switch ($channel) {
case self::CHANNEL_ALIPAY_APP:
$creatChargeParams['extra'] = [
'alipay_app' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_ALIPAY_LITE:
$creatChargeParams['extra'] = [
'alipay_lite' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_ALIPAY_PAGE:
$creatChargeParams['extra'] = [
'alipay_page' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_ALIPAY_WAP:
$creatChargeParams['extra'] = [
'alipay_wap' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_ALIPAY_QR:
$creatChargeParams['extra'] = [
'alipay_qr' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_WECHATPAY_APP:
$creatChargeParams['extra'] = [
'wechatpay_app' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_WECHATPAY_H5:
$creatChargeParams['extra'] = [
'wechatpay_h5' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_WECHATPAY_NATIVE:
$creatChargeParams['extra'] = [
'wechatpay_native' => new \stdClass()
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_WECHATPAY_LITE:
$creatChargeParams['extra'] = [
'wechatpay_lite' => [
'payer' => [
'openid' => $params['openid'],
]
]
];
$resp = $this->client->createCharge($creatChargeParams);
break;
case self::CHANNEL_WECHATPAY_JSAPI:
$creatChargeParams['extra'] = [
'wechatpay_jsapi' => [
'payer' => [
'openid' => $params['openid'],
]
]
];
$resp = $this->client->createCharge($creatChargeParams);
break;
default:
return DataReturn('渠道['.$channel.']暂未支付', -1);
}
if (strpos($resp['headers'][0], '200') < 0) {
return DataReturn('支付接口下单失败', -1);
}
$body = json_decode($resp['body'], true);
if (isset($body['code']) && $body['code'] != 0) {
throw new \Exception($body['message']);
}
return $body;
}
public function Config()
{
// 基础信息
$base = [
'name' => '开源聚合支付', // 插件名称
'version' => 'v1.4.0', // 插件版本
'apply_version' => '不限', // 适用系统版本描述
'apply_terminal' => ['pc', 'h5', 'ios', 'android', 'alipay', 'weixin'], // 适用终端 默认全部 ['pc', 'h5', 'ios', 'android', 'alipay', 'weixin', 'baidu', 'toutiao']
'desc' => '<h1>开源聚合(TM)支付</h1>
<p style="margin-top: 10px;">不论是使用微信、支付宝,不论是在App、小程序、公众号哪种载体,一点接入所有主流支付渠道,开源聚合(TM)支付给你最简单、快捷的接入体验,彻底告别繁琐的支付接入流程。</p>
<div style="margin-top: 10px;"><a href="https://justap.cn" target="_blank">立即免费申请</a></div>
',
'author' => '开源聚合支付', // 开发者
'author_url' => 'https://justap.cn', // 开发者主页
];
// 配置信息
$element = [
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'justap_app_id',
'placeholder' => 'App ID',
'title' => '开源聚合支付 APP ID',
'is_required' => 1,
'message' => '请填写开源聚合支付平台商家APP ID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'justap_secret_key',
'placeholder' => 'App Secret',
'title' => '开源聚合支付平台商家 App Secret',
'is_required' => 1,
'message' => '请填写开源聚合支付平台商家App Secret',
],
[
'element' => 'textarea',
'default' => '',
'name' => 'justap_public_key',
'placeholder' => '开源聚合支付平台公钥',
'title' => '开源聚合支付平台公钥',
'is_required' => 1,
'message' => '请填写开源聚合支付平台公钥',
],
[
'element' => 'textarea',
'default' => '',
'name' => 'justap_merchant_private_key',
'placeholder' => '商家私钥',
'title' => '商家私钥',
'is_required' => 1,
'message' => '请填写商家私钥',
]
];
return [
'base' => $base,
'element' => $element,
];
}
/**
* 同步返回
* justap 不通过同步方式返回支付结果。忽略处理
* @param $params
* @return array
*/
public function Respond($params = []) {
$data = empty($_POST) ? $_GET : array_merge($_GET, $_POST);
ksort($data);
$data['trade_no'] = $data['merchant_trade_id']; // 支付平台 - 订单号
$data['out_trade_no'] = $data['charge_no']; // 本系统发起支付的 - 订单号
return DataReturn('请进入[我的订单]查看支付结果', 0, $data);
}
/**
* 异步通知
* 接收来自服务器的支付结果,以此为准
* @param $params
* @return array|void
*/
public function Notify($params = []) {
$this->initSdk();
// 来自 justap 的通知数据是 json 格式
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['is_encrypted']) && $data['is_encrypted']) {
if (!$this->config['justap_public_key']) {
return DataReturn('缺少配置', -1);
}
$decrypted = DecryptRsaJustapAlipayMini($this->config['justap_public_key'], $data['data']);
if (!$decrypted) {
return DataReturn('解密失败', -1);
}
$data['data'] = json_decode($decrypted, true);
}
$notifyData = $data['data'];
// TradeType_CHARGE_PAID 支付通知
if ($notifyData['trade_type'] == 2) {
if ($notifyData['is_paid']) {
return DataReturn('支付成功', 0, $this->ReturnPaidData($notifyData));
}
}
return DataReturn('未知的状态', -1);
}
public function Refund($params = []) {
// 参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_no',
'error_msg' => '订单号不能为空',
],
[
'checked_type' => 'empty',
'key_name' => 'trade_no',
'error_msg' => '交易平台订单号不能为空',
],
[
'checked_type' => 'empty',
'key_name' => 'refund_price',
'error_msg' => '退款金额不能为空',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 退款原因
$refund_reason = empty($params['refund_reason']) ? $params['order_no'].'订单退款'.$params['refund_price'].'元' : $params['refund_reason'];
$chargeId = $params['trade_no'];
$this->initSdk();
$createRefundParams = [
'charge_id' => $chargeId,
'app_id' => $this->config['justap_app_id'],
'description' => $refund_reason,
'merchant_refund_id' => $params['order_no'],
'amount' => $params['refund_price']
];
$resp = $this->client->createRefund($createRefundParams);
if (empty($resp) || !isset($resp['headers']) || count($resp['headers']) < 0) {
return DataReturn('支付平台下单失败', -1);
}
if (strpos($resp['headers'][0], '200') < 0) {
return DataReturn('支付接口下单失败', -1);
}
$body = json_decode($resp['body'], true);
if (isset($body['code']) && $body['code'] != 0) {
throw new \Exception($body['message']);
}
if ($body['data']['refund_id']) {
return DataReturn('退款成功', 0, [
'out_trade_no' => $body['data']['charge_merchant_trade_id'],
'trade_no' => $chargeId,
'buyer_user' => '',
'refund_price' => $body['data']['amount'],
'return_params' => [],
]);
}
if (strtolower($body['data']['status']) == 'refunding') {
// todo 临时的做法
return DataReturn('退款成功,可前往支付平台进一步确认结果', 0, [
'out_trade_no' => $body['data']['charge_merchant_trade_id'],
'trade_no' => $chargeId,
'buyer_user' => '',
'refund_price' => $body['data']['amount'],
'return_params' => [],
]);
if (!$body['data']['is_success']) {
// 请求退款成功,
// 需要查询退款结果 4 次
for ($i = 0; $i <= 3; $i++) {
$queryResp = $this->client->queryRefund([
'charge_id' => $chargeId,
'refund_id' => $body['data']['refund_id'],
'app_id' => $this->config['justap_app_id']
]);
if (empty($queryResp) || !isset($queryResp['headers']) || count($queryResp['headers']) < 0) {
return DataReturn('请求退款成功但查询结果失败,建议前往支付平台确认', -1);
}
if (strpos($queryResp['headers'][0], '200') < 0) {
return DataReturn('请求退款成功但查询结果失败,建议前往支付平台确认', -1);
}
$refundQueryBody = json_decode($queryResp['body'], true);
if (isset($refundQueryBody['code']) && $refundQueryBody['code'] != 0) {
return DataReturn('请求退款成功但查询结果失败,建议前往支付平台确认', -1);
}
if ($refundQueryBody['data']['is_success'] && strtolower($refundQueryBody['data']['status']) == 'refunded') {
return DataReturn('退款成功', 0, [
'out_trade_no' => $refundQueryBody['data']['charge_merchant_trade_id'],
'trade_no' => $chargeId,
'buyer_user' => '',
'refund_price' => $refundQueryBody['data']['amount'],
'return_params' => [],
]);
}
sleep(2);
}
}
return DataReturn('退款请求正在处理中,结果请稍后进入支付平台查询', -1);
}
return DataReturn('请求退求失败,请进入支付平台查询', -1);
}
public function SuccessReturn($params = [])
{
if (empty($params)) {
$params = input();
}
return 'success';
}
public function ErrorReturn($params = [])
{
throw new \Exception('支付失败');
}
public function OrderAutoCloseTime()
{
return intval(MyC('common_order_close_limit_time', 30, true)) * 60;
}
private function ReturnPaidData($data)
{
// 返回数据固定基础参数
$data['trade_no'] = $data['charge_id']; // 支付平台 - 订单号
$data['buyer_user'] = ""; // 支付平台 - 用户
$data['out_trade_no'] = $data['merchant_trade_id']; // 本系统发起支付的 - 订单号