基于springboot微信小程序支付功能实现
简单的封装微信小程序支付功能,支付工具类所依赖的fastjson、lombok、wagegger,
1、添加maven依赖:
版本号可根据自己项目的实际情况修改

         <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId></dependency>

2、创建支付基类BasePayEntity 实现 Serializable 接口:
required = true 表示必填字段

@Data
@NoArgsConstructor
@Accessors(chain = true)
public class BasePayEntity implements Serializable {@JSONField(name = "appid")@ApiModelProperty(name = "appId"        , required = true , value = "应用ID")protected String appId ;@JSONField(name = "mchid")@ApiModelProperty(name = "mchId"        , required = true , value = "直连商户号")protected String mchId ;@JSONField(name = "description")@ApiModelProperty(name = "description"  , required = true , value = "商品描述")protected String description ;@JSONField(name = "out_trade_no")@ApiModelProperty(name = "outTradeNo"   , required = true , value = "商户订单号")protected String outTradeNo ;@JSONField(name = "time_expire")@ApiModelProperty(name = "timeExpire"                     , value = "交易结束时间")protected String timeExpire ;@JSONField(name = "attach")@ApiModelProperty(name = "attach"                         , value = "附加数据")protected String attach ;@JSONField(name = "notify_url")@ApiModelProperty(name = "notifyUrl"    , required = true , value = "通知地址")protected String notifyUrl ;@JSONField(name = "amount")@ApiModelProperty(name = "amount"       , required = true , value = "订单金额")protected Amount amount ;@JSONField(name = "detail")@ApiModelProperty(name = "detail"                         , value = "优惠功能")protected Detail detail ;@JSONField(name = "settle_info")@ApiModelProperty(name = "settleInfo"                      , value = "结算信息")protected SettleInfo settleInfo ;@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class Amount implements Serializable {@ApiModelProperty(name = "total"    , required = true  , value = "总金额")protected Integer total ;@ApiModelProperty(name = "currency" , required = true  , value = "CNY:人民币,境内商户号仅支持人民币。")protected String currency ;}@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class Detail implements Serializable{@JSONField(name = "cost_price")@ApiModelProperty(name = "costPrice"                  , value = "总金额")protected Integer costPrice ;@JSONField(name = "invoice_id")@ApiModelProperty(name = "invoiceId"                  , value = "商品小票ID")protected String invoiceId ;}@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class SettleInfo implements Serializable{@JSONField(name = "profit_sharing")@ApiModelProperty(name = "profitSharing"              , value = "是否指定分账")protected Boolean profitSharing ;}public BasePayEntity(String appId, String mchId, String description, String outTradeNo, String timeExpire, String attach, String notifyUrl, Amount amount, Detail detail,  SettleInfo settleInfo) {this.appId = appId;this.mchId = mchId;this.description = description;this.outTradeNo = outTradeNo;this.timeExpire = timeExpire;this.attach = attach;this.notifyUrl = notifyUrl;this.amount = amount;this.detail = detail;this.settleInfo = settleInfo;}
}

3、创建Jsapi下单实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("Jsapi下单")
@Accessors(chain = true)
@Url("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi")
public class JsApiPay extends BasePayEntity {@JSONField(name = "scene_info")@ApiModelProperty(name = "sceneInfo"                     , value = "支付场景描述")private SceneInfo sceneInfo ;@JSONField(name = "payer")@ApiModelProperty(name = "payer"       , required = true , value = "支付者信息")private Payer payer ;@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class Payer implements Serializable{@ApiModelProperty(name = "openid"  , required = true , value = "用户标识")private String openid ;}@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class SceneInfo implements Serializable{@JSONField(name = "payer_client_ip")@ApiModelProperty(name = "payerClientIp"        , value = "用户的客户端IP,支持IPv4和IPv6两种格式的IP地址")private String payerClientIp ;@JSONField(name = "device_id")@ApiModelProperty(name = "deviceId"             , value = "商户端设备号(门店号或收银设备ID)")private String deviceId ;@JSONField(name = "store_info")@ApiModelProperty(name = "storeInfo"            , value = "商户门店信息")private StoreInfo storeInfo ;@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class StoreInfo implements Serializable{@JSONField(name = "id")@ApiModelProperty(name = "id"           , value = "商户侧门店编号")private String id ;@JSONField(name = "name")@ApiModelProperty(name = "name"         , value = "商户侧门店名称")private String name ;@JSONField(name = "area_code")@ApiModelProperty(name = "areaCode"     , value = "地区编码,详细请见省市区编号对照表。")private String areaCode ;@JSONField(name = "address")@ApiModelProperty(name = "address"      , value = "详细的商户门店地址")private String address ;}}@Builder(toBuilder = true)public JsApiPay(String appId, String mchId, String description, String outTradeNo, String timeExpire, String attach, String notifyUrl, Amount amount, Detail detail, SettleInfo settleInfo, SceneInfo sceneInfo, Payer payer) {super(appId, mchId, description, outTradeNo, timeExpire, attach, notifyUrl, amount, detail, settleInfo);this.sceneInfo = sceneInfo;this.payer = payer;}
}

