Aliexpress API notes. Export of orders to Bitrix24, RetailCRM, amoCRM

I want to share my experience in automating the export of orders from Aliexpress to several CRMs. The examples given are written in PHP, but there are libraries for working with Aliexpress for other languages ​​as well. The structure of requests and responses is similar. If you are faced with the task of integrating Aliexpress, I hope this article will be useful to you.





So, let's say you already have a store on Aliexpress. And you have chosen the menu item "Developers". At the time of publication, there were glitches in the localization of the developer's console, perhaps they will be fixed in the future, but if already created applications are not displayed in the console or entering the console from the seller's office does not work, there are a couple of tips:





  1. Follow the link https://seller.aliexpress.ru/login?return_url=https%3A%2F%2Fseller.aliexpress.ru This will fix a glitch with a redirect to the Chinese version of the console, in which it is impossible to create an application





  2. , . , - , . .. , . , «main account not auth subAccount»





SDK . - App Key App Secret. – access_token . . App Key, App Secret access_token, SDK, PHP-, appkey, secretKey sessionKey. , access_token :





  1. . .. , Aliexpress CRM. Aliexpress, . code, curl https://oauth.aliexpress.com/token access_token





  2. https://oauth.aliexpress.com/authorize. access_token





. , access_token – 1 . . «testing». - 1 . , AliPay ( ). , .. – , , , , .. . Aliexpress 2020 .





. , - .





Selenium , .. access_token .





, Ailexpress , CRM.





include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai'); 
$c = new TopClient;
$c->appkey = 'xxxxxxxxxxx';
$c->secretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$sessionKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$i = 1;
$req = new AliexpressSolutionProductListGetRequest;
$aeop_a_e_product_list_query = new ItemListQuery;
$aeop_a_e_product_list_query->current_page=$i;
$aeop_a_e_product_list_query->product_status_type="onSelling";
$req->setAeopAEProductListQuery(json_encode($aeop_a_e_product_list_query));
$rez = $c->execute($req, $sessionKey);
var_dump($rez);
      
      



$rez . $i. 1, , $rez->result->total_page . . – . . . , . id ( , )





foreach($rez->result->aeop_a_e_product_display_d_t_o_list->item_display_dto as $item){  
    echo $item->product_id."\t".$item->subject."\n";
}
      
      



(, ..), id , . .





$req = new AliexpressSolutionProductInfoGetRequest;
$req->setProductId($id);
$rez = $c->execute($req, $sessionKey);
var_dump ($rez);
      
      



, , .





foreach($rez->result->aeop_ae_product_s_k_us->global_aeop_ae_product_sku as $sku){  
    echo $sku ->id."\t".$sku ->sku_code."\n";
}
      
      



c . , , API . , , , .





. , , Product code . . , , , Product code.





. CRM.





$req = new AliexpressSolutionOrderGetRequest;
$param0 = new OrderQuery;
$param0->create_date_end="2221-12-31 12:12:12";
$param0->create_date_start="2021-03-30 12:12:12";
$param0->order_status_list = array("PLACE_ORDER_SUCCESS","IN_CANCEL","WAIT_SELLER_SEND_GOODS","SELLER_PART_SEND_GOODS","WAIT_BUYER_ACCEPT_GOODS","FUND_PROCESSING","IN_ISSUE","IN_FROZEN","WAIT_SELLER_EXAMINE_MONEY","RISK_CONTROL", "FINISH");
$param0->current_page=$i;
$req->setParam0(json_encode($param0));
$rez = $c->execute($req, $sessionKey);
      
      



, , . order_status_list. , . , , , .





. «RU shopper» .., , . , . , Aliexpress, .





, . , Aliexpress , - « ». . , Aliexpress, , , 20 .





