|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Overtrue\EasySms\Gateways; |
| 4 | + |
| 5 | +use Overtrue\EasySms\Contracts\MessageInterface; |
| 6 | +use Overtrue\EasySms\Contracts\PhoneNumberInterface; |
| 7 | +use Overtrue\EasySms\Exceptions\GatewayErrorException; |
| 8 | +use Overtrue\EasySms\Gateways\Gateway; |
| 9 | +use Overtrue\EasySms\Support\Config; |
| 10 | +use Overtrue\EasySms\Traits\HasHttpRequest; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class CtyunGateway |
| 14 | + * |
| 15 | + * @see https://www.ctyun.cn/document/10020426/10021544 |
| 16 | + */ |
| 17 | +class CtyunGateway extends Gateway |
| 18 | +{ |
| 19 | + use HasHttpRequest; |
| 20 | + |
| 21 | + public const SUCCESS_CODE = 'OK'; |
| 22 | + |
| 23 | + public const ENDPOINT_HOST = 'https://sms-global.ctapi.ctyun.cn'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Send a short message. |
| 27 | + * |
| 28 | + * @return array |
| 29 | + * |
| 30 | + * @throws GatewayErrorException |
| 31 | + */ |
| 32 | + public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
| 33 | + { |
| 34 | + $data = $message->getData($this); |
| 35 | + $endpoint = self::ENDPOINT_HOST . '/sms/api/v1'; |
| 36 | + return $this->execute($endpoint, [ |
| 37 | + 'phoneNumber' => (string)$to, |
| 38 | + 'templateCode' => $this->config->get('template_code'), |
| 39 | + 'templateParam' => '{"code":"' . $data['code'] . '"}', |
| 40 | + 'signName' => $this->config->get('sign_name'), |
| 41 | + 'action' => 'SendSms' |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * @return array |
| 48 | + * |
| 49 | + * @throws GatewayErrorException |
| 50 | + */ |
| 51 | + protected function execute(string $url, array $data) |
| 52 | + { |
| 53 | + $uuid = date('ymdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999)); |
| 54 | + $time = date('Ymd', time()) . 'T' . date('His') . 'Z'; |
| 55 | + $timeDate = substr($time, 0, 8); |
| 56 | + |
| 57 | + $body = bin2hex(hash("sha256", json_encode($data), true)); |
| 58 | + $query = ''; |
| 59 | + $strSignature = "ctyun-eop-request-id:" . $uuid . "\n" . "eop-date:" . $time . "\n" . "\n" . $query . "\n" . $body; |
| 60 | + |
| 61 | + $kTime = $this->sha256($time, $this->config->get('secret_key')); |
| 62 | + $kAk = $this->sha256($this->config->get('access_key'), $kTime); |
| 63 | + |
| 64 | + $kDate = $this->sha256($timeDate, $kAk); |
| 65 | + |
| 66 | + $signature = base64_encode($this->sha256(($strSignature), $kDate)); |
| 67 | + $headers['Content-Type'] = 'application/json'; |
| 68 | + $headers['ctyun-eop-request-id'] = $uuid; |
| 69 | + $headers['Eop-Authorization'] = $this->config->get('access_key') . ' Headers=ctyun-eop-request-id;' . 'eop-date Signature=' . $signature; |
| 70 | + $headers['eop-date'] = $time; |
| 71 | + |
| 72 | + $result = $this->postJson($url, $data, $headers); |
| 73 | + if ($result['code'] !== self::SUCCESS_CODE) { |
| 74 | + throw new GatewayErrorException($result['message'], $result['code'], $result); |
| 75 | + } |
| 76 | + return $result; |
| 77 | + } |
| 78 | + |
| 79 | + public function sha256($str, $pass): string |
| 80 | + { |
| 81 | + return (hash_hmac("sha256", ($str), ($pass), true)); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments