package com.mzl.flower.service.payment; import com.alibaba.fastjson.JSONObject; import com.mzl.flower.service.system.WeChatService; import com.mzl.flower.utils.HttpUtil; import com.wechat.pay.java.core.http.*; import com.wechat.pay.java.core.util.GsonUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @Service @Transactional @Slf4j public class WxDeliveryGoodService { public JSONObject wxDeliveryGood(String orderId, String openid, String transactionId, String itemDesc, String merchantId,String accessToken, Long partnerID,String deliveryNo,String logisticsCompanyCode, String maskedNumber) throws Exception { String requestPath = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token="+accessToken; Map headers = new HashMap<>(); headers.put("Accept", MediaType.APPLICATION_JSON.getValue()); headers.put("Content-Type", MediaType.APPLICATION_JSON.getValue()); Map body = new HashMap<>(); Map orderKey = new HashMap<>(); orderKey.put("order_number_type",2);//枚举值1,使用下单商户号和商户侧单号;枚举值2,使用微信支付单号。 orderKey.put("transaction_id",transactionId);//原支付交易对应的微信订单号 orderKey.put("mchid",merchantId);//支付下单商户的商户号,由微信支付生成并下发。 orderKey.put("out_trade_no",orderId);//商户系统内部订单号 body.put("order_key",orderKey); body.put("delivery_mode",1);//发货模式,发货模式枚举值:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货) 示例值: UNIFIED_DELIVERY if (StringUtils.isEmpty(partnerID) && !StringUtils.isEmpty(deliveryNo)) { // 如果是散户的情况下保存订单号 body.put("logistics_type", 1);//物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提 } else { body.put("logistics_type", 2);//物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提 } //商品信息 List> shippingList = new ArrayList<>(); Map itemMap = new HashMap<>(); itemMap.put("item_desc",itemDesc);//商品描述 if (StringUtils.isEmpty(partnerID) && !StringUtils.isEmpty(deliveryNo)) { itemMap.put("tracking_no", deliveryNo);//物流单号,物流快递发货时必填,示例值: 323244567777 字符字节限制: [1, 128] itemMap.put("express_company", logisticsCompanyCode);//物流公司编码,快递公司ID,参见「查询物流公司编码列表」,物流快递发货时必填, 示例值: DHL 字符字节限制: [1, 128] //联系方式,当发货的物流公司为顺丰时,联系方式为必填,收件人或寄件人联系方式二选一 Map contact = new HashMap<>(); contact.put("receiver_contact",maskedNumber);//收件人联系方式,收件人联系方式为,采用掩码传输,最后4位数字不能打掩码 示例值: `189****1234, 021-****1234, ****1234, 0**2-***1234, 0**2-******23-10, ****123-8008` 值限制: 0 ≤ value ≤ 1024 itemMap.put("contact", contact); } shippingList.add(itemMap); body.put("shipping_list",shippingList); // 获取当前日期和时间 ZonedDateTime zonedDateTime = ZonedDateTime.now(); // 创建一个DateTimeFormatter DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // 使用formatter格式化ZonedDateTime String formattedDateTime = zonedDateTime.format(formatter);//2024-08-19T14:02:28:424+08:00 body.put("upload_time",formattedDateTime);//上传时间,用于标识请求的先后顺序 示例值: `2022-12-15T13:29:35.120+08:00` //支付者,支付者信息 Map payer = new HashMap<>(); payer.put("openid",openid); body.put("payer",payer); log.info("微信发货请求信息:"+GsonUtil.toJson(body)); JSONObject json = HttpUtil.doRequest(HttpMethod.POST.name(), requestPath, "json", headers, null, body, new HashMap<>()); log.info("微信发货返回信息:"+GsonUtil.toJson(json)); return json; } }