Waitme9 2023-01-15 12:55 采纳率: 79.4%
浏览 54
已结题

在微信小程序开发微信支付的过程中出现的问题:

在微信小程序开发微信支付的过程中出现了以下问题:

过滤器执行了
2023-01-15 12:46:50,121 INFO  com.***.InterceptOrFilter.TokenInterceptor 77 preHandle - 请求方法 wxPay 上有注解 @com.***.note.IgnoreToken() 
2023-01-15 12:46:50,577 INFO  com.github.binarywang.wxpay.service.impl.WxPayServiceApacheHttpImpl 75 post - 
【请求地址】:https://api.mch.weixin.qq.com/pay/unifiedorder
【请求数据】:<xml>
  <appid>***</appid>
  <mch_id>***</mch_id>
  <nonce_str>1673758010122</nonce_str>
  <sign>***</sign>
  <sign_type>MD5</sign_type>
  <body>描述</body>
  <out_trade_no>123</out_trade_no>
  <total_fee>100</total_fee>
  <spbill_create_ip>0:0:0:0:0:0:0:1</spbill_create_ip>
  <time_start>20230115124650</time_start>
  <time_expire>20230115125650</time_expire>
  <notify_url>http://localhost:8080/***/pay/payNotify</notify_url>
  <trade_type>JSAPI</trade_type>
  <openid>***</openid>
</xml>
【响应数据】:<xml><return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[签名错误,请检查后再试]]></return_msg>
</xml>
2023-01-15 12:46:50,593 ERROR com.github.binarywang.wxpay.bean.result.BaseWxPayResult 363 checkResult - 
结果业务代码异常,返回结果:{return_msg=签名错误,请检查后再试, return_code=FAIL},
返回代码:FAIL,返回信息:签名错误,请检查后再试
收到事件ServletRequestHandledEvent: url=[/***/pay/api/topay/***]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[473ms]; status=[OK]
io.undertow.util.BadRequestException
    at com.sldndemo.controller.PayAdminController.wxPay(PayAdminController.java:133)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:523)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:590)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at com.sldndemo.config.CorsFilter.doFilter(CorsFilter.java:28)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:370)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2019)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1558)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1449)
    at java.lang.Thread.run(Thread.java:745)


以下是业务代码:

@Resource
    private final WxPayService wxPayService = null;


@IgnoreToken
    @PostMapping("/api/topay/{openId}")
    public Object wxPay(HttpServletRequest request,@PathVariable String openId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //获取请求的ip地址
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if(ip.indexOf(",")!=-1){
            String[] ips = ip.split(",");
            ip = ips[0].trim();
        }
        /**
         * 处理内部业务,校验订单等
         */
        final WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder()
                //调起支付的人的 openId
                .openid(openId)
                //订单编号
                .outTradeNo("123")//订单号
                //小程序支付
                .tradeType(WxPayConstants.TradeType.JSAPI)
                //订单金额(单位是分)BaseWxPayRequest.yuanToFen()这个方法是将元转分
                .totalFee(BaseWxPayRequest.yuanToFen("1"))
                //商品描述
                .body("描述")
                //获取本地IP
                .spbillCreateIp(ip)
                //回调的 URL 地址
                .notifyUrl(PublicInfo.MyURL+"pay/payNotify")
                //过期时间 格式yyyyMMddHHmmss (一般设置个10分钟后)
                .timeExpire(sdf.format(System.currentTimeMillis() + 10 * 60 * 1000))
                //当前时间 格式yyyyMMddHHmmss
                .timeStart(sdf.format(System.currentTimeMillis()))
                .build();
        wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5);

        Object order = null;
        try {
            order = wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            try {
                throw new BadRequestException(e.getErrCodeDes());
            } catch (BadRequestException ex) {
                ex.printStackTrace();
            }
        }
        return order;
    }



//回调接口
    @ApiOperation("微信支付回调接口")
    @RequestMapping(value = "/payNotify")
    public String payNotify(HttpServletRequest request) {
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlResult);
            //getOutTradeNo 自己生成的订单号
            String orderId = notifyResult.getOutTradeNo();
            //支付成功
            if("SUCCESS".equals(notifyResult.getResultCode())) {
                System.out.println("微信支付回调:订单号"+orderId);
                //自己处理订单的业务逻辑,需要判断订单是否已经支付过,否则可能会重复调用

            }
            //必须要给微信返回回调成功状态,否则微信会一直回调
            return WxPayNotifyResponse.success("成功");
        } catch (Exception e) {
            System.out.println("微信回调结果异常,异常原因"+e.getMessage());
            return WxPayNotifyResponse.success("code:"+9999+"微信回调结果异常,异常原因:"+e.getMessage());
        }
    }

目前密钥(mch-key)是一串六位的数字,例如123456
请问该如何解决?

  • 写回答

3条回答 默认 最新

  • Halifax ‎ 2023-01-15 13:27
    关注

    望采纳。。。


    有可能是openid或者appid用错了
    还有可能是:API密钥格式的原因,记住:一定要数字,大小写字母组合。


    建议按照下面的步骤先排查一下:
    1、使用签名检查工具(https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=20_1%EF%BC%89%E6%A0%A1%E9%AA%8C%E7%AD%BE%E5%90%8D%E7%AE%97%E6%B3%95%E6%98%AF%E5%90%A6%E6%9C%89%E8%AF%AF

    2、确认秘钥是否有误(服务商模式使用服务商商户号秘钥,秘钥是在商户平台配置,如果同一商户号调用其它接口成功可排除是秘钥问题)

    3、确认接口实际的请求参数与生成签名原串的参数一致,不能增加或缺少参数(可通过打印签名原串进行排查)

    4、确认参数的大小写,参数名与接口文档一致

    5、签名原串的参数值使用原始值,不需要encode

    6、接口需要使用UTF-8编码

    7、调用“小程序支付API”中appid没有按照文档要求的格式

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月1日
  • 已采纳回答 1月24日
  • 修改了问题 1月15日
  • 修改了问题 1月15日
  • 展开全部

悬赏问题

  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码