广告
返回顶部
首页 > 资讯 > 后端开发 > Python >UniApp + SpringBoot 实现微信支付和退款功能
  • 828
分享到

UniApp + SpringBoot 实现微信支付和退款功能

2024-04-02 19:04:59 828人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录开发准备微信支付开发后端部分前端部分开发准备 一台用于支付的测试机,必须得是一个安卓机因为需要打支付基座才能使用。用于编写的后端框架接口的 IDE (idea 或者 Eclips

开发准备

  • 一台用于支付的测试机,必须得是一个安卓机因为需要打支付基座才能使用。
  • 用于编写的后端框架接口的 IDE (idea 或者 Eclipse 都可以)
  • HBuilder X 用来编辑 UniApp 项目的编辑器和编译器
  • 基本的 SpringBoot 的脚手架,可以去 https://start.spring.io/ 或者 IDEA 自带的快速生成脚手架插件
  • jdk 11

微信支付开发

我这里省略了申请等步骤。如果没有申请过企业支付的可以去官网申请 Https://pay.weixin.qq.com/static/applyment_guide/applyment_detail_app.shtml 。安卓测试必须要打成基座,或者是正式APP应用。

后端部分

在 SpringBoot 中添加以下坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-WEB</artifactId>
</dependency>

<!-- 微信支付坐标 start-->
<dependency>
    <groupId>com.GitHub.binarywang</groupId>
    <artifactId>weixin-java-pay</artifactId>
    <version>4.2.5.B</version>
</dependency>
<!-- 退款用 -->
<dependency>
    <groupId>org.jodd</groupId>
    <artifactId>jodd-http</artifactId>
    <version>6.0.8</version>
</dependency>
<!-- 微信支付坐标 end-->

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

在 resources 目录下添加 application.yml 我们不去用默认的 application.properties 文件,毕竟 yml 更好看点。并在 yml 中添加以下内容

# 服务启动端口
server:
  port: 8080

# 微信支付
wxpay:
  appId: 开放平台的AppID
  mchId: 商户号
  mchKey: 商户密钥
  #  p12证书文件的绝对路径或者以classpath:开头的类路径.
  keyPath: classpath:/wxpay_cert/apiclient_cert.p12
  #  apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径.
  privateKeyPath: classpath:/wxpay_cert/apiclient_key.pem
  privateCertPath: classpath:/wxpay_cert/apiclient_cert.pem
  notifyUrl: https://4789j06630.wocp.fun/wechat/pay/notify
  refundNotifyUrl: https://4789j06630.wocp.fun/wechat/pay/refund_notify

创建一个 WechatPayConfig.java 使用上面的 ****wxpay

@Data
@ConfigurationProperties(prefix = "wxpay")
public class WechatPayConfig {
    private String appId;
    private String mchId;
    private String mchKey;
    private String keyPath;
    private String privateKeyPath;
    private String privateCertPath;
    private String notifyUrl;
    private String refundNotifyUrl;
}

创建一个 BizWechatPayService.java

package com.runbrick.paytest.util.wxpay;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.net.InetAddress;


@Service
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WechatPayConfig.class)
@AllArgsConstructor
public class BizWechatPayService {

