最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

UniApp?+?SpringBoot?實現(xiàn)微信支付和退款功能

 更新時間:2022年06月06日 10:01:42   作者:奔跑的磚頭  
這篇文章主要介紹了UniApp?+?SpringBoot?實現(xiàn)微信支付和退款功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

開發(fā)準(zhǔn)備

  • 一臺用于支付的測試機(jī),必須得是一個安卓機(jī)因為需要打支付基座才能使用。
  • 用于編寫的后端框架接口的 IDE (IDEA 或者 Eclipse 都可以)
  • HBuilder X 用來編輯 UniApp 項目的編輯器和編譯器
  • 基本的 SpringBoot 的腳手架,可以去 https://start.spring.io/ 或者 IDEA 自帶的快速生成腳手架插件。
  • Jdk 11

微信支付開發(fā)

我這里省略了申請等步驟。如果沒有申請過企業(yè)支付的可以去官網(wǎng)申請 https://pay.weixin.qq.com/static/applyment_guide/applyment_detail_app.shtml 。安卓測試必須要打成基座,或者是正式APP應(yīng)用。

后端部分

在 SpringBoot 中添加以下坐標(biāo)

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

<!-- 微信支付坐標(biāo) 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>
<!-- 微信支付坐標(biāo) 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 我們不去用默認(rèn)的 application.properties 文件,畢竟 yml 更好看點。并在 yml 中添加以下內(nèi)容

# 服務(wù)啟動端口
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

創(chuàng)建一個 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;
}

創(chuàng)建一個 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());
        // 可以指定是否使用沙箱環(huán)境
        payConfig.setUseSandboxEnv(false);
        payConfig.setSignType("MD5");

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

    /**
     * 創(chuàng)建微信訂單給APP
     *
     * @param productTitle 商品標(biāo)題
     * @param outTradeNo   訂單號
     * @param totalFee     總價
     * @return
     */
    public Object createOrder(String productTitle, String outTradeNo, Integer totalFee) {
        try {
            WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
            // 支付描述
            request.setBody(productTitle);
            // 訂單號
            request.setOutTradeNo(outTradeNo);
            // 請按照分填寫
            request.setTotalFee(totalFee);
            // 回調(diào)鏈接
            request.setNotifyUrl(wechatPayConfig.getNotifyUrl());
            // 終端IP.
            request.setSpbillCreateIp(InetAddress.getLocalHost().getHostAddress());
            // 設(shè)置類型為APP
            request.setTradeType(WxPayConstants.TradeType.APP);
            // 一定要用 createOrder 不然得自己做二次校驗
            Object order = wxPayService().createOrder(request);
            return order;
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 退款
     *
     * @param tradeNo
     * @param totalFee
     * @return
     */
    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;
    }
}