$req = new AliexpressSolutionOrderInfoGetRequest;
$param1 = new OrderDetailQuery;
$param1->ext_info_bit_flag="11111";
$param1->order_id=$order;
$req->setParam1(json_encode($param1));
$rez = $c->execute($req, $sessionKey);
$n = str_replace("'","''",$rez->result->data->receipt_address->contact_person); //    
$p = $rez->result->data->receipt_address->phone_country . $rez->result->data->receipt_address->mobile_no;
//$p = normalize_phone($p) //         ..
$zi = $rez->result->data->receipt_address->zip;
$gor = $rez->result->data->receipt_address->city;
$pro = $rez->result->data->receipt_address->province;
$adr = $rez->result->data->receipt_address->detail_address;
if(isset ($rez->result->data->receipt_address->address2)) $adr .=$rez->result->data->receipt_address->address2;
foreach($rez->result->data->child_order_ext_info_list->global_aeop_tp_order_product_info_dto as $item){
    $pid = $item->product_id;
    $j=json_decode($item->sku);
    $s = "";
    if(isset ($j->sku[0])) {
        $s = $j->sku[0]->pValueId;
    }
    $cn =$item->quantity;
    $pr =$item->unit_price->amount ;
    echo $pid."\t". $s." \t " .$cn ." \t ".$pr."\n";
}
      
      



, Aliexpress .





, . CRM . , . , Aliexpress, - , .





I took them from my projects and untied them from integrations (including Aliexpress). Each example creates a contact, then a lead / deal, assigns a contact to it and adds one product. I will gladly help you with the integration for a small fee if you decide to start selling on Aliexpress. Moreover, the time for vacations is approaching and additional money will not interfere with either me or you. Mail for communication tlx {dog} list.ru. There is extensive experience in integrating fulfillment, telephony and other APIs with various CRMs and general automation skills.





amoCRM
$subdomain = "xxxxxxxxx";	
function amo_call($access_token, $link, $data) {
	$headers = [
		'Authorization: Bearer ' . $access_token
	];
	$curl = curl_init(); //   cURL
	curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl,CURLOPT_USERAGENT,'amoCRM-oAuth-client/1.0');
	curl_setopt($curl,CURLOPT_URL, $link);
	curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'POST');
	curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($data));
	curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl,CURLOPT_HEADER, false);
	curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, 1);
	curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, 2);
	$out = curl_exec($curl); //   API     
	$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
	curl_close($curl);
	$code = (int)$code;
	$errors = [
		400 => 'Bad request',
		401 => 'Unauthorized',
		403 => 'Forbidden',
		404 => 'Not found',
		500 => 'Internal server error',
		502 => 'Bad gateway',
		503 => 'Service unavailable',
	];

	try
	{
		if ($code < 200 || $code > 204) {
			return  -1;
		}
	}
	catch(\Exception $e)
	{
		return  -1;
	}

	$response = json_decode($out, true);

	return  $response;
}

	
/*	
 //    $access_token.   ,    , . 
 
$link = 'https://' . $subdomain . '.amocrm.ru/oauth2/access_token'; // URL  

$data = [
	'client_id' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
	'client_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
	'grant_type' => 'authorization_code',
	'code' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
	'redirect_uri' => 'http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
];

$curl = curl_init(); //   cURL
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_USERAGENT,'amoCRM-oAuth-client/1.0');
curl_setopt($curl,CURLOPT_URL, $link);
curl_setopt($curl,CURLOPT_HTTPHEADER,['Content-Type:application/json']);
curl_setopt($curl,CURLOPT_HEADER, false);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, 2);

$out = curl_exec($curl); //   API     
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$code = (int)$code;
$errors = [
	400 => 'Bad request',
	401 => 'Unauthorized',
	403 => 'Forbidden',
	404 => 'Not found',
	500 => 'Internal server error',
	502 => 'Bad gateway',
	503 => 'Service unavailable',
];

try
{
	if ($code < 200 || $code > 204) {
		throw new Exception(isset($errors[$code]) ? $errors[$code] : 'Undefined error', $code);
	}
}
catch(\Exception $e)
{
	die(": ' . $e->getMessage() . PHP_EOL . ' : ' . $e->getCode());
}
$response = json_decode($out, true);
var_dump($response);
$access_token = $response['access_token']; //Access 
$refresh_token = $response['refresh_token']; //Refresh 
$token_type = $response['token_type']; // 
$expires_in = $response['expires_in']; //    

*/
$access_token ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";


//  .           .  /api/v4/contacts    filter[custom_fields_values]	


// 

$p ="+79000000000";
$n= " ";
$qnt = 1;
$ord = "xxxxxxxxxxxxxxxxx";

