novaposhta.php
12.1 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace
{
/**
* novaposhta
*
* @author Jane Bezmaternykh
* @version 0.1.20140506
*/
class novaposhta extends \core
{
/////////////////////////////////////////////////////////////////////////////
protected $_out_city = 'Київ';
protected $_sender_city_ref = 'Київ';
protected $_out_company = 'Професійне насіння';
protected $_out_warehouse = '1';
protected $_sender_warehouse_ref = '1';
protected $_out_name = 'Петров Иван Иваныч';
protected $_out_phone = '0671234567';
protected $_api_key = '46eb7be54b344abfe21feab2ee17071c';
protected $_description = 'Насіння';
protected $_pack = 'Пакет';
protected $_rcpt_name = 'Приватна особа';
/////////////////////////////////////////////////////////////////////////////
/**
* Функция отправки запроса на сервер Новой почты
$xml — запрос
*/
public function send($xml)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.novaposhta.ua/v2.0/xml/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/////////////////////////////////////////////////////////////////////////////
/**
* Запрос на расчёт стоимости доставки
$to_city — город получатель
$weight — вес
$pub_price — заявленная стоимость
$height — высота коробки
$width — ширина коробки
$depth — длинна коробки
*/
public function price( $to_city, $pub_price, $weight, $height = 50, $width = 20, $depth = 20 )
{
$xml=
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<countPrice>
<senderCity>'.$this->_out_city.'</senderCity>
<recipientCity>'.$to_city.'</recipientCity>
<mass>'.$weight.'</mass>
<height>'.$height.'</height>
<width>'.$width.'</width>
<depth>'.$depth.'</depth>
<publicPrice>'.$pub_price.'</publicPrice>
<deliveryType_id>4</deliveryType_id>
<floor_count>0</floor_count>
<date>'.date( 'd.m.Y' ).'</date>
</countPrice>
</file>';
//p($xml,1);
$xml = simplexml_load_string($this->send($xml));
return strval($xml->cost);
}
/////////////////////////////////////////////////////////////////////////////
/**
* Запрос на создание декларации на отправку
$order_id — номер заказа на вашем сайте (для вашего удобства)
$city — город получения
$warehouse — номер склада получения
$name — имя получателя
$phone — телефон получателя
$weight — вес посылки
$pub_price — заявленная стоимость
$date — дата отправки
$payer — плательщик (1 — получатель, 0 — отправитель, 2 — третья сторона)
*/
/////////////////////////////////////////////////////////////////////////////
public function ttn( $order_id, $city, $warehouse, $street_name, $name, $phone, $weight, $pub_price, $payer=0)
{
$xml =
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<order
order_id="'.$order_id.'"
sender_city="'.$this->_out_city.'"
sender_company="'.$this->_out_company.'"
sender_address="'.$this->_out_warehouse.'"
sender_contact="'.$this->_out_name.'"
sender_phone="'.$this->_out_phone.'"
rcpt_name="'.$this->_rcpt_name.'"
rcpt_city_name="'.$city.'" '.
(
!empty( $warehouse )
?
'rcpt_warehouse="'.$warehouse.'" '
:
'rcpt_street_name="'.$street_name.'" '
).
'rcpt_contact="'.$name.'"
rcpt_phone_num="'.$phone.'"
pack_type="'.$this->_pack.'"
description="'.$this->_description.'"
pay_type="1"
payer="'.$payer.'"
cost="'.$pub_price.'"
date="'.date( 'd.m.Y' ).'"
weight="'.$weight.'">
<order_cont
cont_description="'.$this->_description.'" />
</order>
</file>';
$xml = simplexml_load_string($this->send($xml));
//return array('oid'=>$order_id,'ttn'=>trim($xml->order->attributes()->np_id));
return strval($xml->order->attributes()->np_id);
}
/////////////////////////////////////////////////////////////////////////////
public function ttn_ref( $order_id, $city_ref, $warehouse_ref, $street_name, $name, $phone, $weight, $pub_price, $payer=0)
{
$xml =
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<order
order_id="'.$order_id.'"
sender_city_ref="'.$this->_sender_city_ref.'"
sender_company="'.$this->_out_company.'"
sender_warehouse_ref="'.$this->_sender_warehouse_ref.'"
sender_contact="'.$this->_out_name.'"
sender_phone="'.$this->_out_phone.'"
rcpt_name="'.$this->_rcpt_name.'"
rcpt_city_ref="'.$city_ref.'" '.
(
!empty( $warehouse_ref )
?
'rcpt_warehouse_ref="'.$warehouse_ref.'" '
:
'rcpt_street_name="'.$street_name.'" '
).
'rcpt_contact="'.$name.'"
rcpt_phone_num="'.$phone.'"
pack_type="'.$this->_pack.'"
description="'.$this->_description.'"
pay_type="1"
payer="'.$payer.'"
cost="'.$pub_price.'"
date="'.date( 'd.m.Y' ).'"
weight="'.$weight.'">
<order_cont
cont_description="'.$this->_description.'" />
</order>
</file>';
$xml = simplexml_load_string($this->send($xml));
//return array('oid'=>$order_id,'ttn'=>trim($xml->order->attributes()->np_id));
return strval($xml->order->attributes()->np_id);
}
/////////////////////////////////////////////////////////////////////////////
/**
* Запрос на удаление декларации из базы Новой почты
$ttn — номер декларации, которую нужно удалить
*/
public function remove($ttn)
{
$xml =
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<close>'.$ttn.'</close>
</file>';
$xml = simplexml_load_string($this->send($xml));
return $xml;
}
/////////////////////////////////////////////////////////////////////////////
/**
* Запрос на печать маркировок для декларации (производит перенаправление на страницу печати)
$ttn — номер декларации, которую нужно напечатать
*/
public static function printit($ttn){
header('location: http://orders.novaposhta.ua/pformn.php?o='.$ttn.'&num_copy=4&token='.NP::$api_key);
}
/////////////////////////////////////////////////////////////////////////////
/**
* Запрос на получение списка складов Новой почты для определённого города (или полный список, если город не указан)
$filter — город, по которому нужно отфильтровать список складов Новой почты
*/
public function warenhouse($filter = false)
{
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<file>
<apiKey>'.$this->_api_key.'</apiKey>
<calledMethod>getWarehouses</calledMethod>
<methodProperties>'.
($filter ? "<CityRef>$filter</CityRef>" : "")
.'</methodProperties>
<modelName>Address</modelName>
</file>';
$xml = simplexml_load_string($this->send($xml));
return($xml->data);
}
/////////////////////////////////////////////////////////////////////////////
/**
* Запрос на получение списка населённых пунктов, в которых есть склады Новой почты
*/
public function city()
{
$xml =
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<city/>
</file>';
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<file>
<apiKey>'.$this->_api_key.'</apiKey>
<calledMethod>getCities</calledMethod>
<methodProperties/>
<modelName>Address</modelName>
</file>';
$xml = simplexml_load_string( $this->send($xml) );
return($xml->data);
}
/////////////////////////////////////////////////////////////////////////////
public function street( $filter )
{
$xml =
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<streets get_version="0"/>
<filter>c5f70e4c-4bd0-11dd-9198-001d60451983</filter>
</file>';
$xml = simplexml_load_string( $this->send($xml) );
return($xml);
}
/////////////////////////////////////////////////////////////////////////////
public function track( $en )
{
$xml =
'<?xml version="1.0" encoding="utf-8"?>
<file>
<auth>'.$this->_api_key.'</auth>
<track>
<en>'.$en.'</en>
</track>
</file>';
$xml = simplexml_load_string( $this->send($xml) );
return($xml);
}
/////////////////////////////////////////////////////////////////////////////
}
}