欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款

 更新時間:2024年05月27日 11:27:14   作者:layman0528  
最近做項目需要實現(xiàn)在pc端需要實現(xiàn)微信的支付,本文主要介紹了springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款,具有一定的參考價值,感興趣的可以了解一下

前提

微信小程序中實現(xiàn)微信支付是從小程序中調(diào)去微信支付的界面直接進行支付,那么在pc端需要實現(xiàn)微信的支付呢,是需要出現(xiàn)一個二維碼讓用戶使用掃碼支付的。

注意:

需要實現(xiàn)pc端的微信支付,需要在微信商戶平臺開通native支付,并且下載并配置商戶證書

在這里插入圖片描述

在這里插入圖片描述

設(shè)置好這些之后,直接看看在springboot 或者springclound 中如何實現(xiàn)。

maven依賴

 		<dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-pay</artifactId>
            <version>4.1.0</version>
        </dependency>

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

步驟

在yml中定義微信的商戶號,密碼等等

wxpay:
  appId: appId
  mchId: mchId
  mchKey: mchKey
  keyPath: /home/wxpay_cert/apiclient_cert.p12
  privateKeyPath: /home/wxpay_cert/apiclient_key.pem
  privateCertPath: /home/wxpay_cert/apiclient_cert.pem

  notifyUrl: notifyUrl
  refundNotifyUrl: refundNotifyUrl
  serialNo: serialNo

notifuyUrl 是 支付回調(diào)地址
refundNotifyUrl是 退款回調(diào)地址
這兩個地址都必須是外網(wǎng)可訪問的地址

新建WechatPayConfig類來讀取yml文件的中信息

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

@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 redUrl;
    private String refundNotifyUrl;

    private String serialNo;
}

獲取ip地址幫助類

IpUtils

public class IpUtils
{
       public static String getIpAddr()
    {
        return getIpAddr(ServletUtils.getRequest());
    }
    }

ServletUtils (客戶端工具類)

   public static HttpServletRequest getRequest()
    {
        try
        {
            return getRequestAttributes().getRequest();
        }
        catch (Exception e)
        {
            return null;
        }
    }
    
       public static ServletRequestAttributes getRequestAttributes()
    {
        try
        {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            return (ServletRequestAttributes) attributes;
        }
        catch (Exception e)
        {
            return null;
        }
    }

微信支付Service類

