You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
5.4 KiB
127 lines
5.4 KiB
using BBWYB.Common.Http;
|
|
using com.alibaba.openapi.client;
|
|
using com.alibaba.openapi.client.entity;
|
|
using com.alibaba.openapi.client.policy;
|
|
using Newtonsoft.Json.Linq;
|
|
using SDKAdapter.PurchasePlatform.Models;
|
|
|
|
namespace SDKAdapter.PurchasePlatform.Client
|
|
{
|
|
public class PP_1688Client : PP_PlatformClient
|
|
{
|
|
private _1688TradeTypeCompare _1688TradeTypeCompare;
|
|
public PP_1688Client(RestApiService restApiService) : base(restApiService)
|
|
{
|
|
_1688TradeTypeCompare = new _1688TradeTypeCompare();
|
|
}
|
|
|
|
private SyncAPIClient GetSyncAPIClient(string appKey, string appSecret)
|
|
{
|
|
return new SyncAPIClient(appKey, appSecret, restApiService);
|
|
}
|
|
|
|
public override PP_PreviewOrderResponse PreviewOrder(PP_PreviewOrderRequest request)
|
|
{
|
|
var client = GetSyncAPIClient(request.AppKey, request.AppSecret);
|
|
RequestPolicy reqPolicy = new RequestPolicy();
|
|
reqPolicy.HttpMethod = "POST";
|
|
reqPolicy.NeedAuthorization = false;
|
|
reqPolicy.RequestSendTimestamp = false;
|
|
reqPolicy.UseHttps = false;
|
|
reqPolicy.UseSignture = true;
|
|
reqPolicy.AccessPrivateApi = false;
|
|
|
|
Request _request = new Request();
|
|
APIId apiId = new APIId();
|
|
apiId.Name = "alibaba.createOrder.preview";
|
|
apiId.NamespaceValue = "com.alibaba.trade";
|
|
apiId.Version = 1;
|
|
_request.ApiId = apiId;
|
|
|
|
var param = new CreateOrderPreview()
|
|
{
|
|
addressParam = new AddressParam()
|
|
{
|
|
fullName = request.Consignee.ContactName,
|
|
mobile = request.Consignee.Mobile,
|
|
phone = request.Consignee.TelePhone,
|
|
postCode = "000000",
|
|
address = request.Consignee.Address,
|
|
provinceText = request.Consignee.Province,
|
|
cityText = request.Consignee.City,
|
|
areaText = request.Consignee.County,
|
|
townText = request.Consignee.Town
|
|
},
|
|
cargoParamList = new List<CargoParam>(),
|
|
flow = request.PurchaseMode == AdapterEnums.PurchaseMode.批发 ? "general" : "saleproxy"
|
|
};
|
|
foreach (var cargo in request.OrderProductParamList)
|
|
{
|
|
param.cargoParamList.Add(new CargoParam()
|
|
{
|
|
offerId = long.Parse(cargo.ProductId),
|
|
specId = cargo.SpecId,
|
|
quantity = cargo.Quantity
|
|
});
|
|
}
|
|
_request.RequestEntity = param;
|
|
if (!string.IsNullOrEmpty(request.AppToken))
|
|
_request.AccessToken = request.AppToken;
|
|
JObject result = null;
|
|
try
|
|
{
|
|
result = client.NewRequest(_request, reqPolicy);
|
|
if (result.Value<bool>("success") != true)
|
|
throw new Exception(result.Value<string>("errorMsg"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
|
|
var orderPreviewResuslt = (JArray)result["orderPreviewResuslt"];
|
|
List<JToken> intersectTradeModeList = new List<JToken>();
|
|
|
|
foreach (var orderPreviewJToken in orderPreviewResuslt)
|
|
{
|
|
if (orderPreviewJToken["tradeModelList"] == null)
|
|
throw new Exception("当前交易不可通过API下单,请使用1688网页交易 [交易模式列表为空]");
|
|
var tradeModeJArray = ((JArray)orderPreviewJToken["tradeModelList"]).Where(tradeJToken => tradeJToken.Value<bool>("opSupport"));
|
|
if (tradeModeJArray.Count() == 0)
|
|
throw new Exception("当前交易不可通过API下单,请使用1688网页交易 [没有支持开放平台下单的交易模式]");
|
|
|
|
if (intersectTradeModeList.Count() == 0)
|
|
intersectTradeModeList.AddRange(tradeModeJArray);
|
|
else
|
|
intersectTradeModeList = intersectTradeModeList.Intersect(tradeModeJArray, _1688TradeTypeCompare).ToList();
|
|
}
|
|
|
|
if (intersectTradeModeList.Count() == 0)
|
|
throw new Exception("当前交易不可通过API下单,请使用1688网页交易 [多个拆单之间没有相同的交易模式]");
|
|
return new PP_PreviewOrderResponse()
|
|
{
|
|
FreightAmount = orderPreviewResuslt.Sum(jt => jt.Value<decimal>("sumCarriage")) / 100M,
|
|
ProductAmount = orderPreviewResuslt.Sum(jt => jt.Value<decimal>("sumPaymentNoCarriage")) / 100M,
|
|
TotalAmount = orderPreviewResuslt.Sum(jt => jt.Value<decimal>("sumPayment")) / 100M,
|
|
//OrderTradeType = new OrderTradeTypeResponse()
|
|
//{
|
|
// Code = intersectTradeModeList.First().Value<string>("tradeType"),
|
|
// Name = intersectTradeModeList.First().Value<string>("name"),
|
|
//}
|
|
};
|
|
}
|
|
}
|
|
|
|
public class _1688TradeTypeCompare : IEqualityComparer<JToken>
|
|
{
|
|
public bool Equals(JToken x, JToken y)
|
|
{
|
|
return x.Value<string>("tradeType").Equals(y.Value<string>("tradeType"));
|
|
}
|
|
|
|
public int GetHashCode(JToken obj)
|
|
{
|
|
return obj.GetHashCode();
|
|
}
|
|
}
|
|
}
|
|
|