leandrorosa/cnova-sdk

dev-develop 2016-11-12 15:37 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:36:54 UTC


README

提供PHP组件以使用CNova提供的API V2(商家API)。

Vantagens na utilização de SDKs

创建API客户端

在使用API之前,需要创建一个客户端,并包含基本路径和访问凭证。

以下是一个示例代码:

require 'vendor/autoload.php';

use CNovaApiLojistaV2\client\Configuration;
use CNovaApiLojistaV2\client\ApiClient;
use CNovaApiLojistaV2\LoadsApi;
use CNovaApiLojistaV2\model\Product;
use CNovaApiLojistaV2\model\ProductLoadPrices;
use CNovaApiLojistaV2\model\ProductLoadStock;
use CNovaApiLojistaV2\model\Dimensions;
use CNovaApiLojistaV2\model\ProductAttribute;
use CNovaApiLojistaV2\client\ApiException;

Configuration::$apiKey['client_id'] = 'll0rQx9SSshr';
Configuration::$apiKey['access_token'] = 'nllKgtXTMv0G';

$api_client = new ApiClient('https://sandbox.cnova.com/api/v2');

辅助操作

处理API调用中接收到的错误结构

function deserializeErrors($errorsJson, $apiClient) {

	$errors = null;

	try {
		$errors = $apiClient->deserialize(json_decode($errorsJson), 'Errors');
	} catch (\Exception $e) {}

	return $errors;

}

格式化日期以进行特定时间段的查询

function formatDateRange($initialDate, $finalDate, $apiClient) {

	$dtIni = '*';
	$dtEnd = '*';

	if ($initialDate != null && $initialDate instanceof \DateTime) {
		$dtIni = $initialDate->format(\DateTime::ISO8601);
	}

	if ($finalDate != null && $finalDate instanceof \DateTime) {
		$dtEnd = $finalDate->format(\DateTime::ISO8601);
	}

	return $dtIni . ',' . $dtEnd;

}

可用的API

以下将展示API及其主要市场操作示例。

加载API

用于操作的API。

产品加载

$loads = new LoadsApi($api_client);

// Criação de um novo produto

$product = new Product();

$product->sku_seller_id = 'CEL_LGG4';
$product->product_seller_id = 'CEL';
$product->title = 'Produto de testes LG G4';
$product->description = '<h2>O novo produto de testes</h2>, LG G4';
$product->brand = 'LG';
$product->gtin = array('123456ft');
$product->categories = array (
		'Teste>API'
);
$product->images = array (
		'http://img.g.org/img1.jpeg'
);

$price = new ProductLoadPrices();
$price->default = 1999.0;
$price->offer = 1799.0;

$product->price = $price;

$stock = new ProductLoadStock();
$stock->quantity = 100;
$stock->cross_docking_time = 0;

$product->stock = $stock;

$dimensions = new Dimensions();
$dimensions->weight = 10;
$dimensions->length = 10;
$dimensions->width = 10;
$dimensions->height = 10;

$product->dimensions = $dimensions;

$product_attr = new ProductAttribute();
$product_attr->name = 'cor';
$product_attr->value = 'Verde';

$product->attributes =  array($product_attr);

// Adiciona o novo produto na lista a ser enviada
$products = array($product);

try {

	// Envia a carga de produtos
	$loads->postProducts($products);

} catch (ApiException $e) {

	$errors = deserializeErrors($e->getResponseBody(), $api_client);

	if ($errors != null) {
		foreach ($errors->errors as $error) {
			echo ($error->code . ' - ' . $error->message . "\n");
		}
	} else {
		echo ($e->getMessage());
	}

}

查询已发送的加载

$loads_api = new LoadsApi($api_client);

try {

	$get_products_response = $loads_api->getProducts(null, null, 0, 10);
	var_dump($get_products_response);

} catch (ApiException $e) {

	$errors = deserializeErrors($e->getResponseBody(), $api_client);

	if ($errors != null) {
		foreach ($errors->errors as $error) {
			echo ($error->code . ' - ' . $error->message . "\n");
		}
	} else {
		echo ($e->getMessage());
	}

}

查询已发送加载中的特定产品

$loads_api = new LoadsApi($api_client);

try {

	$get_product_with_errors_response = $loads_api->getProduct('CEL_LGG4');
	var_dump($get_product_with_errors_response);

} catch (ApiException $e) {

	$errors = deserializeErrors($e->getResponseBody(), $api_client);

	if ($errors != null) {
		foreach ($errors->errors as $error) {
			echo ($error->code . ' - ' . $error->message . "\n");
		}
	} else {
		echo ($e->getMessage());
	}

}

使用加载API修改一个或多个订单的跟踪信息

$loads_api = new LoadsApi($api_client);

$orders_trackings = new OrdersTrackings();

$order_tracking = new OrderTracking();

$order_id = new OrderId();
$order_id->id = 123;
$order_tracking->order = $order_id;

$order_tracking->control_point = 'ABC';
$order_tracking->cte = '123';

$oif = new OrderItemReference();
$oif->sku_seller_id = '123456';
$oif->quantity = 1;

$order_tracking->items = array($oif);

$order_tracking->occurred_at = new \DateTime('NOW');
$order_tracking->seller_delivery_id = '99995439701';
$order_tracking->number = '01092014';
$order_tracking->url = 'servico envio2';

$carrier = new Carrier();
$carrier->cnpj = '72874279234';
$carrier->name = 'Sedex';

$order_tracking->carrier = $carrier;

$invoice = new Invoice();
$invoice->cnpj = '72874279234';
$invoice->number = '123';
$invoice->serie = '456';
$invoice->issued_at = new \DateTime('NOW');
$invoice->access_key = '01091111111111111111111111111111111111101092';
$invoice->link_xml = 'link xlm teste5';
$invoice->link_danfe = 'link nfe teste5';