創(chuàng)建一個 WechatController.java 來實現(xiàn)接口給前端調(diào)用時使用

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);

    /**
     * 創(chuàng)建微信訂單給APP
     *
     * @return
     */
    @RequestMapping(value = "/unified/request", method = RequestMethod.GET)
    public Object appPayUnifiedRequest() {
        // totalFee 必須要以分為單位
        Object createOrderResult = wechatPayService.createOrder("測試支付", String.valueOf(System.currentTimeMillis()), 1);
        logger.info("統(tǒng)一下單的生成的參數(shù):{}", 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())) {
                // 可以實現(xiàn)自己的邏輯
                logger.info("來自微信支付的回調(diào):{}", result);
            }

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

    /**
     * 退款
     *
     * @param transaction_id
     */
    @RequestMapping(method = RequestMethod.POST, value = "refund")
    public void refund(String transaction_id) {
        // totalFee 必須要以分為單位,退款的價格可以這里只做的全部退款
        WxPayRefundResult refund = wechatPayService.refund(transaction_id, 1);
        // 實現(xiàn)自己的邏輯
        logger.info("退款本地回調(diào):{}", refund);
    }

    /**
     * 退款回調(diào)
     *
     * @param xmlData
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "refund_notify")
    public String refundNotify(@RequestBody String xmlData) {
        // 實現(xiàn)自己的邏輯
        logger.info("退款遠(yuǎn)程回調(diào):{}", xmlData);
        // 必須要返回 SUCCESS 不過有 WxPayNotifyResponse 給整合成了 xml了
        return WxPayNotifyResponse.success("成功");
    }

}

上面的 controller 寫了兩個接口一個用來 app端的調(diào)用,一個給支付用來回調(diào)。回調(diào)接口的地址要放到剛才配置中的 notifyUrl 屬性里。還有一個是微信的退款接口。

  • 由于支付寶回調(diào)要使用線上的地址作為回調(diào)地址,這里我推薦兩個解決辦法
  • 使用一臺服務(wù)器+備案的域名搭建上面的后臺地址
  • 使用 花生殼 來實現(xiàn)本地內(nèi)網(wǎng)穿透

我使用的是 花生殼 作為本次的開發(fā)環(huán)境,啟動 springboot 的服務(wù),配置好花生殼。后臺部分到目前為止已經(jīng)結(jié)束了。

前端部分

創(chuàng)建部分和我寫的支付寶那個一樣,如果不知道可以去看一下。所以跳過創(chuàng)建部分了,直接來到了代碼實現(xiàn)。要在 manifest.json 勾選微信支付支持

創(chuàng)建前端支付代碼 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>

點擊按鈕就可以前往微信支付看下后臺的生成的組合參數(shù)

跳轉(zhuǎn)微信支付之后會跳回這里,提示支付成功。查看一下后臺回調(diào)

之后的業(yè)務(wù)按照支付邏輯開發(fā)就可以,簡單的支付已經(jīng)完成。在按照剛才給的回調(diào)參數(shù)做個退款操作

我們使用 apipost 一個很強(qiáng)大的工具,被同事安利的。那就正好拿他測測退款借口,就不寫代碼了。

此時如果沒有任何錯誤,后臺控制臺會返回退款本地和遠(yuǎn)程回調(diào)信息

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

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

到此這篇關(guān)于UniApp + SpringBoot 實現(xiàn)微信支付和退款的文章就介紹到這了,更多相關(guān)SpringBoot 微信支付和退款內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Boot設(shè)置支持跨域請求過程詳解

    Spring Boot設(shè)置支持跨域請求過程詳解

    這篇文章主要介紹了Spring Boot設(shè)置支持跨域請求過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 詳解springboot整合ehcache實現(xiàn)緩存機(jī)制

    詳解springboot整合ehcache實現(xiàn)緩存機(jī)制

    這篇文章主要介紹了詳解springboot整合ehcache實現(xiàn)緩存機(jī)制,ehcache提供了多種緩存策略,主要分為內(nèi)存和磁盤兩級,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Java基礎(chǔ)之集合Set詳解

    Java基礎(chǔ)之集合Set詳解

    這篇文章主要介紹了Java基礎(chǔ)之集合Set詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決

    IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決

    這篇文章主要介紹了IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • java啟動命令中-D和--的區(qū)別解析

    java啟動命令中-D和--的區(qū)別解析

    在 SpringBoot 項目中,啟動時,通過 -D 或 -- 添加參數(shù),都可以直接覆蓋 yml 或 properties 配置文件中的同名配置,如果不存在則相當(dāng)于添加了一個配置,這篇文章主要介紹了java啟動命令中-D和--的區(qū)別,需要的朋友可以參考下
    2024-08-08
  • java TreeMap源碼解析詳解

    java TreeMap源碼解析詳解

    這篇文章主要介紹了java TreeMap源碼解析詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 如何在IDEA啟動多個Spring Boot工程實例(圖文)

    如何在IDEA啟動多個Spring Boot工程實例(圖文)

    這篇文章主要介紹了如何在IDEA啟動多個Spring Boot工程實例(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 利用JSONObject.toJSONString()包含或排除指定的屬性

    利用JSONObject.toJSONString()包含或排除指定的屬性

    這篇文章主要介紹了利用JSONObject.toJSONString()包含或排除指定的屬性,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級)

    Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級)

    壓縮文件在生活中經(jīng)常能用到,在Java中提供了壓縮和解壓縮文件的功能,本文主要介紹了Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級),感興趣的可以了解一下
    2024-03-03
  • hadoop運行java程序(jar包)并運行時動態(tài)指定參數(shù)

    hadoop運行java程序(jar包)并運行時動態(tài)指定參數(shù)

    這篇文章主要介紹了hadoop如何運行java程序(jar包)并運行時動態(tài)指定參數(shù),使用hadoop 運行 java jar包,Main函數(shù)一定要加上全限定類名,需要的朋友可以參考下
    2021-06-06

最新評論

卓资县| 滁州市| 顺昌县| 永善县| 微博| 微博| 内黄县| 尖扎县| 长武县| 卓尼县| 微博| 应城市| 合水县| 射阳县| 右玉县| 张掖市| 若尔盖县| 宁明县| 治县。| 昭觉县| 南郑县| 潜山县| 临武县| 浦江县| 绍兴市| 星子县| 江油市| 杭锦旗| 怀来县| 奈曼旗| 合作市| 道孚县| 宜春市| 进贤县| 章丘市| 牡丹江市| 汽车| 高雄县| 河北区| 神木县| 南江县|