$link='https://'.$subdomain.'.amocrm.ru/api/v2/contacts';
$data = array (
	'add' =>
	array (		
		0 =>
			array (
			'name' => $n,
				'custom_fields'=>array(
					array(
					'id' => '953127',
						'values' => array(
							array(
									"value"=>$p,
									"enum"=> "MOB"
							),
						),
					),
			),								
		) ,
	) ,	  
) ;

$response = amo_call($access_token, $link, $data);
if ($response==-1) {
	die("-       /n");
} else {
	var_dump($response);
	$cid = $response["_embedded"]["items"][0]["id"];

	//         
	$pipeline_id = '4188580';
	$lead_status_id = '39384853';
	$lead_name  ="  Aliexpress â„– " . $ord;
	$link='https://'.$subdomain.'.amocrm.ru/api/v2/leads';
	$data = array (
	'add' =>
	  array (
		0 =>
		array (
			'name' => $lead_name,
			'status_id' => $lead_status_id, //id 
			'pipeline_id' => $pipeline_id,
			'contacts_id'=>  array (
				$cid
			 ),			
			), 
		),
	);

	$response = amo_call($access_token, $link, $data);
	if ($response==-1) {
		die("-       /n");
	} else {	
		$did = $response["_embedded"]["items"][0]["id"];

		var_dump($response);

		//    
		$link='https://'.$subdomain.'.amocrm.ru/api/v4/leads/'.$did.'/link';
		$data = array (	
			0 =>array (
						'to_entity_id' => 327219, //id     api   html    (   )
						'to_entity_type' =>'catalog_elements',
						'metadata' => array(
								"quantity" =>  $qnt,
								"catalog_id" =>  5321 // id ,        
						),			
					),			
		);				
		$response = amo_call($access_token, $link, $data);
		if ($response==-1) {
			die("-         /n");
		} else {		
			var_dump($response);
			//    
		}
}
      
      



Bitrix24
function b24_call($queryUrl, $queryData) {
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_SSL_VERIFYPEER => 0,
		CURLOPT_POST => 1,
		CURLOPT_HEADER => 0,
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_URL => $queryUrl,
		CURLOPT_POSTFIELDS => $queryData,
	));
	$out =  curl_exec($curl);
	$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);	
	if ($code < 200 || $code > 204) {
		return  -1;
	}	
	
	curl_close($curl);
	$response = json_decode($out, true);
	return  $response;
}

$p ="+79000000000";
$n= " ";
$zi = "111111";
$pro = " ";
$gor = "";
$adr  = ".   1";
$qnt = 1;
$ord = "xxxxxxxxxxxxxxxxx";

//       crm.contact.list   array('filter' => array('PHONE' => $ph))

$queryUrl  = 'https://xxxxxxxxxx.ru/rest/48/xxxxxxxxxxxxxxxxx/crm.contact.add.json';
$queryData = http_build_query(array(
	'fields' => array(
		"NAME"=> $n, 
		"PHONE"  => array(array('VALUE' =>$p, 'VALUE_TYPE' => 'MOBILE')),
	),
	'params' => array("REGISTER_SONET_EVENT" => "N")
));

$response= b24_call($queryUrl, $queryData);
    
