n11.com api usage – Get orders
In this article, I would like to show you how we can easily access order informations in n11.com api with using php code.
First of all, we need to n11 api key and password.
Getting your api key and password pair is easy enough.
Goto the n11 seller office home page and login to your account. For the purposes of this tutorial, we assume you are already an approved n11 seller. https://so.n11.com/
Click on the “Hesabım->API Hesapları” link at the top of the page. https://so.n11.com/selleroffice/integration/apiAccounts
Then click the button to “+ Yeni Hesap Oluştur”
Then your api key and password will be sent by e-mail.
For get orders our endpoint url : https://api.n11.com/ws/OrderService.wsdl
We can use to list orders in this service these methods
- OrderListRequest
- DetailedOrderListRequest
Sample OrderListRequest response:
<result>
<status>success</status>
</result>
<pagingData>
<currentPage>0</currentPage>
<pageSize>100</pageSize>
<totalCount>2</totalCount>
<pageCount>1</pageCount>
</pagingData>
<orderList>
<order>
<citizenshipId>35314149022</citizenshipId>
<createDate>20/08/2020 10:44</createDate>
<id>247253473</id>
<orderNumber>123456789</orderNumber>
<paymentType>1</paymentType>
<status>2</status>
</order>
<order>
<citizenshipId>10495173202</citizenshipId>
<createDate>20/08/2020 13:45</createDate>
<id>247271092</id>
<orderNumber>546546412</orderNumber>
<paymentType>1</paymentType>
<status>2</status>
</order>
</orderList>
Sample DetailedOrderListRequest response:
<result>
<status>success</status>
</result>
<pagingData>
<currentPage>0</currentPage>
<pageSize>100</pageSize>
<totalCount>2</totalCount>
<pageCount>1</pageCount>
</pagingData>
<orderList>
<order>
<citizenshipId>35314149022</citizenshipId>
<createDate>20/08/2020 10:44</createDate>
<id>247253473</id>
<orderItemList>
<orderItem>
<approvedDate>20/08/2020</approvedDate>
<attributes/>
<commission>164.9</commission>
<customTextOptionValues/>
<deliveryFeeType>5</deliveryFeeType>
<dueAmount>1649</dueAmount>
<id>237034963</id>
<installmentChargeWithVAT>0</installmentChargeWithVAT>
<mallDiscount>0</mallDiscount>
<price>1245</price>
<productId>123456</productId>
<productName>Urun Adi</productName>
<productSellerCode>Urun Kodu</productSellerCode>
<quantity>1</quantity>
<sellerCouponDiscount>0</sellerCouponDiscount>
<sellerDiscount>612</sellerDiscount>
<sellerInvoiceAmount>1649</sellerInvoiceAmount>
<shipmenCompanyCampaignNumber>1212121212122</shipmenCompanyCampaignNumber>
<shipmentInfo>
<campaignNumber>1212121212122</campaignNumber>
<campaignNumberStatus>2</campaignNumberStatus>
<shipmentCode>12312123</shipmentCode>
<shipmentCompany>
<id>345</id>
<name>Aras</name>
<shortName>ARAS</shortName>
</shipmentCompany>
<shipmentMethod>1</shipmentMethod>
</shipmentInfo>
<shippingDate></shippingDate>
<sppApproved>true</sppApproved>
<status>5</status>
<totalMallDiscountPrice>0</totalMallDiscountPrice>
<updatedDate>20/08/2020 11:09</updatedDate>
<version>102</version>
</orderItem>
</orderItemList>
<orderNumber>123456789</orderNumber>
<paymentType>1</paymentType>
<status>2</status>
<totalAmount>1649</totalAmount>
<totalDiscountAmount>612</totalDiscountAmount>
</order>
<order>
<citizenshipId>10495173202</citizenshipId>
<createDate>20/08/2020 13:45</createDate>
<id>247271092</id>
<orderItemList>
<orderItem>
<approvedDate>20/08/2020</approvedDate>
<attributes/>
<commission>197.2</commission>
<customTextOptionValues/>
<deliveryFeeType>3</deliveryFeeType>
<dueAmount>1972</dueAmount>
<id>465623</id>
<installmentChargeWithVAT>0</installmentChargeWithVAT>
<mallDiscount>0</mallDiscount>
<price>2963</price>
<productId>23465</productId>
<productName>Urun Adi 2</productName>
<productSellerCode>12354</productSellerCode>
<quantity>1</quantity>
<sellerCouponDiscount>0</sellerCouponDiscount>
<sellerDiscount>991</sellerDiscount>
<sellerInvoiceAmount>1972</sellerInvoiceAmount>
<shipmenCompanyCampaignNumber>23132132123132</shipmenCompanyCampaignNumber>
<shipmentInfo>
<campaignNumber>23132132123132</campaignNumber>
<campaignNumberStatus>2</campaignNumberStatus>
<shipmentCode>12313246</shipmentCode>
<shipmentCompany>
<id>345</id>
<name>Aras</name>
<shortName>ARAS</shortName>
</shipmentCompany>
<shipmentMethod>1</shipmentMethod>
</shipmentInfo>
<shippingDate></shippingDate>
<sppApproved>true</sppApproved>
<status>5</status>
<totalMallDiscountPrice>0</totalMallDiscountPrice>
<updatedDate>20/08/2020 13:51</updatedDate>
<version>67</version>
</orderItem>
</orderItemList>
<orderNumber>546546412</orderNumber>
<paymentType>1</paymentType>
<status>2</status>
<totalAmount>1972</totalAmount>
<totalDiscountAmount>991</totalDiscountAmount>
</order>
</orderList>
Benim işime daha detaylı sonuç veren DetailedOrderListRequest isteğini kullanmak geliyor. Satılan ürünü görebiliyorum vs. Fakat bu detaylı istekte zaman sınırı bulunuyor. İki DetailedOrderListRequest isteği arasında 5 saniye beklemek gerekiyor.
Zaman sınırından bize ne demiyoruz. Çünkü tek sorgu ile işi çözemiyoruz. Şöyle açıklayalım. Bu isteği yaparken gönderdiğimiz parametrelerden biri sipariş durumu(status). Bu durumlar da şunlar oluyor:
- “New”: Yeni siparişler
- “Approved”: Onaylanmış Siparişler
- “Rejected”: İptal Edilmiş Siparişler
- “Shipped”: Kargolanmış Siparişler
- “Delivered”: Teslim Edilen Siparişler
- “Completed”: Tamamlanmış Siparişler
- “Claimed”: İptal/İade/Değişim Durumundaki Siparişler
- “LATE_SHIPMENT” Kargolanması Geciken Siparişler
Normal akış düzeninde (müşterilerin sipariş verip bizim bunları onaylayıp hazırlayıp kargoya verdiğimiz düzen) bizim görmemiz gereken aktif siparişler New(Yeni Siparişler), Approved(Onaylanmış Siparişler) ve LATE_SHIPMENT(Kargolanması Geciken Siparişler). Bu 3 durumdaki siparişleri tek sorgu ile çekemiyoruz. -Yada ben fark edemedim.- Bu 3 durumdaki aktif olan siparişlerin hepsini çekmek için sıra ile 3 sorgu çalıştırmamız gerekiyor.
Ben burada zaman sınırına en az takılmak için yeni ve geciken siparişleri OrderListRequest ile onaylanmış siparişleri DetailedOrderListRequest ile çekiyorum. Yeni ve geciken sipariş sorgularından gelen cevaplarda sipariş adedini gösteren totalCount alandaki değeri kontrol ediyorum. 0‘sa 3 sorgu 1 saniye içerisinde tamamlanmış oluyor. totalCount alanları 0 değilse 6 saniye arayla bekleyip 0 olmayan yeni yada geciken siparişleri DetailedOrderListRequest ile çekiyorum.
Nasıl çekiyorum derseniz. Multi_curl kullandığım php kodum şu şekilde.
$app_key = "fd79f0ba-5ead-49c9-b058-2dea4287797b";
$app_secret = "ypwgJwJ4nVaO0Qe2";
$ct = array();
////////////////////////////////////////////////////
$status = "New";
$post = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sch='http://www.n11.com/ws/schemas'>
<soapenv:Header/>
<soapenv:Body>
<sch:OrderListRequest>
<auth>
<appKey>$app_key</appKey>
<appSecret>$app_secret</appSecret>
</auth>
<searchData>
<status>$status</status>
</searchData>
</sch:OrderListRequest>
</soapenv:Body>
</soapenv:Envelope>";
$ct[0] = curl_init();
curl_setopt($ct[0], CURLOPT_URL,"https://api.n11.com/ws/OrderService.wsdl");
curl_setopt($ct[0], CURLOPT_POST, 1);
curl_setopt($ct[0], CURLOPT_TIMEOUT, 10);
curl_setopt($ct[0], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ct[0], CURLOPT_POSTFIELDS, $post);
curl_setopt($ct[0], CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8"));
curl_setopt($ct[0], CURLOPT_RETURNTRANSFER, true);
////////////////////////////////////////////////////
$status = "LATE_SHIPMENT";
$post = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sch='http://www.n11.com/ws/schemas'>
<soapenv:Header/>
<soapenv:Body>
<sch:OrderListRequest>
<auth>
<appKey>$app_key</appKey>
<appSecret>$app_secret</appSecret>
</auth>
<searchData>
<status>$status</status>
</searchData>
</sch:OrderListRequest>
</soapenv:Body>
</soapenv:Envelope>";
$ct[1] = curl_init();
curl_setopt($ct[1], CURLOPT_URL,"https://api.n11.com/ws/OrderService.wsdl");
curl_setopt($ct[1], CURLOPT_POST, 1);
curl_setopt($ct[1], CURLOPT_TIMEOUT, 10);
curl_setopt($ct[1], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ct[1], CURLOPT_POSTFIELDS, $post);
curl_setopt($ct[1], CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8"));
curl_setopt($ct[1], CURLOPT_RETURNTRANSFER, true);
////////////////////////////////////////////////////
$status = "Approved";
$post = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sch='http://www.n11.com/ws/schemas'>
<soapenv:Header/>
<soapenv:Body>
<sch:DetailedOrderListRequest>
<auth>
<appKey>$app_key</appKey>
<appSecret>$app_secret</appSecret>
</auth>
<searchData>
<status>$status</status>
</searchData>
</sch:DetailedOrderListRequest>
</soapenv:Body>
</soapenv:Envelope>";
$ct[2] = curl_init();
curl_setopt($ct[2], CURLOPT_URL,"https://api.n11.com/ws/OrderService.wsdl");
curl_setopt($ct[2], CURLOPT_POST, 1);
curl_setopt($ct[2], CURLOPT_TIMEOUT, 10);
curl_setopt($ct[2], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ct[2], CURLOPT_POSTFIELDS, $post);
curl_setopt($ct[2], CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8"));
curl_setopt($ct[2], CURLOPT_RETURNTRANSFER, true);
////////////////////////////////////////////////////
$cct = curl_multi_init();
for ($j=0;$j<count($ct);$j++) {
curl_multi_add_handle($cct,$ct[$j]);
}
$running=null;
do { curl_multi_exec($cct,$running);
} while ($running > 0);
$new_veri = xml_to_array(curl_multi_getcontent($ct[0]));
$late_veri = xml_to_array(curl_multi_getcontent($ct[1]));
$appr_veri = xml_to_array(curl_multi_getcontent($ct[2]));
Ben bu gelen verileri nedense okuyamıyorum. Bunları diziye çevirmek için şöyle bir fonksiyonum var. Son satırlarda bunu kullandım.
function xml_to_array($veri) {
$xml = simplexml_load_string($veri);
foreach($xml->xpath('//env:Body') as $path) {
$orderList = $path->xpath('//orderList');
$pagingData = $path->xpath('//pagingData');
return array($pagingData[0],$orderList[0]);
}
}
Kodu çalıştırıp bu dizilerin birinin print_r halini de buraya koyarsak yazı tamamdır.
Array
(
[0] => SimpleXMLElement Object
(
[currentPage] => 0
[pageSize] => 100
[totalCount] => 4
[pageCount] => 1
)
[1] => SimpleXMLElement Object
(
[order] => Array
(
[0] => SimpleXMLElement Object
(
[citizenshipId] => 76767676786
[createDate] => 15/08/2020 22:36
[id] => 687667867676
[orderItemList] => SimpleXMLElement Object
(
[orderItem] => SimpleXMLElement Object
(
[approvedDate] => 17/08/2020
[attributes] => SimpleXMLElement Object
(
)
[commission] => 4.29
[customTextOptionValues] => SimpleXMLElement Object
(
)
[deliveryFeeType] => 3
[dueAmount] => 42.9
[id] => 6767676687
[installmentChargeWithVAT] => 0
[mallDiscount] => 0
[price] => 42.9
[productId] => 67867676786
[productName] => sfhsdkhsdhs
[productSellerCode] => 464564
[quantity] => 1
[sellerCouponDiscount] => 0
[sellerDiscount] => 0
[sellerInvoiceAmount] => 42.9
[shipmenCompanyCampaignNumber] => 465645646456
[shipmentInfo] => SimpleXMLElement Object
(
[campaignNumber] => 456566465
[campaignNumberStatus] => 1
[shipmentCode] => 4656464645
[shipmentCompany] => SimpleXMLElement Object
(
[id] => 345
[name] => Aras
[shortName] => ARAS
)
[shipmentMethod] => 1
[trackingNumber] => 46664565
)
[shippingDate] => 18/08/2020
[sppApproved] => true
[status] => 6
[totalMallDiscountPrice] => 0
[updatedDate] => 18/08/2020 11:09
[version] => 4
)
)
[orderNumber] => 465546456645
[paymentType] => 1
[status] => 2
[totalAmount] => 42.9
[totalDiscountAmount] => 149.99
)
[1] => SimpleXMLElement Object
(
[citizenshipId] => 45645645654
[createDate] => 17/08/2020 16:08
[id] => 246956526
[orderItemList] => SimpleXMLElement Object
(
[orderItem] => SimpleXMLElement Object
(
[approvedDate] => 17/08/2020
[attributes] => SimpleXMLElement Object
(
)
[commission] => 54.9
[customTextOptionValues] => SimpleXMLElement Object
(
)
[deliveryFeeType] => 5
[dueAmount] => 9
[id] => 465646645
[installmentChargeWithVAT] => 0
[mallDiscount] => 0
[price] => 741
[productId] => 45464545
[productName] => fdfhdjkf
[productSellerCode] => 45645
[quantity] => 1
[sellerCouponDiscount] => 0
[sellerDiscount] => 192
[sellerInvoiceAmount] => 59
[shipmenCompanyCampaignNumber] => 456456465489
[shipmentInfo] => SimpleXMLElement Object
(
[campaignNumber] => 4465454654
[campaignNumberStatus] => 1
[shipmentCode] => 546456465
[shipmentCompany] => SimpleXMLElement Object
(
[id] => 345
[name] => Aras
[shortName] => ARAS
)
[shipmentMethod] => 1
[trackingNumber] => 54646484
)
[shippingDate] => 17/08/2020
[sppApproved] => true
[status] => 6
[totalMallDiscountPrice] => 0
[updatedDate] => 17/08/2020 18:18
[version] => 65
)
)
[orderNumber] => 445643212
[paymentType] => 1
[status] => 2
[totalAmount] => 59
[totalDiscountAmount] => 192
)
[2] => SimpleXMLElement Object
(
[citizenshipId] => 4654564312
[createDate] => 18/08/2020 12:22
[id] => 247040322
[orderItemList] => SimpleXMLElement Object
(
[orderItem] => SimpleXMLElement Object
(
[approvedDate] => 18/08/2020
[attributes] => SimpleXMLElement Object
(
)
[commission] => 94.9
[customTextOptionValues] => SimpleXMLElement Object
(
)
[deliveryFeeType] => 5
[dueAmount] => 949
[id] => 56465456321132
[installmentChargeWithVAT] => 0
[mallDiscount] => 0
[price] => 146068
[productId] => 645645645
[productName] => jdslkgjslkfg sd
[productSellerCode] => 465645
[quantity] => 1
[sellerCouponDiscount] => 0
[sellerDiscount] => 119
[sellerInvoiceAmount] => 564
[shipmenCompanyCampaignNumber] => 87676676
[shipmentInfo] => SimpleXMLElement Object
(
[campaignNumber] => 456456456
[campaignNumberStatus] => 1
[shipmentCode] => 45656456456
[shipmentCompany] => SimpleXMLElement Object
(
[id] => 345
[name] => Aras
[shortName] => ARAS
)
[shipmentMethod] => 1
[trackingNumber] => 45645656654
)
[shippingDate] => 18/08/2020
[sppApproved] => true
[status] => 6
[totalMallDiscountPrice] => 0
[updatedDate] => 18/08/2020 18:46
[version] => 20
)
)
[orderNumber] => 456464564
[paymentType] => 1
[status] => 2
[totalAmount] => 949
[totalDiscountAmount] => 119
)
[3] => SimpleXMLElement Object
(
[citizenshipId] => 456565
[createDate] => 18/08/2020 22:17
[id] => 247113030
[orderItemList] => SimpleXMLElement Object
(
[orderItem] => SimpleXMLElement Object
(
[approvedDate] => 19/08/2020
[attributes] => SimpleXMLElement Object
(
)
[commission] => 134.9
[customTextOptionValues] => SimpleXMLElement Object
(
)
[deliveryFeeType] => 3
[dueAmount] => 1349
[id] => 45645645
[installmentChargeWithVAT] => 0
[mallDiscount] => 0
[price] => 1349
[productId] => 6544645
[productName] => fhsdkjfhsjk
[productSellerCode] => 4566
[quantity] => 1
[sellerCouponDiscount] => 0
[sellerDiscount] => 0
[sellerInvoiceAmount] => 1349
[shipmenCompanyCampaignNumber] => 112025421462740
[shipmentInfo] => SimpleXMLElement Object
(
[campaignNumber] => 112025421462740
[campaignNumberStatus] => 1
[shipmentCode] => 46466346
[shipmentCompany] => SimpleXMLElement Object
(
[id] => 345
[name] => Aras
[shortName] => ARAS
)
[shipmentMethod] => 1
[trackingNumber] => 35029275032
)
[shippingDate] => 19/08/2020
[sppApproved] => true
[status] => 6
[totalMallDiscountPrice] => 0
[updatedDate] => 19/08/2020 17:21
[version] => 19
)
)
[orderNumber] => 786786564
[paymentType] => 1
[status] => 2
[totalAmount] => 1349
[totalDiscountAmount] => 0
)
)
)
)
Buradan istediğiniz dataya erişin kullanın kopyalayın. Örneğin;
- Sipariş adedi için $appr_veri[0]->totalCount;
- Tek tek siparişleri işlemek for döngüsü
foreach ($appr_veri[1]->order as $satis) {
echo $satis->id;
}