@Service
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WechatPayConfig.class)
@AllArgsConstructor
public class WechatPayService {
	 static Logger logger = LoggerFactory.getLogger(WechatPayService.class);
	 private WechatPayConfig wechatPayConfig;
	     public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wechatPayConfig.getAppId());
        payConfig.setMchId(wechatPayConfig.getMchId());
        payConfig.setMchKey(wechatPayConfig.getMchKey());
        payConfig.setApiV3Key(wechatPayConfig.getMchKey());
        payConfig.setKeyPath(wechatPayConfig.getKeyPath());
        payConfig.setUseSandboxEnv(false);

        logger.info("wechatPayConfig.getPrivateKeyPath():{}",wechatPayConfig.getPrivateKeyPath());
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig); //微信配置信息
        return wxPayService;
    }
	
	//生成支付二維碼
	//BookingInfo 是你訂單表
	 public String generatePayQrCode(BookingInfo booking, String ip) {
        try {

       
            WxPayService wxPayService = wxPayService();
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setOutTradeNo(booking.getDisplayNo()); //訂單號
            orderRequest.setTotalFee(BigDecimal.valueOf(booking.getDisplayPrice()).multiply(new BigDecimal(100)).intValue()); //金額,轉(zhuǎn)換成分 ,至少支付1分錢
            orderRequest.setSpbillCreateIp(ip);
            orderRequest.setNotifyUrl(wechatPayConfig.getRedUrl());
            orderRequest.setBody("");
            orderRequest.setAttach("");
            orderRequest.setTradeType("NATIVE");//交易類型
            orderRequest.setProductId(booking.getId().toString());

            WxPayNativeOrderResult wxPayNativeOrderResult = (WxPayNativeOrderResult) wxPayService.createOrder(orderRequest);
            return wxPayNativeOrderResult.getCodeUrl();
        } catch (Exception e) {
            // 處理異常
            e.printStackTrace();
            return e.getMessage();
        }
    }
	
	//支付回調(diào)函數(shù)
	 public String payNotify(String xmlData) {
        try {
            WxPayService wxPayService = wxPayService();
            WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
            String orderId = notifyResult.getOutTradeNo();//拿到訂單號獲取訂單
            logger.info("wechatPayConfig.rePayNotify():{}", xmlData);
          	
          	//處理的業(yè)務(wù)
          	//因為會重復(fù)調(diào)用,如果你這里是錢包這種,那么一定要判斷是否支付過了


            return WxPayNotifyResponse.success("成功_" + orderInfo.getId());
        } catch (WxPayException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }

	//退款訂單請求
	//RefundInfo 退款的訂單,這里單筆訂單,如果是多筆,你可以構(gòu)建list 要循環(huán)執(zhí)行
	//RefundInfo 中需要包括 支付訂單的訂單號、支付金額、退款的訂單號、退款金額
	  public String redRefundPay(RefundInfo refund){
        try {
            if (refund == null) {
                return "訂單獲取失敗";
            }
            WxPayService wxPayService = wxPayService();
            WxPayRefundRequest orderRequest = new WxPayRefundRequest();
            orderRequest.setOutTradeNo(refund.getDisplayNo());
            orderRequest.setNotifyUrl(wechatPayConfig.getRefundNotifyUrl());
            orderRequest.setTotalFee(BigDecimal.valueOf(refund.getDisplaySum()).multiply(new BigDecimal(100)).intValue());
            orderRequest.setRefundFee(BigDecimal.valueOf(refund.getDisplayPrice()).multiply(new BigDecimal(100)).intValue());
            orderRequest.setOutRefundNo(memberRedRefund.getDisplayNo());

            WxPayRefundResult wxPayRefundResult = wxPayService.refund(orderRequest);

            if ("SUCCESS".equals(wxPayRefundResult.getReturnCode())
                    && "SUCCESS".equals(wxPayRefundResult.getResultCode())) {
                /**
                 * 系統(tǒng)內(nèi)部業(yè)務(wù)邏輯
                 */
               
                logger.info("wechatPayConfig.redRefundNotify():{}", refund.toString());
                
                return "退款中";
            }
            return "退款失敗";


        } catch (Exception e) {
            // 處理異常
            e.printStackTrace();
            return e.getMessage();
        }
    }

	//退款回調(diào)函數(shù)
	 public String redRefundNotify(String xmlData) {
        try {
            WxPayService wxPayService = wxPayService();
            WxPayRefundNotifyResult notifyResult = wxPayService.parseRefundNotifyResult(xmlData);
            String orderId = notifyResult.getReqInfo().getOutRefundNo();//拿到訂單號獲取訂單

            logger.info("wechatPayConfig.redRefundNotify():{}", xmlData);
            //編寫自己的業(yè)務(wù)處理邏輯


            return WxPayNotifyResponse.success("成功_" + orderInfo.getId());
        } catch (WxPayException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }
}

到此這篇關(guān)于springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款的文章就介紹到這了,更多相關(guān)springboot微信支付和退款內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Spring MVC中使用Controller如何進行重定向

    Spring MVC中使用Controller如何進行重定向

    這篇文章主要介紹了Spring MVC中使用Controller如何進行重定向操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • JAVA十大排序算法之希爾排序詳解

    JAVA十大排序算法之希爾排序詳解

    這篇文章主要介紹了java中的希爾排序,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • springboot多模塊包掃描問題的解決方法

    springboot多模塊包掃描問題的解決方法

    這篇文章主要介紹了springboot多模塊包掃描問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java 選擇、冒泡排序、折半查找(實例講解)

    Java 選擇、冒泡排序、折半查找(實例講解)

    下面小編就為大家?guī)硪黄狫ava 選擇、冒泡排序、折半查找(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java數(shù)據(jù)結(jié)構(gòu) 遞歸之迷宮回溯案例講解

    Java數(shù)據(jù)結(jié)構(gòu) 遞歸之迷宮回溯案例講解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)遞歸之迷宮回溯案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java多線程中的ThreadPoolExecutor解讀

    Java多線程中的ThreadPoolExecutor解讀

    這篇文章主要介紹了Java多線程中的ThreadPoolExecutor解讀,線程池中的核心線程數(shù),當(dāng)提交一個任務(wù)時,線程池創(chuàng)建一個新線程執(zhí)行任務(wù),直到當(dāng)前線程數(shù)等于corePoolSize;如果當(dāng)前線程數(shù)為corePoolSize,繼續(xù)提交的任務(wù)被保存到阻塞隊列中,等待被執(zhí)行,需要的朋友可以參考下
    2023-09-09
  • JPA原生SQL實現(xiàn)增刪改查的示例詳解

    JPA原生SQL實現(xiàn)增刪改查的示例詳解

    JPA除了對JPQL提供支持外,還對原生SQL語句也提供了支持。本文將利用生SQL實現(xiàn)增刪改查功能,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-09-09
  • 詳解Java如何通過Socket實現(xiàn)查詢IP

    詳解Java如何通過Socket實現(xiàn)查詢IP

    在本文中,我們來學(xué)習(xí)下如何找到連接到服務(wù)器的客戶端計算機的IP地址。我們將創(chuàng)建一個簡單的客戶端-服務(wù)器場景,讓我們探索用于TCP/IP通信的java.net?API,感興趣的可以了解一下
    2022-10-10
  • SpringBoot中的JPA(Java?Persistence?API)詳解

    SpringBoot中的JPA(Java?Persistence?API)詳解

    這篇文章主要介紹了SpringBoot中的JPA(Java?Persistence?API)詳解,JPA用于將?Java?對象映射到關(guān)系型數(shù)據(jù)庫中,它提供了一種面向?qū)ο蟮姆绞絹聿僮鲾?shù)據(jù)庫,使得開發(fā)者可以更加方便地進行數(shù)據(jù)持久化操作,需要的朋友可以參考下
    2023-07-07
  • SpringBoot項目實現(xiàn)統(tǒng)一異常處理的最佳方案

    SpringBoot項目實現(xiàn)統(tǒng)一異常處理的最佳方案

    在前后端分離的項目開發(fā)過程中,我們通常會對數(shù)據(jù)返回格式進行統(tǒng)一的處理,這樣可以方便前端人員取數(shù)據(jù),后端發(fā)生異常時同樣會使用此格式將異常信息返回給前端,本文介紹了如何在SpringBoot項目中實現(xiàn)統(tǒng)一異常處理,如有錯誤,還望批評指正
    2024-02-02

最新評論