    private WechatPayConfig wechatPayConfig;

    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wechatPayConfig.getAppId());
        payConfig.setMchId(wechatPayConfig.getMchId());
        payConfig.setMchKey(wechatPayConfig.getMchKey());
        payConfig.seTKEyPath(wechatPayConfig.getKeyPath());
        payConfig.setPrivateKeyPath(wechatPayConfig.getPrivateKeyPath());
        payConfig.setPrivateCertPath(wechatPayConfig.getPrivateCertPath());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        payConfig.setSignType("MD5");

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    
    public Object createOrder(String productTitle, String outTradeNo, Integer totalFee) {
        try {
            WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
            // 支付描述
            request.setBody(productTitle);
            // 订单号
            request.setOutTradeNo(outTradeNo);
            // 请按照分填写
            request.setTotalFee(totalFee);
            // 回调链接
            request.setNotifyUrl(wechatPayConfig.getNotifyUrl());
            // 终端IP.
            request.setSpbillCreateIp(InetAddress.getLocalHost().getHostAddress());
            // 设置类型为APP
            request.setTradeType(WxPayConstants.TradeType.APP);
            // 一定要用 createOrder 不然得自己做二次校验
            Object order = wxPayService().createOrder(request);
            return order;
        } catch (Exception e) {
            return null;
        }

    }

    
    public WxPayRefundResult refund(String tradeNo, Integer totalFee) {
        WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
        wxPayRefundRequest.setTransactionId(tradeNo);
        wxPayRefundRequest.setOutRefundNo(String.valueOf(System.currentTimeMillis()));
        wxPayRefundRequest.setTotalFee(totalFee);
        wxPayRefundRequest.setRefundFee(totalFee);
        wxPayRefundRequest.setNotifyUrl(wechatPayConfig.getRefundNotifyUrl());
        try {
            WxPayRefundResult refund = wxPayService().refundV2(wxPayRefundRequest);
            if (refund.getReturnCode().equals("SUCCESS") && refund.getResultCode().equals("SUCCESS")) {
                return refund;
            }

        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }
}

创建一个 WechatController.java 来实现接口给前端调用时使用

package com.runbrick.paytest.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.runbrick.paytest.util.wxpay.BizWechatPayService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wechat/pay")
@AllArgsConstructor
public class WechatController {

    BizWechatPayService wechatPayService;

    private static Logger logger = LoggerFactory.getLogger(WechatController.class);

    
    @RequestMapping(value = "/unified/request", method = RequestMethod.GET)
    public Object appPayUnifiedRequest() {
        // totalFee 必须要以分为单位
        Object createOrderResult = wechatPayService.createOrder("测试支付", String.valueOf(System.currentTimeMillis()), 1);
        logger.info("统一下单的生成的参数:{}", createOrderResult);
        return createOrderResult;
    }

    @RequestMapping(method = RequestMethod.POST, value = "notify")
    public String notify(@RequestBody String xmlData) {
        try {
            WxPayOrderNotifyResult result = wechatPayService.wxPayService().parseOrderNotifyResult(xmlData);
            // 支付返回信息
            if ("SUCCESS".equals(result.getReturnCode())) {
                // 可以实现自己的逻辑
                logger.info("来自微信支付的回调:{}", result);
            }

            return WxPayNotifyResponse.success("成功");
        } catch (WxPayException e) {
            logger.error(e.getMessage());
            return WxPayNotifyResponse.fail("失败");
        }
    }

    
    @RequestMapping(method = RequestMethod.POST, value = "refund")
    public void refund(String transaction_id) {
        // totalFee 必须要以分为单位,退款的价格可以这里只做的全部退款
        WxPayRefundResult refund = wechatPayService.refund(transaction_id, 1);
        // 实现自己的逻辑
        logger.info("退款本地回调:{}", refund);
    }

    
    @RequestMapping(method = RequestMethod.POST, value = "refund_notify")
    public String refundNotify(@RequestBody String xmlData) {
        // 实现自己的逻辑
        logger.info("退款远程回调:{}", xmlData);
        // 必须要返回 SUCCESS 不过有 WxPayNotifyResponse 给整合成了 xml了
        return WxPayNotifyResponse.success("成功");
    }

}

上面的 controller 写了两个接口一个用来 app端的调用,一个给支付用来回调。回调接口的地址要放到刚才配置中的 notifyUrl 属性里。还有一个是微信的退款接口。

  • 由于支付宝回调要使用线上的地址作为回调地址,这里我推荐两个解决办法
  • 使用一台服务器+备案的域名搭建上面的后台地址
  • 使用 花生壳 来实现本地内网穿透

