$blocksize) { $key = pack('H*', $hashfunc($key)); } $key = str_pad($key, $blocksize, chr(0x00)); $ipad = str_repeat(chr(0x36), $blocksize); $opad = str_repeat(chr(0x5c), $blocksize); $hmac = pack( 'H*', $hashfunc( ($key ^ $opad) . pack( 'H*', $hashfunc( ($key ^ $ipad) . $str ) ) ) ); $signature = bin2hex($hmac); } return $signature; } /** * 发送post请求 * @param string $url 请求地址 * @param array $data post键值对数据 * @return string */ public static function sendPost($url,$data,$app_id,$privateKey) { $url .= '?p1='.$data['p1'].'&p2='.$data['p2'].'&p3='.$data['p3'].'×tamp='.$data['timestamp']; file_put_contents('url.txt',$url); $str = PayUtils::mkSign($data); file_put_contents('str.txt',$str); if(empty($str)){ return false; } $sign = strtoupper(PayUtils::getSignature($str,$privateKey)); //加密后(大写):5F51F8065B325EC3491526612CB2A47B84E5E10B file_put_contents('sign.txt',$sign); $headers = array( 'content-type:application/json', 'access_key:'.$sign, 'app_id:'.$app_id ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //执行命令 $data = curl_exec($curl); $dat = print_r($data,true); file_put_contents("dat.txt",$dat); //关闭URL请求 curl_close($curl); return $data; } /** * @param array $data 参数 * @param string $secretKey * @return bool */ // 回调验证函数 public static function checkSign($data, $secretKey) { if(floor($data['amount'])== $data['amount']){ $amount = sprintf("%.1f",$data['amount']); //入金金额(美元) } else { $amount = $data['amount']; } if(floor($data['exchangeRate'])== $data['exchangeRate']){ $exchangeRate = sprintf("%.1f",$data['exchangeRate']); //入金金额(美元) } else { $exchangeRate = $data['exchangeRate']; //转换汇率 } $poundage = sprintf("%.6f",$data['poundage']); //手续费(美元) $merchantNo = $data['merchantNo']; //商户的商户号 $merchantOrderNo = $data['merchantOrderNo']; //商户订单号 $orderNo = $data['orderNo']; //FastPay 交易流水号 $timestamp = $data['timestamp']; //时间戳 $sign = $data['sign']; //签名 $str = "{$amount}".'&'."{$exchangeRate}".'&'."{$poundage}".'&'."{$merchantNo}".'&'."{$merchantOrderNo}".'&'."{$orderNo}".'&'."{$timestamp}"; //加密字符串 file_put_contents('huanqiustr.txt',$str); $sign_new = PayUtils::getSignature($str,$secretKey); //根据加密规则生成的新签名 $sign_new = strtoupper($sign_new); //加密后(大写) // 比较两者之间的规则 if($sign_new == $sign){ return true; }else{ return false; } } }