$order_tracking->invoice = $invoice;

$orders_trackings->trackings = array($order_tracking);

try {

	$loads_api->postOrdersTrackingSent($orders_trackings);

} catch (ApiException $e) {

	$errors = deserializeErrors($e->getResponseBody(), $api_client);

	if ($errors != null) {
		foreach ($errors->errors as $error) {
			echo ($error->code . ' - ' . $error->message . "\n");
		}
	} else {
		echo ($e->getMessage());
	}

}

卖家物品API

用于管理卖家发送的资源。

查询卖家物品

$seller_items_Api = new SellerItemsApi($api_client);

try {

	$get_seller_items_response = $seller_items_Api->getSellerItems('EX', 0, 100);
	var_dump($get_seller_items_response);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

更改价格

$seller_items_api = new SellerItemsApi($api_client);

try {

	$prices = new Prices();
	$prices->default = 100;
	$prices->offer = 100;

	$seller_items_api->putSellerItemPrices('31.0019', $prices);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

更改库存

$seller_items_api = new SellerItemsApi($api_client);

try {

	$stock = new Stock();
	$stock->quantity = 200;
	$stock->cross_docking_time = 0;

	$seller_items_api->putSellerItemStock('31.0019', $stock);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

订单API

用于管理订单的API。

查询所有订单

$orders_api = new OrdersApi($api_client);

try {
	$get_orders_response =  $orders_api->getOrders(0, 100);
	var_dump($get_orders_response);
} catch (ApiException $e) {
	echo ($e->getMessage());
}

查询所有状态为“新”的订单

$orders_api = new OrdersApi($api_client);

try {

	$purchased_at = formatDateRange(null, new \DateTime('NOW'), $api_client);

	$get_orders_response =  $orders_api->getOrdersByStatusNew($purchased_at, null, null, 0, 100);
	var_dump($get_orders_response);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

创建新的跟踪,通知订单已发送

$orders_api = new OrdersApi($api_client);

$new_tracking = new NewTracking();

$new_tracking->items = array('23258312-1', '23258312-2');

$new_tracking->occurred_at = new \DateTime('NOW');

$new_tracking->seller_delivery_id = '99995439701';
$new_tracking->number = '"01092014';
$new_tracking->url = 'servico envio2';

$carrier = new Carrier();
$carrier->cnpj = '72874279234';
$carrier->name = 'Sedex';

$new_tracking->carrier = $carrier;

$invoice = new Invoice();
$invoice->cnpj = '72874279234';
$invoice->number = '123';
$invoice->serie = '456';
$invoice->issued_at = new \DateTime('NOW');
$invoice->access_key = '01091111111111111111111111111111111111101092';
$invoice->link_xml = 'link xlm teste5';
$invoice->link_danfe = 'link nfe teste5';

$new_tracking->invoice = $invoice;

try {
	$orders_api->postOrderTrackingSent($new_tracking, '1024101');
} catch (ApiException $e) {

	$errors = deserializeErrors($e->getResponseBody(), $api_client);

	if ($errors != null) {
		foreach ($errors->errors as $error) {
			echo ($error->code . ' - ' . $error->message . "\n");
		}
	} else {
		echo ($e->getMessage());
	}

}

查询状态为“已发送”的订单

$orders_api = new OrdersApi($api_client);

try {

	$get_orders_response = $orders_api->getOrdersByStatusSent(null, null, null, 0, 100);
	var_dump($get_orders_response);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

票据API

用于管理票据。

创建新的票据

$tickets_api = new TicketsApi($api_client);

$new_ticket = new NewTicket();
$new_ticket->to = 'atendimento+OS_706000500000@mktp.extra.com.br';
$new_ticket->body = 'Corpo da mensagem do ticket';

try {
	$tickets_api->postTicket($new_ticket);
} catch (ApiException $e) {

	$errors = deserializeErrors($e->getResponseBody(), $api_client);

	if ($errors != null) {
		foreach ($errors->errors as $error) {
			echo ($error->code . ' - ' . $error->message . "\n");
		}
	} else {
		echo ($e->getMessage());
	}

}

查询状态为“开放”的票据

$tickets_api = new TicketsApi($api_client);

try {

	$tickets = $tickets_api->getTickets('opened', '439211092852', null, null, null, 0, 5);
	var_dump($tickets);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

更改票据的状态

$tickets_api = new TicketsApi($api_client);

try {

	$ticket_status = new TicketStatus();
	$ticket_status->ticket_status = 'Em Acompanhamento';

	$tickets_api->putTicketStatus('123123', $ticket_status);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

分类API

用于获取可用分类树的API。

查询可用分类

$categories_api = new CategoriesApi($api_client);

try {

	$get_categories_response = $categories_api->getCategories(0, 5);

	foreach ($get_categories_response->categories as $categorie) {
		echo ($categorie->id . ' - ' . $categorie->name . "\n");
	}

} catch (ApiException $e) {
	echo ($e->getMessage());
}

站点API

用于获取站点列表的API。

查询可用站点

$sites_api = new SitesApi($api_client);

try {

	$get_sites_response = $sites_api->getSites();
	var_dump($get_sites_response);

} catch (ApiException $e) {
	echo ($e->getMessage());
}

仓库API

用于获取仓库列表(仓库)的API。

查询可用仓库

$warehouses_api = new WarehousesApi($api_client);

try {

	$get_warehouses_response = $warehouses_api->getWarehouses();
	var_dump($get_warehouses_response);

} catch (ApiException $e) {
	echo ($e->getMessage());
}