gongzuming
2024-09-19 a768dc3daa04d35fedfbe75c0a59b9b2545b85c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 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) throws Exception {
        String requestPath = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token="+accessToken;
        Map<String, Object> headers = new HashMap<>();
        headers.put("Accept", MediaType.APPLICATION_JSON.getValue());
        headers.put("Content-Type", MediaType.APPLICATION_JSON.getValue());
        Map<String,Object> body = new HashMap<>();
        Map<String,Object> 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("logistics_type",2);//物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
        body.put("delivery_mode",1);//发货模式,发货模式枚举值:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货) 示例值: UNIFIED_DELIVERY
 
        //商品信息
        List<Map<String,Object>> shippingList = new ArrayList<>();
        Map<String,Object> itemMap = new HashMap<>();
        itemMap.put("item_desc",itemDesc);//商品描述
        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<String,Object> 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;
    }
 
}