| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Services;
- use App\Util;
- use http\Client\Curl\User;
- use function GuzzleHttp\Psr7\build_query;
- class SitoBank
- {
- public $privateKey;
- public $publicKey;
- public $config;
- public function __construct()
- {
- $payConfigService = new PayConfig();
- $this->config = $payConfigService->getConfig('SitoBank');
- $this->publicKey = '-----BEGIN PUBLIC KEY-----'."\n".chunk_split($this->config['platPublicKey'], 64, "\n").'-----END PUBLIC KEY-----';
- $this->privateKey = '-----BEGIN PRIVATE KEY-----'."\n".chunk_split($this->config['mchPrivateKey'], 64, "\n").'-----END PRIVATE KEY-----';
- }
- public function sign($msg)
- {
- if (openssl_sign($msg, $sign, $this->privateKey, OPENSSL_ALGO_SHA256)) {
- return $sign;
- }
- return false;
- }
- public function verify($msg, $sign)
- {
- if (openssl_verify($msg, $sign, $this->publicKey, OPENSSL_ALGO_SHA256)) {
- return true;
- }
- return false;
- }
- public function getNonceStr( $length = 16 ) {
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- $str = "";
- for ( $i = 0; $i < $length; $i++ ) {
- $str .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
- }
- return $str;
- }
- public function curlPost($url, $payload)
- {
- $timeout=20;
- $data = json_encode($payload);
- $sign = $this->sign($data);
- $signB64 = base64_encode($sign);
- $headers = [
- 'X-Signature:' . $signB64,
- 'X-Account:' . $this->config['merchantCode'],
- 'Content-Type: application/json',
- ];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $result = curl_exec($ch);
- if(strstr($result,'code')){
- Util::WriteLog('sito_error','error_state_'.$httpCode."|".$result);
- }
- curl_close($ch);
- return $result;
- }
- public function notifySign($jsonStr,$sign){
- return $this->verify($jsonStr,$sign);
- }
- }
|