| @@ -1,5 +1,6 @@ | |||
| package com.simple.controller; | |||
| import com.simple.enums.EnumOrderStatus; | |||
| import org.apache.log4j.Logger; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.util.Assert; | |||
| @@ -17,33 +18,80 @@ import io.swagger.annotations.ApiOperation; | |||
| @RestController | |||
| @RequestMapping("wxOrders") | |||
| public class WxOrdersController extends BaseController | |||
| { | |||
| @Autowired | |||
| public class WxOrdersController extends BaseController { | |||
| @Autowired | |||
| private WxOrdersService wxOrdersService; | |||
| private Logger logger = Logger.getLogger(WxOrdersController.class); | |||
| @ApiOperation("分页列表接口") | |||
| @ApiOperation("分页列表接口") | |||
| @GetMapping("list") | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true), | |||
| @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)}) | |||
| public ResultData list(@ModelAttribute WxOrders wxOrders,Integer pageNum, Integer pageSize) { | |||
| if (null == wxOrders) wxOrders = new WxOrders(); | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name = "cUserId", value = "用户ID", dataType = "bigint", paramType = "query", required = true), | |||
| @ApiImplicitParam(name = "status", value = "订单状态:-1全部;0-已下单/待付款;1-已支付;2-已取消(限定时间内未付款);3-未退款/待退款;4-已退款;5-退款失败", dataType = "int", paramType = "query", required = true), | |||
| @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true), | |||
| @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)}) | |||
| public ResultData list(@ModelAttribute WxOrders wxOrders, Integer pageNum, Integer pageSize) { | |||
| if (null == wxOrders) wxOrders = new WxOrders(); | |||
| final PageInfo<WxOrders> page = wxOrdersService.listAsPage(wxOrders, pageNum, pageSize); | |||
| return new ResultData(page); | |||
| } | |||
| @ApiOperation("新增接口") | |||
| @PostMapping("add") | |||
| public ResultData add(@RequestBody WxOrders wxOrders) { | |||
| @ApiOperation("提交订单") | |||
| @PostMapping("save") | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name = "couponId", value = "券ID", required = true, dataType = "Long"), | |||
| @ApiImplicitParam(name = "cUserId", value = "用户ID", required = true, dataType = "Long"), | |||
| @ApiImplicitParam(name = "payment", value = "支付金额", required = true, dataType = "BigDecimal"), | |||
| @ApiImplicitParam(name = "status", value = "订单状态:-1全部;0-已下单/待付款;1-已支付;2-已取消(限定时间内未付款);3-未退款/待退款;4-已退款;5-退款失败", defaultValue = "0", required = false, dataType = "Integer") | |||
| }) | |||
| public ResultData saveOrder(@RequestBody WxOrders wxOrders) { | |||
| //Assert.notNull(wxOrders.getName(), "角色名不能为空"); | |||
| //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); | |||
| wxOrdersService.saveOrUpdate(wxOrders); | |||
| wxOrdersService.saveOrder(wxOrders); | |||
| return new ResultData(); | |||
| } | |||
| @ApiOperation("取消订单") | |||
| @PostMapping("cancel") | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long"), | |||
| @ApiImplicitParam(name = "cUserId", value = "用户ID", required = true, dataType = "Long") | |||
| }) | |||
| public ResultData cancelOrder(@RequestBody WxOrders wxOrders) { | |||
| //Assert.notNull(wxOrders.getName(), "角色名不能为空"); | |||
| //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); | |||
| wxOrdersService.updateOrderStatus(wxOrders.getId(), EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL); | |||
| return new ResultData(); | |||
| } | |||
| @ApiOperation("订单过期") | |||
| @PostMapping("expiring") | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long"), | |||
| @ApiImplicitParam(name = "cUserId", value = "用户ID", required = true, dataType = "Long") | |||
| }) | |||
| public ResultData expiringOrder(@RequestBody WxOrders wxOrders) { | |||
| //Assert.notNull(wxOrders.getName(), "角色名不能为空"); | |||
| //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); | |||
| wxOrdersService.updateOrderStatus(wxOrders.getId(), EnumOrderStatus.ORDER_STATUS_PENDING_REFUND); | |||
| return new ResultData(); | |||
| } | |||
| @ApiOperation("订单已过期") | |||
| @PostMapping("expired") | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long"), | |||
| @ApiImplicitParam(name = "cUserId", value = "用户ID", required = true, dataType = "Long") | |||
| }) | |||
| public ResultData expiredOrder(@RequestBody WxOrders wxOrders) { | |||
| //Assert.notNull(wxOrders.getName(), "角色名不能为空"); | |||
| //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); | |||
| wxOrdersService.updateOrderStatus(wxOrders.getId(), EnumOrderStatus.ORDER_STATUS_REFUND_SUCCESS); | |||
| return new ResultData(); | |||
| } | |||
| @ApiOperation("根据id更新接口") | |||
| @PostMapping("update") | |||
| public ResultData update(@RequestBody WxOrders wxOrders) { | |||
| @@ -53,19 +101,18 @@ public class WxOrdersController extends BaseController | |||
| @ApiOperation("根据id删除接口") | |||
| @GetMapping("/del") | |||
| @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) | |||
| @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) | |||
| public ResultData delete(Long id) { | |||
| wxOrdersService.deleteById(id); | |||
| return new ResultData(Result.SUCCESS, "删除成功", null); | |||
| } | |||
| @ApiOperation("根据id查询接口") | |||
| @GetMapping("/findById") | |||
| @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) | |||
| @ApiOperation("根据id查询接口") | |||
| @GetMapping("/findById") | |||
| @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) | |||
| public ResultData findById(Long id) { | |||
| return new ResultData(Result.SUCCESS,"查询成功",wxOrdersService.getById(id)); | |||
| return new ResultData(Result.SUCCESS, "查询成功", wxOrdersService.getById(id)); | |||
| } | |||
| } | |||
| @@ -0,0 +1,42 @@ | |||
| package com.simple.enums; | |||
| /** | |||
| * Created by Stormeye on 2018/08/09. | |||
| */ | |||
| public enum EnumOrderStatus { | |||
| // 0-已下单/待付款;1-已支付;2-已取消(限定时间内未付款);3-未退款/待退款;4-已退款;5-退款失败 | |||
| ORDER_STATUS_PENDING_PAYMENT(0, "待付款"), | |||
| ORDER_STATUS_PAYMENT_SUCCESS(1, "已支付"), | |||
| ORDER_STATUS_OVERTIME_CANCEL(2, "已取消"), | |||
| ORDER_STATUS_PENDING_REFUND(3, "待退款"), | |||
| ORDER_STATUS_REFUND_SUCCESS(4,"已退款"), | |||
| ORDER_STATUS_REFUND_FAILD(5, "退款失败") | |||
| ; | |||
| public static EnumOrderStatus getEnum(Integer code) { | |||
| for (EnumOrderStatus value : values()) { | |||
| if (value.getCode().equals(code)) { | |||
| return value; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| private Integer code; | |||
| private String message; | |||
| EnumOrderStatus(Integer code, String message) { | |||
| this.code = code; | |||
| this.message = message; | |||
| } | |||
| public Integer getCode() { | |||
| return code; | |||
| } | |||
| public String getMessage() { | |||
| return message; | |||
| } | |||
| } | |||
| @@ -66,7 +66,7 @@ public class WxPayment { | |||
| * @param paternerKey | |||
| * @return | |||
| */ | |||
| public static Map<String, String> buildParasMap(String appid, String sub_appid, String mch_id, String sub_mch_id, | |||
| public static Map<String, String> buildQueryParasMap(String appid, String sub_appid, String mch_id, String sub_mch_id, | |||
| String transaction_id, String out_trade_no, String paternerKey) { | |||
| Map<String, String> params = new HashMap<String, String>(); | |||
| @@ -101,15 +101,13 @@ public class WxPayment { | |||
| * @param spbill_create_ip | |||
| * @param paternerKey | |||
| * @param notify_url | |||
| * @param trade_type | |||
| * @param product_id | |||
| * 扫码支付必传 | |||
| * @param openId // JSAPI 必传 | |||
| * @return | |||
| */ | |||
| public static Map<String, String> buildUnifiedOrderParasMap(String appid, String sub_appid, String mch_id, | |||
| String sub_mch_id, String device_info, String body, String detail, String attach, String out_trade_no, | |||
| String total_fee, String spbill_create_ip, String notify_url, String trade_type, String paternerKey, | |||
| String product_id) { | |||
| String total_fee, String spbill_create_ip, String notify_url, String paternerKey, | |||
| String openId) { | |||
| Map<String, String> params = new HashMap<String, String>(); | |||
| params.put("appid", appid); | |||
| params.put("sub_appid", sub_appid); | |||
| @@ -120,11 +118,15 @@ public class WxPayment { | |||
| params.put("detail", detail); | |||
| params.put("attach", attach); | |||
| params.put("out_trade_no", out_trade_no); | |||
| params.put("total_fee", total_fee); | |||
| params.put("spbill_create_ip", spbill_create_ip); | |||
| params.put("notify_url", notify_url); | |||
| params.put("trade_type", trade_type); | |||
| params.put("product_id", product_id); | |||
| params.put("trade_type", "JSAPI"); | |||
| // params.put("product_id", product_id); // trade_type=NATIVE时(即扫码支付)必传 | |||
| params.put("openid", openId); | |||
| return buildSignAfterParasMap(params, paternerKey); | |||
| } | |||
| @@ -153,6 +155,93 @@ public class WxPayment { | |||
| } | |||
| /** | |||
| * 关闭订单 | |||
| * | |||
| * @param appid | |||
| * @param sub_appid | |||
| * @param mch_id | |||
| * @param sub_mch_id | |||
| * @param out_trade_no | |||
| * @param paternerKey | |||
| * @return | |||
| */ | |||
| public static Map<String, String> buildCloseMap(String appid, String sub_appid, String mch_id, String sub_mch_id, | |||
| String out_trade_no, String paternerKey) { | |||
| Map<String, String> params = new HashMap<String, String>(); | |||
| params.put("appid", appid); | |||
| params.put("sub_appid", sub_appid); | |||
| params.put("mch_id", mch_id); | |||
| params.put("sub_mch_id", sub_mch_id); | |||
| params.put("out_trade_no", out_trade_no); | |||
| return buildSignAfterParasMap(params, paternerKey); | |||
| } | |||
| /** | |||
| * 申请退款 | |||
| * | |||
| * @param appid | |||
| * @param sub_appid | |||
| * @param mch_id | |||
| * @param sub_mch_id | |||
| * @param out_trade_no | |||
| * @param paternerKey | |||
| * @return | |||
| */ | |||
| public static Map<String, String> buildRefundMap(String appid, String sub_appid, String mch_id, String sub_mch_id, | |||
| String transaction_id, String out_trade_no, String out_refund_no, | |||
| String total_fee, String refund_fee, String refund_desc, String paternerKey) { | |||
| Map<String, String> params = new HashMap<String, String>(); | |||
| params.put("appid", appid); | |||
| params.put("sub_appid", sub_appid); | |||
| params.put("mch_id", mch_id); | |||
| params.put("sub_mch_id", sub_mch_id); | |||
| params.put("transaction_id", transaction_id); | |||
| params.put("out_trade_no", out_trade_no); | |||
| params.put("out_refund_no", out_refund_no); | |||
| params.put("total_fee", total_fee); | |||
| params.put("refund_fee", refund_fee); | |||
| params.put("refund_desc", refund_desc); | |||
| return buildSignAfterParasMap(params, paternerKey); | |||
| } | |||
| /** | |||
| * 查询退款 | |||
| * | |||
| * @param appid | |||
| * @param sub_appid | |||
| * @param mch_id | |||
| * @param sub_mch_id | |||
| * @param out_trade_no | |||
| * @param paternerKey | |||
| * @return | |||
| */ | |||
| public static Map<String, String> buildRefundQueryMap(String appid, String sub_appid, String mch_id, String sub_mch_id, | |||
| String transaction_id, String out_trade_no, String out_refund_no, String refund_id, | |||
| String paternerKey) { | |||
| Map<String, String> params = new HashMap<String, String>(); | |||
| params.put("appid", appid); | |||
| params.put("sub_appid", sub_appid); | |||
| params.put("mch_id", mch_id); | |||
| params.put("sub_mch_id", sub_mch_id); | |||
| if(!StringUtils.isBlank(refund_id)) { | |||
| params.put("refund_id", refund_id); | |||
| } else if (!StringUtils.isBlank(out_refund_no)) { | |||
| params.put("out_refund_no", out_refund_no); | |||
| } else if (!StringUtils.isBlank(transaction_id)) { | |||
| params.put("transaction_id", transaction_id); | |||
| } else if (!StringUtils.isBlank(out_trade_no)) { | |||
| params.put("out_trade_no", out_trade_no); | |||
| } | |||
| return buildSignAfterParasMap(params, paternerKey); | |||
| } | |||
| /** | |||
| * 组装签名的字段 | |||
| * | |||
| @@ -3,6 +3,7 @@ package com.simple.service; | |||
| import java.util.*; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.simple.domain.po.WxOrders; | |||
| import com.simple.enums.EnumOrderStatus; | |||
| public interface WxOrdersService { | |||
| @@ -15,6 +16,20 @@ public interface WxOrdersService { | |||
| * @return | |||
| */ | |||
| PageInfo<WxOrders> listAsPage(WxOrders record, Integer pageIndex, Integer pageSize); | |||
| /** | |||
| * 提交订单 | |||
| * @param record | |||
| * @return 订单id | |||
| */ | |||
| WxOrders saveOrder(WxOrders record); | |||
| /** | |||
| * 更新订单状态 | |||
| * @param orderId | |||
| * @param enumOrderStatus | |||
| */ | |||
| void updateOrderStatus(Long orderId,EnumOrderStatus enumOrderStatus); | |||
| /** | |||
| * 根据Id获得实体 | |||
| @@ -4,8 +4,10 @@ import java.util.*; | |||
| import com.github.pagehelper.PageHelper; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.simple.domain.po.WxOrders; | |||
| import com.simple.enums.EnumOrderStatus; | |||
| import com.simple.mapper.WxOrdersMapper; | |||
| import com.simple.service.WxOrdersService; | |||
| import com.simple.utils.Utility; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import com.simple.common.IdWorker; | |||
| @@ -22,6 +24,28 @@ public class WxOrdersServiceImpl implements WxOrdersService { | |||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxOrdersMapper.findList(record)); | |||
| } | |||
| @Override | |||
| public WxOrders saveOrder(WxOrders record) { | |||
| // 检查 优惠券 库存 | |||
| // 保存订单 | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| record.setId(idWorker.nextId()); | |||
| record.setStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | |||
| Date curr = new Date(); | |||
| record.setCreateDate(curr); | |||
| record.setUpdateDate(curr); | |||
| wxOrdersMapper.insertSelective(record); | |||
| return record; | |||
| } | |||
| @Override | |||
| public void updateOrderStatus(Long orderId,EnumOrderStatus enumOrderStatus) { | |||
| WxOrders record = wxOrdersMapper.selectByPrimaryKey(orderId); | |||
| record.setStatus(enumOrderStatus.getCode()); | |||
| record.setUpdateDate(new Date()); | |||
| wxOrdersMapper.updateByPrimaryKey(record); | |||
| } | |||
| @Override | |||
| public WxOrders getById(Long id) { | |||
| return wxOrdersMapper.selectByPrimaryKey(id); | |||
| @@ -11,11 +11,13 @@ import org.apache.http.client.entity.UrlEncodedFormEntity; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.client.methods.HttpPost; | |||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.apache.http.impl.client.HttpClients; | |||
| import org.apache.http.message.BasicNameValuePair; | |||
| import org.apache.http.protocol.HTTP; | |||
| import org.apache.http.ssl.SSLContexts; | |||
| import org.apache.http.util.EntityUtils; | |||
| import javax.net.ssl.HttpsURLConnection; | |||
| @@ -255,5 +257,61 @@ public class HttpUtil { | |||
| } | |||
| } | |||
| private static String ClientCustomSSL(String url, String p12file, String mch_id, String xmlStr) throws Exception{ | |||
| KeyStore keyStore = KeyStore.getInstance("PKCS12"); | |||
| FileInputStream instream = new FileInputStream(new File(p12file)); | |||
| try { | |||
| keyStore.load(instream, mch_id.toCharArray()); | |||
| } finally { | |||
| instream.close(); | |||
| } | |||
| // Trust own CA and all self-signed certs | |||
| SSLContext sslcontext = SSLContexts.custom() | |||
| .loadKeyMaterial(keyStore, mch_id.toCharArray()) | |||
| .build(); | |||
| // Allow TLSv1 protocol only | |||
| SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( | |||
| sslcontext, | |||
| new String[] { "TLSv1" }, | |||
| null, | |||
| SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); | |||
| CloseableHttpClient httpclient = HttpClients.custom() | |||
| .setSSLSocketFactory(sslsf) | |||
| .build(); | |||
| try { | |||
| HttpPost httpPost = new HttpPost(url); | |||
| StringEntity entityStr = new StringEntity(xmlStr); | |||
| entityStr.setContentType("text/xml"); | |||
| System.out.println("entityStr--------------"+entityStr); | |||
| httpPost.setEntity(entityStr); | |||
| CloseableHttpResponse response = httpclient.execute(httpPost); | |||
| try { | |||
| HttpEntity entity = response.getEntity(); | |||
| System.out.println("----------------------------------------"); | |||
| System.out.println(response.getStatusLine()); | |||
| if (entity != null) { | |||
| System.out.println("Response content length: " + entity.getContentLength()); | |||
| BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); | |||
| StringBuilder sb = new StringBuilder(); | |||
| String line = null; | |||
| while ((line = bufferedReader.readLine()) != null) { | |||
| sb.append(line).append("\n"); | |||
| } | |||
| return sb.toString(); | |||
| } | |||
| EntityUtils.consume(entity); | |||
| } finally { | |||
| response.close(); | |||
| } | |||
| } finally { | |||
| httpclient.close(); | |||
| return null; | |||
| } | |||
| } | |||
| } | |||