From d189b8eb189c59aaa8338b142fe81664e8c747f7 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 3 May 2020 22:50:56 +0800 Subject: [PATCH] Fix SMS delivery receipt functionality #394 --- config/packages/security.yaml | 4 ++ config/routes/sms.yaml | 6 +++ config/services.yaml | 2 +- src/Controller/SMSController.php | 71 +++++++++++++++++++++++++++++++ src/Entity/SMSMessage.php | 23 ++++++++++ src/Service/RisingTideGateway.php | 4 ++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 config/routes/sms.yaml create mode 100644 src/Controller/SMSController.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index dc9aa78d..4339eba4 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -30,6 +30,10 @@ security: pattern: ^\/api\/ security: false + sms: + pattern: ^/sms\/ + security: false + rider_api: pattern: ^\/rapi\/ security: false diff --git a/config/routes/sms.yaml b/config/routes/sms.yaml new file mode 100644 index 00000000..0c6fb363 --- /dev/null +++ b/config/routes/sms.yaml @@ -0,0 +1,6 @@ +# sms handling - rising tide + +sms_delivery_receipt: + path: /sms/delivery_receipt + controller: App\Controller\SMSController::deliveryReceipt + methods: [POST] diff --git a/config/services.yaml b/config/services.yaml index d235bff2..697e0428 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -74,7 +74,7 @@ services: $pass: "%env(RT_PASS)%" $usage_type: "%env(RT_USAGE_TYPE)%" $shortcode: "%env(RT_SHORTCODE)%" - $dr_url: "" + $dr_url: "https://resqapi.jankstudio.com/sms/delivery_receipt" App\Service\MQTTClient: arguments: diff --git a/src/Controller/SMSController.php b/src/Controller/SMSController.php new file mode 100644 index 00000000..b001e668 --- /dev/null +++ b/src/Controller/SMSController.php @@ -0,0 +1,71 @@ +getContent(); + + error_log($content); + + $data = json_decode($content, true); + error_log(print_r($data, true)); + + // data has id? + if (!isset($data['id'])) + { + error_log('SMSDR: no id sent'); + $resp = new Response(); + $resp->setStatusCode(400); + return $resp; + } + + // check structure + if (!isset($data['delivered']) || + !isset($data['reason_code']) || + !isset($data['reason_description']) + ) + { + error_log('SMSDR: bad data structure'); + $resp = new Response(); + $resp->setStatusCode(400); + return $resp; + } + + // find sms + $sms = $em->getRepository(SMSMessage::class)->find($data['id']); + if ($sms == null) + { + error_log('SMSDR: no sms found'); + $resp = new Response(); + $resp->setStatusCode(202); + return $resp; + } + + $sms->setReasonCode($data['reason_code']); + if ($data['delivered']) + $sms->setStatus('delivered'); + else + $sms->setStatus('not_delivered'); + $em->flush(); + + $resp = new Response(); + $resp->setStatusCode(202); + return $resp; + } +} diff --git a/src/Entity/SMSMessage.php b/src/Entity/SMSMessage.php index cb6b2e0a..4869ee29 100644 --- a/src/Entity/SMSMessage.php +++ b/src/Entity/SMSMessage.php @@ -48,6 +48,11 @@ class SMSMessage */ protected $status; + /** + * @ORM\Column(type="integer", length=5) + */ + protected $reason_code; + public function __construct() { @@ -57,6 +62,13 @@ class SMSMessage $this->to = ''; $this->message = ''; $this->status = 'created'; + $this->reason_code = 0; + + // status are: + // created + // sent + // delivered + // not_delivered } public function getID() @@ -123,4 +135,15 @@ class SMSMessage { return $this->status; } + + public function setReasonCode($code) + { + $this->reason_code = $code; + return $this; + } + + public function getReasonCode() + { + return $this->reason_code; + } } diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index c33c0ae9..7eabb251 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -51,6 +51,7 @@ class RisingTideGateway $date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s'); $data = [ + 'id' => $sms->getID(), 'from' => $this->shortcode, 'from_alias' => $mask, 'to' => $mobile_num, @@ -58,7 +59,10 @@ class RisingTideGateway 'body' => $message, 'date' => $date_string, 'usagetype' => $this->usage_type, + 'delivery_receipt_url' => $dr_url, ]; + error_log(print_r($data, true)); + $data_json = json_encode($data); $userpwd = $this->user . ':' . $this->pass;