|
@@ -0,0 +1,85 @@
|
|
|
+package com.management.platform.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.management.platform.entity.Bill;
|
|
|
+import com.management.platform.entity.User;
|
|
|
+import com.management.platform.mapper.BillMapper;
|
|
|
+import com.management.platform.mapper.UserMapper;
|
|
|
+import com.management.platform.service.BillService;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {
|
|
|
+
|
|
|
+ private static final String APP_ID = "wxaaf19cfbbe1ff950";
|
|
|
+
|
|
|
+ private static final String MAIN_COM = "https://api.mch.weixin.qq.com";
|
|
|
+
|
|
|
+ private static final String PRE_PAY_URL = "/v3/pay/transactions/jsapi";
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private BillMapper billMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg getPrepayId(Integer id, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("TOKEN"));
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
|
|
+// headers.add("Authorization", authCode);//TODO 设置签名认证auth
|
|
|
+
|
|
|
+ Bill bill = billMapper.selectById(id);
|
|
|
+ Date now = new Date();
|
|
|
+ String seq = new SimpleDateFormat("yyyyMMddHHmmss").format(now);
|
|
|
+ bill.setSeq(seq + (1000 + bill.getId()%1000));
|
|
|
+
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("appid", APP_ID);
|
|
|
+// requestBody.put("mchid", mchid);//TODO 商户号
|
|
|
+// requestBody.put("description","")//TODO 商品真实信息
|
|
|
+ requestBody.put("out_trade_no",bill.getSeq());
|
|
|
+// requestBody.put("notify_url",notify_url)//TODO 异步回调地址
|
|
|
+ Map<String,Object> amount = new HashMap<>();
|
|
|
+ amount.put("total",bill.getMoney());// TODO 此处默认库中money为分,如改动
|
|
|
+ amount.put("currency","CNY");
|
|
|
+ requestBody.put("amount",amount);
|
|
|
+
|
|
|
+ Map<String,Object> payer = new HashMap<>();
|
|
|
+// payer.put("openid",openid);//TODO 用户在商户appid下的唯一标识 user.getXXX
|
|
|
+
|
|
|
+ HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
|
|
|
+ ResponseEntity<String> prepayResponse = restTemplate.exchange(
|
|
|
+ MAIN_COM+PRE_PAY_URL,
|
|
|
+ HttpMethod.POST,
|
|
|
+ requestEntity,
|
|
|
+ String.class
|
|
|
+ );
|
|
|
+ if (prepayResponse.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String prepayJson = prepayResponse.getBody();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(prepayJson);
|
|
|
+ String prepay_id = jsonObject.get("prepay_id").toString();
|
|
|
+ //TODO 更新prepay_id
|
|
|
+ }
|
|
|
+
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+}
|