4、创建支付工具类:
注:BaseException为自定义异常类,WechatProperties 对应yml配置文件也可使用其他的方式传递配置,SignUtils签名,

public class MiniPayUtils {private WechatProperties wechatProperties ;public static MiniPayUtils newInstance(WechatProperties wechatProperties){MiniPayUtils miniPayUtils = new MiniPayUtils();miniPayUtils.wechatProperties = wechatProperties ;return miniPayUtils ;}public MiniPayResult pay(JsApiPay jsApiPay) {BasePayEntity.Amount amount = jsApiPay.getAmount();jsApiPay = jsApiPay.toBuilder().appId(wechatProperties.getMini().getAppid()).amount(ObjectUtils.isEmpty(amount) ? BasePayEntity.Amount.builder().currency("CNY").build() : amount.setCurrency("CNY")).notifyUrl(wechatProperties.getNotifyUrl()).mchId(wechatProperties.getMerchant().getMchId()).build();String nonceStr = SignUtils.getRandom() , timeStamp = String.valueOf(System.currentTimeMillis() / 1000) ;MiniPayResult miniPayResult = MiniPayResult.builder().appId(jsApiPay.getAppId()).timeStamp(timeStamp).nonceStr(nonceStr).packages(newInstance(wechatProperties).prepayId(jsApiPay , nonceStr , timeStamp)).signType("RSA").build();miniPayResult.setPaySign(SignUtils.sign(miniPayResult.toJsonString().getBytes() , wechatProperties.getMerchant().getCertPath() , ProjectStatus.windows操作系统.getValue().equals(wechatProperties.getEnvironment())));return miniPayResult ;}public String prepayId(JsApiPay jsapiPay , String nonceStr , String timeStamp){try{String url = jsapiPay.getClass().getAnnotation(Url.class).value() , body = JSONObject.toJSONString(jsapiPay);HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add(HttpHeaders.AUTHORIZATION , SignUtils.getToken(HttpMethod.POST.name(), new URL(url), body, nonceStr, timeStamp, jsapiPay.getMchId(), wechatProperties.getMerchant().getSerialNo(), wechatProperties.getMerchant().getCertPath(), ProjectStatus.windows操作系统.getValue().equals(wechatProperties.getEnvironment())));httpHeaders.add(HttpHeaders.CONTENT_TYPE  , MediaType.APPLICATION_JSON_VALUE);ResponseEntity<String> exchange = getRestTemplate().exchange(url , HttpMethod.POST , new HttpEntity(body , httpHeaders), String.class );if(HttpStatus.OK.equals(exchange.getStatusCode())) {JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());return "prepay_id=" + jsonObject.getString("prepay_id");}throw BaseException.throwEx(JSONObject.parseObject(exchange.getBody()).getString("message"));}catch (Exception e){String message = e.getMessage();if(e instanceof HttpClientErrorException){HttpClientErrorException exception = (HttpClientErrorException)e;if(403 == exception.getRawStatusCode()) throw BaseException.throwEx(JSONObject.parseObject(message.substring(message.indexOf("[") + 1 , message.lastIndexOf("]"))).getString("message"));}throw BaseException.throwEx(message);}}}

