微信公众号支付

微信官方说明:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1

1、在微信商户平台(pay.weixin.qq.com)设置您的公众号支付支付目录,设置路径:商户平台-->产品中心-->开发配置,如图7.7所示。公众号支付在请求支付的时候会校验请求来源是否有在商户平台做了配置,所以必须确保支付目录已经正确的被配置,否则将验证失败,请求支付不成功。

æ¯ä»ç®å½éç½®

2、后台使用java

使用社区开源的优秀封装pom文件引入

<dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-pay</artifactId>
            <version>3.0.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>qrcode-utils</artifactId>
                    <groupId>com.github.binarywang</groupId>
                </exclusion>
            </exclusions>
        </dependency>

3、设置config配置文件

@Configuration
@ConfigurationProperties(prefix = "wx")
public class WxProperties {

    private String appId;

    private String appSecret;

    private String mchId;

    private String mchKey;

    private String notifyUrl;

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getMchKey() {
        return mchKey;
    }

    public void setMchKey(String mchKey) {
        this.mchKey = mchKey;
    }

    public String getAppId() {
        return this.appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

}

4、配置application-dev.properties配置文件

wx.app-id=公众号appid
wx.app-secret=公众号secret
wx.mch-id=商户id
wx.mch-key=32位密钥
wx.notify-url=回调地址

5、创建注入config

@Configuration
public class WeixinConfig {
    @Autowired
    private WxProperties properties;
   

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        return payConfig;
    }


    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}

6、获取微信调用需要字段,创建微信订单

@PostMapping("{orderId}/prepay")
    public Object prepay(@PathVariable Long orderId, HttpServletRequest request) {
        //业务逻辑判断,查看订单是否合适
        try {
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setOutTradeNo(order.getOrderSn());
            orderRequest.setOpenid(openid);//使用微信授权登陆获取的id
            orderRequest.setBody("订单:" + order.getOrderSn());
            // 元转成分
            Integer fee = 0;
            BigDecimal actualPrice = order.getActualPrice();
            fee = actualPrice.multiply(new BigDecimal(100)).intValue();
            orderRequest.setTotalFee(fee);
            orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));

            result = wxPayService.createOrder(orderRequest);

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(403, "订单不能支付");
        }

     
        return ResponseUtil.ok(result);
    }

7、html页面获取到第6步的返回参数发起调用

WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', {
                            "appId": resp.data.appId, //公众号名称,由商户传入
                            "timeStamp": resp.data.timeStamp, //时间戳,自1970年以来的秒数
                            "nonceStr": resp.data.nonceStr, //随机串
                            "package": resp.data.packageValue,
                            "signType": resp.data.signType, //微信签名方式:
                            "paySign": resp.data.paySign //微信签名
                        },
                        function (res) {
                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                //跳转到支付完成页面
                            }
                            // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回    ok,但并不保证它绝对可靠。
                        }
                    );

8、用户输入密码付款成功后会到回调函数

@RequestMapping("pay-notify")
    public Object payNotify(@RequestBody String body) {
        try {
            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(body);

            String orderSn = result.getOutTradeNo();
            String payId = result.getTransactionId();
            // 分转化成元
            String totalFee = BaseWxPayResult.feeToYuan(result.getTotalFee());

       
            return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            logger.error("微信回调结果异常,异常原因 " + e.getMessage());
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }

9、流程完成,下面是时序图

微信åç½é¡µæ¯ä»æ¶åºå¾

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值