| | |
| | | package org.springblade.modules.pay.controller; |
| | | |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; |
| | | import com.github.binarywang.wxpay.constant.WxPayConstants; |
| | | import com.google.gson.Gson; |
| | | import com.wechat.pay.contrib.apache.httpclient.auth.ScheduledUpdateCertificatesVerifier; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.AllArgsConstructor; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springblade.common.config.WxPayConfig; |
| | | import org.springblade.common.utils.HttpUtils; |
| | | import org.springblade.common.utils.WechatPay2ValidatorForRequest; |
| | | import org.springblade.core.boot.ctrl.BladeController; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.modules.pay.entity.WxPayInfo; |
| | | import org.springblade.modules.pay.service.IWxPayService; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @RestController |
| | | @AllArgsConstructor |
| | | @RequestMapping("/wxpay") |
| | | @Api(value = "微信支付接口", tags = "微信支付接口") |
| | | public class WxPayController extends BladeController { |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(WxPayController.class); |
| | | |
| | | private IWxPayService wxPayService; |
| | | |
| | |
| | | |
| | | /** |
| | | * 获取openId |
| | | * |
| | | * @param code |
| | | * @return |
| | | */ |
| | | @PostMapping("getOpenId") |
| | | public R getOpenId(String code){ |
| | | return R.data(wxPayService.getOpenId(code)); |
| | | public R getOpenId(String code) { |
| | | return R.data(wxPayService.getOpenId(code)); |
| | | } |
| | | |
| | | /** |
| | | * jsapi下单 |
| | | * |
| | | * @param productId |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | @ApiOperation("调用jsapi统一下单API") |
| | | @GetMapping("/jsapiPay") |
| | | public R jsapiPay(@RequestParam("productId") Long productId) throws Exception { |
| | | |
| | | logger.info("发起支付请求 v3"); |
| | | |
| | | //返回支付二维码连接和订单号 |
| | | Map<String, String> map = wxPayService.jsapiPay(productId); |
| | | |
| | | return R.data(map); |
| | | } |
| | | |
| | | /** |
| | | * 支付通知 |
| | | * 微信支付通过支付通知接口将用户支付成功消息通知给商户 |
| | | */ |
| | | @ApiOperation("支付通知") |
| | | @PostMapping("/native/notify") |
| | | public String nativeNotify(HttpServletRequest request, HttpServletResponse response) { |
| | | |
| | | Gson gson = new Gson(); |
| | | Map<String, String> map = new HashMap<>();//应答对象 |
| | | |
| | | try { |
| | | |
| | | //处理通知参数 |
| | | String body = HttpUtils.readData(request); |
| | | Map<String, Object> bodyMap = gson.fromJson(body, HashMap.class); |
| | | String requestId = (String) bodyMap.get("id"); |
| | | logger.info("支付通知的id ===> {}", requestId); |
| | | logger.info("支付通知的完整数据 ===> {}", body); |
| | | //int a = 9 / 0; |
| | | ScheduledUpdateCertificatesVerifier verifier = new WxPayConfig().getVerifier("111", "111", "111", "111"); |
| | | //签名的验证 |
| | | WechatPay2ValidatorForRequest wechatPay2ValidatorForRequest |
| | | = new WechatPay2ValidatorForRequest(verifier, requestId, body); |
| | | if (!wechatPay2ValidatorForRequest.validate(request)) { |
| | | logger.error("通知验签失败"); |
| | | //失败应答 |
| | | response.setStatus(500); |
| | | map.put("code", "ERROR"); |
| | | map.put("message", "通知验签失败"); |
| | | return gson.toJson(map); |
| | | } |
| | | logger.info("通知验签成功"); |
| | | |
| | | //处理订单 |
| | | wxPayService.processOrder(bodyMap); |
| | | |
| | | //应答超时 |
| | | //模拟接收微信端的重复通知 |
| | | // TimeUnit.SECONDS.sleep(5); |
| | | |
| | | //成功应答 |
| | | response.setStatus(200); |
| | | map.put("code", "SUCCESS"); |
| | | map.put("message", "成功"); |
| | | return gson.toJson(map); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | //失败应答 |
| | | response.setStatus(500); |
| | | map.put("code", "ERROR"); |
| | | map.put("message", "失败"); |
| | | return gson.toJson(map); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | } |