国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

這篇具有很好參考價(jià)值的文章主要介紹了springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言

微信支付適用于許多場(chǎng)合,如小程序、網(wǎng)頁(yè)支付、但微信支付相對(duì)于其他支付方式略顯麻煩,我們使用IJpay框架進(jìn)行整合

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

一、IJpay是什么?

JPay 讓支付觸手可及,封裝了微信支付、支付寶支付、銀聯(lián)支付常用的支付方式以及各種常用的接口。不依賴任何第三方 mvc 框架,僅僅作為工具使用簡(jiǎn)單快速完成支付模塊的開(kāi)發(fā),可輕松嵌入到任何系統(tǒng)里。

二、使用步驟

1.準(zhǔn)備小程序必要信息

1.1 要在小程序端關(guān)聯(lián)商戶號(hào)
springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

1.2在application.yml文件中配置相關(guān)信息

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

這是微信平臺(tái)下載的證書

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

1.3導(dǎo)入IJpay依賴

    <dependency>
            <groupId>com.github.javen205</groupId>
            <artifactId>IJPay-WxPay</artifactId>
            <version>2.9.6</version>
        </dependency>

2.具體操作

2.1新建控制器WxPayApiContoller

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

2.2控制器代碼:


package cn.cnvp.web.api.wechart;

import cn.cnvp.web.token.message.JsonResult;
import cn.cnvp.web.utils.WxUtils;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ijpay.core.IJPayHttpResponse;
import com.ijpay.core.enums.RequestMethodEnum;
import com.ijpay.core.kit.AesUtil;
import com.ijpay.core.kit.HttpKit;
import com.ijpay.core.kit.PayKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.core.utils.DateTimeZoneUtil;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.enums.WxDomainEnum;
import com.ijpay.wxpay.enums.v3.BasePayApiEnum;
import com.ijpay.wxpay.enums.v3.OtherApiEnum;
import com.ijpay.wxpay.model.v3.Amount;
import com.ijpay.wxpay.model.v3.Payer;
import com.ijpay.wxpay.model.v3.UnifiedOrderModel;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Scoot
 * @Date: 2023/3/16 15:52
 * @Description: 微信支付控制器
 */
@Slf4j
@Api(tags = "微信支付控制器")
@RestController
@RequestMapping("/api/wx/pay/v1")
@Scope("prototype")
public class WxPayApiController {


    /**微信小程序appid**/
    @Value("${wechat.ma.appId}")
    String appid;

    /**微信小程序secretId**/
    @Value("${wechat.ma.secret}")
    String secret;

    /**商戶號(hào)**/
    @Value("${wechat.ma.mchid}")
    String mchid;

    /**商戶密鑰**/
    @Value("${wechat.ma.mchKey}")
    String mchKey;

    /**回調(diào)地址**/
    @Value("${wechat.ma.notifyUrl}")
    String notifyUrl;

    /**證書地址**/
    @Value("${wechat.ma.certPath}")
    String certPath;
    /**證書密鑰地址**/
    @Value("${wechat.ma.certKeyPath}")
    String certKeyPath;
    /**微信平臺(tái)證書**/
    @Value("${wechat.ma.platFormPath}")
    String platFormPath;



