JAVA中调用微信小程序的JSAPI支付接口完成支付、退款功能

最近在搞微信小程序的支付,在网上查了很多资料可算弄出来,记录一下。

首先需要获取 openid

前端代码

wx.login({
			success: (res) => {
				console.log('请球成功');
				if (res.code) {
					// 获取到用户登录凭证 code
					const code = res.code;
					// console.log(code);
					// 将 code 发送给后端服务器
					wx.request({
						url: 'http://192.168.8.111:8083/wechat/getWeiXinOpenid?code='+code,
						// data: {
						// 	code: code
						// },
						method: 'POST',
						success: (res) => {
							console.log('请球成功',res);
							const openid = res.data.openid
              //下方代码为微信小程序支付内容
							const params = {
								"details": "这是一个订单",
								"name": "商品名称",
								"openId": openid,
								"payMoney": 1,
								"userId": 123531
							}
							wx.request({
								url: 'http://192.168.8.111:8083/wechat/pay',
								data: params,
								method: 'POST',
								success:(res)=>{
									console.log(res.data);
									wx.requestPayment({
										"timeStamp": res.data.timeStamp,
										"nonceStr": res.data.nonceStr,
										"package": res.data.packageVal,
										"signType": res.data.signType,
										"paySign": res.data.paySign,
										"success": function(res){
												console.log('success', res)
												// if (res.errMsg === "requestPayment:ok") {
												// 		_this.refreshPaymentStatus()
												// 		_this.showToast()
												// 		_this.queryPayment()
												// }
										},
										"fail": function(res){
												console.log('fail', res)
										},
										"complete": function(res){
												console.log('complete', res)
										}
								})
								}
							})
							
							// console.log(res.data.openid);
						},
						fail: (err) => {
							console.error('请求后端接口失败', err);
						},
					});
				} else {
					console.error('获取用户登录凭证失败', res.errMsg);
				}
			},
			fail: (err) => {
				console.error('调用 wx.login 失败', err);
			},
		});

后端接口

 @PostMapping("/getWeiXinOpenid")
    public String login(String code)  {
        String appId = "1234124214";//替换为你的appid
        String secret = "rewr3423424235";//替换为你的appSecret 小程序秘钥
        String tokenUrl = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, secret,code);
        // 返回结果 {session_key: "weqweqweqweqw", openid: "eqweqwirqwtu"}
        return HttpUtil.get(tokenUrl);
    }

 导入依赖

<dependency>
            <groupId>com.github.wechatpay-apiv3</groupId>
            <artifactId>wechatpay-java</artifactId>
            <version>0.2.12</version>
        </dependency>

 配置类

# 微信小程序支付
wechatpay:
  #应用编号 正式号id
  appId: sdasfafsfasf
  # 小程序密钥
  appSecret: sdasfafsfasf
  #商户号
  mchId: 123456789
  # APIv3密钥(在微信支付平台中自己设置)
  apiV3Key: 8888888888

  # 支付成功后微信官方会自动调用我们设定的接口:域名/接口名
  # 支付通知回调, 本地测试内网穿透地址(仅用于做本地测试)
  # 支付通知回调, 线上域名地址(正常使用服务器配置的域名即可),小程序上线后前后端访问数据时只能用https不能用http,因此还需要为域名配置SSL证书,可根据使用的服务器查看如何配置
  notifyUrl: http://m52w93.natappfree.cc/wechat/notify-pay
  # 退款成功回调
  refundUrl: http://m52w93.natappfree.cc/wechat/refund-pay

  # 密钥路径 微信商户平台中下载证书
  keyPemPath: src/main/resources/wechatPay/apiclient_key.pem
  certPath: src/main/resources/wechatPay/apiclient_cert.pem
  certP12Path: src/main/resources/wechatPay/apiclient_cert.p12
  # 商户证书序列号(可在微信支付平台商户信息中查看)
  serialNo: 231241231241241241241

微信支付配置类 


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

/**
 * 微信支付配置类
 */
@Component
@Data
@ConfigurationProperties(prefix = "wechatpay")
public class WechatPayConfig {
    /**
     * 小程序appId
     */
    private String appId;
    /**
     * 小程序密钥
     */
    private String appSecret;
    /**
     * 商户号(微信支付平台设置)
     */
    private String mchId;
    /**
     * APIv3密钥(微信支付平台设置)
     */
    private String apiV3Key;
    /**
     * 支付成功回调地址
     */
    private String notifyUrl;

    /**
     * 退款成功回调地址
     */
    private String refundUrl;
    /**
     * 商户证书序列号
     */
    private String serialNo;

}

 微信支付工具类

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import javax.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值