我使用的是 花生壳 作为本次的开发环境,启动 springboot 的服务,配置好花生壳。后台部分到目前为止已经结束了。

前端部分

创建部分和我写的支付宝那个一样,如果不知道可以去看一下。所以跳过创建部分了,直接来到了代码实现。要在 manifest.JSON 勾选微信支付支持

创建前端支付代码 index.Vue

<template>
    <view class="content">
        <view class="text-area">
            <text class="title">{{title}}</text>
        </view>
        <button type="default" @click="GoPay()">点我前去支付</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: '跟我去支付'
            }
        },
        onLoad() {

        },
        methods: {
            goPay() {
                uni.request({
                    url: "https://4789j06630.wocp.fun/wechat/pay/unified/request",
                    success(res) {
                        let obj = {
                            appid: res.data.appId,
                            noncestr: res.data.nonceStr,
                            package: res.data.packageValue,
                            partnerid: res.data.partnerId,
                            prepayid: res.data.prepayId,
                            timestamp: parseInt(res.data.timeStamp),
                            sign: res.data.sign,
                        };

                        uni.requestPayment({
                            provider: "wxpay",
                            orderInfo: obj,
                            success(res) {
                                uni.showModal({
                                    content: "支付成功",
                                    showCancel: false
                                })
                            },
                            fail(e) {
                                uni.showModal({
                                    content: "支付失败,原因为: " + e.errMsg,
                                    showCancel: false
                                })
                            },
                            complete() {
                                console.log("啥也没干");
                            }
                        });

                    }
                })

            }
        }
    }
</script>

<style>
    page {
        background-color: #ff5500;
    }

    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

点击按钮就可以前往微信支付看下后台的生成的组合参数

跳转微信支付之后会跳回这里,提示支付成功。查看一下后台回调

之后的业务按照支付逻辑开发就可以,简单的支付已经完成。在按照刚才给的回调参数做个退款操作

我们使用 apipost 一个很强大的工具,被同事安利的。那就正好拿他测测退款借口,就不写代码了。

此时如果没有任何错误,后台控制台会返回退款本地和远程回调信息

此时微信也收到退款信息了。

整套支付流程都上传到 github 了可以查看 github的源码 https://github.com/runbrick/pay_spring

到此这篇关于UniApp + SpringBoot 实现微信支付和退款的文章就介绍到这了,更多相关SpringBoot 微信支付和退款内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: UniApp + SpringBoot 实现微信支付和退款功能

