YuntongxunGateway.php 3.1 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 YuntongxunGateway.
  18. *
  19. * @see http://www.yuntongxun.com/doc/rest/sms/3_2_2_2.html
  20. */
  21. class YuntongxunGateway extends Gateway
  22. {
  23. use HasHttpRequest;
  24. const ENDPOINT_TEMPLATE = 'https://%s:%s/%s/%s/%s/%s/%s?sig=%s';
  25. const SERVER_IP = 'app.cloopen.com';
  26. const DEBUG_SERVER_IP = 'sandboxapp.cloopen.com';
  27. const DEBUG_TEMPLATE_ID = 1;
  28. const SERVER_PORT = '8883';
  29. const SDK_VERSION = '2013-12-26';
  30. const SUCCESS_CODE = '000000';
  31. /**
  32. * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
  33. * @param \Overtrue\EasySms\Contracts\MessageInterface $message
  34. * @param \Overtrue\EasySms\Support\Config $config
  35. *
  36. * @return array
  37. *
  38. * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
  39. */
  40. public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
  41. {
  42. $datetime = date('YmdHis');
  43. $endpoint = $this->buildEndpoint('SMS', 'TemplateSMS', $datetime, $config);
  44. $result = $this->request('post', $endpoint, [
  45. 'json' => [
  46. 'to' => $to,
  47. 'templateId' => (int) ($this->config->get('debug') ? self::DEBUG_TEMPLATE_ID : $message->getTemplate($this)),
  48. 'appId' => $config->get('app_id'),
  49. 'datas' => $message->getData($this),
  50. ],
  51. 'headers' => [
  52. 'Accept' => 'application/json',
  53. 'Content-Type' => 'application/json;charset=utf-8',
  54. 'Authorization' => base64_encode($config->get('account_sid').':'.$datetime),
  55. ],
  56. ]);
  57. if (self::SUCCESS_CODE != $result['statusCode']) {
  58. throw new GatewayErrorException($result['statusCode'], $result['statusCode'], $result);
  59. }
  60. return $result;
  61. }
  62. /**
  63. * Build endpoint url.
  64. *
  65. * @param string $type
  66. * @param string $resource
  67. * @param string $datetime
  68. * @param \Overtrue\EasySms\Support\Config $config
  69. *
  70. * @return string
  71. */
  72. protected function buildEndpoint($type, $resource, $datetime, Config $config)
  73. {
  74. $serverIp = $this->config->get('debug') ? self::DEBUG_SERVER_IP : self::SERVER_IP;
  75. $accountType = $this->config->get('is_sub_account') ? 'SubAccounts' : 'Accounts';
  76. $sig = strtoupper(md5($config->get('account_sid').$config->get('account_token').$datetime));
  77. return sprintf(self::ENDPOINT_TEMPLATE, $serverIp, self::SERVER_PORT, self::SDK_VERSION, $accountType, $config->get('account_sid'), $type, $resource, $sig);
  78. }
  79. }