PayUtils.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace common\pay\shanfutong;
  3. class PayUtils
  4. {
  5. /**
  6. * @param array $data
  7. * @return string
  8. */
  9. public static function makeSign($data, $key)
  10. {
  11. $merchantid = $data['uid'];
  12. $price = sprintf("%.2f",$data['price']);
  13. $paytype = $data['paytype'];
  14. $notify_url = $data['notify_url'];
  15. $return_url = $data['return_url'];
  16. $user_order_no = $data['user_order_no'];
  17. $sign = md5("{$merchantid}"."{$price}"."{$paytype}"."{$notify_url}"."{$return_url}"."{$user_order_no}"."{$key}");
  18. return strtolower($sign);
  19. }
  20. /**
  21. * @param array $data
  22. * @param $key
  23. * @return bool|int
  24. */
  25. public static function verify($data, $key)
  26. {
  27. // 签名
  28. if (!isset($data['sign']) || trim($data['sign']) === '') {
  29. return false;
  30. }
  31. $user_order_no = $data['user_order_no']; //订单号码(商户传递过去的)
  32. $orderno = $data['orderno']; //平台生成的订单
  33. $tradeno = $data['tradeno']; //支付的流水号
  34. $price = sprintf("%.2f",$data['price']); //订单金额 (这里保留两位小数)
  35. $realprice = sprintf("%.2f",$data['realprice']); //真实的金额 (这里保留两位小数)
  36. $tokenkey = $key; //token
  37. $sign = $data['sign'];
  38. $sing_new = md5("{$user_order_no}"."{$orderno}"."{$tradeno}"."{$price}"."{$realprice}"."{$tokenkey}"); //自己加密的密钥
  39. $sing_new = strtolower($sing_new); //转化为小写
  40. // 比较两次的签名是否一致
  41. if($sign == $sing_new){
  42. return true;
  43. }else{
  44. return false;
  45. }
  46. }
  47. }