package com.hfkj.jd; import com.alibaba.fastjson.JSONObject; import com.hfkj.common.utils.HttpsUtils; import com.hfkj.common.utils.RsaCoderUtils; import com.hfkj.config.CommonSysConst; import com.hfkj.entity.BsOrderGoods; import com.hfkj.entity.GoodsUserAddress; import com.jd.open.api.sdk.DefaultJdClient; import com.jd.open.api.sdk.JdClient; import com.jd.open.api.sdk.domain.vopdd.OperaOrderOpenProvider.request.submitOrder.*; import com.jd.open.api.sdk.domain.vopkc.SkuInfoGoodsProvider.request.getNewStockById.AreaBaseInfoGoodsReq; import com.jd.open.api.sdk.domain.vopkc.SkuInfoGoodsProvider.request.getNewStockById.GetStockByIdGoodsReq; import com.jd.open.api.sdk.domain.vopkc.SkuInfoGoodsProvider.request.getNewStockById.SkuNumBaseGoodsReq; import com.jd.open.api.sdk.request.vopdd.VopOrderSubmitOrderRequest; import com.jd.open.api.sdk.request.vopdz.VopAddressConvertFourAreaByDetailStrRequest; import com.jd.open.api.sdk.request.vopdz.VopAddressQueryJdAreaIdListRequest; import com.jd.open.api.sdk.request.vopdz.VopAddressVerifyAreaFourIdOpenReqRequest; import com.jd.open.api.sdk.request.vopkc.VopGoodsGetNewStockByIdRequest; import com.jd.open.api.sdk.request.vopsp.*; import com.jd.open.api.sdk.response.vopdd.VopOrderSubmitOrderResponse; import com.jd.open.api.sdk.response.vopdz.VopAddressConvertFourAreaByDetailStrResponse; import com.jd.open.api.sdk.response.vopdz.VopAddressQueryJdAreaIdListResponse; import com.jd.open.api.sdk.response.vopdz.VopAddressVerifyAreaFourIdOpenReqResponse; import com.jd.open.api.sdk.response.vopkc.VopGoodsGetNewStockByIdResponse; import com.jd.open.api.sdk.response.vopsp.*; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Resource; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; /** * @ClassName JdService * @Author Sum1Dream * @Description 京东服务 * @Date 2024/10/11 下午3:25 **/ public class JdPostService { private static Logger log = LoggerFactory.getLogger(JdPostService.class); /** * UTF_8 */ public static final String UTF_8 = "utf-8"; /** * 用户名 */ private static final String USERNAME = "惠兑重庆VOP", /** * 明文密码,后续需要通过RSA加密 */ PLAINTEXT_PASSWORD = "hdcs1234", /** * 回调地址,注册应用时配置的授权回调地址 */ REDIRECT_URI = CommonSysConst.getSysConfig().getDomainName() + "/order/jdNotify/callbackToken", /** * 请求地址 */ SERVER_URL = "https://api.jd.com/routerjson", /** * RSA私钥 */ PRIVATE_RSA_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMEtt0kF02YBxPXzXyqzCtr1zkuj8NXLiqqSCDAfU6zWB1T1GUEpCfNukPBeZ9nXGj+jbTom8RFllNXbnOkW01Zn2VoyePh8TRWQbToXi+i/JQ162GzpF0GdPsUhJSrB5Z2QzK4UVXXkpPHhQHR8NKQhvTRkoRLfOy+oXWL8PS2hAgMBAAECgYBySo/j/jRiZ62WLlUhuCg1/7P8AJSeiPwTiq6Zeg9RdJeF5jT43kTq54GNFO2wbpkzCYe4Hg4GUulJ1dLx/PUvYquRpM4jGznxDxBA10mNyjx0O+SHcY+ZlOYWIKhEq4MeeJhtHhoFrlgcMNIyofhRDbFzrRak48GrcZDQC4/cIQJBAPwxtEwMnetPveLExR1UsygdVKwoWoGNL5Hkhcx6b2N2Qapk5aHB5haUEXeg1ShrL4C/kE1lsMAIdpVS9lWd4x0CQQDEGASnnB2oPHjvNfj88klpYTvPJRi3ORQPtlfz36kv+29QFxB9Mt1u39ttopw5G2x7QvTtL542w878brrmS8lVAkEA4k7QFjZ0N8cVBLvCjrGFG4hGhT6pCPxjJa5GCtoLvttNzdRA5EkVaklw60LeRSj6NbSxj2Kjm498qj8KYoYOWQJATJhRISMy1mcgmdeMNUlycW4gjY4g9FigRG7mNgU0MeDVnwQTVcQLiGZ6cH2m5guXOSJzRz5lX2DmteWOrawGrQJBAMKLfeTnYkVpXw9MABcR/9L9myG3FKwOHBIqD0wK+QVahi9byFk8GFXD4OlBQ1ZSxHqsUuxWqLVSa3sWBCKpLo8="; // 获取token public static JSONObject getAccessToken() throws Exception { String encodeRedirectUri = URLEncoder.encode(REDIRECT_URI, UTF_8); String encodeUsername = URLEncoder.encode(USERNAME, UTF_8); String md5Password = DigestUtils.md5Hex(PLAINTEXT_PASSWORD); String ciphertextPassword = RsaCoderUtils.encryptByPrivateKey(md5Password, PRIVATE_RSA_KEY); String encodePassword = URLEncoder.encode(ciphertextPassword, UTF_8); JSONObject object = new JSONObject(); object.put("app_key", CommonSysConst.getSysConfig().getJDAppKey()); object.put("redirect_uri", encodeRedirectUri); object.put("username", encodeUsername); object.put("password", encodePassword); object.put("response_type", "code"); object.put("scope", "snsapi_base"); return HttpsUtils.doGet("https://open-oauth.jd.com/oauth2/authorizeForVOP", object); } // 查询四级地址ID列表 public static VopAddressQueryJdAreaIdListResponse queryJdAreaIdList(String token , Integer areaLevel , Long areaId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL, token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopAddressQueryJdAreaIdListRequest request=new VopAddressQueryJdAreaIdListRequest(); request.setAreaLevel(areaLevel); request.setJdAreaId(areaId); return client.execute(request); } //验证四级地址ID有效性 public static VopAddressVerifyAreaFourIdOpenReqResponse verifyAreaFourIdOpenReq (String token , Long provinceId , Long cityId , Long countyId , Long townId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopAddressVerifyAreaFourIdOpenReqRequest request=new VopAddressVerifyAreaFourIdOpenReqRequest(); request.setProvinceId(provinceId); request.setCityId(cityId); request.setTownId(townId); request.setCountyId(countyId); return client.execute(request); } // 地址详情转换京东地址编码 public static VopAddressConvertFourAreaByDetailStrResponse convertFourAreaByDetailStr(String token , String address) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopAddressConvertFourAreaByDetailStrRequest request=new VopAddressConvertFourAreaByDetailStrRequest(); request.setAddressDetailStr(address); return client.execute(request); } // 查询商品池编号 public static VopGoodsGetSkuPoolInfoResponse getSkuPoolInfo(String token) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsGetSkuPoolInfoRequest request=new VopGoodsGetSkuPoolInfoRequest(); return client.execute(request); } // 根据商品池获取所有商品信息 public static VopGoodsQuerySkuByPageResponse querySkuByPage(String token , String bizPoolId , Integer offset) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsQuerySkuByPageRequest request=new VopGoodsQuerySkuByPageRequest(); request.setBizPoolId(bizPoolId); request.setOffset(offset); request.setPageSize(10); return client.execute(request); } // 获取商品详情及商品扩展信息 public static VopGoodsGetSkuDetailInfoResponse getSkuDetailInfo(String token , Long skuId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsGetSkuDetailInfoRequest request=new VopGoodsGetSkuDetailInfoRequest(); request.setSkuId(skuId); return client.execute(request); } // 查询商品图片 public static VopGoodsGetSkuImageListResponse getSkuImageList(String token , String skuId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsGetSkuImageListRequest request=new VopGoodsGetSkuImageListRequest(); request.setSkuId(skuId); return client.execute(request); } // 批量查询商品售卖价 public static VopGoodsGetSellPriceResponse getSellPrice(String token , String skuId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsGetSellPriceRequest request=new VopGoodsGetSellPriceRequest(); request.setSkuId(skuId); return client.execute(request); } // 根据分类id查询分类信息 public static VopGoodsGetCategoryInfoListResponse getCategoryInfoList(String token , String CategoryId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsGetCategoryInfoListRequest request=new VopGoodsGetCategoryInfoListRequest(); request.setCategoryId(CategoryId); return client.execute(request); } // 校验商品购买区域是否受限 public static VopGoodsCheckAreaLimitListResponse checkAreaLimitList(String token , String skuId , Long provinceId , Long cityId , Long countyId , Long townId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsCheckAreaLimitListRequest request=new VopGoodsCheckAreaLimitListRequest(); request.setSkuId(skuId); request.setProvinceId(provinceId); request.setCityId(cityId); request.setCountyId(countyId); request.setTownId(townId == null ? 0 : townId); return client.execute(request); } // 提交预占订单服务 public static VopOrderSubmitOrderResponse submitOrder(String token , GoodsUserAddress address , String orderNo , Integer skuNum , Long skuId) throws Exception { // 创建京东客户端 JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); // 创建订单提交请求 VopOrderSubmitOrderRequest request=new VopOrderSubmitOrderRequest(); // 创建订单提交请求参数 SubmitOrderOpenReq submitOrderOpenReq=new SubmitOrderOpenReq(); // 设置订单提交状态类型 submitOrderOpenReq.setSubmitStateType(0); // 设置第三方订单号 submitOrderOpenReq.setThirdOrderId(orderNo); // 创建订单提交商品列表 List submitOrderOpenSkus = new ArrayList<>(); // 创建订单提交商品信息 SkuInfoOrderOpenReq skuInfoOrderOpenReq=new SkuInfoOrderOpenReq(); // 设置商品数量 skuInfoOrderOpenReq.setSkuNum(skuNum); // 设置商品ID skuInfoOrderOpenReq.setSkuId(skuId); // 将商品信息添加到商品列表 submitOrderOpenSkus.add(skuInfoOrderOpenReq); // 设置订单提交商品列表 submitOrderOpenReq.setSkuInfoList(submitOrderOpenSkus); // 创建订单提交支付信息 PaymentInfoOrderOpenReq paymentInfoOrderOpenReq=new PaymentInfoOrderOpenReq(); // 设置支付类型 // 1:货到付款 4:预存款 5:公司转账 101:京东金采 102:商城金采(一般不适用,仅限确认开通商城账期的特殊情况使用,请与业务确认后使用) 20为混合支付 paymentInfoOrderOpenReq.setPaymentType(4); // 设置订单提交支付信息 submitOrderOpenReq.setPaymentInfo(paymentInfoOrderOpenReq); // 创建订单提交收货人信息 ConsigneeInfoOrderOpenReq infoOrderOpenReq = new ConsigneeInfoOrderOpenReq(); // 设置收货人地址 infoOrderOpenReq.setConsigneeAddress(address.getAddress()); // 设置收货人姓名 infoOrderOpenReq.setConsigneeName(address.getConsignee()); // 设置收货人手机号 infoOrderOpenReq.setConsigneeMobile(address.getPhone()); // 设置收货人电话 infoOrderOpenReq.setConsigneePhone(address.getPhone()); // 设置收货人省份ID infoOrderOpenReq.setConsigneeProvinceId(address.getProvinceId()); // 设置收货人城市ID infoOrderOpenReq.setConsigneeCityId(address.getCityId()); // 设置收货人区县ID infoOrderOpenReq.setConsigneeCountyId(address.getCountyId()); // 设置收货人乡镇ID infoOrderOpenReq.setConsigneeTownId(address.getTownId()); // 设置订单提交收货人信息 submitOrderOpenReq.setConsigneeInfo(infoOrderOpenReq); // 创建订单提交发票信息 InvoiceInfoOrderOpenReq invoiceInfoOrderOpenReq = new InvoiceInfoOrderOpenReq(); // 设置发票类型 invoiceInfoOrderOpenReq.setInvoiceType(24); // 设置发票开具类型 invoiceInfoOrderOpenReq.setInvoicePutType(2); // 设置发票抬头 invoiceInfoOrderOpenReq.setInvoiceCompanyName("惠兑礼品(重庆)有限公司"); invoiceInfoOrderOpenReq.setInvoiceRegCompanyName("惠兑礼品(重庆)有限公司"); invoiceInfoOrderOpenReq.setInvoiceRegPhone("13983618575"); // 设置发票抬头 invoiceInfoOrderOpenReq.setInvoiceSelectedTitle(5); // 设置发票类型 invoiceInfoOrderOpenReq.setInvoiceContentType(1); // 设置发票姓名 invoiceInfoOrderOpenReq.setInvoiceName("魏涛"); // 设置发票电话 invoiceInfoOrderOpenReq.setInvoicePhone("13983618575"); // 设置发票地址 invoiceInfoOrderOpenReq.setInvoiceAddress("重庆渝中区国贸中心1308"); // 设置发票开户银行 invoiceInfoOrderOpenReq.setInvoiceRegBank("中国民生银行股份有限公司重庆大坪支行"); // 设置发票纳税人识别号 invoiceInfoOrderOpenReq.setInvoiceRegCode("91500103322277192B"); // 设置发票注册地址 invoiceInfoOrderOpenReq.setInvoiceRegAddress("重庆渝中区国贸中心1308"); // 设置发票开户银行账号 invoiceInfoOrderOpenReq.setInvoiceRegBankAccount("172792234"); // 设置发票省份ID invoiceInfoOrderOpenReq.setInvoiceProvinceId(4L); // 设置发票城市ID invoiceInfoOrderOpenReq.setInvoiceCityId(51026L); // 设置发票区县ID invoiceInfoOrderOpenReq.setInvoiceCountyId(58459L); // 设置订单提交发票信息 submitOrderOpenReq.setInvoiceInfo(invoiceInfoOrderOpenReq); // 设置订单提交请求参数 request.setSubmitOrderOpenReq(submitOrderOpenReq); // 执行订单提交请求 return client.execute(request); } // 批量获取库存接口 public static VopGoodsGetNewStockByIdResponse getNewStockById(String token , Long skuId , Long provinceId , Long cityId , Long countyId , Long townId) throws Exception { JdClient client=new DefaultJdClient(SERVER_URL,token,CommonSysConst.getSysConfig().getJDAppKey(),CommonSysConst.getSysConfig().getJDAppSecret()); VopGoodsGetNewStockByIdRequest request=new VopGoodsGetNewStockByIdRequest(); GetStockByIdGoodsReq getStockByIdGoodsReq=new GetStockByIdGoodsReq(); List skuNumBaseGoodsReqs = new ArrayList<>(); SkuNumBaseGoodsReq skuNumBaseGoodsReq=new SkuNumBaseGoodsReq(); skuNumBaseGoodsReq.setSkuId(skuId); skuNumBaseGoodsReq.setSkuNumber(1); skuNumBaseGoodsReqs.add(skuNumBaseGoodsReq); getStockByIdGoodsReq.setSkuNumInfoList(skuNumBaseGoodsReqs); AreaBaseInfoGoodsReq areaBaseInfoGoodsReq=new AreaBaseInfoGoodsReq(); areaBaseInfoGoodsReq.setCityId(cityId); areaBaseInfoGoodsReq.setCountyId(countyId); areaBaseInfoGoodsReq.setProvinceId(provinceId); areaBaseInfoGoodsReq.setTownId(townId == null ? 0 : townId); getStockByIdGoodsReq.setAreaInfo(areaBaseInfoGoodsReq); request.setGetStockByIdGoodsReq(getStockByIdGoodsReq); return client.execute(request); } }