diff --git a/README.md b/README.md index a44a10e..9755f93 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Add to your `config/services.php`: 'secret' => env('AWSSMS_SECRET'), 'region' => env('AWSSMS_REGION'), 'from' => env('AWSSMS_FROM'), // optional + 'max_price_usd' => '0.50' // Max price, sms wont send if price of the sms is more then this. ], ... ``` diff --git a/src/AWSSMS.php b/src/AWSSMS.php index d48135b..db3af9b 100644 --- a/src/AWSSMS.php +++ b/src/AWSSMS.php @@ -21,10 +21,19 @@ class AWSSMS */ protected $from; - public function __construct(\Aws\Sns\SnsClient $snsClient, $from) + /** + * The maximum amount in USD that you are willing to spend to send the SMS message. Amazon SNS will not send the message if it determines that doing so would incur a cost that exceeds the maximum price. + * This attribute has no effect if your month-to-date SMS costs have already exceeded the limit set for the MonthlySpendLimit attribute, which you set by using the SetSMSAttributes request. + * If you are sending the message to an Amazon SNS topic, the maximum price applies to each message delivery to each phone number that is subscribed to the topic. + * @var + */ + protected $maxPrice; + + public function __construct(\Aws\Sns\SnsClient $snsClient, $from, $maxPrice) { $this->snsClient = $snsClient; $this->from = $from; + $this->maxPrice = $maxPrice; } /** * Send a TwilioMessage to the a phone number. @@ -43,12 +52,27 @@ public function sendMessage(AWSSMSMessage $message, $to) } protected function sendSmsMessage(AWSSMSMessage $message, $to) { - $args = array( - "SenderID" => $this->getFrom($message), - "SMSType" => $message->type, - "Message" => $message->content, - "PhoneNumber" => $to - ); + + $args = [ + 'Message' => $message->content, + 'PhoneNumber' => $to, + 'MessageAttributes' => [ + 'AWS.SNS.SMS.SenderID' => [ + 'DataType' => 'String', + 'StringValue' => $this->getFrom($message) + ], + + 'AWS.SNS.SMS.SMSType' => [ + 'DataType' => 'String', + 'StringValue' => $message->type + ], + 'AWS.SNS.SMS.MaxPrice' => [ + 'DataType' => 'String', + 'StringValue' => $this->maxPrice + ] + ] + ]; + return $this->snsClient->publish($args); diff --git a/src/AWSSMSServiceProvider.php b/src/AWSSMSServiceProvider.php index 875b642..aa7d079 100644 --- a/src/AWSSMSServiceProvider.php +++ b/src/AWSSMSServiceProvider.php @@ -33,7 +33,8 @@ public function boot() return new AWSSMS( $client, - $config['from'] + $config['from'], + isset($config['max_price_usd']) ? $config['max_price_usd'] : '0.50' ); });