RoutineNotify.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace service;
  3. /**
  4. * 小程序支付异步通知
  5. * Class RoutineNotify
  6. * @package service
  7. */
  8. class RoutineNotify
  9. {
  10. public static function options(){
  11. $payment = SystemConfigService::more(['routine_appId','routine_appsecret','pay_routine_mchid','pay_routine_key']);
  12. return $payment;
  13. }
  14. public static function notify()
  15. {
  16. $config = self::options();
  17. $postStr = file_get_contents('php://input');
  18. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  19. if ($postObj === false) die('parse xml error');
  20. if ($postObj->return_code != 'SUCCESS') die($postObj->return_msg);
  21. if ($postObj->result_code != 'SUCCESS') die($postObj->err_code);
  22. $arr = (array)$postObj;
  23. unset($arr['sign']);
  24. if (self::getSign($arr, $config['pay_routine_key']) == $postObj->sign) {
  25. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  26. return $arr;
  27. }
  28. }
  29. /**
  30. * 获取签名
  31. */
  32. public static function getSign($params, $key)
  33. {
  34. ksort($params, SORT_STRING);
  35. $unSignParaString = self::formatQueryParaMap($params, false);
  36. $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  37. return $signStr;
  38. }
  39. protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  40. {
  41. $buff = "";
  42. ksort($paraMap);
  43. foreach ($paraMap as $k => $v) {
  44. if (null != $v && "null" != $v) {
  45. if ($urlEncode) {
  46. $v = urlencode($v);
  47. }
  48. $buff .= $k . "=" . $v . "&";
  49. }
  50. }
  51. $reqPar = '';
  52. if (strlen($buff) > 0) {
  53. $reqPar = substr($buff, 0, strlen($buff) - 1);
  54. }
  55. return $reqPar;
  56. }
  57. }