|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sarahman\SmsService\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
use Sarahman\HttpRequestApiLog\Traits\WritesHttpLogs; |
|
10
|
|
|
use Sarahman\SmsService\Interfaces\NeedsAuthenticationInterface; |
|
11
|
|
|
use Sarahman\SmsService\Response; |
|
12
|
|
|
use Sarahman\SmsService\Traits\Guzzles; |
|
13
|
|
|
|
|
14
|
|
|
class ValueFirst extends BaseProvider implements NeedsAuthenticationInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use Guzzles; |
|
17
|
|
|
use WritesHttpLogs; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(array $config = [], $url = null) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($config, $url); |
|
22
|
|
|
$this->enableLogging = Config::get('sms-service-with-bd-providers::config.enable_api_call_logging', false); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getUrl() |
|
26
|
|
|
{ |
|
27
|
|
|
return parent::getUrl().'/sendsms'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getUsername() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->config['username']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function mapParams($recipient, $message, array $params = []) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!preg_match($this->recipientPattern, $recipient, $matches)) { |
|
38
|
|
|
return []; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$recipient = '880'.$matches[3]; |
|
42
|
|
|
|
|
43
|
|
|
if (!array_key_exists('coding', $this->config) || $this->config['coding'] != 3) { |
|
44
|
|
|
$message = preg_replace('/[^a-zA-Z0-9\.@!?&\-,%\(\):\"]/', ' ', $message); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return [ |
|
48
|
|
|
'to' => $recipient, |
|
49
|
|
|
'from' => $this->config['from'], |
|
50
|
|
|
'text' => $message, |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getValidationRules() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
'username' => 'required', |
|
58
|
|
|
'password' => 'required', |
|
59
|
|
|
'to' => 'required|regex:/^8801[3456789]\d{8}$/', |
|
60
|
|
|
'text' => 'required', |
|
61
|
|
|
'coding' => 'integer', |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function parseResponse($response) |
|
66
|
|
|
{ |
|
67
|
|
|
preg_match('/^Sent\.*/', $response, $matches); |
|
68
|
|
|
|
|
69
|
|
|
return new Response(is_array($matches) && array_key_exists(0, $matches), $response); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getAccessToken($generate = false) |
|
73
|
|
|
{ |
|
74
|
|
|
$cacheKey = sprintf('%s_AccessToken:%s', __CLASS__, $this->getUsername()); |
|
75
|
|
|
|
|
76
|
|
|
if (!$generate && Cache::has($cacheKey)) { |
|
77
|
|
|
return Cache::get($cacheKey); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$api = $this->url.'/api/sendsms/token?action=generate'; |
|
81
|
|
|
$request = [ |
|
82
|
|
|
'headers' => [ |
|
83
|
|
|
'Authorization' => ['Basic '.base64_encode($this->config['username'].':'.$this->config['password'])], |
|
84
|
|
|
], |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
try { |
|
88
|
|
|
$client = $this->buildClient(); |
|
89
|
|
|
$response = $this->makeRequestWithHandlingException($client, $method = 'post', $api, $request); |
|
90
|
|
|
|
|
91
|
|
|
$this->log($method, $api, $request, $response); |
|
92
|
|
|
|
|
93
|
|
|
if (200 === $response->getStatusCode()) { |
|
94
|
|
|
$responseData = $this->parseJson($response); |
|
95
|
|
|
$api = $this->url.'/api/sendsms/token?action=enable&token=all'; |
|
96
|
|
|
$request['form_params'] = ['token' => $responseData['token']]; |
|
97
|
|
|
$response = $this->makeRequestWithHandlingException($client, $method = 'post', $api, $request); |
|
98
|
|
|
|
|
99
|
|
|
$this->log($method, $api, $request, $response); |
|
100
|
|
|
|
|
101
|
|
|
if (200 === $response->getStatusCode()) { |
|
102
|
|
|
Cache::put($cacheKey, $responseData['token'], $responseData['expiryDate']); |
|
103
|
|
|
|
|
104
|
|
|
return $responseData['token']; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} catch (Exception $exception) { |
|
108
|
|
|
Log::error($exception); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|