订单推送接口接入之前必须接入担保支付。具体操作请参见担保支付接入指南。
/**
* 普通小程序订单订单状态,POI 订单可以忽略
* 0:待支付
* 1:已支付
* 2:已取消
* 4:已核销
* 5:退款中
* 6:已退款
* 8:退款失败
* 注意:普通小程序订单必传,担保支付分账依赖该状态
*
*
* @param open_id 用户openid
* @param order_status
* @param orderNo. 商户订单号
* @param status
* @param totalPrice 金额分
* @return
*/
public String pushOrder(String open_id , Integer order_status , String orderNo , String status , Integer totalPrice){
JSONArray list = new JSONArray();
JSONObject item = new JSONObject();
item.put("item_code" , System.currentTimeMillis()+"");
item.put("img" , "https://m.z2oddstips.com/static/img/bn1.jpg");
item.put("title" , "智二足球年会员");
item.put("sub_title" , "智二足球年会员");
item.put("price" , totalPrice);
list.add(item);
JSONObject orderDetail = new JSONObject();
orderDetail.put("order_id" , orderNo);
orderDetail.put("create_time" , System.currentTimeMillis());
orderDetail.put("status" , status);
orderDetail.put("amount" , 1);
orderDetail.put("total_price" , totalPrice);
orderDetail.put("detail_url" , "pages/paymentList/paymentList");
orderDetail.put("item_list" , list); //子订单商品列表,不可为空
JSONObject json = new JSONObject();
String accessToken = getAccessToken();
json.put("access_token",accessToken);
json.put("app_name","douyin");
json.put("open_id", open_id);
json.put("order_status", order_status);
json.put("order_type", 0);
json.put("update_time", System.currentTimeMillis());
json.put("order_detail", orderDetail.toJSONString());
doPostJson("https://developer.toutiao.com/api/apps/order/v2/push",json.toJSONString(), "UTF-8");
return "success";
}
``
//获取AcessToken
public String getAccessToken( ){
JSONObject json = new JSONObject();
json.put("appid" , appid);
json.put("secret" , appSecret);
json.put("grant_type" , "client_credential");
String postJson = doPostJson("https://developer.toutiao.com/api/apps/v2/token",json.toJSONString(), "UTF-8");
JSONObject jo = JSONObject.parseObject(postJson);
return jo.getJSONObject("data").getString("access_token");
}
//post请求工具类
public static String doPostJson(String url, String jsonStr, String charset) {
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
System.out.println("请求参数"+jsonStr);
// 设置参数
StringEntity s = new StringEntity(jsonStr,"utf-8");
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
httpPost.setHeader("Content-Type","application/json;charset=utf-8");
httpPost.setEntity(s);
org.apache.http.HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
System.out.println("response body:" + result);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}