if ($response==-1) {
	die("-       /n");
} else {			
	var_dump($response);
	$cid = $response["result"];	
	echo "!".$cid."!\n";
	$queryUrl  = 'https://xxxxxxxxxx.ru/rest/48/xxxxxxxxxxxxxxxxx/crm.requisite.add.json';
	$queryData = http_build_query(array(
		'fields'=> array(
			"PRESET_ID"=> 3,
			"ENTITY_TYPE_ID"=> 3,
			"ENTITY_ID"=> $cid,
			"NAME"=>"",
			"ACTIVE"=>"Y",
			"SORT"=>100
		)
	));	
	$response= b24_call($queryUrl, $queryData);
	if ($response==-1) {
		die("-       /n");
	} else {		
		var_dump($response);	
		$rid = $response["result"];
		$queryUrl  = 'https://xxxxxxxxxx.ru/rest/48/xxxxxxxxxxxxxxxxx/crm.address.add.json';
		$queryData = http_build_query(array(
			'fields'=> array(
				"TYPE_ID"=> 1,
				"ENTITY_TYPE_ID"=> 8,
				"ENTITY_ID"=> $rid,
				"POSTAL_CODE"=> "$zi",
				 "PROVINCE"=> "$pro",
				"CITY"=> "$gor",
				"ADDRESS_1"=> "$adr",
				"COUNTRY"=>  ""
			)
		));
		$response= b24_call($queryUrl, $queryData);
		if ($response==-1) {
			die("-         /n");
		} else {
			var_dump($response);				
			$lead_name = "  Aliexpress â„– " . $ord;
			$queryUrl  = 'https://xxxxxxxxxx.ru/rest/48/xxxxxxxxxxxxxxxxx/crm.lead.add.json';
			$queryData = http_build_query(array(
				'fields' => array(
					"TITLE" => $lead_name,
					"TYPE_ID" => "GOODS",
					"STAGE_ID" => "NEW",
					"CONTACT_ID"=> $cid,
					"CURRENCY_ID"=>  "RUB",
				),
			'params' => array("REGISTER_SONET_EVENT" => "Y")
			));
			$response= b24_call($queryUrl, $queryData);
			if ($response==-1) {
				die("-       /n");
			} else {
				var_dump($response);	
				$did = $response["result"];
				
				$items = array();
				$items[] =  array('PRODUCT_ID' => "2968", 'PRICE' => "1.00", 'QUANTITY' => $qnt);					
				$queryUrl  = 'https://xxxxxxxxxx.ru/rest/48/xxxxxxxxxxxxxxxxx/crm.lead.productrows.set.json';
				$queryData = http_build_query(array(
					"id" => $did,
					"rows"=>$items
				));
				$response= b24_call($queryUrl, $queryData);
				if ($response==-1) {
					die("-         /n");
				} else {
					var_dump($response);		
					//    				
				}								
			}				
		}
	}	
}

      
      



RetailCRM
$crmKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

function retail_call($url, $postData) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($curl, CURLOPT_FAILONERROR, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);            
	$out =  curl_exec($curl);
	$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);	
	if ($code < 200 || $code > 204) {
		return  -1;
	}		
	curl_close($curl);
	$response = json_decode($out, true);
	return  $response;
}

$p ="+79000000000";
$n= " ";
$zi = "111111";
$pro = " ";
$gor = "";
$adr  = ".   1";
$qnt = 1;
$ord = "xxxxxxxxxxxxxxxxx";

$url  = 'https://xxxxxxxxxx..ru/api/v5/customers/create';
$postData = array(
	'site' =>'xxxxxxxxxx.ru',
	'customer' => json_encode(array(
		'firstName' =>$n,
	)),
	'apiKey' => $crmKey,
);

$rez = retail_call($url, $postData);
var_dump($rez);
$cid = $rez["id"];

$url  = 'https://xxxxxxxxxx.ru/api/v5/orders/create';
$postData = array(
	'site' =>'xxxxxxxxxx.ru',
	'order' => json_encode(array(
		'customer'=> array(
		'id' => $cid,
	),    
	'items' => array(
		array(
			'quantity' => 6,
			'offer'=> array(
				'externalId' => 'xxxxxxxxxxx',
			)
		),
		))),
	'apiKey' => $crmKey,
);
$rez = retail_call($url, $postData);
var_dump($rez);

echo "\n".$rez["id"]."\n";

      
      



Selenium (for getting access_token)
from selenium import webdriver
from time import sleep
browser = webdriver.Firefox()
browser.get("https://seller.aliexpress.ru/")
sleep(5)
browser.switch_to.frame(browser.find_element_by_css_selector("iframe.iframe-with-loader_iframe__QQc_0"))
e = browser.find_element_by_id("fm-login-id")
e.send_keys("xxxxxx@xxxxxxxx.ru")
e = browser.find_element_by_id("fm-login-password")
e.send_keys("xxxxxxxxxxxxx")
e = browser.find_element_by_id("fm-login-submit")
e.click()
sleep(15)
browser.get("https://oauth.aliexpress.com/authorize?&response_type=token&client_id=XXXXXXXX&state=1212&view=web&sp=ae")
sleep(5)
try:
    e = driver.find_element_by_id("sub")
    e.click()
except Exception:
    print('Session login')

sleep(5)
txt = "-1"
try:
    e = driver.find_element_by_id("wrap")
    print(e.get_attribute('innerHTML'))
    txt = e.get_attribute('innerHTML')
except Exception:
    print('Session login')


x = txt.split("access_token: ")
y = x[1].split("<br>")
print y[0]
      
      






All Articles