PhpTools.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/13
  6. * Time: 下午2:58
  7. */
  8. namespace App\Libs\allinpay;
  9. class PhpTools
  10. {
  11. public $certFile = 'Libs/allinpay/allinpay-pds.pem';//通联公钥证书
  12. public $privateKeyFile = 'Libs/allinpay/20064100000098104.pem';//商户私钥证书
  13. public $password = '111111';//商户私钥密码以及用户密码
  14. public $arrayXml ;
  15. // public $apiUrl = 'https://tlt.allinpay.com/aipg/ProcessServlet';//(生产环境地址,上线时打开该注释)
  16. public $apiUrl = 'http://tlt.allinpay.com/aipg/ProcessServlet';//(生产环境地址,上线时打开该注释)
  17. public function __construct()
  18. {
  19. $this->arrayXml = new ArrayXML();
  20. }
  21. /**
  22. * PHP版本低于 5.4.1 的在通联返回的是 GBK编码环境使用
  23. * 但是本地文件编码是 UTF-8
  24. *
  25. * @param string $hexstr
  26. * @return binary string
  27. */
  28. public function hextobin($hexstr) {
  29. $n = strlen($hexstr);
  30. $sbin = "";
  31. $i = 0;
  32. while($i < $n) {
  33. $a = substr($hexstr, $i, 2);
  34. $c = pack("H*",$a);
  35. if ($i==0) {
  36. $sbin = $c;
  37. } else {
  38. $sbin .= $c;
  39. }
  40. $i+=2;
  41. }
  42. return $sbin;
  43. }
  44. /**
  45. * 验签
  46. */
  47. public function verifyXml($xmlResponse){
  48. // dd($xmlResponse);
  49. // 本地反馈结果验证签名开始
  50. $signature = '';
  51. if (preg_match('/<SIGNED_MSG>(.*)<\/SIGNED_MSG>/i', $xmlResponse, $matches)) {
  52. $signature = $matches[1];
  53. }
  54. $xmlResponseSrc = preg_replace('/<SIGNED_MSG>.*<\/SIGNED_MSG>/i', '', $xmlResponse);
  55. $xmlResponseSrc1 = mb_convert_encoding(str_replace('<','&lt;',$xmlResponseSrc), "UTF-8", "GBK");
  56. // print_r ('验签原文');
  57. // print_r ($xmlResponseSrc1);
  58. $pubKeyId = openssl_get_publickey(file_get_contents(app_path($this->certFile)));
  59. $flag = (bool) openssl_verify($xmlResponseSrc, hex2bin($signature), $pubKeyId);
  60. openssl_free_key($pubKeyId);
  61. // echo '<br/>'+$flag;
  62. if ($flag) {
  63. // echo '<br/>Verified: <font color=green>Passed</font>.';
  64. // 变成数组,做自己相关业务逻辑
  65. $xmlResponse = mb_convert_encoding(str_replace('<?xml version="1.0" encoding="GBK"?>', '<?xml version="1.0" encoding="UTF-8"?>', $xmlResponseSrc), 'UTF-8', 'GBK');
  66. $results = $this->arrayXml->parseString( $xmlResponse , TRUE);
  67. // dd($results);
  68. // echo "<br/><br/><font color=blue>-------------华丽丽的分割线--------------------</font><br/><br/>";
  69. // echo $results;
  70. return $results;
  71. } else {
  72. // echo '<br/>Verified: <font color=red>Failed</font>.';
  73. return FALSE;
  74. }
  75. }
  76. /**
  77. * 验签
  78. */
  79. public function verifyStr($orgStr,$signature){
  80. echo '签名原文:'.$orgStr;
  81. $pubKeyId = openssl_get_publickey(file_get_contents($this->certFile));
  82. $flag = (bool) openssl_verify($orgStr, hex2bin($signature), $pubKeyId);
  83. openssl_free_key($pubKeyId);
  84. if ($flag) {
  85. echo '<br/>Verified: <font color=red>SUCC</font>.';
  86. return TRUE;
  87. } else {
  88. echo '<br/>Verified: <font color=red>Failed</font>.';
  89. return FALSE;
  90. }
  91. }
  92. /**
  93. * 签名
  94. */
  95. public function signXml($params){
  96. $xmlSignSrc = $this->arrayXml->toXmlGBK($params, 'AIPG');
  97. // dd($xmlSignSrc);
  98. $xmlSignSrc=str_replace("TRANS_DETAIL2", "TRANS_DETAIL",$xmlSignSrc);
  99. // dd($xmlSignSrc);
  100. // echo ($xmlSignSrc);
  101. $privateKey = file_get_contents(app_path($this->privateKeyFile));
  102. // dd($privateKey);
  103. $pKeyId = openssl_pkey_get_private($privateKey, $this->password);
  104. openssl_sign($xmlSignSrc, $signature, $pKeyId);
  105. openssl_free_key($pKeyId);
  106. $params['INFO']['SIGNED_MSG'] = bin2hex($signature);
  107. $xmlSignPost = $this->arrayXml->toXmlGBK($params, 'AIPG');
  108. return $xmlSignPost;
  109. }
  110. /**
  111. * 发送请求
  112. */
  113. public function send($params){
  114. $xmlSignPost=$this->signXml($params);
  115. // dd($xmlSignPost);
  116. // dd($this->apiUrl);
  117. $xmlSignPost=str_replace("TRANS_DETAIL2", "TRANS_DETAIL",$xmlSignPost);
  118. $response = CURL::factory()->post($this->apiUrl, $xmlSignPost);
  119. if (! isset($response['body'])) {
  120. die('Error: HTTPS REQUEST Bad.');
  121. }
  122. //获取返回报文
  123. $xmlResponse = $response['body'];
  124. // print_r("返回报文如下:\n");
  125. // print_r(str_replace('<','&lt;',$xmlResponse));
  126. //验证返回报文
  127. $result=$this->verifyXml($xmlResponse);
  128. return $result;
  129. }
  130. }