微信支付的订单 需要发货后才能从冻结账户转移到正常账户
需要参数:
订单号/支付号,用户openID,物流单号,快递公司编号,商品信息
不废话 直接干货 上代码
第一步 获取会话token
获取会话token token可以缓存至redis 有效期好像是七天
@Slf4j
public class AccessTokenUtil {
/**
* 获取accessToken
* @param appid 小程序appid
* @param secret 小程序secret
* @return
*/
public static String getAccessToken(String appid, String secret) {
String accessToken = null;
//获取会话tokcen接口
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appid+"&secret="+secret;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是GET
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = JSONObject.parseObject(message);
accessToken = demoJson.getString("access_token");
log.info("返回accessToken:"+accessToken);
log.info("initAccessToken:运行结束...");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
}
第二步 小程序发货
public static String uploadShippingInfo(String channel,String desc,String outTradeNo, String openid, String expressCompany, String trackingNo) {
String appId = null;
String secret = null;
appId = "***********";
secret = "***********";
String url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" + AccessTokenUtil.getAccessToken(
appId,
secret
);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
JSONObject orderKey = new JSONObject();
// 订单单号类型枚举值1/使用微信支付单号枚举值2。
orderKey.put("order_number_type", 1);
orderKey.put("mchid", "***********");
orderKey.put("out_trade_no", outTradeNo);
JSONObject shippingList = new JSONObject();
//商品信息
shippingList.put("item_desc", desc);
//物流单号
shippingList.put("tracking_no", trackingNo);
// 快递公司编码
shippingList.put("express_company", expressCompany);
JSONObject payer = new JSONObject();
payer.put("openid", "买家的openid 对应不上的话不能自动发货");
JSONObject signObject = new JSONObject();
JSONArray shipping_list = new JSONArray();
shipping_list.add(shippingList);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String uploadTime = simpleDateFormat.format(System.currentTimeMillis());
signObject.put("order_key", orderKey);
// 发货模式,发货模式枚举值:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货) 示例值: 1
signObject.put("delivery_mode", 1);
// 物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
signObject.put("logistics_type", 1);
signObject.put("shipping_list", shipping_list);
signObject.put("upload_time", uploadTime);
signObject.put("payer", payer);
String responseStr = HttpUtil.post(url, signObject.toString());
log.info("自动发货返回:" + responseStr);
String msgCode = JSONObject.parseObject(responseStr).getString("errcode");
return msgCode;
}
到此结束
可以定时器定期扫 根据自己平台的发货时效去扫