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.
164 lines
7.7 KiB
164 lines
7.7 KiB
|
6 days ago
|
package common.ecommerce.edayun.probe;
|
||
|
|
|
||
|
|
import common.ecommerce.edayun.EdayunConstants;
|
||
|
|
import common.ecommerce.edayun.adapter.EdayunAdapter;
|
||
|
|
import common.ecommerce.edayun.adapter.EdayunOrderMapper;
|
||
|
|
import common.ecommerce.edayun.dto.EdayunCreateOrderDtos;
|
||
|
|
import common.ecommerce.http.JacksonJsonCodec;
|
||
|
|
import common.ecommerce.http.OkHttpTransport;
|
||
|
|
import common.ecommerce.shared.error.ChannelException;
|
||
|
|
import common.ecommerce.shared.model.Platform;
|
||
|
|
import common.ecommerce.shared.spi.ChannelSecret;
|
||
|
|
import common.ecommerce.shared.support.Money;
|
||
|
|
import common.ecommerce.warehouse.enums.LabelMode;
|
||
|
|
import common.ecommerce.warehouse.enums.WarehouseProvider;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundCommand;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundLine;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundReceipt;
|
||
|
|
import common.ecommerce.warehouse.model.ShipTo;
|
||
|
|
import common.ecommerce.warehouse.sdk.WarehouseConnection;
|
||
|
|
import common.ecommerce.warehouse.sdk.WarehouseContext;
|
||
|
|
import org.junit.jupiter.api.Assumptions;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
|
||
|
|
|
||
|
|
import java.time.Clock;
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.util.UUID;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 创建出库单探针:默认只打印报文,<b>不联网</b>。
|
||
|
|
*
|
||
|
|
* <p>创建出库单是<b>写操作</b>,会真实建单并向仓方下发发货任务,所以真跑要显式加
|
||
|
|
* {@code -Dedayun.probe.write=true};平台单号用 {@code PROBE-} 前缀的时间戳避免撞车。</p>
|
||
|
|
*
|
||
|
|
* <pre>
|
||
|
|
* # 只看报文(安全)
|
||
|
|
* mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \
|
||
|
|
* -Dedayun.probe.order=true -Dtest=EdayunCreateOrderProbeTest test
|
||
|
|
*
|
||
|
|
* # 真发一次(会在测试仓建单)
|
||
|
|
* ... -Dedayun.probe.order=true -Dedayun.probe.write=true \
|
||
|
|
* -Dedayun.probe.clientId=... -Dedayun.probe.clientSecret=... \
|
||
|
|
* -Dedayun.probe.appKey=... -Dedayun.probe.appSecret=... \
|
||
|
|
* -Dedayun.probe.warehouseId=2 -Dedayun.probe.sku=ybtest
|
||
|
|
* </pre>
|
||
|
|
*
|
||
|
|
* <p>建单成功后记下响应里的 {@code orderNo},取消时要用它。
|
||
|
|
* 写操作不可逆,跑之前请确认这是测试仓。</p>
|
||
|
|
*/
|
||
|
|
@EnabledIfSystemProperty(named = "edayun.probe.order", matches = "true")
|
||
|
|
class EdayunCreateOrderProbeTest {
|
||
|
|
|
||
|
|
private static final String BASE_URL = opt("edayun.probe.baseUrl", EdayunConstants.PR_BASE_URL);
|
||
|
|
private static final String CLIENT_ID = System.getProperty("edayun.probe.clientId", "");
|
||
|
|
private static final String CLIENT_SECRET = System.getProperty("edayun.probe.clientSecret", "");
|
||
|
|
private static final String APP_KEY = System.getProperty("edayun.probe.appKey", "");
|
||
|
|
private static final String APP_SECRET = System.getProperty("edayun.probe.appSecret", "");
|
||
|
|
private static final String WAREHOUSE_ID = System.getProperty("edayun.probe.warehouseId", "");
|
||
|
|
private static final String SKU = opt("edayun.probe.sku", "ybtest");
|
||
|
|
private static final boolean WRITE = Boolean.getBoolean("edayun.probe.write");
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void probeCreateOrder() {
|
||
|
|
String poNumber = "PROBE-"
|
||
|
|
+ Long.toString(System.currentTimeMillis(), 36).toUpperCase();
|
||
|
|
|
||
|
|
OutboundCommand cmd = OutboundCommand.builder()
|
||
|
|
.outboundRequestNo("OB-" + poNumber)
|
||
|
|
.orderRef(poNumber)
|
||
|
|
.warehouseId(emptyToNull(WAREHOUSE_ID))
|
||
|
|
.labelMode(LabelMode.WAREHOUSE_LABEL)
|
||
|
|
.carrier("FedEx")
|
||
|
|
.salesPlatform(Platform.AMAZON_SC)
|
||
|
|
.currency("USD")
|
||
|
|
.declaredTotal(Money.of("19.99", "USD"))
|
||
|
|
.latestShipDate(Instant.now().plusSeconds(7 * 24 * 3600))
|
||
|
|
// 地址:易达云要州全称
|
||
|
|
.shipTo(ShipTo.builder()
|
||
|
|
.name("Probe Buyer")
|
||
|
|
.phone("+1-555-0100")
|
||
|
|
.email("probe@example.com")
|
||
|
|
.countryCode("US")
|
||
|
|
.stateOrProvince("California")
|
||
|
|
.city("Escondido")
|
||
|
|
.addressLine1("1308 E Washington Ave")
|
||
|
|
.postalCode("92027")
|
||
|
|
.build())
|
||
|
|
.line(OutboundLine.builder()
|
||
|
|
.lineNo("1")
|
||
|
|
.omsSku(SKU)
|
||
|
|
.quantity(1)
|
||
|
|
.declareValue(Money.of("19.99", "USD"))
|
||
|
|
.insuredAmount(Money.of("19.99", "USD"))
|
||
|
|
.build())
|
||
|
|
.signatureRequired(Boolean.TRUE)
|
||
|
|
.build();
|
||
|
|
|
||
|
|
EdayunCreateOrderDtos.CreateOrderRequest req =
|
||
|
|
EdayunOrderMapper.toCreateRequest(cmd, emptyToNull(WAREHOUSE_ID));
|
||
|
|
String bodyJson = new JacksonJsonCodec().toJson(req);
|
||
|
|
|
||
|
|
System.out.println("\n================ edayun / POST "
|
||
|
|
+ EdayunConstants.PATH_ORDER_CREATE + " ================");
|
||
|
|
System.out.println("[probe] baseUrl=" + BASE_URL
|
||
|
|
+ " warehouseId=" + mask(WAREHOUSE_ID) + " sku=" + mask(SKU)
|
||
|
|
+ " poNumber=" + poNumber);
|
||
|
|
System.out.println("[request] " + bodyJson);
|
||
|
|
if (!APP_SECRET.trim().isEmpty()) {
|
||
|
|
System.out.println("[sign] " + common.ecommerce.edayun.http.EdayunRequestFactory
|
||
|
|
.sign(bodyJson, APP_SECRET));
|
||
|
|
}
|
||
|
|
|
||
|
|
Assumptions.assumeTrue(WRITE, "dry-run:未加 -Dedayun.probe.write=true,不实际建单");
|
||
|
|
Assumptions.assumeTrue(!CLIENT_ID.trim().isEmpty() && !CLIENT_SECRET.trim().isEmpty()
|
||
|
|
&& !APP_KEY.trim().isEmpty() && !APP_SECRET.trim().isEmpty(),
|
||
|
|
"真跑需要 -Dedayun.probe.clientId / clientSecret / appKey / appSecret");
|
||
|
|
|
||
|
|
try {
|
||
|
|
WarehouseContext ctx = WarehouseContext.of("EDY-PROBE", WarehouseProvider.EDAYUN,
|
||
|
|
emptyToNull(WAREHOUSE_ID),
|
||
|
|
WarehouseConnection.of(BASE_URL,
|
||
|
|
ChannelSecret.builder()
|
||
|
|
.appKey(APP_KEY).appSecret(APP_SECRET)
|
||
|
|
.clientId(CLIENT_ID).clientSecret(CLIENT_SECRET)
|
||
|
|
.build()),
|
||
|
|
"probe-" + UUID.randomUUID(), Clock.systemUTC());
|
||
|
|
EdayunAdapter adapter = new EdayunAdapter(
|
||
|
|
new OkHttpTransport("common-ecommerce/1.0.0 (edayun-probe)"),
|
||
|
|
new JacksonJsonCodec());
|
||
|
|
|
||
|
|
OutboundReceipt receipt = adapter.create(ctx, cmd);
|
||
|
|
System.out.println("[OK] outboundRequestNo=" + receipt.outboundRequestNo()
|
||
|
|
+ " providerOrderId=" + receipt.providerOrderId());
|
||
|
|
} catch (RuntimeException e) {
|
||
|
|
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
|
||
|
|
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": "
|
||
|
|
+ (msg.length() <= 800 ? msg : msg.substring(0, 800) + "…"));
|
||
|
|
if (e instanceof ChannelException) {
|
||
|
|
ChannelException ce = (ChannelException) e;
|
||
|
|
System.out.println(" platformCode=" + ce.platformCode()
|
||
|
|
+ " retryable=" + ce.retryable());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String opt(String name, String fallback) {
|
||
|
|
String value = System.getProperty(name, "");
|
||
|
|
String trimmed = value == null ? "" : value.trim();
|
||
|
|
return trimmed.isEmpty() ? fallback : trimmed;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String emptyToNull(String v) {
|
||
|
|
return v == null || v.trim().isEmpty() ? null : v.trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String mask(String v) {
|
||
|
|
if (v == null || v.trim().isEmpty()) {
|
||
|
|
return "(empty)";
|
||
|
|
}
|
||
|
|
String t = v.trim();
|
||
|
|
return t.length() <= 6 ? "***" : t.substring(0, 3) + "***" + t.substring(t.length() - 2);
|
||
|
|
}
|
||
|
|
}
|