| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace common\pay\huanqiu;
- class PayUtils
- {
- /**
- * @param array $data
- * @return string
- */
- //对字符串进行排序
- public static function mkSign($data){
- if(!is_array($data)){
- return false;
- }
- $str = '';
- $flag = false;
- foreach ($data as $v){
- if(empty($v)){
- continue;
- }
- $str.=$v.'&';
- }
- $str = substr($str,0,strlen($str)-1);
- return $str;
- }
-
- // 获得签名方法
- public static function getSignature($str, $key) {
- $signature = "";
- if (function_exists('hash_hmac')) {
- $signature = bin2hex(hash_hmac("sha1", $str, $key, true));
- } else {
- $blocksize = 64;
- $hashfunc = 'sha1';
- if (strlen($key) > $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;
- }
- }
-
- }
|