基于springboot微信小程序支付功能实现相关推荐

  1. SpringBoot对接微信小程序支付功能开发(一,下单功能)

    1,接入前准备: 接入模式选择直连模式: 申请小程序,得到APPID,并开通微信支付: 申请微信商户号,得到mchid,并绑定APPID: 配置商户API key,下载并配置商户证书,根据微信官方文档 ...

  2. SpringBoot对接微信小程序支付功能开发(二,支付回调功能)

    接着上一篇: SpringBoot对接微信小程序支付功能开发(一,下单功能) 在上一篇下单功能中我们有传支付结果回调地址. 下面是回调接口实现 package com.office.miniapp.c ...

  3. 微信小程序支付功能用服务器吗,微信小程序 支付功能 服务器端(TP5.1)实现...

    首先下载微信支付SDK ,将整个目录的文件放在 /application/extend/WxPay 目录下 在使用SDK之前我们需要对 WxPay.Config.php 进行配置 namespace ...

  4. (附源码)基于springboot微信小程序的长沙县图书馆图书导览系统 毕业设计 170900

    基于springboot微信小程序的长沙县图书馆图书导览系统 摘  要 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也 ...

  5. 微信小程序支付功能-服务器端实现(附源码)

    实现了小程序最新的V3版本支付功能, 包括:支付.支付通知.退款.退款通知. 服务器端使用java开发,springboot框架 源码链接在评论中 微信小程序支付功能-服务器端实现(附源码)_哔哩哔哩 ...

  6. 微信小程序php后台支付,微信小程序 支付功能实现PHP实例详解

    微信小程序 支付功能实现PHP实例详解 前端代码: wx.request({ url: 'https://www.yourhost.com/weixin/WeiActivity/payJoinfee' ...

  7. python个人微信支付接口_Python实现微信小程序支付功能

    正文 由于最近自己在做小程序的支付,就在这里简单介绍一下讲一下用python做小程序支付这个流程.当然在进行开发之前还是建议读一下具体的流程,清楚支付的过程. 1.支付交互流程 2.获取openid( ...

  8. 基于SpringBoot+微信小程序的壁纸小程序

    基于SpringBoot+微信小程序的壁纸小程序 ✌全网粉丝20W+,csdn特邀作者.博客专家.CSDN新星计划导师.java领域优质创作者,博客之星.掘金/华为云/阿里云/InfoQ等平台优质作者 ...

  9. python微信小程序抢购_Python实现微信小程序支付功能!Python确实强的一批!

    正文 由于最近自己在做小程序的支付,就在这里简单介绍一下讲一下用python做小程序支付这个流程.当然在进行开发之前还是建议读一下具体的流程,清楚支付的过程. 1.支付交互流程 2.获取openid( ...

最新文章

  1. .NET Winform也能画出类似QQ、飞信这样的窗口风格和控件效果
  2. mysql模糊查询的优化方法--亲自实践
  3. 任务的协作的基本实现
  4. c语言贪心算法合并箭,LeetCode刷题题库:贪心算法
  5. Tensorflow【实战Google深度学习框架】基于tensorflow + Vgg16进行图像分类识别
  6. hdu2167 方格取数 状态压缩dp
  7. 【windows gdi+】GDI+ Image类加载图片时异常问题处理与分析
  8. 团队Alpha冲刺(三)
  9. bootstrap 树形表格渲染慢_layUI之树状表格异步加载组件treetableAsync.js(基于treetable.js)...
  10. 面试官最爱的 volatile 关键字,这些问题你都搞懂了没?
  11. Qt自定义委托在QTableView中绘制控件、图片、文字
  12. 如何解决cellIndex在IE下兼容性问题
  13. 蓝桥杯 ALGO-68 算法训练 判定数字
  14. 网管面试题1-windows
  15. mysql 新建用户并赋予远程访问权限
  16. 基础VLAN划分(思科)
  17. laravel-mix打包 js css
  18. java画菱形_JavaSE之绘制菱形
  19. 教你做Android逆向
  20. golang 实现 syn_sent flood洪水攻击

热门文章

  1. java 私有类_Java类属性的私有化
  2. 2030年的6G:5大趋势,13个核心技术
  3. 开关二极管和肖特基二极管比较
  4. 记第一次写出自己的简单python爬虫:GCZW3
  5. codeforces 314 (Div 1) 题解
  6. 谷歌真被ChatGPT搞慌了!两位创始人紧急回归制定战术,搜索广告根基不容有失...
  7. 政务数据共享开放的意义?
  8. C++图常用库boost graph library
  9. 母に捧げる作者木小歌
  10. 赛扬处理器_首批15瓦四核处理器即将成为历史:英特尔宣布停产4个型号