1724 lines
76 KiB
Java
1724 lines
76 KiB
Java
package it.acxent.api.amz;
|
|
|
|
import it.acxent.art.AmzFeaturedPrice;
|
|
import it.acxent.art.Articolo;
|
|
import it.acxent.art.ArticoloCR;
|
|
import it.acxent.cc.Attivita;
|
|
import it.acxent.common.StatusMsg;
|
|
import it.acxent.db.ApplParm;
|
|
import it.acxent.db.ApplParmFull;
|
|
import it.acxent.db.DBAdapter;
|
|
import it.acxent.db.ResParm;
|
|
import it.acxent.util.DoubleOperator;
|
|
import it.acxent.util.StringTokenizer;
|
|
import it.acxent.util.Vectumerator;
|
|
import java.sql.Timestamp;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.TreeMap;
|
|
import org.apache.http.Consts;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.NameValuePair;
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpDelete;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPut;
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
public class AmzSellerApi {
|
|
private Attivita attivita;
|
|
|
|
public AmzSellerApi(String lwaClientId, String lwaClientSecret, String lwaAuthToken) {
|
|
this.attivita = this.attivita;
|
|
this.lwaClientId = lwaClientId;
|
|
this.lwaClientSecret = lwaClientSecret;
|
|
this.lwaAuthToken = lwaAuthToken;
|
|
}
|
|
|
|
private boolean debug = false;
|
|
|
|
private ApplParmFull apFull = null;
|
|
|
|
private String lwaClientId;
|
|
|
|
private String lwaClientSecret;
|
|
|
|
private String lwaAuthToken;
|
|
|
|
private String lwaAccessToken;
|
|
|
|
private String lwaRefreshToken;
|
|
|
|
private Timestamp lwaAccessTokenExpireTS = null;
|
|
|
|
private String iamRoleARN;
|
|
|
|
private String iamAccessKey;
|
|
|
|
private String iamSecretKey;
|
|
|
|
private String stsAccessKeyId;
|
|
|
|
private String stsSecretAccessKey;
|
|
|
|
private String stsSessionToken;
|
|
|
|
private Timestamp stsSessionTokenTS = null;
|
|
|
|
private HashMap<String, String> hmMarketplaces;
|
|
|
|
public static final String P_AMZ_LWA_CLIENT_ID = "AMZ_LWA_CLIENT_ID";
|
|
|
|
public static final String P_AMZ_LWA_CLIENT_SECRET = "AMZ_LWA_CLIENT_SECRET";
|
|
|
|
public static final String P_AMZ_LWA_AUTH_TOKEN = "AMZ_LWA_AUTH_TOKEN";
|
|
|
|
public static final String P_AMZ_IAM_ROLE_ARN = "AMZ_IAM_ROLE_ARN";
|
|
|
|
public static final String P_AMZ_IAM_ACCESS_KEY = "AMZ_IAM_ACCESS_KEY";
|
|
|
|
public static final String P_AMZ_IAM_SECRET_KEY = "AMZ_IAM_SECRET_KEY";
|
|
|
|
public static final String P_AMZ_LWA_REFRESH_TOKEN = "AMZ_LWA_REFRESH_TOKEN";
|
|
|
|
public static final String P_AMZ_LWA_REFRESH_TOKEN_TS = "AMZ_LWA_REFRESH_TOKEN_TS";
|
|
|
|
public static final String P_AMZ_STS_ACCESS_KEY_ID = "AMZ_STS_ACCESS_KEY_ID";
|
|
|
|
public static final String P_AMZ_STS_SECRET_ACCESS_KEY = "AMZ_STS_SECRET_ACCESS_KEY";
|
|
|
|
public static final String P_AMZ_STS_ACCESS_TOKEN = "AMZ_STS_ACCESS_TOKEN";
|
|
|
|
public static final String P_AMZ_STS_ACCESS_TOKEN_TS = "AMZ_STS_ACCESS_TOKEN_TS";
|
|
|
|
private static final String URI_CMD_LWA_ACCESS_TOKEN = "https://api.amazon.com/auth/o2/token";
|
|
|
|
private static final String URI_CMD_STS_TEMPORARY_TOKEN = "https://sts.amazonaws.com";
|
|
|
|
private static final String URI_CMD_MARKETPLACES_HOST = "https://sellingpartnerapi-eu.amazon.com";
|
|
|
|
private static final String URI_CMD_MARKETPLACES_ID_ENDPOINT = "/sellers/v1/marketplaceParticipations";
|
|
|
|
private static final String URI_CMD_LISTINGS_HOST = "https://sellingpartnerapi-eu.amazon.com";
|
|
|
|
private static final String URI_CMD_LISTINGS_ENDPOINT = "/listings/2021-08-01/items/:sellerId/:sku";
|
|
|
|
private static final String URI_CMD_PRODUCT_TYPE_DEFINITION_HOST = "https://sellingpartnerapi-eu.amazon.com";
|
|
|
|
private static final String URI_CMD_PRODUCT_TYPE_DEFINITION_ENDPOINT = "/definitions/2020-09-01/productTypes/:productType";
|
|
|
|
private static final String URI_CMD_GET_CATALOG_ITEMS_HOST = "https://sellingpartnerapi-eu.amazon.com";
|
|
|
|
private static final String URI_CMD_GET_CATALOG_ITEMS_ENDPOINT = "/catalog/2022-04-01/items";
|
|
|
|
private static final String URI_CMD_GET_PRICING_V0_ITEMS_HOST = "https://sellingpartnerapi-eu.amazon.com";
|
|
|
|
private static final String URI_CMD_GET_PRICING_V0_BATCH_ITEMS_HOST = "/batches/products/pricing/v0/itemOffers";
|
|
|
|
private static final String URI_CMD_GET_PRICING_V0_LOWEST_ASIN_ENDPOINT = "/products/pricing/v0/items/:Asin/offers";
|
|
|
|
private static final String URI_CMD_GET_PRICING_ITEMS_HOST = "https://sellingpartnerapi-eu.amazon.com";
|
|
|
|
private static final String URI_CMD_GET_PRICING_ITEMS_ENDPOINT = "/batches/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice";
|
|
|
|
public AmzSellerApi(Attivita attivita) {
|
|
this.apFull = attivita.getApFull();
|
|
this.lwaClientId = attivita.getAmzLwaClientId();
|
|
this.lwaClientSecret = attivita.getAmzLwaClientSecret();
|
|
this.lwaAuthToken = attivita.getAmzLwaAuthToken();
|
|
this.lwaAccessToken = attivita.getAmzLwaAccessToken();
|
|
this.lwaAccessTokenExpireTS = attivita.getAmzLwaAccessTokenExpireTS();
|
|
this.iamAccessKey = attivita.getAmzIamAccessKey();
|
|
this.iamRoleARN = attivita.getAmzIamRoleARN();
|
|
this.iamSecretKey = attivita.getAmzIamSecretKey();
|
|
this.stsAccessKeyId = attivita.getAmzStsAccessKeyId();
|
|
this.stsSecretAccessKey = attivita.getAmzStsSecretAccessKey();
|
|
this.stsSessionToken = attivita.getAmzStsSessionToken();
|
|
this.stsSessionTokenTS = attivita.getAmzStsSessionTokenTS();
|
|
StringTokenizer st = new StringTokenizer(attivita.getAmzMarketplaces(), ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
String currentId = st.nextToken();
|
|
getHmMarketplaces().put(currentLang, currentId);
|
|
}
|
|
}
|
|
|
|
public AmzSellerApi() {}
|
|
|
|
public static void initApplicationParms(ApplParmFull ap) {
|
|
if (ap != null) {
|
|
DBAdapter.logDebug(true, "AMAZON initParms: start");
|
|
DBAdapter.logDebug(true, "AMAZON initParms: stop");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
String hostname = "localhost:3308";
|
|
String db = "cc";
|
|
ApplParmFull ap = new ApplParmFull(new ApplParm(3, "//" + hostname + "/" + db, db, "root", "root", 1, 10, 300));
|
|
AmzSellerApi bean = new AmzSellerApi(Attivita.getDefaultInstance(ap));
|
|
Articolo articolo = new Articolo(ap);
|
|
articolo.findByCodiceEanSerie("8714574523347", null);
|
|
ArticoloCR CR = new ArticoloCR();
|
|
CR.setId_articolo(articolo.getId_articolo());
|
|
JSONObject item = bean.getJsonOfferByArticolo(articolo, "it");
|
|
System.out.println("json amz item:\n" + item.toString(4));
|
|
AmzResult res = bean.amzPutListingsItem(articolo, "it");
|
|
System.out.println(res.getMsg() + "\n" + res.getMsg());
|
|
}
|
|
|
|
public ApplParmFull getApFull() {
|
|
return this.apFull;
|
|
}
|
|
|
|
public void setApFull(ApplParmFull apFull) {
|
|
this.apFull = apFull;
|
|
}
|
|
|
|
public Attivita getAttivita() {
|
|
if (this.attivita == null)
|
|
this.attivita = Attivita.getDefaultInstance(getApFull());
|
|
return this.attivita;
|
|
}
|
|
|
|
public AmzResult amzGetStsAwsCredential() {
|
|
AmzResult resAR = amzCheckLwaTokens();
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzGetStsAwsCredential");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sts.amazonaws.com".replace("https://", "");
|
|
String endPoint = "/";
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("Version", "2011-06-15");
|
|
queryParms.put("Action", "AssumeRole");
|
|
queryParms.put("RoleSessionName", "_sName" + DBAdapter.getNow().getTime());
|
|
queryParms.put("RoleArn", getIamRoleARN());
|
|
HttpGet request = new HttpGet("https://sts.amazonaws.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("host", host);
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getIamAccessKey(), getIamSecretKey()).regionName("us-east-1").serviceName("sts")
|
|
.httpMethodName("GET")
|
|
.canonicalURI(endPoint)
|
|
.queryParametes(queryParms)
|
|
.awsHeaders(awsHeaders)
|
|
.payload(null)
|
|
|
|
.build();
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
request.setHeader("Accept", "application/json");
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
JSONObject jo = new JSONObject(content);
|
|
resAR.setMsg(jo.toString(2));
|
|
resAR.setResult(jo);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
JSONObject joCredential = jo.getJSONObject("AssumeRoleResponse").getJSONObject("AssumeRoleResult")
|
|
.getJSONObject("Credentials");
|
|
setStsAccessKeyId(joCredential.getString("AccessKeyId"));
|
|
setStsSecretAccessKey(joCredential.getString("SecretAccessKey"));
|
|
setStsSessionToken(joCredential.getString("SessionToken"));
|
|
Calendar cal = Calendar.getInstance();
|
|
cal.add(12, 59);
|
|
Timestamp ts = new Timestamp(cal.getTime().getTime());
|
|
if (this.debug)
|
|
System.out.println("Token expires on " + ts.toString());
|
|
setStsSessionTokenTS(ts);
|
|
getAttivita().setAmzStsAccessKeyId(getStsAccessKeyId());
|
|
getAttivita().setAmzStsSecretAccessKey(getStsSecretAccessKey());
|
|
getAttivita().setAmzStsSessionToken(getStsSessionToken());
|
|
getAttivita().setAmzStsSessionTokenTS(getStsSessionTokenTS());
|
|
getAttivita().save();
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("SessionToken found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(getStsSessionToken());
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
private String getInlineQueryParms(TreeMap<String, String> queryParms) {
|
|
if (queryParms.size() == 0)
|
|
return "";
|
|
String delim = "?";
|
|
StringBuilder sbParms = new StringBuilder();
|
|
for (Map.Entry<String, String> entry : queryParms.entrySet()) {
|
|
sbParms.append(delim);
|
|
sbParms.append(entry.getKey());
|
|
sbParms.append("=");
|
|
sbParms.append(entry.getValue());
|
|
delim = "&";
|
|
}
|
|
return sbParms.toString();
|
|
}
|
|
|
|
public String getLwaClientId() {
|
|
return (this.lwaClientId == null) ? "" : this.lwaClientId.trim();
|
|
}
|
|
|
|
public void setLwaClientId(String googleApiKey) {
|
|
this.lwaClientId = googleApiKey;
|
|
}
|
|
|
|
public String getLwaClientSecret() {
|
|
return this.lwaClientSecret;
|
|
}
|
|
|
|
public void setLwaClientSecret(String lwaClientSecret) {
|
|
this.lwaClientSecret = lwaClientSecret;
|
|
}
|
|
|
|
public String getLwaAuthToken() {
|
|
return this.lwaAuthToken;
|
|
}
|
|
|
|
public void setLwaAuthToken(String lwaAuthToken) {
|
|
this.lwaAuthToken = lwaAuthToken;
|
|
}
|
|
|
|
public String getLwaAccessToken() {
|
|
return (this.lwaAccessToken == null) ? "" : this.lwaAccessToken.trim();
|
|
}
|
|
|
|
public void setLwaAccessToken(String lwaAccessToken) {
|
|
this.lwaAccessToken = lwaAccessToken;
|
|
}
|
|
|
|
public String getLwaRefreshToken() {
|
|
return this.lwaRefreshToken;
|
|
}
|
|
|
|
public void setLwaRefreshToken(String lwaRefreshToken) {
|
|
this.lwaRefreshToken = lwaRefreshToken;
|
|
}
|
|
|
|
public Timestamp getLwaAccessTokenExpireTS() {
|
|
return this.lwaAccessTokenExpireTS;
|
|
}
|
|
|
|
public void setLwaAccessTokenExpireTS(Timestamp lwaAccessTokenExpireTS) {
|
|
this.lwaAccessTokenExpireTS = lwaAccessTokenExpireTS;
|
|
}
|
|
|
|
public String getStsAccessKeyId() {
|
|
return this.stsAccessKeyId;
|
|
}
|
|
|
|
public void setStsAccessKeyId(String stsAccessKeyId) {
|
|
this.stsAccessKeyId = stsAccessKeyId;
|
|
}
|
|
|
|
public String getStsSecretAccessKey() {
|
|
return this.stsSecretAccessKey;
|
|
}
|
|
|
|
public void setStsSecretAccessKey(String stsSecretAccessKey) {
|
|
this.stsSecretAccessKey = stsSecretAccessKey;
|
|
}
|
|
|
|
public String getStsSessionToken() {
|
|
return (this.stsSessionToken == null) ? "" : this.stsSessionToken.trim();
|
|
}
|
|
|
|
public void setStsSessionToken(String stsSessionToken) {
|
|
this.stsSessionToken = stsSessionToken;
|
|
}
|
|
|
|
public Timestamp getStsSessionTokenTS() {
|
|
return this.stsSessionTokenTS;
|
|
}
|
|
|
|
public void setStsSessionTokenTS(Timestamp stsSessionTokenTS) {
|
|
this.stsSessionTokenTS = stsSessionTokenTS;
|
|
}
|
|
|
|
public String getIamRoleARN() {
|
|
return this.iamRoleARN;
|
|
}
|
|
|
|
public void setIamRoleARN(String iamRoleARN) {
|
|
this.iamRoleARN = iamRoleARN;
|
|
}
|
|
|
|
public String getIamAccessKey() {
|
|
return this.iamAccessKey;
|
|
}
|
|
|
|
public void setIamAccessKey(String iamAccessKey) {
|
|
this.iamAccessKey = iamAccessKey;
|
|
}
|
|
|
|
public String getIamSecretKey() {
|
|
return this.iamSecretKey;
|
|
}
|
|
|
|
public void setIamSecretKey(String iamSecretKey) {
|
|
this.iamSecretKey = iamSecretKey;
|
|
}
|
|
|
|
public AmzResult amzPostLwaAccessToken() {
|
|
AmzResult resAR = new AmzResult();
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzGetLwaAccessToken");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
HttpPost request = new HttpPost("https://api.amazon.com/auth/o2/token");
|
|
request.setHeader("Accept", "application/json");
|
|
request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
request.setHeader("Content-Language", "it-IT");
|
|
List<NameValuePair> form = new ArrayList<>();
|
|
form.add(new BasicNameValuePair("client_id", getLwaClientId()));
|
|
form.add(new BasicNameValuePair("client_secret", getLwaClientSecret()));
|
|
form.add(new BasicNameValuePair("grant_type", "refresh_token"));
|
|
form.add(new BasicNameValuePair("refresh_token", getLwaAuthToken()));
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
|
|
request.setEntity((HttpEntity)entity);
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
JSONObject jo = new JSONObject(content);
|
|
resAR.setMsg(jo.toString(2));
|
|
resAR.setResult(jo);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
setLwaAccessToken(jo.getString("access_token"));
|
|
setLwaRefreshToken(jo.getString("refresh_token"));
|
|
long expiresSecs = jo.getLong("expires_in");
|
|
long expiresMils = (expiresSecs - 60L) * 1000L;
|
|
Calendar cal = Calendar.getInstance();
|
|
Timestamp ts = new Timestamp(cal.getTime().getTime() + expiresMils);
|
|
if (this.debug)
|
|
System.out.println("Token expires on " + ts.toString());
|
|
setLwaAccessTokenExpireTS(ts);
|
|
getAttivita().setAmzLwaAccessToken(getLwaAccessToken());
|
|
getAttivita().setAmzLwaRefreshToken(getLwaRefreshToken());
|
|
getAttivita().setAmzLwaAccessTokenExpireTS(getLwaAccessTokenExpireTS());
|
|
getAttivita().save();
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("access_token found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(getLwaAccessToken());
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public boolean isLwaAccessTokenExpired() {
|
|
if (getLwaAccessToken().isEmpty())
|
|
return true;
|
|
if (getLwaAccessTokenExpireTS() == null)
|
|
return true;
|
|
if (DBAdapter.getTimeDiff(DBAdapter.getTimestamp(), getLwaAccessTokenExpireTS()) <= 0L)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
public boolean isStsSessionTokenExpired() {
|
|
if (isLwaAccessTokenExpired())
|
|
return true;
|
|
if (getStsSessionToken().isEmpty())
|
|
return true;
|
|
if (getStsSessionTokenTS() == null)
|
|
return true;
|
|
if (DBAdapter.getTimeDiff(DBAdapter.getTimestamp(), getStsSessionTokenTS()) <= 0L)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
public HashMap<String, String> getHmMarketplaces() {
|
|
if (this.hmMarketplaces == null)
|
|
this.hmMarketplaces = new HashMap<>();
|
|
return this.hmMarketplaces;
|
|
}
|
|
|
|
private String getMarketplacesIds(String marketplaceLangs) {
|
|
StringBuilder sb = new StringBuilder();
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
String theComma = "";
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
if (getHmMarketplaces().containsKey(currentLang)) {
|
|
sb.append(theComma);
|
|
sb.append(getHmMarketplaces().get(currentLang));
|
|
theComma = ",";
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
public void setHmMarketplaces(HashMap<String, String> hmMarketplaces) {
|
|
this.hmMarketplaces = hmMarketplaces;
|
|
}
|
|
|
|
public String getMatketplaceId(String countryCode) {
|
|
if (getHmMarketplaces().containsKey(countryCode))
|
|
return getHmMarketplaces().get(countryCode);
|
|
return "";
|
|
}
|
|
|
|
public AmzResult amzGetCatalogItems(Articolo articolo, String marketplaceLangs) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLangs);
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzGetCatalogItems");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/catalog/2022-04-01/items";
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("marketplaceIds", getMarketplacesIds(marketplaceLangs));
|
|
queryParms.put("identifiers", articolo.getCodiceEan());
|
|
queryParms.put("identifiersType", "EAN");
|
|
queryParms.put("includedData", "productTypes,summaries");
|
|
queryParms.put("locale", "it_IT");
|
|
queryParms.put("sellerId", getAttivita().getAmzSellerid());
|
|
HttpGet request = new HttpGet("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("Content-Type".toLowerCase(), "application/json");
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("GET").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(queryParms).payload(null)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
JSONArray jaItems = jo.getJSONArray("items");
|
|
if (jo.getLong("numberOfResults") > 0L) {
|
|
boolean asinTrovato = false;
|
|
for (int i = 0; i < jaItems.length(); i++) {
|
|
JSONObject itemI = jaItems.getJSONObject(i);
|
|
JSONObject summariesI = itemI.getJSONArray("summaries").getJSONObject(0);
|
|
String asinI = itemI.getString("asin");
|
|
String productTypeI = itemI.getJSONArray("productTypes").getJSONObject(0).getString("productType");
|
|
if (summariesI.has("modelNumber")) {
|
|
String modelNumberI = summariesI.getString("modelNumber");
|
|
if (articolo.getCodiceProduttore().toLowerCase().equals(modelNumberI.toLowerCase()) ||
|
|
articolo.getDescrizioneSearch().toLowerCase().contains(modelNumberI.toLowerCase())) {
|
|
String l_descAmz = asinI + " " + asinI + " " + modelNumberI + " " + summariesI.getString("brand");
|
|
if (articolo.getAsinAmz().isEmpty() || articolo.getProductTypeAmz().isEmpty() ||
|
|
!articolo.getDescAmz().equals(l_descAmz)) {
|
|
if (articolo.getAsinAmz().isEmpty())
|
|
articolo.setAsinAmz(asinI);
|
|
if (articolo.getProductTypeAmz().isEmpty())
|
|
articolo.setProductTypeAmz(productTypeI);
|
|
articolo.setDescAmz(l_descAmz);
|
|
articolo.setFlgAmzWarn(0L);
|
|
articolo.superSave();
|
|
}
|
|
asinTrovato = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (asinTrovato) {
|
|
resAR.setMsg("Item found: " + articolo.getDescAmz());
|
|
resAR.setOk(true);
|
|
resAR.setResult(jo);
|
|
} else {
|
|
resAR.setMsg("Item found but item MPN not found. Get first and warn!");
|
|
resAR.setOk(false);
|
|
resAR.setResult(jo);
|
|
JSONObject item0 = jaItems.getJSONObject(0);
|
|
String productType0 = item0.getJSONArray("productTypes").getJSONObject(0).getString("productType");
|
|
String asin = item0.getString("asin");
|
|
JSONObject summaries0 = item0.getJSONArray("summaries").getJSONObject(0);
|
|
String modelNumber0 = "";
|
|
if (summaries0.has("modelNumber"))
|
|
modelNumber0 = summaries0.getString("modelNumber");
|
|
String l_descAmz = "(!) " + asin + " " + modelNumber0 + " " + summaries0.getString("brand") + " " +
|
|
summaries0.getString("itemName");
|
|
articolo.setFlgAmzWarn(1L);
|
|
if (articolo.getAsinAmz().isEmpty())
|
|
articolo.setAsinAmz(asin);
|
|
if (articolo.getProductTypeAmz().isEmpty())
|
|
articolo.setProductTypeAmz(productType0);
|
|
articolo.setFlgAmazon(2L);
|
|
articolo.setDescAmz(l_descAmz);
|
|
articolo.superSave();
|
|
}
|
|
} else {
|
|
if (!articolo.getAsinAmz().isEmpty()) {
|
|
articolo.setAsinAmz("");
|
|
articolo.setProductTypeAmz("");
|
|
articolo.setDescAmz(articolo.getCodiceEanNoZero() + " NON TROVATO");
|
|
articolo.superSave();
|
|
}
|
|
resAR.setMsg("NO item in catalog for ean " + articolo.getCodiceEanNoZero());
|
|
resAR.setOk(false);
|
|
resAR.setResult(jo);
|
|
}
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
private AmzResult amzCheckTokensApi(String marketplaceLangs) {
|
|
AmzResult resAR = new AmzResult();
|
|
if (isStsSessionTokenExpired()) {
|
|
System.out.println("AmzSellerApi --> amzCheckTokensApi ");
|
|
resAR = amzGetStsAwsCredential();
|
|
}
|
|
if (resAR.isOk()) {
|
|
boolean merchantIdsOk = true;
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
if (!getHmMarketplaces().containsKey(currentLang)) {
|
|
merchantIdsOk = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!merchantIdsOk) {
|
|
resAR = amzGetMarketplaces();
|
|
st = new StringTokenizer(marketplaceLangs, ",");
|
|
merchantIdsOk = true;
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
if (!getHmMarketplaces().containsKey(currentLang)) {
|
|
merchantIdsOk = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!merchantIdsOk) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg("ERRORE! Non sono stati trovati tutti i merchantIds: " + marketplaceLangs + " trovati: " +
|
|
getAttivita().getAmzMarketplaces());
|
|
}
|
|
}
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
private AmzResult amzCheckLwaTokens() {
|
|
AmzResult resAR = new AmzResult();
|
|
if (isLwaAccessTokenExpired()) {
|
|
System.out.println("AmzSellerApi --> amzCheckLwaTokens");
|
|
resAR = amzPostLwaAccessToken();
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public AmzResult amzGetMarketplaces() {
|
|
AmzResult resAR = new AmzResult();
|
|
if (isStsSessionTokenExpired()) {
|
|
System.out.println("AmzSellerApi --> amzGetMarketplaces --> isStsSessionTokenExpired");
|
|
resAR = amzGetStsAwsCredential();
|
|
}
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzGetMarketplaces");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/sellers/v1/marketplaceParticipations";
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
HttpGet request = new HttpGet("https://sellingpartnerapi-eu.amazon.com" + endPoint);
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("GET").canonicalURI(endPoint).awsHeaders(awsHeaders).payload(null)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
JSONObject jo = new JSONObject(content);
|
|
resAR.setMsg(jo.toString(2));
|
|
resAR.setResult(jo);
|
|
} else {
|
|
if (getHmMarketplaces().size() > 0)
|
|
this.hmMarketplaces = new HashMap<>();
|
|
StringBuilder sbMPlace = new StringBuilder();
|
|
JSONObject jo = new JSONObject(content);
|
|
JSONArray joPayload = jo.getJSONArray("payload");
|
|
for (int i = 0; i < joPayload.length(); i++) {
|
|
JSONObject joMPlace = joPayload.getJSONObject(i).getJSONObject("marketplace");
|
|
getHmMarketplaces().put(joMPlace.getString("countryCode").toLowerCase(), joMPlace.getString("id"));
|
|
sbMPlace.append(joMPlace.getString("countryCode").toLowerCase());
|
|
sbMPlace.append(",");
|
|
sbMPlace.append(joMPlace.getString("id"));
|
|
if (i + 1 < joPayload.length())
|
|
sbMPlace.append(",");
|
|
}
|
|
getAttivita().setAmzMarketplaces(sbMPlace.toString());
|
|
getAttivita().save();
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("Marketplaces found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(getAttivita().getAmzMarketplaces());
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public AmzResult amzGetDefinitionProductType(Articolo articolo, String marketplaceLangs) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLangs);
|
|
if (resAR.isOk()) {
|
|
if (articolo.getProductTypeAmz().isEmpty())
|
|
resAR = amzGetListingsItem(articolo, marketplaceLangs);
|
|
if (articolo.getProductTypeAmz().isEmpty()) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg("ERRORE! Non riesco a trovare il product type per " + articolo.getCodiceEan() + " " + articolo.getNome());
|
|
}
|
|
}
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzGetDefinitionProductType");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/definitions/2020-09-01/productTypes/:productType".replace(":productType", articolo.getProductTypeAmz());
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("marketplaceIds", getMarketplacesIds(marketplaceLangs));
|
|
queryParms.put("productTypeVersion", "LATEST");
|
|
queryParms.put("requirements", "LISTING_OFFER_ONLY");
|
|
queryParms.put("requirementsEnforced", "ENFORCED");
|
|
queryParms.put("locale", "it_IT");
|
|
HttpGet request = new HttpGet("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("Content-Type".toLowerCase(), "application/json");
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("GET").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(queryParms).payload(null)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("Product Type JSON Schema found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(jo);
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
private JSONObject getJsonFeaturedOfferExpectedPriceRequestByArticoloCR(ArticoloCR CR, String marketplaceLangs) {
|
|
String URI_FOEP = "/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice";
|
|
String GET_METHOD = "GET";
|
|
JSONObject jo = new JSONObject();
|
|
JSONArray jaRequest = new JSONArray();
|
|
Articolo bean = new Articolo(getApFull());
|
|
Vectumerator<Articolo> vec = bean.findByCR(CR, 0, 0);
|
|
while (vec.hasMoreElements()) {
|
|
Articolo row = (Articolo)vec.nextElement();
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
JSONObject joRequest = new JSONObject();
|
|
joRequest.put("marketplaceId", getHmMarketplaces().get(currentLang));
|
|
joRequest.put("sku", row.getCodiceEanNoZero());
|
|
joRequest.put("method", "GET");
|
|
joRequest.put("uri", "/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice");
|
|
jaRequest.put(joRequest);
|
|
}
|
|
}
|
|
jo.put("requests", jaRequest);
|
|
return jo;
|
|
}
|
|
|
|
public AmzResult amzGetListingsItem(Articolo articolo, String marketplaceLangs) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLangs);
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzGetListingsItem");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/listings/2021-08-01/items/:sellerId/:sku".replace(":sellerId", getAttivita().getAmzSellerid()).replace(":sku",
|
|
articolo.getCodiceEanNoZero());
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("marketplaceIds", getMarketplacesIds(marketplaceLangs));
|
|
queryParms.put("issueLocale", "it_IT");
|
|
queryParms.put("includedData", "summaries,offers,attributes,issues,fulfillmentAvailability");
|
|
HttpGet request = new HttpGet("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("Content-Type".toLowerCase(), "application/json");
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("GET").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(queryParms).payload(null)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
JSONObject joSummaries0 = jo.getJSONArray("summaries").getJSONObject(0);
|
|
if (!articolo.getProductTypeAmz().equals(joSummaries0.getString("productType")) ||
|
|
!articolo.getAsinAmz().equals(joSummaries0.getString("asin"))) {
|
|
articolo.setProductTypeAmz(joSummaries0.getString("productType"));
|
|
articolo.setAsinAmz(joSummaries0.getString("asin"));
|
|
articolo.superSave();
|
|
}
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("Listing found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(jo);
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public AmzResult amzPutListingsItem(Articolo articolo, String marketplaceLangs) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLangs);
|
|
if (resAR.isOk()) {
|
|
if (articolo.getAsinAmz().isEmpty() || articolo.getProductTypeAmz().isEmpty())
|
|
resAR = amzGetCatalogItems(articolo, marketplaceLangs);
|
|
if (articolo.getAsinAmz().isEmpty() || articolo.getProductTypeAmz().isEmpty()) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg("ERRORE! Non riesco a trovare il codice asin o product type nel catalogo " + articolo.getCodiceEan() + " " +
|
|
articolo.getNome());
|
|
}
|
|
}
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzPutListingsItem");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/listings/2021-08-01/items/:sellerId/:sku".replace(":sellerId", getAttivita().getAmzSellerid()).replace(":sku",
|
|
articolo.getCodiceEanNoZero());
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("marketplaceIds", getMarketplacesIds(marketplaceLangs));
|
|
queryParms.put("issueLocale", "it_IT");
|
|
HttpPut request = new HttpPut("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
JSONObject item = getJsonOfferByArticolo(articolo, marketplaceLangs);
|
|
String body = item.toString();
|
|
StringEntity stringEntity = new StringEntity(body, "UTF-8");
|
|
request.setEntity((HttpEntity)stringEntity);
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("x-amz-content-sha256", AWSV4Auth.generateHex(body));
|
|
awsHeaders.put("Content-Type".toLowerCase(), "application/json");
|
|
awsHeaders.put("Accept".toLowerCase(), "application/json");
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("PUT").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(queryParms).payload(body)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
System.out.println(".");
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
if (jo.get("status").equals("ACCEPTED")) {
|
|
articolo.setQtaSuAmz(articolo.getQtaAmzDaInviare());
|
|
articolo.setPrezzoSuAmzIva(articolo.getPrezzoArticoloAmazonIvaExport("it", 1L));
|
|
articolo.setFlgAmzWarn(0L);
|
|
articolo.superSave();
|
|
resAR.setMsg("Articolo ean " + articolo.getCodiceEan() + " asin " + articolo.getAsinAmz() + " inserito/aggiornato");
|
|
resAR.setOk(true);
|
|
resAR.setResult(jo);
|
|
} else {
|
|
resAR.setMsg("Articolo ean " + articolo.getCodiceEan() + " asin " + articolo.getAsinAmz() + " NON AGGIORNATO: " +
|
|
jo.getJSONArray("issues").toString(2));
|
|
resAR.setOk(false);
|
|
resAR.setResult(jo);
|
|
articolo.setFlgAmzWarn(1L);
|
|
articolo.superSave();
|
|
System.out.println(jo.toString(4));
|
|
}
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public AmzResult amzDelListingsItem(Articolo articolo, String marketplaceLangs) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLangs);
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzDelListingsItem");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/listings/2021-08-01/items/:sellerId/:sku".replace(":sellerId", getAttivita().getAmzSellerid()).replace(":sku",
|
|
articolo.getCodiceEanNoZero());
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("marketplaceIds", getMarketplacesIds(marketplaceLangs));
|
|
queryParms.put("issueLocale", "it_IT");
|
|
HttpDelete request = new HttpDelete("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("Accept".toLowerCase(), "application/json");
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("DELETE").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(queryParms).payload(null)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
if (jo.get("status").equals("ACCEPTED")) {
|
|
articolo.setFlgAmazon(0L);
|
|
articolo.superSave();
|
|
resAR.setMsg("Articolo ean " + articolo.getCodiceEan() + " asin " + articolo.getAsinAmz() + " cancellato");
|
|
resAR.setOk(true);
|
|
resAR.setResult(jo);
|
|
} else {
|
|
resAR.setMsg("Articolo ean " + articolo.getCodiceEan() + " asin " + articolo.getAsinAmz() + " NON CANCELLATO: " +
|
|
jo.getJSONArray("issues").toString(2));
|
|
resAR.setOk(false);
|
|
resAR.setResult(jo);
|
|
articolo.setFlgAmzWarn(1L);
|
|
articolo.superSave();
|
|
System.out.println(jo.toString(4));
|
|
}
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("Listing found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(jo);
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public AmzResult amzPostItemsOffersBatch(ArticoloCR CR, String marketplaceLang) {
|
|
int pageRow = 20;
|
|
StringBuilder res = new StringBuilder();
|
|
boolean result = true;
|
|
Articolo bean = new Articolo(getApFull());
|
|
Vectumerator<Articolo> vec = bean.findByCR(CR, 1, pageRow);
|
|
long totPages = (long)vec.getTotNumberOfPages();
|
|
CR.setPageRow(pageRow);
|
|
String TAG_THREAD_MSG = CR.getTAG_THREAD_MSG();
|
|
if (TAG_THREAD_MSG.isEmpty())
|
|
TAG_THREAD_MSG = "AMZ POST ITEMS OFFERS BATCH";
|
|
for (int pageNumber = 1; (long)pageNumber <= totPages; pageNumber++) {
|
|
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "amzPostItemsOffersBatch pagina " + pageNumber + " su " + totPages + "...");
|
|
CR.setPageNumber(pageNumber);
|
|
AmzResult amzResult = amzPostItemsOffersBatchNotPaged(CR, marketplaceLang);
|
|
res.append(amzResult.getMsg());
|
|
result &= amzResult.isOk();
|
|
}
|
|
AmzResult resAR = new AmzResult(res.toString(), result, null);
|
|
StatusMsg.deleteMsgByTag(getApFull(), TAG_THREAD_MSG);
|
|
return resAR;
|
|
}
|
|
|
|
private AmzResult amzPostItemsOffersBatchNotPaged(ArticoloCR CR, String marketplaceLang) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLang);
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzgetItemsOffersBatch");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/batches/products/pricing/v0/itemOffers";
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
queryParms.put("MarketplaceId", getMarketplacesIds(marketplaceLang));
|
|
queryParms.put("ItemCondition", "New");
|
|
HttpPost request = new HttpPost("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("Content-Type".toLowerCase(), "application/json");
|
|
JSONObject items = getJsonItemsOffersBatchRequestByArticoloCR(CR, marketplaceLang);
|
|
String body = items.toString(4);
|
|
StringEntity stringEntity = new StringEntity(items.toString(4), "UTF-8");
|
|
request.setEntity((HttpEntity)stringEntity);
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("POST").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(queryParms).payload(body)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
System.out.println(jo.toString(4));
|
|
ResParm rp = updatePricingOffers(jo);
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("ItemsOffersBatch found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(rp.getMsg());
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
public AmzResult amzPostPricing(ArticoloCR CR, String marketplaceLangs) {
|
|
AmzResult resAR = amzCheckTokensApi(marketplaceLangs);
|
|
if (resAR.isOk())
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("AmzSellerApi --> amzPostPricing");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
String host = "https://sellingpartnerapi-eu.amazon.com".replace("https://", "");
|
|
String endPoint = "/batches/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice";
|
|
TreeMap<String, String> queryParms = new TreeMap<>();
|
|
HttpPost request = new HttpPost("https://sellingpartnerapi-eu.amazon.com" + endPoint + getInlineQueryParms(queryParms));
|
|
TreeMap<String, String> awsHeaders = new TreeMap<>();
|
|
awsHeaders.put("host", host);
|
|
awsHeaders.put("x-amz-access-token", getLwaAccessToken());
|
|
awsHeaders.put("x-amz-security-token", getStsSessionToken());
|
|
awsHeaders.put("Content-Type".toLowerCase(), "application/json");
|
|
JSONObject items = getJsonFeaturedOfferExpectedPriceRequestByArticoloCR(CR, marketplaceLangs);
|
|
String body = items.toString(4);
|
|
StringEntity stringEntity = new StringEntity(items.toString(4), "UTF-8");
|
|
request.setEntity((HttpEntity)stringEntity);
|
|
AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder(getStsAccessKeyId(), getStsSecretAccessKey()).regionName("eu-west-1")
|
|
.serviceName("execute-api").httpMethodName("POST").canonicalURI(endPoint).awsHeaders(awsHeaders)
|
|
.queryParametes(null).payload(body)
|
|
|
|
.build();
|
|
if (this.debug) {
|
|
System.out.println("stsaccesskey: " + getStsAccessKeyId());
|
|
System.out.println("stsSecretAccessKey: " + getStsSecretAccessKey());
|
|
}
|
|
Map<String, String> header = aWSV4Auth.getHeaders();
|
|
for (Map.Entry<String, String> entrySet : header.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
for (Map.Entry<String, String> entrySet : awsHeaders.entrySet()) {
|
|
String key = entrySet.getKey();
|
|
String value = entrySet.getValue();
|
|
if (this.debug)
|
|
System.out.println(key + ":" + key);
|
|
request.setHeader(key, value);
|
|
}
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resAR.setOk(false);
|
|
resAR.setMsg(content);
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent= " + content);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
ResParm rp = updateFeaturedOEPrice(jo);
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
resAR.setMsg("FEOPrice found");
|
|
resAR.setOk(true);
|
|
resAR.setResult(rp.getMsg());
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resAR.setOk(false);
|
|
resAR.setMsg(e.getMessage());
|
|
}
|
|
return resAR;
|
|
}
|
|
|
|
private ResParm updatePricingOffers(JSONObject jo) {
|
|
ResParm rp = new ResParm(true);
|
|
try {
|
|
long recordUpdate = 0L, recordError = 0L;
|
|
StringBuilder sb = new StringBuilder();
|
|
Articolo bean = new Articolo(getApFull());
|
|
JSONArray jaResponses = jo.getJSONArray("responses");
|
|
for (int i = 0; i < jaResponses.length(); i++) {
|
|
JSONObject joResponse = jaResponses.getJSONObject(i);
|
|
JSONObject joStatus = joResponse.getJSONObject("status");
|
|
if (joStatus.getInt("statusCode") == 200) {
|
|
JSONObject joPayload = joResponse.getJSONObject("body").getJSONObject("payload");
|
|
String currentLang = (String)getKeyByValue((Map)getHmMarketplaces(), joPayload.getString("marketplaceId"));
|
|
String currentAsin = joPayload.getString("ASIN");
|
|
bean.findByAsinAmz(currentAsin);
|
|
if (bean.getId_articolo() > 0L) {
|
|
DoubleOperator featuredOfferExpectedPrice = new DoubleOperator();
|
|
DoubleOperator competingFeaturedOffer = new DoubleOperator();
|
|
DoubleOperator currentFeaturedOffer = new DoubleOperator();
|
|
featuredOfferExpectedPrice.setScale(2, 5);
|
|
competingFeaturedOffer.setScale(2, 5);
|
|
currentFeaturedOffer.setScale(2, 5);
|
|
if (joPayload.has("Summary")) {
|
|
if (joPayload.getJSONObject("Summary").has("LowestPrices")) {
|
|
JSONObject joLowestPrices0 = joPayload.getJSONObject("Summary").getJSONArray("LowestPrices")
|
|
.getJSONObject(0);
|
|
featuredOfferExpectedPrice.add(joLowestPrices0.getJSONObject("ListingPrice").getDouble("Amount"));
|
|
featuredOfferExpectedPrice.add(joLowestPrices0.getJSONObject("Shipping").getDouble("Amount"));
|
|
}
|
|
if (joPayload.getJSONObject("Summary").has("BuyBoxPrices")) {
|
|
JSONObject joBuyBoxPrices0 = joPayload.getJSONObject("Summary").getJSONArray("BuyBoxPrices")
|
|
.getJSONObject(0);
|
|
competingFeaturedOffer.add(joBuyBoxPrices0.getJSONObject("ListingPrice").getDouble("Amount"));
|
|
competingFeaturedOffer.add(joBuyBoxPrices0.getJSONObject("Shipping").getDouble("Amount"));
|
|
}
|
|
AmzFeaturedPrice afp = new AmzFeaturedPrice(getApFull());
|
|
afp.findByArticoloLang(bean, currentLang);
|
|
afp.setId_articolo(bean.getId_articolo());
|
|
afp.setCompetingFOPriceAmz(competingFeaturedOffer.getResult());
|
|
afp.setCurrentFOPriceAmz(currentFeaturedOffer.getResult());
|
|
afp.setFeaturedOEPriceAmz(featuredOfferExpectedPrice.getResult());
|
|
afp.setLang(currentLang);
|
|
afp.setDataPriceAmz(DBAdapter.getToday());
|
|
rp = afp.save();
|
|
if (rp.getStatus()) {
|
|
recordUpdate++;
|
|
} else {
|
|
recordError++;
|
|
sb.append("err! ");
|
|
sb.append(rp.getMsg());
|
|
sb.append("\n");
|
|
}
|
|
} else {
|
|
sb.append("err! ");
|
|
sb.append(currentAsin);
|
|
sb.append(" Summary non trovato!\n");
|
|
}
|
|
} else {
|
|
sb.append("err! ");
|
|
sb.append(currentAsin);
|
|
sb.append(" non trovato!\n");
|
|
}
|
|
} else {
|
|
sb.append("err! ");
|
|
sb.append(joStatus.getString("reasonPhrase"));
|
|
sb.append("\n");
|
|
}
|
|
}
|
|
rp.setMsg("u:" + recordUpdate + " e:" + recordError + " " + sb.toString());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
|
|
for (Map.Entry<T, E> entry : map.entrySet()) {
|
|
if (Objects.equals(value, entry.getValue()))
|
|
return entry.getKey();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private JSONObject getJsonOfferByArticolo(Articolo bean, String marketplaceLangs) {
|
|
return getJsonOfferByArticoloSicura(bean, marketplaceLangs);
|
|
}
|
|
|
|
private JSONObject getJsonListingOffersBatchRequestByArticoloCR(ArticoloCR CR, String marketplaceLangs) {
|
|
String URI_PRICING = "/products/pricing/v0/listings/:Sku/offers";
|
|
String GET_METHOD = "GET";
|
|
String ITEM_CONDITION = "New";
|
|
String CUSTOMER_TYPE = "Consumer";
|
|
JSONObject jo = new JSONObject();
|
|
JSONArray jaRequest = new JSONArray();
|
|
Articolo bean = new Articolo(getApFull());
|
|
Vectumerator<Articolo> vec = bean.findByCR(CR, 0, 0);
|
|
while (vec.hasMoreElements()) {
|
|
Articolo row = (Articolo)vec.nextElement();
|
|
if (!row.getCodiceEanNoZero().isEmpty()) {
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
JSONObject joRequest = new JSONObject();
|
|
joRequest.put("marketplaceId", getHmMarketplaces().get(currentLang));
|
|
joRequest.put("method", "GET");
|
|
joRequest.put("uri", "/products/pricing/v0/listings/:Sku/offers".replace(":Sku", row.getCodiceEanNoZero()));
|
|
joRequest.put("ItemCondition", "New");
|
|
joRequest.put("CustomerType", "Consumer");
|
|
jaRequest.put(joRequest);
|
|
}
|
|
}
|
|
}
|
|
jo.put("requests", jaRequest);
|
|
return jo;
|
|
}
|
|
|
|
private JSONObject getJsonItemsOffersBatchRequestByArticoloCR(ArticoloCR CR, String marketplaceLangs) {
|
|
String URI_PRICING = "/products/pricing/v0/items/:Asin/offers";
|
|
String GET_METHOD = "GET";
|
|
String ITEM_CONDITION = "New";
|
|
String CUSTOMER_TYPE = "Consumer";
|
|
JSONObject jo = new JSONObject();
|
|
JSONArray jaRequest = new JSONArray();
|
|
Articolo bean = new Articolo(getApFull());
|
|
Vectumerator<Articolo> vec = bean.findByCR(CR, CR.getPageNumber(), CR.getPageRow());
|
|
ResParm rp = new ResParm(true);
|
|
while (vec.hasMoreElements()) {
|
|
Articolo row = (Articolo)vec.nextElement();
|
|
if (!row.getAsinAmz().isEmpty()) {
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
JSONObject joRequest = new JSONObject();
|
|
joRequest.put("MarketplaceId", getHmMarketplaces().get(currentLang));
|
|
joRequest.put("method", "GET");
|
|
joRequest.put("uri", "/products/pricing/v0/items/:Asin/offers".replace(":Asin", row.getAsinAmz()));
|
|
joRequest.put("ItemCondition", "New");
|
|
joRequest.put("CustomerType", "Consumer");
|
|
jaRequest.put(joRequest);
|
|
}
|
|
}
|
|
}
|
|
jo.put("requests", jaRequest);
|
|
return jo;
|
|
}
|
|
|
|
private ResParm updateFeaturedOEPrice(JSONObject jo) {
|
|
ResParm rp = new ResParm(true);
|
|
try {
|
|
long recordUpdate = 0L, recordError = 0L;
|
|
StringBuilder sb = new StringBuilder();
|
|
Articolo bean = new Articolo(getApFull());
|
|
JSONArray jaResponses = jo.getJSONArray("responses");
|
|
for (int i = 0; i < jaResponses.length(); i++) {
|
|
JSONObject joResponse = jaResponses.getJSONObject(i);
|
|
JSONObject joStatus = joResponse.getJSONObject("status");
|
|
if (joStatus.getInt("statusCode") == 200) {
|
|
JSONObject joRequest = joResponse.getJSONObject("request");
|
|
String currentLang = (String)getKeyByValue((Map)getHmMarketplaces(), joRequest.getString("marketplaceId"));
|
|
String currentSku = joRequest.getString("sku");
|
|
bean.findByCodiceEanSerie(currentSku, null);
|
|
if (bean.getId_articolo() > 0L) {
|
|
DoubleOperator featuredOfferExpectedPrice = new DoubleOperator();
|
|
DoubleOperator competingFeaturedOffer = new DoubleOperator();
|
|
DoubleOperator currentFeaturedOffer = new DoubleOperator();
|
|
featuredOfferExpectedPrice.setScale(2, 5);
|
|
competingFeaturedOffer.setScale(2, 5);
|
|
currentFeaturedOffer.setScale(2, 5);
|
|
JSONObject joBody = joResponse.getJSONObject("body");
|
|
if (joBody.has("featuredOfferExpectedPriceResults")) {
|
|
JSONObject joFeaturedOfferExpectedPriceResult = joBody.getJSONArray("featuredOfferExpectedPriceResults")
|
|
.getJSONObject(0);
|
|
if (joFeaturedOfferExpectedPriceResult.has("featuredOfferExpectedPrice")) {
|
|
String resultStatus = joFeaturedOfferExpectedPriceResult.getString("resultStatus");
|
|
if (resultStatus.equals("VALID_FOEP") || resultStatus.equals("NO_COMPETING_OFFER"))
|
|
featuredOfferExpectedPrice.add(
|
|
joFeaturedOfferExpectedPriceResult.getJSONObject("featuredOfferExpectedPrice").getJSONObject("listingPrice").getDouble("amount"));
|
|
}
|
|
if (joFeaturedOfferExpectedPriceResult.has("competingFeaturedOffer")) {
|
|
competingFeaturedOffer.add(joFeaturedOfferExpectedPriceResult.getJSONObject("competingFeaturedOffer")
|
|
.getJSONObject("price").getJSONObject("listingPrice").getDouble("amount"));
|
|
competingFeaturedOffer.add(joFeaturedOfferExpectedPriceResult.getJSONObject("competingFeaturedOffer")
|
|
.getJSONObject("price").getJSONObject("shippingPrice").getDouble("amount"));
|
|
}
|
|
if (joFeaturedOfferExpectedPriceResult.has("currentFeaturedOffer")) {
|
|
currentFeaturedOffer.add(joFeaturedOfferExpectedPriceResult.getJSONObject("currentFeaturedOffer")
|
|
.getJSONObject("price").getJSONObject("listingPrice").getDouble("amount"));
|
|
currentFeaturedOffer.add(joFeaturedOfferExpectedPriceResult.getJSONObject("currentFeaturedOffer")
|
|
.getJSONObject("price").getJSONObject("shippingPrice").getDouble("amount"));
|
|
}
|
|
AmzFeaturedPrice afp = new AmzFeaturedPrice(getApFull());
|
|
afp.findByArticoloLang(bean, currentLang);
|
|
afp.setId_articolo(bean.getId_articolo());
|
|
afp.setCompetingFOPriceAmz(competingFeaturedOffer.getResult());
|
|
afp.setCurrentFOPriceAmz(currentFeaturedOffer.getResult());
|
|
afp.setFeaturedOEPriceAmz(featuredOfferExpectedPrice.getResult());
|
|
afp.setLang(currentLang);
|
|
afp.setDataPriceAmz(DBAdapter.getToday());
|
|
rp = afp.save();
|
|
if (rp.getStatus()) {
|
|
recordUpdate++;
|
|
} else {
|
|
recordError++;
|
|
sb.append("err! ");
|
|
sb.append(rp.getMsg());
|
|
sb.append("\n");
|
|
}
|
|
} else {
|
|
sb.append("err! ");
|
|
sb.append(currentSku);
|
|
sb.append(" featuredOfferExpectedPriceResults non trovato!\n");
|
|
}
|
|
} else {
|
|
sb.append("err! ");
|
|
sb.append(currentSku);
|
|
sb.append(" non trovato!\n");
|
|
}
|
|
} else {
|
|
sb.append("err! ");
|
|
sb.append(joStatus.getString("reasonPhrase"));
|
|
sb.append("\n");
|
|
}
|
|
}
|
|
rp.setMsg("u:" + recordUpdate + " e:" + recordError + " " + sb.toString());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
private JSONObject getJsonOfferByArticoloMod(Articolo bean, String marketplaceLangs) {
|
|
JSONObject jo = new JSONObject();
|
|
String lang = "it";
|
|
jo.put("sku", bean.getCodiceEanNoZero());
|
|
JSONArray jaOffers = new JSONArray();
|
|
JSONArray jaPurchasable_offer = new JSONArray();
|
|
JSONArray jaFulfillment_availability = new JSONArray();
|
|
JSONArray jaCondition_type = new JSONArray();
|
|
JSONArray jaCountry_of_origin = new JSONArray();
|
|
JSONArray jaMerchant_shipping_group = new JSONArray();
|
|
JSONArray jaMerchant_suggested_asin = new JSONArray();
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
String marketplaceID = getHmMarketplaces().get(currentLang);
|
|
JSONObject joFulfillment_availability = new JSONObject();
|
|
joFulfillment_availability.put("marketplaceid", marketplaceID);
|
|
joFulfillment_availability.put("quantity", bean.getQtaAmzDaInviare());
|
|
joFulfillment_availability.put("fulfillment_channel_code", "DEFAULT");
|
|
joFulfillment_availability.put("lead_time_to_ship_max_days", 3);
|
|
jaFulfillment_availability.put(joFulfillment_availability);
|
|
JSONObject joCondition_type = new JSONObject();
|
|
joCondition_type.put("marketplaceid", marketplaceID);
|
|
joCondition_type.put("value", "new_new");
|
|
jaCondition_type.put(joCondition_type);
|
|
JSONObject joCountry_of_origin = new JSONObject();
|
|
joCountry_of_origin.put("marketplaceid", marketplaceID);
|
|
joCountry_of_origin.put("value", "IT");
|
|
jaCountry_of_origin.put(joCountry_of_origin);
|
|
JSONObject joMerchant_shipping_group = new JSONObject();
|
|
joMerchant_shipping_group.put("marketplaceid", marketplaceID);
|
|
if (bean.getFlgPriceTypeAmz() == 0L) {
|
|
joMerchant_shipping_group.put("value", "legacy-template-id");
|
|
} else {
|
|
joMerchant_shipping_group.put("value", getAttivita().getAmzMerchantShippingGroupFree());
|
|
}
|
|
jaMerchant_shipping_group.put(joMerchant_shipping_group);
|
|
JSONObject joMerchant_suggested_asin = new JSONObject();
|
|
joMerchant_suggested_asin.put("marketplaceid", marketplaceID);
|
|
joMerchant_suggested_asin.put("value", bean.getAsinAmz());
|
|
jaMerchant_suggested_asin.put(joMerchant_suggested_asin);
|
|
JSONObject joPurchasable_offerB2C = new JSONObject();
|
|
joPurchasable_offerB2C.put("marketplaceid", marketplaceID);
|
|
joPurchasable_offerB2C.put("currency", "EUR");
|
|
joPurchasable_offerB2C.put("offerType", "B2C");
|
|
JSONObject joPurchasable_offerB2B = new JSONObject();
|
|
joPurchasable_offerB2B.put("marketplaceid", marketplaceID);
|
|
joPurchasable_offerB2B.put("currency", "EUR");
|
|
joPurchasable_offerB2B.put("offerType", "B2B");
|
|
JSONArray jaOur_price = new JSONArray();
|
|
JSONObject joOur_price = new JSONObject();
|
|
JSONArray jaSchedule = new JSONArray();
|
|
JSONObject joSchedule = new JSONObject();
|
|
joSchedule.put("value_with_tax", bean.getPrezzoArticoloAmazonIvaExport(currentLang, 1L));
|
|
jaSchedule.put(joSchedule);
|
|
joOur_price.put("schedule", jaSchedule);
|
|
jaOur_price.put(joOur_price);
|
|
joPurchasable_offerB2C.put("our_price", jaOur_price);
|
|
joPurchasable_offerB2B.put("our_price", jaOur_price);
|
|
jaPurchasable_offer.put(joPurchasable_offerB2C);
|
|
jaPurchasable_offer.put(joPurchasable_offerB2B);
|
|
}
|
|
jo.put("productType", bean.getProductTypeAmz());
|
|
jo.put("requirements", "LISTING_OFFER_ONLY");
|
|
JSONObject joAttributes = new JSONObject();
|
|
joAttributes.put("purchasable_offer", jaPurchasable_offer);
|
|
joAttributes.put("fulfillment_availability", jaFulfillment_availability);
|
|
joAttributes.put("condition_type", jaCondition_type);
|
|
joAttributes.put("country_of_origin", jaCountry_of_origin);
|
|
joAttributes.put("merchant_shipping_group", jaMerchant_shipping_group);
|
|
joAttributes.put("merchant_suggested_asin", jaMerchant_suggested_asin);
|
|
jo.put("attributes", joAttributes);
|
|
return jo;
|
|
}
|
|
|
|
private JSONObject getJsonOfferByArticoloSicura(Articolo bean, String marketplaceLangs) {
|
|
JSONObject jo = new JSONObject();
|
|
String lang = "it";
|
|
jo.put("sku", bean.getCodiceEanNoZero());
|
|
JSONArray jaOffers = new JSONArray();
|
|
JSONArray jaPurchasable_offer = new JSONArray();
|
|
JSONArray jaFulfillment_availability = new JSONArray();
|
|
JSONArray jaCondition_type = new JSONArray();
|
|
JSONArray jaCountry_of_origin = new JSONArray();
|
|
JSONArray jaMerchant_shipping_group = new JSONArray();
|
|
JSONArray jaMerchant_suggested_asin = new JSONArray();
|
|
StringTokenizer st = new StringTokenizer(marketplaceLangs, ",");
|
|
while (st.hasMoreTokens()) {
|
|
String currentLang = st.nextToken();
|
|
String marketplaceID = getHmMarketplaces().get(currentLang);
|
|
JSONObject joOfferB2C = new JSONObject();
|
|
joOfferB2C.put("marketplaceid", marketplaceID);
|
|
joOfferB2C.put("offerType", "B2C");
|
|
JSONObject joPriceB2C = new JSONObject();
|
|
joPriceB2C.put("amount", bean.getPrezzoArticoloAmazonIvaExport(currentLang, 1L));
|
|
joPriceB2C.put("currency", "EUR");
|
|
joPriceB2C.put("currencyCode", "EUR");
|
|
joOfferB2C.put("price", joPriceB2C);
|
|
jaOffers.put(joOfferB2C);
|
|
JSONObject joOfferB2B = new JSONObject();
|
|
joOfferB2B.put("marketplaceid", marketplaceID);
|
|
joOfferB2B.put("offerType", "B2B");
|
|
JSONObject joPriceB2B = new JSONObject();
|
|
joPriceB2B.put("amount", bean.getPrezzoArticoloAmazonIvaExport(currentLang, 1L));
|
|
joPriceB2B.put("currency", "EUR");
|
|
joPriceB2B.put("currencyCode", "EUR");
|
|
joOfferB2B.put("price", joPriceB2B);
|
|
jaOffers.put(joOfferB2B);
|
|
JSONObject joFulfillment_availability = new JSONObject();
|
|
joFulfillment_availability.put("marketplaceid", marketplaceID);
|
|
joFulfillment_availability.put("quantity", bean.getQtaAmzDaInviare());
|
|
joFulfillment_availability.put("fulfillment_channel_code", "DEFAULT");
|
|
joFulfillment_availability.put("lead_time_to_ship_max_days", 3);
|
|
jaFulfillment_availability.put(joFulfillment_availability);
|
|
JSONObject joCondition_type = new JSONObject();
|
|
joCondition_type.put("marketplaceid", marketplaceID);
|
|
joCondition_type.put("value", "new_new");
|
|
jaCondition_type.put(joCondition_type);
|
|
JSONObject joCountry_of_origin = new JSONObject();
|
|
joCountry_of_origin.put("marketplaceid", marketplaceID);
|
|
joCountry_of_origin.put("value", "IT");
|
|
jaCountry_of_origin.put(joCountry_of_origin);
|
|
JSONObject joMerchant_shipping_group = new JSONObject();
|
|
joMerchant_shipping_group.put("marketplaceid", marketplaceID);
|
|
if (bean.getFlgPriceTypeAmz() == 0L) {
|
|
joMerchant_shipping_group.put("value", "legacy-template-id");
|
|
} else {
|
|
joMerchant_shipping_group.put("value", getAttivita().getAmzMerchantShippingGroupFree());
|
|
}
|
|
jaMerchant_shipping_group.put(joMerchant_shipping_group);
|
|
JSONObject joMerchant_suggested_asin = new JSONObject();
|
|
joMerchant_suggested_asin.put("marketplaceid", marketplaceID);
|
|
joMerchant_suggested_asin.put("value", bean.getAsinAmz());
|
|
jaMerchant_suggested_asin.put(joMerchant_suggested_asin);
|
|
JSONObject joPurchasable_offer = new JSONObject();
|
|
joPurchasable_offer.put("marketplaceid", marketplaceID);
|
|
joPurchasable_offer.put("currency", "EUR");
|
|
JSONArray jaOur_price = new JSONArray();
|
|
JSONObject joOur_price = new JSONObject();
|
|
JSONArray jaSchedule = new JSONArray();
|
|
JSONObject joSchedule = new JSONObject();
|
|
joSchedule.put("value_with_tax", bean.getPrezzoArticoloAmazonIvaExport(currentLang, 1L));
|
|
jaSchedule.put(joSchedule);
|
|
joOur_price.put("schedule", jaSchedule);
|
|
jaOur_price.put(joOur_price);
|
|
joPurchasable_offer.put("our_price", jaOur_price);
|
|
jaPurchasable_offer.put(joPurchasable_offer);
|
|
}
|
|
jo.put("productType", bean.getProductTypeAmz());
|
|
jo.put("requirements", "LISTING_OFFER_ONLY");
|
|
JSONObject joAttributes = new JSONObject();
|
|
joAttributes.put("purchasable_offer", jaPurchasable_offer);
|
|
joAttributes.put("fulfillment_availability", jaFulfillment_availability);
|
|
joAttributes.put("condition_type", jaCondition_type);
|
|
joAttributes.put("country_of_origin", jaCountry_of_origin);
|
|
joAttributes.put("merchant_shipping_group", jaMerchant_shipping_group);
|
|
joAttributes.put("merchant_suggested_asin", jaMerchant_suggested_asin);
|
|
jo.put("attributes", joAttributes);
|
|
return jo;
|
|
}
|
|
}
|