SpringBoot + 微信公眾號JSAPI支付功能的實(shí)現(xiàn)
1、pom.xml依賴配置
<!-- 微信支付 --> <dependency> <groupId>com.egzosn</groupId> <artifactId>pay-java-wx</artifactId> <version>2.12.4</version> </dependency>
2、application.yml文件配置微信公眾號的基礎(chǔ)信息
#微信公眾號支付配置 wechatpay: mchId: # 商戶Id appId: #應(yīng)用id storePassword: #秘鑰支付密碼 secretKey: # 密鑰 notifyUrl: #微信支付回調(diào) keyStore: # 證書所在位置
3、設(shè)置配置文件 WechatPayConfig.java
package com.example.emoticon.wechat; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @ClassName WechatPayConfig * @Description 微信支付配置 * @Author WangJing * @Date 2021/3/23 4:38 下午 * @Version V1.1.0 */ @Data @Component @ConfigurationProperties(prefix = "wechatpay") public class WechatPayConfig { private String mchId;//合作者id(商戶號 private String appId;//應(yīng)用id private String secretKey;//密鑰 private String notifyUrl; private String keyStore;// 支付密鑰存放位置 文件是以.p12為后綴名字 private String storePassword; }
4、controller 邏輯代碼
package com.example.emoticon.controller; import com.egzosn.pay.common.api.PayService; import com.egzosn.pay.common.bean.PayOrder; import com.egzosn.pay.common.bean.RefundOrder; import com.egzosn.pay.common.http.HttpConfigStorage; import com.egzosn.pay.common.util.sign.SignUtils; import com.egzosn.pay.wx.api.WxPayConfigStorage; import com.egzosn.pay.wx.api.WxPayService; import com.egzosn.pay.wx.bean.WxTransactionType; import com.example.emoticon.wechat.WechatPayConfig; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.math.BigDecimal; import java.util.Date; import java.util.Map; /** * @ClassName WechatPayController * @Description 微信支付Controller * @Author WangJing * @Date 2021/3/23 4:35 下午 * @Version V1.1.0 */ @RestController @RequestMapping("/wechatPay") @Slf4j public class WechatPayController { @Autowired WechatPayConfig wechatPayConfig; private PayService service = null; @PostConstruct public void init() { WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage(); wxPayConfigStorage.setMchId(wechatPayConfig.getMchId()); // 合作者id(商戶號 wxPayConfigStorage.setAppid(wechatPayConfig.getAppId()); // 應(yīng)用id wxPayConfigStorage.setSecretKey(wechatPayConfig.getSecretKey()); // 密鑰 wxPayConfigStorage.setNotifyUrl(wechatPayConfig.getNotifyUrl()); // 異步回調(diào)地址 http://域名:端口號/項(xiàng)目名/回調(diào)接口名稱 wxPayConfigStorage.setSignType(SignUtils.MD5.name()); wxPayConfigStorage.setInputCharset("utf-8"); // 支付api證書設(shè)置,退款必須 方式一 HttpConfigStorage httpConfigStorage = new HttpConfigStorage(); httpConfigStorage.setKeystore(wechatPayConfig.getKeyStore());// 支付密鑰存放位置 文件是以.p12為后綴名字 httpConfigStorage.setStorePassword(wechatPayConfig.getStorePassword()); // 是否為證書地址 httpConfigStorage.setPath(true); service = new WxPayService(wxPayConfigStorage, httpConfigStorage); // 請求連接池配置 // 最大連接數(shù) httpConfigStorage.setMaxTotal(20); // 默認(rèn)的每個(gè)路由的最大連接數(shù) httpConfigStorage.setDefaultMaxPerRoute(10); service.setRequestTemplateConfigStorage(httpConfigStorage); } @ApiOperation("返回訂單信息") @RequestMapping(value = "weixinpay", method = RequestMethod.POST) public Map<String, Object> weixinpay(HttpServletRequest request) { //備注:這個(gè)可以獲取預(yù)支付的訂單,根據(jù)需要調(diào)用這個(gè)接口,可有前臺向后臺傳遞參數(shù)。然后后臺根據(jù)需要進(jìn)行處理。 init(); // 在這一步,可以傳入一個(gè)訂單Id,自行去搜索訂單信息,并填寫以下內(nèi)容 PayOrder payOrder = new PayOrder();// 這個(gè)就是支付成功后,在微信支付里面返回的信息(支付訂單信息) // 一下內(nèi)容需要分情況而定,自行填寫。 payOrder.setSubject("商品名稱"); payOrder.setBody("商品描述"); payOrder.setAddition("附加信息"); payOrder.setPrice(new BigDecimal(0.2));// 價(jià)格 payOrder.setOutTradeNo("商戶訂單號"); payOrder.setBankType("銀行卡類型"); payOrder.setDeviceInfo("設(shè)備信息"); payOrder.setSpbillCreateIp("支付創(chuàng)建ip");// 可用IPUtils.getIpAddr(request) payOrder.setOpenid("用戶微信openid"); payOrder.setTransactionType(WxTransactionType.JSAPI);// 支付方式 Map orderInfo = service.orderInfo(payOrder);// 返回創(chuàng)建的訂單信息 log.debug("獲取預(yù)支付訂單信息回參" + orderInfo.toString()); // 可自行選擇 ,是否將支付的流水插入到數(shù)據(jù)庫中。返回的信息由:signType appId timeStamp nonceStr package sign return orderInfo; } /** * 這就是支付回調(diào)地址 * * @param request * @return * @throws IOException */ @ApiOperation("回調(diào)地址") @RequestMapping(value = "weixinpayBack") public String payBack(HttpServletRequest request) throws IOException { init(); // 獲取支付方返回的對應(yīng)參數(shù) Map<String, Object> params = service.getParameter2Map(request.getParameterMap(), request.getInputStream()); if (null == params) { log.debug("通知失敗"); return service.getPayOutMessage("failed", "通知失敗").toMessage(); } log.debug("微信公眾號支付結(jié)果通知:" + params.toString()); // 校驗(yàn) if (service.verify(params)) { // 這里處理業(yè)務(wù)邏輯 支付成功后的代碼邏輯塊 // ......業(yè)務(wù)邏輯處理塊........ log.debug("通知支付成功"); return service.getPayOutMessage("success", "支付成功").toMessage(); } log.debug("通知支付失敗"); return service.getPayOutMessage("fail", "支付失敗").toMessage(); } @ApiOperation("微信公眾號退款") @RequestMapping(value = "weixinAccRefund") public String weixinRefund() { init(); RefundOrder refundOrder = new RefundOrder();//退款訂單信息 refundOrder.setRefundNo("退款單號,每次進(jìn)行退款的單號,此處唯一"); refundOrder.setTradeNo("支付平臺訂單號,交易號"); refundOrder.setOutTradeNo("商戶單號"); refundOrder.setRefundAmount(new BigDecimal(0.2));//退款金額 refundOrder.setTotalAmount(new BigDecimal(0.5));//訂單總金額 refundOrder.setOrderDate(new Date());//退款交易日期 refundOrder.setDescription("退款說明"); Map<String, Object> refund = service.refund(refundOrder);//微信退款 //退款成功后,寫其他的邏輯 log.debug("微信公眾號退款結(jié)果:=" + refund.toString()); return "Success"; } }
到此這篇關(guān)于SpringBoot + 微信公眾號JSAPI支付功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 微信公眾號JSAPI支付內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java?Springboot對接開發(fā)微信支付詳細(xì)流程
- springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁上的支付和退款
- UniApp?+?SpringBoot?實(shí)現(xiàn)微信支付和退款功能
- springboot對接微信支付的完整流程(附前后端代碼)
- SpringBoot微信掃碼支付的實(shí)現(xiàn)示例
- SpringCloud解決Feign異步回調(diào)問題(SpringBoot+Async+Future實(shí)現(xiàn))
- 詳解springboot通過Async注解實(shí)現(xiàn)異步任務(wù)及回調(diào)的方法
- SpringBoot實(shí)現(xiàn)微信支付接口調(diào)用及回調(diào)函數(shù)(商戶參數(shù)獲取)
相關(guān)文章
SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案
這篇文章主要介紹了SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08SpringCloud之Zuul服務(wù)網(wǎng)關(guān)詳解
這篇文章主要介紹了SpringCloud之Zuul服務(wù)網(wǎng)關(guān)詳解,服務(wù)網(wǎng)關(guān)是微服務(wù)架構(gòu)中一個(gè)不可或缺的部分,通過服務(wù)網(wǎng)關(guān)統(tǒng)一向外系統(tǒng)提供REST?API的過程中,除了具備服務(wù)路由、均衡負(fù)載功能之外,它還具備了權(quán)限控制(鑒權(quán))等功能,需要的朋友可以參考下2023-08-08Java求解兩個(gè)非負(fù)整數(shù)最大公約數(shù)算法【循環(huán)法與遞歸法】
這篇文章主要介紹了Java求解兩個(gè)非負(fù)整數(shù)最大公約數(shù)算法,結(jié)合實(shí)例形式分析了java求解最大公約數(shù)的實(shí)現(xiàn)方法,并附帶了循環(huán)法與遞歸法算法思路,需要的朋友可以參考下2018-03-03一文探索Apache HttpClient如何設(shè)定超時(shí)時(shí)間
Apache HttpClient是一個(gè)流行的Java庫,用于發(fā)送HTTP請求,這篇文章主要為大家介紹了Apache HttpClient如何設(shè)定超時(shí)時(shí)間,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問題
中文亂碼是每個(gè)程序員都會遇到的問題,本篇文章主要介紹了談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問題,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12Spring?Boot最經(jīng)典的20道面試題你都會了嗎
Spring Boot是現(xiàn)代化的Java應(yīng)用程序開發(fā)框架,具有高度的靈活性和可擴(kuò)展性,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot最經(jīng)典的20道面試題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06SpringBoot開發(fā)實(shí)戰(zhàn)系列之定時(shí)器
定時(shí)任務(wù)我想諸位童鞋都不陌生,簡而言之名為“設(shè)定定時(shí)鬧鐘做某件事情”,下面這篇文章主要給大家介紹了關(guān)于SpringBoot定時(shí)器的相關(guān)資料,需要的朋友可以參考下2021-08-08