alipay_notify.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. class AlipayNotify
  3. {
  4. /**
  5. * HTTPS形式消息验证地址
  6. */
  7. public $https_verify_url = 'https://mapi.alipay.com/gateway.do?service=notify_verify&';
  8. /**
  9. * HTTP形式消息验证地址
  10. */
  11. public $http_verify_url = 'http://notify.alipay.com/trade/notify_query.do?';
  12. public $alipay_config;
  13. public function __construct($alipay_config)
  14. {
  15. $this->alipay_config = $alipay_config;
  16. }
  17. public function AlipayNotify($alipay_config)
  18. {
  19. $this->__construct($alipay_config);
  20. }
  21. public function verifyNotify()
  22. {
  23. if (empty($_POST)) {
  24. return false;
  25. }
  26. else {
  27. $isSign = $this->getSignVeryfy($_POST, $_POST['sign']);
  28. $responseTxt = 'false';
  29. if (!empty($_POST['notify_id'])) {
  30. $responseTxt = $this->getResponse($_POST['notify_id']);
  31. }
  32. if (preg_match('/true$/i', $responseTxt) && $isSign) {
  33. return true;
  34. }
  35. else {
  36. return false;
  37. }
  38. }
  39. }
  40. public function verifyReturn()
  41. {
  42. if (empty($_GET)) {
  43. return false;
  44. }
  45. else {
  46. $isSign = $this->getSignVeryfy($_GET, $_GET['sign']);
  47. $responseTxt = 'false';
  48. if (!empty($_GET['notify_id'])) {
  49. $responseTxt = $this->getResponse($_GET['notify_id']);
  50. }
  51. if (preg_match('/true$/i', $responseTxt) && $isSign) {
  52. return true;
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58. }
  59. public function getSignVeryfy($para_temp, $sign)
  60. {
  61. $para_filter = paraFilter($para_temp);
  62. $para_sort = argSort($para_filter);
  63. $prestr = createLinkstring($para_sort);
  64. $isSgin = false;
  65. switch (strtoupper(trim($this->alipay_config['sign_type']))) {
  66. case 'MD5':
  67. $isSgin = md5Verify($prestr, $sign, $this->alipay_config['key']);
  68. break;
  69. default:
  70. $isSgin = false;
  71. }
  72. return $isSgin;
  73. }
  74. public function getResponse($notify_id)
  75. {
  76. $transport = strtolower(trim($this->alipay_config['transport']));
  77. $partner = trim($this->alipay_config['partner']);
  78. $veryfy_url = '';
  79. if ($transport == 'https') {
  80. $veryfy_url = $this->https_verify_url;
  81. }
  82. else {
  83. $veryfy_url = $this->http_verify_url;
  84. }
  85. $veryfy_url = $veryfy_url . 'partner=' . $partner . '&notify_id=' . $notify_id;
  86. $responseTxt = getHttpResponseGET($veryfy_url, $this->alipay_config['cacert']);
  87. return $responseTxt;
  88. }
  89. }
  90. require_once 'alipay_core.function.php';
  91. require_once 'alipay_md5.function.php';
  92. ?>