PayUtils.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace common\pay\globalpay;
  3. class PayUtils
  4. {
  5. /**
  6. * @param array $data
  7. * @return string
  8. */
  9. public static function makeSign($data, $privateKey)
  10. {
  11. $temp_str = "";
  12. $temp_str = $data["mchNo"].$data["customerNo"].$data["orderNo"].$data["amount"].$data["currency"].$data["returnUrl"].$data["notifyUrl"].$data["payType"].$privateKey;
  13. $sign = MD5($temp_str);
  14. return $sign;
  15. }
  16. /**
  17. * @param array $data
  18. * @param $pubCert
  19. * @return bool|int
  20. */
  21. public static function verify($data, $privateKey)
  22. {
  23. // 签名
  24. if (!isset($data['sign']) || trim($data['sign']) === '') {
  25. return false;
  26. }
  27. $sign = $data['sign'];
  28. $temp_str = $data["mchNo"].$data["customerNo"].$data["orderNo"].$data["amount"].$data["currency"].$data["orderId"].$data["transactionId"].$privateKey;
  29. $new_sign=MD5($temp_str);
  30. if($new_sign == $sign ){
  31. return true;
  32. }else{
  33. return false;
  34. }
  35. }
  36. // 请求函数
  37. public static function send($data, $payUrl){
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL,$payUrl);
  40. curl_setopt($ch, CURLOPT_POST, 1);
  41. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44. $server_output = curl_exec($ch);
  45. $resp = json_decode($server_output, true);
  46. return $resp;
  47. }
  48. }