微信支付第三弹--SpringBoot整合微信APP支付

本文详细介绍了如何在SpringBoot项目中集成微信APP支付,包括必备参数、统一下单接口实现、签名方法、微信回调处理及签名错误排查。通过步骤讲解和代码示例,帮助开发者解决微信支付过程中可能遇到的问题。
摘要由CSDN通过智能技术生成

吐槽

 做完APP微信支付,就两个字:心累,并不是这个功能有多难,就是想吐槽一下微信,太TMD的店大欺客了!签名,呵呵,参数顺序都得按照他们的排序。。。。。。。。

吐槽归吐槽,还是做一下知识复盘,下面是做APP微信支付步骤和代码,框架用的是SpringBoot

步骤

必备参数:

 ①:appid:微信开放平台上面的应用appid,和公众号appid不同

②:mch_id:商户ID,微信商户平台上商户信息

③:key:商户key(API秘钥)

        登录微信商户平台--->账户中心--->API安全--->设置秘钥

步骤

    ①、根据账号参数拼接进行签名

    ②、根据参数和签名发起微信统一下单接口

    ③、把微信统一下单返回参数返回移动端

    ④、移动端根据参数拉起微信支付

    ⑤、支付成功后微信进行回调通知

    ⑥、判断微信返回状态,成功处理当前平台业务并返回微信return_code和return_msg两个参数,不然微信会一直进行回调

参数配置

#微信APP支付参数
wxpayconfig:
  #商户应用appId
  appid: wx383123456fbb7826
  #商户ID
  mch_id: 1234567011
  #设备号
  device_info: WEB
  #商户key:api秘钥(32位)
  key: VfnmAMI111111111EQjhvglWzDDO
  #统一下单接口
  url: https://api.mch.weixin.qq.com/pay/unifiedorder
  #回调接口
  notify_url: http://baidu.com/home/wechatnotify
  wx_package: Sign=WXPay

 

微信配置类

/**
 * WxpayConfig.java
 * com.prereadweb.order.config
 * Copyright (c) 2019, 
 */
package com.prereadweb.order.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Description: 微信配置类
 * @author: Administrator
 * @date: 2019/6/17 19:35
 */
@Data
@Component
@ConfigurationProperties(prefix="wxpayconfig")
public class WxpayConfig {

    private String appid; // 公众账号ID

    private String mch_id; // 商户号

    private String device_info; // 设备号

    private String key; // 商户的key【API密匙】

    private String url; // api请求地址

    private String notify_url; // 服务器异步通知页面路径

    private String return_url; // 服务器同步通知页面路径
    private String wx_package;
}

 

统一下单代码

controller层

/**
     * @Function: 去支付
     * @author:   YangXueFeng
     * @Date:     2019/6/14 16:46
     */
    @RequestMapping("/gowechatpay")
    public Object goWeChatPay(@Param("orderId") Long orderId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return weChatService.goWeChatPay(orderId, request);
    }

 

 service层代码

/**
     * @Function: 去支付
     * @author:   YangXueFeng
     * @Date:     2019/6/14 16:50
     */
    @Override
    public Map<String, Object> goWeChatPay(Long orderId, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        if(Util.isEmpty(orderId)) {
            map.put("code", UserStatusEnum.ERROR.intKey());
            map.put("msg", UserStatusEnum.ERROR.value());
            return map;
        }
        //获取订单信息
        PayParameterForm payParameter = orderMapper.getPayParameter(orderId);
        double price = payParameter.getActualPrice();
        System.out.println("price:" + price);
        // 微信开放平台审核通过的应用APPID
        System.out.println("appid是:" + wxpayconfig.getAppid());
        System.out.println("mch_id是:" + wxpayconfig.getMch_id());
        String nonce_str = Util.getRandomString(30);
        System.out.println("随机字符串是:" + nonce_str);
        int total_fee = (int) (price * 100);

        String total_price = null;// 订单总金额,单位为分,详见支付金额
        String spbill_create_ip = WXSignUtils.getRemortIP(request);// "127.0.0.1";
        System.out.println("spbill_create_ip===="+spbill_create_ip);
        String notify_url = wxpayconfig.getNotify_url();
        System.out.println("notify_url是:" + notify_url);
        String trade_type = "APP";

        // 参数:开始生成签名
        SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
        parameters.put("appid", wxpayconfig.getAppid());
        parameters.put("body", payParameter.getTitle());
        parameters.put("mch_id", wxpayconfig.getMch_id());
        parameters.put("nonce_str", nonce_str);
        parameters.put("notify_url", notify_url);
        parameters.put("out_trade_no", String.valueOf(payParameter.getOrderId()));
/*
        parameters.put("total_fee", total_fee);
*/
        parameters.put("spbill_create_ip",spbill_create_ip);
        parameters.put("total_fee", 1);
        parameters.put("trade_type", trade_type);
        String sign = WXSignUtils.createSign("UTF-8", parameters);
        System.out.println("签名是:" + sign);
        Unifiedorder unifiedorder = new Unifiedorder();
        unifiedorder.setAppid(wxpayconfig.getAppid());
        unifiedorder.setBody(payParameter.getTitle());
        unifiedorder.setMch_id(wxpayconfig.getMch_id());
        unifiedorder.setNonce_str(nonce_str);
        unifiedorder.setNotify_url(notify_url);
        unifiedorder.setOut_trade_no(String.valueOf(payParameter.getOrderId()));
        unifiedorder.setSpbill_create_ip(spbill_create_ip);
        unifiedorder.setTotal_fee(1);
        unifiedorder.setTrade_type(trade_type);
        unifiedorder.setSign(sign);

        // 构造xml参数
        String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);
        System.out.println("xmlInfo:" + xmlInfo);

        String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        String method = "POST";
        String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();// 请求微信
        System.out.println("weixinPost:" + weixinPost);
        UnifiedorderResult unifiedorderResult = ParseXMLUtils.jdomParseXml(weixinPost);// 解析微信的反馈
        if (unifiedorderResult != null) {
            if ("SUCCESS".equals(unifiedorderResult.getReturn_code())) {
                if("INVALID_REQUEST".equals(unifiedorderResult.getErr_code())){
                    map.put("code", UserStatusEnum.ERROR.intKey());
                    map.put("msg", "参数错误");
                    return map;
                }
                // 开始拼接App调起微信的参数
                SortedMap<Object, Obj
  • 16
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值