22
33namespace Tartan \IranianSms \Adapter ;
44
5+ use Kavenegar \KavenegarApi ;
6+ use Kavenegar \Exceptions \ApiException ;
7+ use Kavenegar \Exceptions \HttpException ;
8+ use phpDocumentor \Reflection \Types \String_ ;
9+
510class KaveNegar extends AdapterAbstract implements AdapterInterface {
611
7- public $ gateway_url ;
812 private $ credential = [
913 'api_key ' => '' ,
1014 ];
1115 public $ sender ;
12-
13- public $ debug = false ;
16+ public $ api ;
1417
1518 public static $ status_real = [
1619 1 => 'SENDING ' ,
@@ -26,264 +29,45 @@ class KaveNegar extends AdapterAbstract implements AdapterInterface {
2629 ];
2730
2831 public function __construct () {
29- $ this ->gateway_url = config ('iranian_sms.kavenegar.gateway ' );
3032 $ this ->credential ['api_key ' ] = config ('iranian_sms.kavenegar.api_key ' );
3133 $ this ->sender = config ('iranian_sms.kavenegar.sender ' );
34+ $ this ->api = new KavenegarApi ($ this ->credential ['api_key ' ]);
3235 }
3336
3437 public function send (String $ number , String $ message ) {
35-
3638 $ number = $ this ->filterNumber ($ number );
3739
38- $ receptor = [$ number ];
39- $ date = null ;
40- $ type = 1 ;
41- $ localid = null ;
42- $ result = $ this ->_send ($ receptor , $ this ->sender , $ message , $ date , $ type , $ localid );
43- dd ($ result );
44- if ($ result ) {
45- $ res = array_shift ($ result );
46- if ($ res ->messageid > 0 )
47- return $ res ->messageid ;
48- }
49- throw new \Exception ("SMS cannot be send! " );
50- }
51-
52- private function execute ($ url , $ data = null ) {
53-
54- $ headers = array (
55- 'Accept: application/json ' ,
56- 'Content-Type: application/x-www-form-urlencoded ' ,
57- 'charset: utf-8 ' ,
58- );
59- $ fields_string = "" ;
60- if (!is_null ($ data )) {
61- $ fields_string = http_build_query ($ data );
62- }
63- if ($ this ->debug ) {
64- echo "[Request_param] : " . $ fields_string . "\r\n" ;
65- }
66-
67- $ handle = curl_init ();
68- curl_setopt ($ handle , CURLOPT_URL , $ url );
69- curl_setopt ($ handle , CURLOPT_HTTPHEADER , $ headers );
70- curl_setopt ($ handle , CURLOPT_RETURNTRANSFER , true );
71- curl_setopt ($ handle , CURLOPT_SSL_VERIFYHOST , false );
72- curl_setopt ($ handle , CURLOPT_SSL_VERIFYPEER , false );
73- curl_setopt ($ handle , CURLOPT_POST , true );
74- curl_setopt ($ handle , CURLOPT_POSTFIELDS , $ fields_string );
75-
76- $ response = curl_exec ($ handle );
77- $ code = curl_getinfo ($ handle , CURLINFO_HTTP_CODE );
78- $ content_type = curl_getinfo ($ handle , CURLINFO_CONTENT_TYPE );
79- $ curl_errno = curl_errno ($ handle );
80- $ curl_error = curl_error ($ handle );
81- if ($ curl_errno ) {
82- throw new HttpException ($ curl_error , $ curl_errno );
83- }
84- $ json_response = json_decode ($ response );
85- if ($ code != 200 && is_null ($ json_response )) {
86- throw new HttpException ("Request have errors " , $ code );
87- } else {
88- $ json_return = $ json_response ->return ;
89- if ($ json_return ->status != 200 ) {
90- throw new ApiException ($ json_return ->message , $ json_return ->status );
91- }
92- if ($ this ->debug == true ) {
93- echo "[Responsive_Message] : " . $ json_return ->message . "\r\n" ;
94- echo "[Responsive_Status] : " . $ json_return ->status . "\r\n" ;
95- if (is_null ($ json_response ->entries )) {
96- echo "[Responsive_Entries] : Null \n=========================== \r\n" ;
97- } else {
98- echo "========================= \r\n\r\n" ;
99- }
100- }
101- return $ json_response ->entries ;
102- }
103-
104- }
105- private function _send ($ receptor , $ sender , $ message , $ date = null , $ type = null , $ localid = null ) {
106- if (is_array ($ receptor )) {
107- $ receptor = implode (", " , $ receptor );
108- }
109- if (is_array ($ localid )) {
110- $ localid = implode (", " , $ localid );
111- }
112- $ path = $ this ->get_path ("send " );
113- $ params = array (
114- "receptor " => $ receptor ,
115- "sender " => $ sender ,
116- "message " => $ message ,
117- "date " => $ date ,
118- "type " => $ type ,
119- "localid " => $ localid ,
120- );
121- return $ this ->execute ($ path , $ params );
122- }
123- public function SendArray ($ receptor , $ sender , $ message , $ date = null , $ type = null , $ localmessageid = null ) {
124- if (!is_array ($ receptor )) {
125- $ receptor = (array ) $ receptor ;
126- }
127- if (!is_array ($ sender )) {
128- $ sender = (array ) $ sender ;
129- }
130- if (!is_array ($ message )) {
131- $ message = (array ) $ message ;
132- }
133- $ repeat = count ($ receptor );
134- if (!is_null ($ type ) && !is_array ($ type )) {
135- $ type = array_fill (0 , $ repeat , $ type );
136- }
137- if (!is_null ($ localmessageid ) && !is_array ($ localmessageid )) {
138- $ localmessageid = array_fill (0 , $ repeat , $ localmessageid );
139- }
140- $ path = $ this ->get_path ("sendarray " );
141- $ params = array (
142- "receptor " => json_encode ($ receptor ),
143- "sender " => json_encode ($ sender ),
144- "message " => json_encode ($ message ),
145- "date " => $ date ,
146- "type " => json_encode ($ type ),
147- "localmessageid " => json_encode ($ localmessageid ),
148- );
149- return $ this ->execute ($ path , $ params );
150- }
151-
152- public function Status ($ messageid ) {
153- $ path = $ this ->get_path ("status " );
154- $ params = array (
155- "messageid " => is_array ($ messageid ) ? implode (", " , $ messageid ) : $ messageid ,
156- );
157- return $ this ->execute ($ path , $ params );
158-
159- }
160-
161- public function StatusLocalMessageid ($ localid ) {
162- $ path = $ this ->get_path ("statuslocalmessageid " );
163- $ params = array (
164- "localid " => is_array ($ localid ) ? implode (", " , $ localid ) : $ localid ,
165- );
166- return $ this ->execute ($ path , $ params );
167- }
168-
169- public function Select ($ messageId ) {
170- $ params = array (
171- "messageid " => is_array ($ messageid ) ? implode (", " , $ messageid ) : $ messageid ,
172- );
173- $ path = $ this ->get_path ("select " );
174- return $ this ->execute ($ path , $ params );
175-
176- }
177-
178- public function SelectOutbox ($ startdate , $ enddate , $ sender ) {
179- $ path = $ this ->get_path ("selectoutbox " );
180- $ params = array (
181- "startdate " => $ startdate ,
182- "enddate " => $ enddate ,
183- "sender " => $ sender ,
184- );
185- return $ this ->execute ($ path , $ params );
186- }
187-
188- public function LatestOutbox ($ pagesize , $ sender ) {
189- $ path = $ this ->get_path ("latestoutbox " );
190- $ params = array (
191- "pagesize " => $ pagesize ,
192- "sender " => $ sender ,
193- );
194- return $ this ->execute ($ path , $ params );
195- }
196-
197- public function CountOutbox ($ startdate , $ enddate , $ status = 0 ) {
198- $ path = $ this ->get_path ("countoutbox " );
199- $ params = array (
200- "startdate " => $ startdate ,
201- "enddate " => $ enddate ,
202- "status " => $ status ,
203- );
204- return $ this ->execute ($ path , $ params );
205- }
206-
207- public function Cancel ($ messageid ) {
208- $ path = $ this ->get_path ("cancel " );
209- $ params = array (
210- "messageid " => is_array ($ messageid ) ? implode (", " , $ messageid ) : $ messageid ,
211- );
212- return $ this ->execute ($ path , $ params );
213-
214- }
215-
216- public function Receive ($ linenumber , $ isread = 0 ) {
217- $ path = $ this ->get_path ("receive " );
218- $ params = array (
219- "linenumber " => $ linenumber ,
220- "isread " => $ isread ,
221- );
222- return $ this ->execute ($ path , $ params );
223- }
224-
225- public function CountInbox ($ startdate , $ enddate , $ linenumber , $ isread = 0 ) {
226- $ path = $ this ->get_path ("countinbox " );
227- $ params = array (
228- "startdate " => $ startdate ,
229- "enddate " => $ enddate ,
230- "linenumber " => $ linenumber ,
231- "isread " => $ isread ,
232- );
233- return $ this ->execute ($ path , $ params );
234- }
235-
236- public function CountPostalcode ($ postalcode ) {
237- $ path = $ this ->get_path ("countpostalcode " );
238- $ params = array (
239- "postalcode " => $ postalcode ,
240- );
241- return $ this ->execute ($ path , $ params );
242- }
243-
244- public function SendbyPostalcode ($ postalcode , $ sender , $ message , $ mcistartindex , $ mcicount , $ mtnstartindex , $ mtncount , $ date ) {
245- $ path = $ this ->get_path ("sendbypostalcode " );
246- $ params = array (
247- "postalcode " => $ postalcode ,
248- "sender " => $ sender ,
249- "message " => $ message ,
250- "mcistartindex " => $ mcistartindex ,
251- "mcicount " => $ mcicount ,
252- "mtnstartindex " => $ mtnstartindex ,
253- "mtncount " => $ mtncount ,
254- "date " => $ date ,
255- );
256- return $ this ->execute ($ path , $ params );
257- }
258-
259- public function AccountInfo () {
260- $ path = $ this ->get_path ("info " , "account " );
261- return $ this ->execute ($ path );
262- }
263-
264- public function AccountConfig ($ apilogs , $ dailyreport , $ debug , $ defaultsender , $ mincreditalarm , $ resendfailed ) {
265- $ path = $ this ->get_path ("config " , "account " );
266- $ params = array (
267- "apilogs " => $ apilogs ,
268- "dailyreport " => $ dailyreport ,
269- "debug " => $ debug ,
270- "defaultsender " => $ defaultsender ,
271- "mincreditalarm " => $ mincreditalarm ,
272- "resendfailed " => $ resendfailed ,
273- );
274- return $ this ->execute ($ path , $ params );
275- }
276-
277- public function VerifyLookup ($ receptor , $ token , $ template ) {
278- $ path = $ this ->get_path ("lookup " , "verify " );
279- $ params = array (
280- "receptor " => $ receptor ,
281- "token " => $ token ,
282- "template " => $ template ,
283- );
284- return $ this ->execute ($ path , $ params );
285- }
286- private function get_path ($ method , $ base = 'sms ' ) {
287- return sprintf ($ this ->gateway_url , $ this ->credential ['api_key ' ], $ base , $ method );
40+ try {
41+ $ api = $ this ->api ;
42+ $ result = $ api ->Send ($ this ->sender ,$ number ,$ message );
43+ if ($ result ){
44+ return var_dump ($ result );
45+ }
46+ }
47+ catch (ApiException $ e ){
48+ // در صورتی که خروجی وب سرویس 200 نباشد این خطا رخ می دهد
49+ return $ e ->errorMessage ();
50+ }
51+ catch (HttpException $ e ){
52+ // در زمانی که مشکلی در برقرای ارتباط با وب سرویس وجود داشته باشد این خطا رخ می دهد
53+ return $ e ->errorMessage ();
54+ }
55+ }
56+
57+ public function verifyLookup (String $ number , String $ token , String $ template )
58+ {
59+ try {
60+ $ api = $ this ->api ;
61+ $ result = $ api ->VerifyLookup ($ number ,$ token ,$ token2 = '' ,$ token3 = '' ,$ template ,$ type = null );
62+ if ($ result ){
63+ var_dump ($ result );
64+ }
65+ }
66+ catch (ApiException $ e ){
67+ echo $ e ->errorMessage ();
68+ }
69+ catch (HttpException $ e ){
70+ echo $ e ->errorMessage ();
71+ }
28872 }
28973}
0 commit comments