Пример подключения к смс шлюзу ePochta SMS на языке программирования PHP для версии API 3.0
image image

Пример работы с SMS шлюзом на языке JavaScript

Отправка СМС:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * @param {type} login
 * @param {type} password
 * @returns {EpochtaApi2}
 */
function EpochtaApiv2(login, password, debug) {
    debug = debug || 0;
    var self = this;
    this.debug = debug ? 1 : 0;
    this.login = login;
    this.password = password;
    this.send_xml = '<?xml version="1.0" encoding="UTF-8"?>\
    <sms>\
        <operations>\
            <operation></operation>\
        </operations>\
        <authentification>\
            <username></username>\
            <password></password>\
        </authentification>\
        <message>\
            <sender></sender>\
            <text></text>\
        </message>\
        <numbers>\
        </numbers>\
    </sms>';
     
    /**
     * Отправка смс
     * @param {String} sender
     * @param {String} message
     * @param {Array} numbers
     * ([
     *      {val: "380933630001", id:"msg1", vars:"var1;var2;var3;"},
     *      {val: "380933630002", id:"msg2"}
     *  ])
     * @param {String} sentdate (2012-05-01 00:20:00)
     * @returns {Boolean}
     */
    this.send_sms = function(sender, message, numbers, sentdate, callback, error_callback) {
        if (typeof sender === 'undefined'
            || typeof message === 'undefined'
            || typeof numbers === 'undefined'
            || ! numbers.length
            || ! is_function(callback)
            || ! is_function(error_callback))
            return false;
        self.xml = $.parseXML( self.send_xml );
        self.xml = $( self.xml );
        self.xml.find( "operation" ).text("SEND");
        self.xml.find( "username" ).text(self.login);
        self.xml.find( "password" ).text(self.password);
        self.xml.find( "sender" ).text(sender);
        self.xml.find( "text" ).text(message);
        if (typeof sentdate !== 'undefined') {
            self.xml.find( "SMS" ).append('<sentdate>'+sentdate+'</sentdate>');
        }  
        var xml_numbers = self.xml.find( "numbers" ),
            node;
        for (var i =0; i < numbers.length; i++) {
            node = $('<number>'+numbers[i].val+'</number>');
            if (typeof numbers[i].id !== 'undefined') {
                node.attr('messageID', numbers[i].id);
            }
            if (typeof numbers[i].vars !== 'undefined') {
                node.attr('variables', numbers[i].vars);
            }
            xml_numbers.append(node);
        }
        return this.request('POST', callback, error_callback);
    }
     
    /**
     * Расчет стоимости отправки смс
     * @param {String} sender
     * @param {String} message
     * @param {Array} numbers
     * ([
     *      {val: "380933630001", id:"msg1", vars:"var1;var2;var3;"},
     *      {val: "380933630002", id:"msg2"}
     *  ])
     * @param {String} sentdate (2012-05-01 00:20:00)
     * @returns {Boolean}
     */
    this.get_price = function(sender, message, numbers, sentdate, callback, error_callback) {
        if (typeof sender === 'undefined'
            || typeof message === 'undefined'
            || typeof numbers === 'undefined'
            || ! numbers.length
            || ! is_function(callback)
            || ! is_function(error_callback))
            return false;
        self.xml = $.parseXML( self.send_xml );
        self.xml = $( self.xml );
        self.xml.find( "operation" ).text("GETPRICE");
        self.xml.find( "username" ).text(self.login);
        self.xml.find( "password" ).text(self.password);
        self.xml.find( "sender" ).text(sender);
        self.xml.find( "text" ).text(message);
        if (typeof sentdate !== 'undefined') {
            self.xml.find( "SMS" ).append('<sentdate>'+sentdate+'</sentdate>');
        }  
        var xml_numbers = self.xml.find( "numbers" ),
            node;
        for (var i =0; i < numbers.length; i++) {
            node = $('<number>'+numbers[i].val+'</number>');
            if (typeof numbers[i].id !== 'undefined') {
                node.attr('messageID', numbers[i].id);
            }
            if (typeof numbers[i].vars !== 'undefined') {
                node.attr('variables', numbers[i].vars);
            }
            xml_numbers.append(node);
        }
        return this.request('POST', callback, error_callback);
    }
     
     
    this.status_xml = '<?xml version="1.0" encoding="UTF-8"?>\
    <sms>\
        <operations>\
            <operation>GETSTATUS</operation>\
        </operations>\
        <authentification>\
            <username></username>\
            <password></password>\
        </authentification>\
        <statistics>\
        </statistics>\
    </sms>';
     
    /**
     * Получения статуса отправленной смс
     * @param {Array} id_list (['msg1', 'msg2'])
     * @returns {Boolean}
     */
    this.get_status = function(id_list, callback, error_callback) {
        if (typeof id_list === 'undefined'
            || ! id_list.length
            || ! is_function(callback)
            || ! is_function(error_callback))
            return false;
        self.xml = $.parseXML( self.status_xml );
        self.xml = $( self.xml );
        self.xml.find( "username" ).text(self.login);
        self.xml.find( "password" ).text(self.password);
        var statistics = self.xml.find( "statistics" );
        for (var i =0; i < id_list.length; i++) {   
            statistics.append('<messageid>'+id_list[i].val+'</messageid>');
        }
        return this.request('POST', callback, error_callback);
    }
     
    this.balance_xml = '<?xml version="1.0" encoding="UTF-8"?>\
    <sms>\
        <operations>\
            <operation>BALANCE</operation>\
        </operations>\
        <authentification>\
            <username></username>\
            <password></password>\
        </authentification>\
    </sms>';
     
    /**
     * Получение баланса
     * @param {Array} id_list (['msg1', 'msg2'])
     * @returns {Boolean}
     */
    this.get_balance = function(callback, error_callback) {
        if ( ! is_function(callback)
            || ! is_function(error_callback))
            return false;
        self.xml = $.parseXML( self.balance_xml );
        self.xml = $( self.xml );
        self.xml.find( "username" ).text(self.login);
        self.xml.find( "password" ).text(self.password);
        return this.request('POST', callback, error_callback);
    }
     
    this.credit_price_xml = '<?xml version="1.0" encoding="UTF-8"?>\
    <sms>\
        <operations>\
            <operation>CREDITPRICE</operation>\
        </operations>\
        <authentification>\
            <username></username>\
            <password></password>\
        </authentification>\
    </sms>';
     
    /**
     * Получение стоимости одного кредита
     * @param {Array} id_list (['msg1', 'msg2'])
     * @returns {Boolean}
     */
    this.get_credit_price = function(callback, error_callback) {
        if ( ! is_function(callback)
            || ! is_function(error_callback))
            return false;
        self.xml = $.parseXML( self.credit_price_xml );
        self.xml = $( self.xml );
        self.xml.find( "username" ).text(self.login);
        self.xml.find( "password" ).text(self.password);
        return this.request('POST', callback, error_callback);
    }
     
    /**
     *
     * @param {String} type
     * @returns {undefined}
     */
    this.request = function(type, callback, error_callback) {
        var option = {
            url: self.gateway,
            data: (new XMLSerializer().serializeToString(self.xml[0])),
            type: type,
            dataType: "text"
        };
        if (this.debug) {
            console.log(option);
            return true;
        }
        var jqxhr = $.ajax(option);
        jqxhr.fail(function (xhr, ajaxOptions, thrownError) { 
            console.log([xhr.status, thrownError]);         
        });
        jqxhr.always(function(xml_result, status, thrownError) {
            var doc_result = $(xml_result),
                status = doc_result.find('status').text();
            if (status >= 0) { // OK
                callback(xml_result, doc_result);
            } else {
                error_callback(xml_result, self.error_handler(status))
            }
        });
        return true;
    }
     
    /**
     
     *
     * @param {type} method
     * @param {type} xml
     * @returns {undefined}
     */
    this.error_handler = function(status) {
        var messages = {
            '-1': { 'status': '-1', 'code' : 'AUTH_FAILED', 'message': 'Неправильний логін та/або пароль' },
            '-2': { 'status': '-2', 'code' : 'XML_ERROR', 'message': 'Неправильний формат XML' },
            '-3': { 'status': '-3', 'code' : 'NOT_ENOUGH_CREDITS', 'message': 'Недостатньо кредитів на акаунті користувача' },
            '-4': { 'status': '-4', 'code' : 'NO_RECIPIENTS', 'message': 'Немає вірних номерів отримувачів' },
            '-7': { 'status': '-7', 'code' : 'BAD_SENDER_NAME', 'message': 'Помилка в імені відправника' },
        }
        if (typeof messages[status] !== 'undefined') {
            return messages[status];
        }
        return {status: status, 'message': 'Unknow error'};
    }
}
 
/**
 *
 * @param {type} value
 * @returns {unresolved}
 */
 
function gettype(value) {
    return Object.prototype.toString.call(value);
}
 
function is_function(value) {
    return (gettype(value) === '[object Function]');
}
 
/**
 * example usage:
 */                                                           
var ea2 = new EpochtaApiv2('useremail1@test.com', 'userpassword');
ea2.send_sms('test', 'test msg', [{val:'380933630001'}], '',
    function(result){
        console.log(result)
    },
    function(result){
        console.log(result)
    }
);
vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки