SitoBank.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Services;
  3. use App\Util;
  4. use http\Client\Curl\User;
  5. use function GuzzleHttp\Psr7\build_query;
  6. class SitoBank
  7. {
  8. public $privateKey;
  9. public $publicKey;
  10. public $config;
  11. public function __construct()
  12. {
  13. $payConfigService = new PayConfig();
  14. $this->config = $payConfigService->getConfig('SitoBank');
  15. $this->publicKey = '-----BEGIN PUBLIC KEY-----'."\n".chunk_split($this->config['platPublicKey'], 64, "\n").'-----END PUBLIC KEY-----';
  16. $this->privateKey = '-----BEGIN PRIVATE KEY-----'."\n".chunk_split($this->config['mchPrivateKey'], 64, "\n").'-----END PRIVATE KEY-----';
  17. }
  18. public function sign($msg)
  19. {
  20. if (openssl_sign($msg, $sign, $this->privateKey, OPENSSL_ALGO_SHA256)) {
  21. return $sign;
  22. }
  23. return false;
  24. }
  25. public function verify($msg, $sign)
  26. {
  27. if (openssl_verify($msg, $sign, $this->publicKey, OPENSSL_ALGO_SHA256)) {
  28. return true;
  29. }
  30. return false;
  31. }
  32. public function getNonceStr( $length = 16 ) {
  33. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  34. $str = "";
  35. for ( $i = 0; $i < $length; $i++ ) {
  36. $str .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
  37. }
  38. return $str;
  39. }
  40. public function curlPost($url, $payload)
  41. {
  42. $timeout=20;
  43. $data = json_encode($payload);
  44. $sign = $this->sign($data);
  45. $signB64 = base64_encode($sign);
  46. $headers = [
  47. 'X-Signature:' . $signB64,
  48. 'X-Account:' . $this->config['merchantCode'],
  49. 'Content-Type: application/json',
  50. ];
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL, $url);
  53. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  54. curl_setopt($ch, CURLOPT_POST, 1);
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  56. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
  57. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  58. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  59. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  60. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  61. $result = curl_exec($ch);
  62. if(strstr($result,'code')){
  63. Util::WriteLog('sito_error','error_state_'.$httpCode."|".$result);
  64. }
  65. curl_close($ch);
  66. return $result;
  67. }
  68. public function notifySign($jsonStr,$sign){
  69. return $this->verify($jsonStr,$sign);
  70. }
  71. }