PayUtils.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace common\pay\huanqiu;
  3. class PayUtils
  4. {
  5. /**
  6. * @param array $data
  7. * @return string
  8. */
  9. //对字符串进行排序
  10. public static function mkSign($data){
  11. if(!is_array($data)){
  12. return false;
  13. }
  14. $str = '';
  15. $flag = false;
  16. foreach ($data as $v){
  17. if(empty($v)){
  18. continue;
  19. }
  20. $str.=$v.'&';
  21. }
  22. $str = substr($str,0,strlen($str)-1);
  23. return $str;
  24. }
  25. // 获得签名方法
  26. public static function getSignature($str, $key) {
  27. $signature = "";
  28. if (function_exists('hash_hmac')) {
  29. $signature = bin2hex(hash_hmac("sha1", $str, $key, true));
  30. } else {
  31. $blocksize = 64;
  32. $hashfunc = 'sha1';
  33. if (strlen($key) > $blocksize) {
  34. $key = pack('H*', $hashfunc($key));
  35. }
  36. $key = str_pad($key, $blocksize, chr(0x00));
  37. $ipad = str_repeat(chr(0x36), $blocksize);
  38. $opad = str_repeat(chr(0x5c), $blocksize);
  39. $hmac = pack(
  40. 'H*', $hashfunc(
  41. ($key ^ $opad) . pack(
  42. 'H*', $hashfunc(
  43. ($key ^ $ipad) . $str
  44. )
  45. )
  46. )
  47. );
  48. $signature = bin2hex($hmac);
  49. }
  50. return $signature;
  51. }
  52. /**
  53. * 发送post请求
  54. * @param string $url 请求地址
  55. * @param array $data post键值对数据
  56. * @return string
  57. */
  58. public static function sendPost($url,$data,$app_id,$privateKey) {
  59. $url .= '?p1='.$data['p1'].'&p2='.$data['p2'].'&p3='.$data['p3'].'&timestamp='.$data['timestamp'];
  60. file_put_contents('url.txt',$url);
  61. $str = PayUtils::mkSign($data);
  62. file_put_contents('str.txt',$str);
  63. if(empty($str)){
  64. return false;
  65. }
  66. $sign = strtoupper(PayUtils::getSignature($str,$privateKey)); //加密后(大写):5F51F8065B325EC3491526612CB2A47B84E5E10B
  67. file_put_contents('sign.txt',$sign);
  68. $headers = array(
  69. 'content-type:application/json',
  70. 'access_key:'.$sign,
  71. 'app_id:'.$app_id
  72. );
  73. $curl = curl_init();
  74. curl_setopt($curl, CURLOPT_URL,$url);
  75. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
  76. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  77. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  78. //执行命令
  79. $data = curl_exec($curl);
  80. $dat = print_r($data,true);
  81. file_put_contents("dat.txt",$dat);
  82. //关闭URL请求
  83. curl_close($curl);
  84. return $data;
  85. }
  86. /**
  87. * @param array $data 参数
  88. * @param string $secretKey
  89. * @return bool
  90. */
  91. // 回调验证函数
  92. public static function checkSign($data, $secretKey)
  93. {
  94. if(floor($data['amount'])== $data['amount']){
  95. $amount = sprintf("%.1f",$data['amount']); //入金金额(美元)
  96. } else {
  97. $amount = $data['amount'];
  98. }
  99. if(floor($data['exchangeRate'])== $data['exchangeRate']){
  100. $exchangeRate = sprintf("%.1f",$data['exchangeRate']); //入金金额(美元)
  101. } else {
  102. $exchangeRate = $data['exchangeRate']; //转换汇率
  103. }
  104. $poundage = sprintf("%.6f",$data['poundage']); //手续费(美元)
  105. $merchantNo = $data['merchantNo']; //商户的商户号
  106. $merchantOrderNo = $data['merchantOrderNo']; //商户订单号
  107. $orderNo = $data['orderNo']; //FastPay 交易流水号
  108. $timestamp = $data['timestamp']; //时间戳
  109. $sign = $data['sign']; //签名
  110. $str = "{$amount}".'&'."{$exchangeRate}".'&'."{$poundage}".'&'."{$merchantNo}".'&'."{$merchantOrderNo}".'&'."{$orderNo}".'&'."{$timestamp}"; //加密字符串
  111. file_put_contents('huanqiustr.txt',$str);
  112. $sign_new = PayUtils::getSignature($str,$secretKey); //根据加密规则生成的新签名
  113. $sign_new = strtoupper($sign_new); //加密后(大写)
  114. // 比较两者之间的规则
  115. if($sign_new == $sign){
  116. return true;
  117. }else{
  118. return false;
  119. }
  120. }
  121. }