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

UniApp?+?SpringBoot?實(shí)現(xiàn)支付寶支付和退款功能

 更新時(shí)間:2022年06月06日 10:18:11   作者:奔跑的磚頭  
這篇文章主要介紹了UniApp?+?SpringBoot?實(shí)現(xiàn)支付寶支付和退款功能,基本的?SpringBoot?的腳手架,可以去IDEA?自帶的快速生成腳手架插件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧

上篇介紹了UniApp + SpringBoot 實(shí)現(xiàn)微信支付和退款功能,喜歡的朋友可以點(diǎn)擊查看。

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

  • 一臺(tái)用于支付的測(cè)試機(jī)
  • 用于編寫(xiě)的后端框架接口的 IDE (IDEA 或者 Eclipse 都可以)
  • HBuilder X 用來(lái)編輯 UniApp 項(xiàng)目的編輯器和編譯器
  • 基本的 SpringBoot 的腳手架,可以去 https://start.spring.io/ 或者 IDEA 自帶的快速生成腳手架插件。
  • Jdk 11

支付寶支付開(kāi)發(fā)

后端部分

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

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

<!-- 支付寶官方 SDK-->
<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.22.32.ALL</version>
</dependency>

<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 更好看點(diǎn)。并在 yml 中添加以下內(nèi)容

# 服務(wù)啟動(dòng)端口
server:
  port: 8080

# 支付寶支付
alipay:
  server_url: https://openapi.alipay.com/gateway.do
  app_id: 企業(yè)支付的 APPID
  private_key: 申請(qǐng)的私鑰
  format: json
  charset: utf-8
  alipay_public_key: 申請(qǐng)的公鑰
  sign_type: RSA2
  notifyUrl: 回調(diào)地址

創(chuàng)建一個(gè) AlipayConfig.java 繼承 com.alipay.api.AlipayConfig

@Getter
@Setter
@ToString
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfig extends com.alipay.api.AlipayConfig {
    private String serverUrl;
    private String appId;
    private String privateKey;
    private String format;
    private String charset;
    private String alipayPublicKey;
    private String signType;
    private String notifyUrl;
}

創(chuàng)建一個(gè) BizAlipayService.java

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 阿里云支付類
 */
@Service
public class BizAlipayService {

    private static Logger logger = LoggerFactory.getLogger(BizAlipayService.class);

    @Autowired
    AlipayConfig alipayConfig;

    private DefaultAlipayClient client() throws AlipayApiException {
        return new DefaultAlipayClient(alipayConfig);
    }

    /**
     * 預(yù)下單
     *
     * @param subject     訂單標(biāo)題
     * @param outTradeNo  商家生成的訂單號(hào)
     * @param totalAmount 訂單總價(jià)值
     * @return
     */
    public String appPay(String subject, String outTradeNo, String totalAmount) {
        String source = "";
        try {
            DefaultAlipayClient client = client();
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setSubject(subject);
            model.setOutTradeNo(outTradeNo);
            model.setTotalAmount(totalAmount);
            // alipay 封裝的接口調(diào)用
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            request.setBizModel(model);
            request.setNotifyUrl(alipayConfig.getNotifyUrl());
            AlipayTradeAppPayResponse response = client.sdkExecute(request);
            source = response.getBody();
        } catch (AlipayApiException e) {
            logger.error("支付出現(xiàn)問(wèn)題,詳情:{}", e.getErrMsg());
            e.printStackTrace();
        }
        return source;
    }
}

