forked from overtrue/easy-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlidayuGateway.php
88 lines (75 loc) · 2.57 KB
/
AlidayuGateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* This file is part of the overtrue/easy-sms.
* (c) overtrue <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Overtrue\EasySms\Gateways;
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;
/**
* Class AlidayuGateway.
*
* @see http://open.taobao.com/doc2/apiDetail?apiId=25450#s2
*/
class AlidayuGateway extends Gateway
{
use HasHttpRequest;
const ENDPOINT_URL = 'https://eco.taobao.com/router/rest';
const ENDPOINT_METHOD = 'alibaba.aliqin.fc.sms.num.send';
const ENDPOINT_VERSION = '2.0';
const ENDPOINT_FORMAT = 'json';
/**
* @param array|int|string $to
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
* @param \Overtrue\EasySms\Support\Config $config
*
* @return array
*
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException;
*/
public function send($to, MessageInterface $message, Config $config)
{
$params = [
'method' => self::ENDPOINT_METHOD,
'format' => self::ENDPOINT_FORMAT,
'v' => self::ENDPOINT_VERSION,
'sign_method' => 'md5',
'timestamp' => date('Y-m-d H:i:s'),
'sms_type' => 'normal',
'sms_free_sign_name' => $config->get('sign_name'),
'app_key' => $config->get('app_key'),
'sms_template_code' => $message->getTemplate(),
'rec_num' => strval($to),
'sms_param' => json_encode($message->getData()),
];
$params['sign'] = $this->generateSign($params);
$result = $this->post(self::ENDPOINT_URL, $params);
if (!empty($result['error_response'])) {
throw new GatewayErrorException($result['error_response']['sub_msg'], $result['error_response']['code'], $result);
}
return $result;
}
/**
* Generate Sign.
*
* @param array $params
*
* @return string
*/
protected function generateSign($params)
{
ksort($params);
$stringToBeSigned = $this->config->get('app_secret');
foreach ($params as $key => $value) {
if (is_string($value) && '@' != substr($value, 0, 1)) {
$stringToBeSigned .= "$key$value";
}
}
$stringToBeSigned .= $this->config->get('app_secret');
return strtoupper(md5($stringToBeSigned));
}
}