本文链接: https://www.lsjlt.com/news/150592.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • UniApp + SpringBoot 实现微信支付和退款功能
    目录开发准备微信支付开发后端部分前端部分开发准备 一台用于支付的测试机,必须得是一个安卓机因为需要打支付基座才能使用。用于编写的后端框架接口的 IDE (IDEA 或者 Eclips...
    99+
    2022-11-13
  • UniApp + SpringBoot 实现支付宝支付和退款功能
    目录开发准备支付宝支付开发后端部分前端部分支付宝退款开发后端部分上篇介绍了UniApp + SpringBoot 实现微信支付和退款功能,喜欢的朋友可以点击查看。 开发准备 一台用于...
    99+
    2022-11-13
  • SpringBoot整合Vue实现微信扫码支付以及微信退款功能详解
    直接上代码,在order模块添加依赖 <dependency> <groupId>com.github.wxpay&l...
    99+
    2022-11-13
  • PHP微信支付与退款功能实现流程详解
    目录一、微信支付二、微信退款三、退款完成微信小程序支付的主要逻辑集中在后端,前端只需携带支付所需的数据请求后端接口然后根据返回结果做相应成功失败处理即可。 一、微信支付 支付主要分为...
    99+
    2022-11-13
  • 微信小程序如何实现支付及退款
    这篇文章将为大家详细讲解有关微信小程序如何实现支付及退款,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一. 支付支付主要分为几个步骤:前端携带支付需要的数据(商品id,购...
    99+
    2022-10-19
  • uniapp实现支付功能
    详细参考:https://gitee.com/copperpeas/uniapp-payment uniapp-payment介绍uniapp支付 微信支付流程测试接入的是uniap...
    99+
    2022-11-13
  • php实现微信支付功能
    一、生成预支付订单并获取预支付ID(prepay_id): ...
    99+
    2023-09-11
    php
  • SpringBoot + 微信公众号JSAPI支付功能的实现
    1、pom.xml依赖配置 <!-- 微信支付 --> <dependency> <groupId>com.egzosn</grou...
    99+
    2022-11-11
  • vue项目中的支付功能实现(微信支付和支付宝支付)
    目录项目中常见的支付方式    支付宝支付微信支付项目中常见的支付方式     支付宝支付  &nbs...
    99+
    2022-11-12
  • 怎么用Java也实现微信和支付宝支付功能
    这篇文章主要介绍“怎么用Java也实现微信和支付宝支付功能”,在日常操作中,相信很多人在怎么用Java也实现微信和支付宝支付功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”...
    99+
    2022-10-19
  • uniapp如何实现支付宝支付的功能
    随着移动互联网的飞速发展,人们越来越依赖于手机支付。而支付宝是其中使用最广泛的支付方式之一。在移动应用中,如果要进行支付宝支付的功能开发,那么使用uniapp框架就是一个非常好的选择。本文将介绍uniapp如何实现支付宝支付的功能。一、准备...
    99+
    2023-05-14
  • PHP实现 APP端微信支付功能
    一丶 PHP后台生成预支付交易单,返回正确的预支付交易回话标识后 调起支付,根据文档拼接微信需要的参数,这里将需要的几个方法进行示例; 传输给微信的参数要组装成xml格式发送,传如参数数组! publ...
    99+
    2023-10-18
    php 微信 android
  • 如何进行vue项目中的支付功能实现(微信支付和支付宝支付)
    如何进行vue项目中的支付功能实现(微信支付和支付宝支付),针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。项目中常见的支付方式    支付宝支付...
    99+
    2023-06-22
  • Android中怎么实现微信支付功能
    Android中怎么实现微信支付功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。 去微信开放平台申请微信支付服务,绑定自己的应用这里具体不多讲,但是一定要申请完成,将会得到是...
    99+
    2023-05-31
    android
  • SpringBoot如何实现整合微信支付
    这篇文章将为大家详细讲解有关SpringBoot如何实现整合微信支付,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.准备工作1.1 数据库表这里涉及微信支付一共两个表:订单表支付记录表1.2 实体类数据...
    99+
    2023-06-22
  • ASP.Net项目中实现微信APP支付功能
    最近挺忙的,没时间写东西。然后在弄微信APP支付,网上的搜索一趟,都比较凌乱,我也遇到一些坑,不过也算弄好了,记录分享一下。 1、准备各种调用接口需要的参数,配置app.config...
    99+
    2022-11-13
  • 微信小程序支付功能如何实现
    这篇文章主要介绍了微信小程序支付功能如何实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序支付功能如何实现文章都会有所收获,下面我们一起来看看吧。微信小程序支付功能开发文档如下:小程序端,保留大部分的...
    99+
    2023-06-26
  • 微信小程序支付功能怎么实现
    实现微信小程序支付功能,需要以下步骤:1. 首先,在微信公众平台申请开通支付功能,并获取到支付相关的配置信息,包括 appID、商户...
    99+
    2023-08-16
    微信小程序
  • 怎么用PHP实现支付宝和微信扫码在线支付的功能
    本篇内容介绍了“怎么用PHP实现支付宝和微信扫码在线支付的功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成...
    99+
    2022-10-18
  • SpringBoot实现简易支付宝网页支付功能
           该文章描述了一个基于SpringBoot程序的支付宝支付demo,由于是个人开发者而非企业,因此设...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作