NotifyService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/1/7
  8. * Time: 15:33
  9. */
  10. namespace app\outside\services;
  11. use app\outside\modes\OutsideAppSetting;
  12. class NotifyService
  13. {
  14. private $app_id; //应用AppID
  15. private $secret; //密钥字符串
  16. private $sign_type = "MD5"; //SHA256 MD5
  17. private $version = "1.0";
  18. public $responseData = []; //data参数
  19. public $verifySign = 0;
  20. public function __construct($data, $outsideApp = null)
  21. {
  22. $this->responseData = $data;
  23. $this->setConfig($outsideApp);
  24. }
  25. /**
  26. * 配置信息
  27. */
  28. public function setConfig($outsideApp)
  29. {
  30. if (is_null($outsideApp)) {
  31. $outsideApp = OutsideAppSetting::current();
  32. }
  33. $this->app_id = $outsideApp->app_id;
  34. $this->secret = $outsideApp->app_secret;
  35. }
  36. public function verifySign()
  37. {
  38. if (strtoupper($this->getResponseData('sign_type')) == 'SHA256') {
  39. return $this->verifySha256();
  40. }
  41. return $this->verifyMd5();
  42. }
  43. protected function verifySha256()
  44. {
  45. $hashSign = hash_hmac('sha256', $this->toQueryString($this->responseData), $this->secret);
  46. return $hashSign == $this->responseData['sign'];
  47. }
  48. protected function verifyMd5()
  49. {
  50. return strtoupper(md5($this->toQueryString($this->responseData).'&secret='.$this->secret)) == $this->responseData['sign'];
  51. }
  52. public function setResponseData($key, $value)
  53. {
  54. $this->responseData[$key] = $value;
  55. }
  56. public function getResponseData($key)
  57. {
  58. return array_get($this->responseData, $key, null);
  59. }
  60. /**
  61. * 将参数转换成k=v拼接的形式
  62. * @param $parameter
  63. * @return string
  64. */
  65. public function toQueryString($parameter)
  66. {
  67. //按key的字典序升序排序,并保留key值
  68. ksort($parameter);
  69. $strQuery="";
  70. foreach ($parameter as $k=>$v){
  71. //不参与签名、验签
  72. if($k == "sign"){
  73. continue;
  74. }
  75. if($v === null) {$v = '';}
  76. $strQuery .= strlen($strQuery) == 0 ? "" : "&";
  77. $strQuery.=$k."=".$v;
  78. }
  79. return $strQuery;
  80. }
  81. }