|
5 | 5 | namespace NotificationChannels\Cmsms; |
6 | 6 |
|
7 | 7 | use GuzzleHttp\Client as GuzzleClient; |
| 8 | +use GuzzleHttp\Exception\GuzzleException; |
8 | 9 | use Illuminate\Support\Arr; |
| 10 | +use NotificationChannels\Cmsms\Events\SMSSendingFailedEvent; |
| 11 | +use NotificationChannels\Cmsms\Events\SMSSentSuccessfullyEvent; |
9 | 12 | use NotificationChannels\Cmsms\Exceptions\CouldNotSendNotification; |
10 | | -use SimpleXMLElement; |
| 13 | +use NotificationChannels\Cmsms\Exceptions\InvalidMessage; |
11 | 14 |
|
12 | 15 | class CmsmsClient |
13 | 16 | { |
14 | | - public const GATEWAY_URL = 'https://gw.messaging.cm.com/gateway.ashx'; |
| 17 | + public const GATEWAY_URL = 'https://gw.cmtelecom.com/v1.0/message'; |
15 | 18 |
|
16 | 19 | public function __construct( |
17 | 20 | protected GuzzleClient $client, |
18 | 21 | protected string $productToken, |
19 | 22 | ) { |
20 | 23 | } |
21 | 24 |
|
| 25 | + /** |
| 26 | + * @throws InvalidMessage |
| 27 | + * @throws GuzzleException |
| 28 | + * @throws CouldNotSendNotification |
| 29 | + */ |
22 | 30 | public function send(CmsmsMessage $message, string $recipient): void |
23 | 31 | { |
24 | | - if (is_null(Arr::get($message->toXmlArray(), 'FROM'))) { |
| 32 | + if (empty($message->getOriginator())) { |
25 | 33 | $message->originator(config('services.cmsms.originator')); |
26 | 34 | } |
27 | 35 |
|
| 36 | + $payload = $this->buildMessageJson($message, $recipient); |
| 37 | + |
28 | 38 | $response = $this->client->request('POST', static::GATEWAY_URL, [ |
29 | | - 'body' => $this->buildMessageXml($message, $recipient), |
| 39 | + 'body' => $payload, |
30 | 40 | 'headers' => [ |
31 | | - 'Content-Type' => 'application/xml', |
| 41 | + 'accept' => 'application/json', |
| 42 | + 'content-type' => 'application/json', |
32 | 43 | ], |
33 | 44 | ]); |
34 | 45 |
|
35 | | - // API returns an empty string on success |
36 | | - // On failure, only the error string is passed |
| 46 | + /** |
| 47 | + * If error code is 0, the message was sent successfully. |
| 48 | + */ |
37 | 49 | $body = $response->getBody()->getContents(); |
38 | | - if (! empty($body)) { |
| 50 | + $errorCode = Arr::get(json_decode($body, true), 'errorCode'); |
| 51 | + if ((int) $errorCode !== 0) { |
| 52 | + SMSSendingFailedEvent::dispatch($body); |
| 53 | + |
39 | 54 | throw CouldNotSendNotification::serviceRespondedWithAnError($body); |
40 | 55 | } |
| 56 | + |
| 57 | + SMSSentSuccessfullyEvent::dispatch($payload); |
41 | 58 | } |
42 | 59 |
|
43 | | - public function buildMessageXml(CmsmsMessage $message, string $recipient): string |
| 60 | + /** |
| 61 | + * See: https://developers.cm.com/messaging/reference/messages_sendmessage-1 |
| 62 | + */ |
| 63 | + public function buildMessageJson(CmsmsMessage $message, string $recipient): string |
44 | 64 | { |
45 | | - $xml = new SimpleXMLElement('<MESSAGES/>'); |
46 | | - |
47 | | - $xml->addChild('AUTHENTICATION') |
48 | | - ->addChild('PRODUCTTOKEN', $this->productToken); |
| 65 | + $body = []; |
| 66 | + $body['content'] = $message->getBody(); |
| 67 | + if (strtoupper($message->getEncodingDetectionType()) === 'AUTO') { |
| 68 | + $body['type'] = 'AUTO'; |
| 69 | + } |
49 | 70 |
|
50 | | - if ($tariff = $message->getTariff()) { |
51 | | - $xml->addChild('TARIFF', (string) $tariff); |
| 71 | + $minimumNumberOfMessageParts = []; |
| 72 | + if ($message->getMinimumNumberOfMessageParts() !== null) { |
| 73 | + $minimumNumberOfMessageParts['minimumNumberOfMessageParts'] = $message->getMinimumNumberOfMessageParts(); |
| 74 | + } |
| 75 | + $maximumNumberOfMessageParts = []; |
| 76 | + if ($message->getMaximumNumberOfMessageParts() !== null) { |
| 77 | + $maximumNumberOfMessageParts['maximumNumberOfMessageParts'] = $message->getMaximumNumberOfMessageParts(); |
52 | 78 | } |
53 | 79 |
|
54 | | - $msg = $xml->addChild('MSG'); |
55 | | - foreach ($message->toXmlArray() as $name => $value) { |
56 | | - $msg->addChild($name, (string) $value); |
| 80 | + $reference = []; |
| 81 | + if ($message->getReference() !== null) { |
| 82 | + $reference['reference'] = $message->getReference(); |
57 | 83 | } |
58 | | - $msg->addChild('TO', $recipient); |
59 | 84 |
|
60 | | - return $xml->asXML(); |
| 85 | + $json = [ |
| 86 | + 'messages' => [ |
| 87 | + 'authentication' => [ |
| 88 | + 'productToken' => $this->productToken, |
| 89 | + ], |
| 90 | + 'msg' => [[ |
| 91 | + 'body' => $body, |
| 92 | + 'to' => [[ |
| 93 | + 'number' => $recipient, |
| 94 | + ]], |
| 95 | + 'dcs' => strtoupper($message->getEncodingDetectionType()) === 'AUTO' ? '0' : $message->getEncodingDetectionType(), |
| 96 | + 'from' => $message->getOriginator(), |
| 97 | + ...$minimumNumberOfMessageParts, |
| 98 | + ...$maximumNumberOfMessageParts, |
| 99 | + ...$reference, |
| 100 | + ]], |
| 101 | + ], |
| 102 | + ]; |
| 103 | + |
| 104 | + return json_encode($json); |
61 | 105 | } |
62 | 106 | } |
0 commit comments