    /**
     * 微信支付
     * @param openId  用戶openId
     * @return
     */
    @RequestMapping("/jsApiPay")
    @ResponseBody
    public JsonResult jsApiPay(@RequestParam(value = "openId", required = false, defaultValue = "o-_-itxuXeGW3O1cxJ7FXNmq8Wf8") String openId) {
        try {
            String timeExpire = DateTimeZoneUtil.dateToTimeZone(System.currentTimeMillis() + 1000 * 60 * 3);
            UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel()
                    // APPID
                    .setAppid(appid)
                    // 商戶號(hào)
                    .setMchid(mchid)
                    .setDescription("IJPay 讓支付觸手可及")
                    .setOut_trade_no(PayKit.generateStr())
                    .setTime_expire(timeExpire)
                    .setAttach("微信系開(kāi)發(fā)腳手架 https://gitee.com/javen205/TNWX")
                    .setNotify_url(notifyUrl)
                    .setAmount(new Amount().setTotal(1))
                    .setPayer(new Payer().setOpenid(openId));

            log.info("統(tǒng)一下單參數(shù) {}", JSONUtil.toJsonStr(unifiedOrderModel));
            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethodEnum.POST,
                    WxDomainEnum.CHINA.toString(),
                    BasePayApiEnum.JS_API_PAY.toString(),
                    mchid,
                    getSerialNumber(),
                    null,
                    certKeyPath,
                    JSONUtil.toJsonStr(unifiedOrderModel)
            );
            log.info("統(tǒng)一下單響應(yīng) {}", response);
            // 根據(jù)證書序列號(hào)查詢對(duì)應(yīng)的證書來(lái)驗(yàn)證簽名結(jié)果
            boolean verifySignature = WxPayKit.verifySignature(response, platFormPath);
            log.info("verifySignature: {}", verifySignature);
            if (response.getStatus() == HttpStatus.HTTP_OK && verifySignature) {
                String body = response.getBody();
                JSONObject jsonObject = JSONUtil.parseObj(body);
                String prepayId = jsonObject.getStr("prepay_id");
                Map<String, String> map = WxPayKit.jsApiCreateSign(appid, prepayId, certKeyPath);
                log.info("喚起支付參數(shù):{}", map);
                return JsonResult.success("獲取成功",JSONUtil.toJsonStr(map));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.error("喚起失敗");
    }

    /**
     * 微信支付回調(diào)
     * @param request
     * @param response
     */
    @RequestMapping(value = "/payNotify", method = {org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
    public void payNotify(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>(12);
        try {
            String timestamp = request.getHeader("Wechatpay-Timestamp");
            String nonce = request.getHeader("Wechatpay-Nonce");
            String serialNo = request.getHeader("Wechatpay-Serial");
            String signature = request.getHeader("Wechatpay-Signature");

            log.info("timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
            String result = HttpKit.readData(request);
            log.info("支付通知密文 {}", result);

            // 需要通過(guò)證書序列號(hào)查找對(duì)應(yīng)的證書,verifyNotify 中有驗(yàn)證證書的序列號(hào)
            String plainText = WxPayKit.verifyNotify(serialNo, result, signature, nonce, timestamp,
                    mchKey, platFormPath);

            log.info("支付通知明文 {}", plainText);

            if (StrUtil.isNotEmpty(plainText)) {
                response.setStatus(200);
                map.put("code", "SUCCESS");
                map.put("message", "SUCCESS");
            } else {
                response.setStatus(500);
                map.put("code", "ERROR");
                map.put("message", "簽名錯(cuò)誤");
            }
            response.setHeader("Content-type", ContentType.JSON.toString());
            response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private String getSerialNumber() {
        // 獲取證書序列號(hào)
        X509Certificate certificate = PayKit.getCertificate(certPath);
        if (null != certificate) {
            String serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
            // 提前兩天檢查證書是否有效
            boolean isValid = PayKit.checkCertificateIsValid(certificate, mchid, -2);
            log.info("證書是否可用 {} 證書有效期為 {}", isValid, DateUtil.format(certificate.getNotAfter(), DatePattern.NORM_DATETIME_PATTERN));
            System.out.println("serialNo:" + serialNo);
            return serialNo;
        }
        return null;
    }



    @RequestMapping("/get")
    @ResponseBody
    public String v3Get() {
        // 獲取平臺(tái)證書列表
        try {
            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethodEnum.GET,
                    WxDomainEnum.CHINA.toString(),
                    OtherApiEnum.GET_CERTIFICATES.toString(),
                    mchid,
                    getSerialNumber(),
                    null,
                    certKeyPath,
                    ""
            );
            String serialNumber = response.getHeader("Wechatpay-Serial");
            String body = response.getBody();
            int status = response.getStatus();
            log.info("serialNumber: {}", serialNumber);
            log.info("status: {}", status);
            log.info("body: {}", body);
            int isOk = 200;
            if (status == isOk) {
                JSONObject jsonObject = JSONUtil.parseObj(body);
                JSONArray dataArray = jsonObject.getJSONArray("data");
                // 默認(rèn)認(rèn)為只有一個(gè)平臺(tái)證書
                JSONObject encryptObject = dataArray.getJSONObject(0);
                JSONObject encryptCertificate = encryptObject.getJSONObject("encrypt_certificate");
                String associatedData = encryptCertificate.getStr("associated_data");
                String cipherText = encryptCertificate.getStr("ciphertext");
                String nonce = encryptCertificate.getStr("nonce");
                String serialNo = encryptObject.getStr("serial_no");
                final String platSerialNo = savePlatformCert(associatedData, nonce, cipherText, platFormPath);
                log.info("平臺(tái)證書序列號(hào): {} serialNo: {}", platSerialNo, serialNo);
            }
            // 根據(jù)證書序列號(hào)查詢對(duì)應(yīng)的證書來(lái)驗(yàn)證簽名結(jié)果
            boolean verifySignature = WxPayKit.verifySignature(response, platFormPath);
            System.out.println("verifySignature:" + verifySignature);
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



    /**
     * 保存平臺(tái)證書
     * @param associatedData    associated_data
     * @param nonce             nonce
     * @param cipherText        cipherText
     * @param certPath          證書保存路徑
     * @return
     */
    private String savePlatformCert(String associatedData, String nonce, String cipherText, String certPath) {
        try {
            AesUtil aesUtil = new AesUtil(mchKey.getBytes(StandardCharsets.UTF_8));
            // 平臺(tái)證書密文解密
            // encrypt_certificate 中的  associated_data nonce  ciphertext
            String publicKey = aesUtil.decryptToString(
                    associatedData.getBytes(StandardCharsets.UTF_8),
                    nonce.getBytes(StandardCharsets.UTF_8),
                    cipherText
            );
            // 保存證書
            cn.hutool.core.io.file.FileWriter writer = new FileWriter(certPath);
            writer.write(publicKey);
            // 獲取平臺(tái)證書序列號(hào)
            X509Certificate certificate = PayKit.getCertificate(new ByteArrayInputStream(publicKey.getBytes()));
            return certificate.getSerialNumber().toString(16).toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

}

2.3下載平臺(tái)證書platForm.pem

調(diào)用上面get方法下載證書下載完成后會(huì)在yml配置的路徑生成platForm.pem證書

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

2.4啟動(dòng)測(cè)試

接口傳入openID(后續(xù)可換其他方式),返回如下json字符串表示成功

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

2.5小程序喚起微信支付

2.5.1uniapp代碼


<template>
    <view class="content">
        <button @click="pay">支付</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                sign :{}
            }
        },
        onLoad() {
        
        },
        methods: {
            pay(){
                uni.request({
                    // 獲取微信支付簽名地址
                    url:"http://192.168.0.231:8087/api/wx/pay/v1/jsApiPay?openId=oEmg75RAQX-7uRqGUkSy0tGcfzpI",
                     // 成功回調(diào)
                     success: (res) => {
                        let sign =JSON.parse(res.data.data);
                        // 僅作為示例,非真實(shí)參數(shù)信息。
                        uni.requestPayment({
                            provider: 'wxpay',
                            timeStamp: sign.timeStamp,
                            nonceStr: sign.nonceStr,
                            package: sign.package,
                            signType: sign.signType,
                            paySign: sign.paySign,
                            success: function (res) {
                                console.log('success:' + JSON.stringify(res));
                            },
                            fail: function (err) {
                                console.log('fail:' + JSON.stringify(err));
                            }
                        });
                    }
                })
                
            }
            
        }
    }
</script>

<style>
    .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>
springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

2.6異步回調(diào)

在yml配置的異步回調(diào)地址(需要外網(wǎng)訪問(wèn))

springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序

總結(jié)

以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了springboot整合IJpay進(jìn)行微信支付的使用,關(guān)于調(diào)用支付jspi缺少參數(shù):total_fee主要是因?yàn)?.金額為空 2.訂單號(hào)重復(fù) 3.訂單號(hào)為空文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-433791.html

到了這里,關(guān)于springboot整合IJPay實(shí)現(xiàn)微信支付-V3---微信小程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java實(shí)現(xiàn)微信小程序V3支付
  • Java實(shí)現(xiàn)微信小程序V3支付 (完整demo)
  • 【微信支付】java-微信小程序支付-V3接口

    【微信支付】java-微信小程序支付-V3接口

    最開(kāi)始需要在微信支付的官網(wǎng)注冊(cè)一個(gè)商戶; 在管理頁(yè)面中申請(qǐng)關(guān)聯(lián)小程序,通過(guò)小程序的 appid 進(jìn)行關(guān)聯(lián);商戶號(hào)和appid之間是多對(duì)多的關(guān)系 進(jìn)入微信公眾平臺(tái),功能-微信支付中確認(rèn)關(guān)聯(lián) 具體流程請(qǐng)瀏覽官方文檔:接入前準(zhǔn)備-小程序支付 | 微信支付商戶平臺(tái)文檔中心 流程走

    2024年02月06日
    瀏覽(33)
  • 【Java】微信小程序V3支付(后臺(tái))

    【Java】微信小程序V3支付(后臺(tái))

    目錄 ????????相關(guān)官網(wǎng)文檔 ????????1.需要的參數(shù) ????????2.引入庫(kù) ????????3.用到的工具類 ????????4.支付下單實(shí)現(xiàn) ????????5.支付回調(diào) 接入前準(zhǔn)備-小程序支付 | 微信支付商戶平臺(tái)文檔中心 微信支付-JSAPI下單 獲取平臺(tái)證書列表-文檔中心-微信支付商戶平

    2024年02月12日
    瀏覽(53)
  • 微信小程序完整實(shí)現(xiàn)微信支付功能(SpringBoot和小程序)

    微信小程序完整實(shí)現(xiàn)微信支付功能(SpringBoot和小程序)

    1.前言 不久前給公司實(shí)現(xiàn)支付功能,折騰了一陣子,終于實(shí)現(xiàn)了,微信支付對(duì)于小白來(lái)說(shuō)真的很困難,特別是沒(méi)有接觸過(guò)企業(yè)級(jí)別開(kāi)發(fā)的大學(xué)生更不用說(shuō),因此嘗試寫一篇我如何從小白實(shí)現(xiàn)微信小程序支付功能的吧,使用的后端是 SpringBoot 。 2.準(zhǔn)備工作 首先,要實(shí)現(xiàn)支付功能

    2024年02月04日
    瀏覽(34)
  • PHP實(shí)現(xiàn)小程序微信支付(v3版本)

    PS:本篇文章是PHP對(duì)小程序進(jìn)行微信支付v3版本的實(shí)現(xiàn),僅用于對(duì)支付流程的了解,具體使用方面需要大家自行調(diào)整 小程序端JS代碼: PHP類的相關(guān)代碼:

    2024年02月12日
    瀏覽(23)
  • uniapp+java/springboot實(shí)現(xiàn)微信小程序APIV3支付功能

    uniapp+java/springboot實(shí)現(xiàn)微信小程序APIV3支付功能

    微信小程序的支付跟H5的支付和APP支付流程不一樣,本次只描述下小程序支付流程。 1.微信小程序賬號(hào) 文檔:小程序申請(qǐng) 小程序支付需要先認(rèn)證,如果你有已認(rèn)證的公眾號(hào),也可以通過(guò)公眾號(hào)免費(fèi)注冊(cè)認(rèn)證小程序。 一般300元,我是認(rèn)證的政府的免費(fèi)。 然后登錄小程序,設(shè)置

    2023年04月19日
    瀏覽(31)
  • 【Springboot】整合wxjava實(shí)現(xiàn) 微信小程序:授權(quán)登錄

    【Springboot】整合wxjava實(shí)現(xiàn) 微信小程序:授權(quán)登錄

    提示:以下是本篇文章正文內(nèi)容,下面案例可供參考 WxJava - 微信開(kāi)發(fā) Java SDK,支持微信支付、開(kāi)放平臺(tái)、公眾號(hào)、企業(yè)號(hào)/企業(yè)微信、小程序等的后端開(kāi)發(fā)。 官方的gitee倉(cāng)庫(kù)地址 官方的github倉(cāng)庫(kù)地址 官方的關(guān)于微信小程序的demo 導(dǎo)入wxjava的maven依賴 WxMaProperties 用于讀取yml配置

    2024年02月01日
    瀏覽(20)
  • Python對(duì)接微信小程序V3接口進(jìn)行支付,并使用uwsgi+nginx+django進(jìn)行https部署

    網(wǎng)上找了很多教程,但是很亂很雜,并且教程資源很少且說(shuō)的詳細(xì)。這里就記錄一下分享給大家 共分為以下幾個(gè)步驟: 目錄 一、開(kāi)始前準(zhǔn)備信息 二、使用前端code獲取用戶的openid 三、對(duì)接小程序v3接口下單 四、小程序支付的回調(diào) 五、安裝并啟動(dòng)uwsgi 六、安裝并啟動(dòng)nginx 七、

    2024年02月12日
    瀏覽(27)
  • SpringBoot對(duì)接微信小程序支付功能開(kāi)發(fā)(二,支付回調(diào)功能)

    SpringBoot對(duì)接微信小程序支付功能開(kāi)發(fā)(二,支付回調(diào)功能)

    接著上一篇: SpringBoot對(duì)接微信小程序支付功能開(kāi)發(fā)(一,下單功能) 在上一篇下單功能中我們有傳支付結(jié)果回調(diào)地址。 下面是回調(diào)接口實(shí)現(xiàn) 根據(jù)官網(wǎng)給的參數(shù)進(jìn)行業(yè)務(wù)處理 這就完成了,微信支付回調(diào)你的地址,并且把支付的信息傳進(jìn)來(lái),剩下就要根據(jù)自己業(yè)務(wù)進(jìn)行操作。

    2024年02月11日
    瀏覽(63)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包