ModuyunGateway.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the overtrue/easy-sms.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\EasySms\Gateways;
  11. use Overtrue\EasySms\Contracts\MessageInterface;
  12. use Overtrue\EasySms\Contracts\PhoneNumberInterface;
  13. use Overtrue\EasySms\Exceptions\GatewayErrorException;
  14. use Overtrue\EasySms\Support\Config;
  15. use Overtrue\EasySms\Traits\HasHttpRequest;
  16. /**
  17. * Class ModuyunGateway.
  18. *
  19. * @see https://www.moduyun.com/doc/index.html#10002
  20. */
  21. class ModuyunGateway extends Gateway
  22. {
  23. use HasHttpRequest;
  24. const ENDPOINT_URL = 'https://live.moduyun.com/sms/v2/sendsinglesms';
  25. /**
  26. * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
  27. * @param \Overtrue\EasySms\Contracts\MessageInterface $message
  28. * @param \Overtrue\EasySms\Support\Config $config
  29. *
  30. * @return array
  31. *
  32. * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
  33. */
  34. public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
  35. {
  36. $urlParams = [
  37. 'accesskey' => $config->get('accesskey'),
  38. 'random' => rand(100000, 999999),
  39. ];
  40. $params = [
  41. 'tel' => [
  42. 'mobile' => $to->getNumber(),
  43. 'nationcode' => $to->getIDDCode() ?: '86',
  44. ],
  45. 'signId' => $config->get('signId', ''),
  46. 'templateId' => $message->getTemplate($this),
  47. 'time' => time(),
  48. 'type' => $config->get('type', 0),
  49. 'params' => array_values($message->getData($this)),
  50. 'ext' => '',
  51. 'extend' => '',
  52. ];
  53. $params['sig'] = $this->generateSign($params, $urlParams['random']);
  54. $result = $this->postJson($this->getEndpointUrl($urlParams), $params);
  55. $result = is_string($result) ? json_decode($result, true) : $result;
  56. if (0 != $result['result']) {
  57. throw new GatewayErrorException($result['errmsg'], $result['result'], $result);
  58. }
  59. return $result;
  60. }
  61. /**
  62. * @param array $params
  63. *
  64. * @return string
  65. */
  66. protected function getEndpointUrl($params)
  67. {
  68. return self::ENDPOINT_URL . '?' . http_build_query($params);
  69. }
  70. /**
  71. * Generate Sign.
  72. *
  73. * @param array $params
  74. * @param string $random
  75. *
  76. * @return string
  77. */
  78. protected function generateSign($params, $random)
  79. {
  80. return hash('sha256', sprintf(
  81. 'secretkey=%s&random=%d&time=%d&mobile=%s',
  82. $this->config->get('secretkey'),
  83. $random,
  84. $params['time'],
  85. $params['tel']['mobile']
  86. ));
  87. }
  88. }