創(chuàng)建一個(gè) AlipayController.java 來(lái)實(shí)現(xiàn)接口給前端調(diào)用時(shí)使用

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AlipayController {
    private static Logger logger = LoggerFactory.getLogger(AlipayController.class);

    @Autowired
    AlipayConfig alipayConfig;
    @Autowired
    BizAlipayService alipayService;

    /**
     * 支付接口
     *
     * @return
     */
    @GetMapping("/pay")
    public String orderPay() {
        String s = alipayService.appPay("測(cè)試支付", String.valueOf(System.currentTimeMillis()), new BigDecimal("0.01").toString());
        logger.info("支付生成信息:{}", s);
        return s;
    }

    /**
     * 訂單回調(diào)
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/notify")
    public String orderNotify(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        try {
            boolean flag = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType());
            if (flag) {
                logger.info("支付回調(diào)信息:{}", params);
                return "success";
            } else {
                return "error";
            }
        } catch (AlipayApiException e) {
            logger.error("支付寶錯(cuò)誤回調(diào):{}", e.getErrMsg());
            e.printStackTrace();
            return "error";
        }
    }
}

上面的 controller 寫(xiě)了兩個(gè)接口一個(gè)用來(lái) app端的調(diào)用,一個(gè)給支付用來(lái)回調(diào)。回調(diào)接口的地址要放到剛才配置中的 notifyUrl 屬性里。

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

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

前端部分

UniApp 是一個(gè)使用Vue.js 開(kāi)發(fā)所有前端應(yīng)用的框架,開(kāi)發(fā)者編寫(xiě)一套代碼,可發(fā)布到iOS、Android、Web(響應(yīng)式)、以及各種小程序(微信/支付寶/百度/頭條/飛書(shū)/QQ/快手/釘釘/淘寶)、快應(yīng)用等多個(gè)平臺(tái)。
牛啊,UniApp 能打這么多的端呢,不過(guò)這次我只使用 APP 端的功能。

在 HBuilder X 中新建一個(gè)項(xiàng)目,我目前使用的版本 3.3.10.20220124

創(chuàng)建好項(xiàng)目之后,在 manifest.json 中勾選以下內(nèi)容

在 index.vue 添加支付相關(guān)功能

<template>
    <view class="content">
        <image class="logo" src="/static/logo.png"></image>
        <view class="text-area">
            <text class="title">{{title}}</text>
        </view>
        <button type="default" @click="goPay()">點(diǎn)我前去支付</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        onLoad() {

        },
        methods: {
            goPay() {
                uni.request({
                    url: "https://4789j06630.wocp.fun/pay",
                    success(res) {
                        uni.requestPayment({
                            provider: 'alipay',
                            orderInfo: res.data.data,
                            success(r) {
                                uni.showModal({
                                    content: "支付成功",
                                    showCancel: false
                                })
                            },
                            fail(e) {
                                uni.showModal({
                                    content: "支付失敗,原因?yàn)? " + e.errMsg,
                                    showCancel: false
                                })
                            },
                            complete: () => {
                                console.log("payment結(jié)束")
                            }
                        })

                    }
                })

            }
        }
    }
</script>

<style>
    page{
        background-color: #ff5500;
    }
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

點(diǎn)擊 點(diǎn)我前去支付 按鈕就可以打開(kāi)支付寶進(jìn)行支付了。查看接口返回的 log 信息

支付成功圖展示

生成預(yù)支付信息

支付成功回調(diào)

要記住里面的 trade_no 一會(huì)退款還需要這個(gè)。

以上就是支付寶支付的內(nèi)容了。下面開(kāi)始實(shí)現(xiàn)退款功能。

支付寶退款開(kāi)發(fā)

后端部分

在剛才的 BizAlipayService.java 添加以下代碼

/**
 * 退款
 *
 * @param tradeNo
 * @param totalAmount
 * @return
 */
public AlipayTradeRefundResponse refund(String tradeNo, String totalAmount) {
    try {
        DefaultAlipayClient client = client();
        AlipayTradeRefundModel alipayTradeRefundModel = new AlipayTradeRefundModel();
        alipayTradeRefundModel.setTradeNo(tradeNo);
        alipayTradeRefundModel.setRefundAmount(totalAmount);

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizModel(alipayTradeRefundModel);
        AlipayTradeRefundResponse response = client.execute(request);
        return response;
    } catch (AlipayApiException e) {
        logger.error("退款出現(xiàn)問(wèn)題,詳情:{}", e.getErrMsg());
        e.printStackTrace();
    }
    return null;
}

在 AlipayController.java 中添加一個(gè)接口用于退款操作

/**
 * 訂單退款
 *
 * @return
 * @TODO 僅實(shí)現(xiàn)了全部退款
 */
@RequestMapping(value = "/order_refund", method = RequestMethod.GET)
public AlipayTradeRefundResponse orderRefund() {
    AlipayTradeRefundResponse refund = alipayService.refund("2022020922001434041429269213", "0.01");
    return refund;
}

用的是剛才支付寶回調(diào)返回的 trade_nototal_amount 。

重啟服務(wù)調(diào)用接口測(cè)試是否可以退款,這里我為了省事使用了 Postman 。

手機(jī)此時(shí)已經(jīng)收到退款已到賬的消息通知,退款還是很簡(jiǎn)單的。后臺(tái)也返回了響應(yīng)的支付寶回調(diào)

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

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

相關(guān)文章

  • 簡(jiǎn)單了解springboot加載配置文件順序

    簡(jiǎn)單了解springboot加載配置文件順序

    這篇文章主要介紹了簡(jiǎn)單了解springboot加載配置文件順序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring?Boot如何配置yml配置文件定義集合、數(shù)組和Map

    Spring?Boot如何配置yml配置文件定義集合、數(shù)組和Map

    這篇文章主要介紹了Spring?Boot?優(yōu)雅配置yml配置文件定義集合、數(shù)組和Map,包括Spring?Boot?yml配置文件定義基本數(shù)據(jù)類型和引用數(shù)據(jù)類型的方式,需要的朋友可以參考下
    2023-10-10
  • 簡(jiǎn)單了解Java位域的一些知識(shí)

    簡(jiǎn)單了解Java位域的一些知識(shí)

    這篇文章主要介紹了簡(jiǎn)單了解Java位域的一些知識(shí),這個(gè)概念是在 Effective Java中了解到的, 可以通過(guò)EnumSet來(lái)代替位域這種方式表達(dá),需要的朋友可以參考下
    2019-07-07
  • Java中的強(qiáng)引用,軟引用,弱引用,虛引用的作用介紹

    Java中的強(qiáng)引用,軟引用,弱引用,虛引用的作用介紹

    這篇文章主要介紹了Java中的強(qiáng)引用,軟引用,弱引用,虛引用的作用,下文內(nèi)容具有一定的知識(shí)參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2022-02-02
  • Java關(guān)鍵字final的實(shí)現(xiàn)原理分析

    Java關(guān)鍵字final的實(shí)現(xiàn)原理分析

    這篇文章主要介紹了Java關(guān)鍵字final的實(shí)現(xiàn)原理分析,在JDK8之前,如果在匿名內(nèi)部類中需要訪問(wèn)局部變量,那么這個(gè)局部變量一定是final修飾的,但final關(guān)鍵字可以省略,需要的朋友可以參考下
    2024-01-01
  • spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解

    spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解

    這篇文章主要介紹了spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解,用戶只需登錄一次就能夠在這兩個(gè)系統(tǒng)中進(jìn)行操作。很明顯這就是單點(diǎn)登錄(Single Sign-On)達(dá)到的效果,需要的朋友可以參考下
    2019-06-06
  • APT?注解處理器實(shí)現(xiàn)?Lombok?常用注解功能詳解

    APT?注解處理器實(shí)現(xiàn)?Lombok?常用注解功能詳解

    這篇文章主要為大家介紹了使用APT?注解處理器實(shí)現(xiàn)?Lombok?常用注解功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 2020年IntelliJ IDEA最新最詳細(xì)配置圖文教程詳解

    2020年IntelliJ IDEA最新最詳細(xì)配置圖文教程詳解

    這篇文章主要介紹了2020年IntelliJ IDEA最新最詳細(xì)配置圖文教程詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • maven環(huán)境變量配置以及失敗原因解析

    maven環(huán)境變量配置以及失敗原因解析

    這篇文章主要為大家詳細(xì)介紹了maven環(huán)境變量配置教程,以及為大家解析了安裝失敗的原因,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Spring注解@Resource和@Autowired區(qū)別對(duì)比詳解

    Spring注解@Resource和@Autowired區(qū)別對(duì)比詳解

    這篇文章主要介紹了Spring注解@Resource和@Autowired區(qū)別對(duì)比詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論

滦南县| 德保县| 延吉市| 武强县| 昂仁县| 山阴县| 鄂托克旗| 开鲁县| 普格县| 时尚| 临泉县| 巴青县| 安阳县| 商水县| 苏尼特右旗| 盐津县| 惠安县| 白河县| 咸宁市| 灯塔市| 和平县| 定州市| 崇义县| 桃园市| 荃湾区| 沈丘县| 南阳市| 南召县| 韶关市| 扎鲁特旗| 临夏县| 崇文区| 台中县| 清徐县| 宁明县| 揭东县| 平南县| 乡宁县| 辽阳县| 玉树县| 敦煌市|