|
|
@ -1,5 +1,8 @@ |
|
|
|
package com.yxt.pay.biz.order; |
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.sun.org.apache.bcel.internal.generic.RETURN; |
|
|
|
import com.yxt.common.base.service.MybatisBaseService; |
|
|
|
import com.yxt.common.core.result.ResultBean; |
|
|
@ -13,7 +16,7 @@ import com.yxt.pay.utils.wx.SignType; |
|
|
|
import com.yxt.pay.utils.wx.WxPayKit; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.http.*; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
@ -23,6 +26,8 @@ import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.URL; |
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.*; |
|
|
@ -88,6 +93,7 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
return rb.setMsg("订单不是未支付,不能重复支付"); |
|
|
|
} |
|
|
|
Map<Object, Object> resultObj = new TreeMap(); |
|
|
|
String date = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); |
|
|
|
if (query.getType().equals(PayTypeEnum.WECHAT.getCode())) {//微信
|
|
|
|
WxPayVo wxPayVo = new WxPayVo(payOrder.getSource()); |
|
|
|
String nonceStr = CharUtil.getRandomString(32); |
|
|
@ -146,6 +152,10 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
resultObj.put("prepayid", prepay_id);//预支付交易会话ID
|
|
|
|
payOrder.setPayType(PayTypeEnum.WECHAT.getCode()); |
|
|
|
baseMapper.updateById(payOrder); |
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
map.put("mainSid", payOrder.getSid()); |
|
|
|
map.put("payTime", date); |
|
|
|
ResponseEntity<ResultBean> postForEntity = new RestTemplate().postForEntity(payOrder.getPayUrl(), map, ResultBean.class); |
|
|
|
return rb.success().setData(resultObj); |
|
|
|
} |
|
|
|
|
|
|
@ -319,6 +329,8 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
baseMapper.updateById(payOrder); |
|
|
|
//调业务的回调地址接口。mainSid传过去。
|
|
|
|
ResponseEntity<ResultBean> postForEntity = new RestTemplate().getForEntity(payOrder.getReturnUrl() + payOrder.getSid(), ResultBean.class); |
|
|
|
String errCode = toSendGoods(payOrder); |
|
|
|
log.info("发货通知errcode======" + errCode); |
|
|
|
// 发送通知等
|
|
|
|
Map<String, String> xml = new HashMap<String, String>(2); |
|
|
|
xml.put("return_code", "SUCCESS"); |
|
|
@ -331,4 +343,75 @@ public class OrderService extends MybatisBaseService<OrderMapper, PayOrder> { |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public String toSendGoods(PayOrder payOrder) { |
|
|
|
String accessToken = getAccessToken(payOrder); |
|
|
|
WxPayVo wxPayVo = new WxPayVo(payOrder.getSource()); |
|
|
|
String url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" + accessToken; |
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON); |
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
map.put("mchid", wxPayVo.getMchId()); |
|
|
|
map.put("out_trade_no", payOrder.getOutTradeNo()); |
|
|
|
map.put("order_number_type", 1); |
|
|
|
|
|
|
|
Map<String, Object> map2 = new HashMap<>(); |
|
|
|
map2.put("openid", payOrder.getOpenId()); |
|
|
|
Map<String, Object> map3 = new HashMap<>(); |
|
|
|
map3.put("item_desc", payOrder.getName()); |
|
|
|
|
|
|
|
JSONObject signObject = new JSONObject(); |
|
|
|
JSONArray objects = new JSONArray(); |
|
|
|
objects.add(map3); |
|
|
|
|
|
|
|
signObject.put("order_key", map); |
|
|
|
signObject.put("delivery_mode", 1); |
|
|
|
//物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式
|
|
|
|
// 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
|
|
|
|
signObject.put("logistics_type", 4); |
|
|
|
signObject.put("shipping_list", objects); |
|
|
|
// signObject.put("upload_time", "2023-10-23T0:08:35.120+08:00");
|
|
|
|
signObject.put("upload_time", DateUtil.format(new Date(), "yyyy-MM-dd'T'HH:mm:ssXXX")); |
|
|
|
signObject.put("payer", map2); |
|
|
|
System.out.println(signObject); |
|
|
|
|
|
|
|
HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(signObject, headers); |
|
|
|
ResponseEntity<String> responseEntity = new RestTemplate().exchange(url, HttpMethod.POST, requestEntity, String.class); |
|
|
|
String body = responseEntity.getBody(); |
|
|
|
JSONObject jsonObject = JSONArray.parseObject(body); |
|
|
|
return jsonObject.getString("errcode"); |
|
|
|
} |
|
|
|
|
|
|
|
public String getAccessToken(PayOrder payOrder) { |
|
|
|
WxPayVo wxPayVo = new WxPayVo(payOrder.getSource()); |
|
|
|
log.info("initAccessToken:开始运行..."); |
|
|
|
String accessToken = null; |
|
|
|
//获取tokcen接口
|
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxPayVo.getAppId() + "&secret=" + wxPayVo.getAppSecret(); |
|
|
|
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");// 连接超时30秒
|
|
|
|
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
|
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|