From 5ace6f149ee4ff3ab154c9668cdddadbc2d2e153 Mon Sep 17 00:00:00 2001 From: yezian Date: Wed, 21 Feb 2024 00:00:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/pay/inweixin/[body]/page.jsx | 106 ++++++++++++++++++++++++------- app/pay/page.jsx | 68 ++++++-------------- 2 files changed, 100 insertions(+), 74 deletions(-) diff --git a/app/pay/inweixin/[body]/page.jsx b/app/pay/inweixin/[body]/page.jsx index b649ca2..c03b3c1 100644 --- a/app/pay/inweixin/[body]/page.jsx +++ b/app/pay/inweixin/[body]/page.jsx @@ -2,41 +2,101 @@ import React, { useState, useEffect } from "react"; import { useRouter, useSearchParams } from "next/navigation"; +import { Toast } from "antd-mobile"; +import { generateSignature } from "@/utils/crypto"; export default function InWeixin({ params }) { const router = useRouter(); const searchParams = useSearchParams(); - const [code, setCode] = useState("no code"); - const [body, setBody] = useState({}); - const [info, setInfo] = useState("no data"); + //微信支付jsapi支付参数 + const [jsapiParams, setJsapiParams] = useState(); + + //获取微信支付jsapi支付参数 useEffect(() => { - // //获取code - // const temCode = searchParams.get("code"); - // setCode(temCode); + const createOrder = async () => { + //获取code + const code = searchParams.get("code"); - // //获取支付body - // const strBody = decodeURIComponent(params.body); - // const temBody = JSON.parse(strBody); - // setBody(temBody); + //构建body + const strBody = decodeURIComponent(params.body); + const temBody = JSON.parse(strBody); + const body = { ...temBody, wechat_auth_code: code }; + const signature = generateSignature(body); + try { + const response = await fetch( + `/api/vas/create_order?signature=${signature}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + } + ); + const data = await response.json(); + if (data.ret === -1) { + Toast.show({ + content: data.msg, + }); + return; + } + setJsapiParams(data.data.wxpay_jsapi_param_obj); + } catch (error) { + console.error(error); + } + }; + createOrder(); + + // function onBridgeReady() { + // WeixinJSBridge.invoke( + // "getBrandWCPayRequest", + // { + // appId: "wxc28fd8aaf31984b6", //公众号ID,由商户传入 + // timeStamp: "1708438397", //时间戳,自1970年以来的秒数 + // nonceStr: "jXEwa7uEExqFjjlByZivG623Akbzg3Cv", //随机串 + // package: "prepay_id=wx2022131953635431d27a15e2c95e460000", + // signType: "RSA", //微信签名方式: + // paySign: + // "0rODbhGbNidvOcdyC3+JW5pP6DEd+DZZs0Zg3oMvbQ1pC857YvISgrS8QM2yEZpJFWCnjmUmjyQijp3ZxfV5kqr4x8+l+cmNlc87sDtkXhn/pUNMSKtdt+X9KmOrykBjsLj7hztaPoFSwkGYpxHUrxwiooW+y3to5QqD550CaER7XhAAK5knItPy6pa8rGtYPpEpCn8OkcHHVkuGAcawej1a6MGw2byEv2eRusr1VbUVWENgu557qBdGA2NHoOHfRL3n/goOvt1KXTYRtb+BkwsssHJQb8gyuz0qBR/aNFcSUBb4eSBenWAm1L+Wp/ndxLMkxr1ixQblwEJ9I2NPZw==", //微信签名 + // }, + // function (res) { + // if (res.err_msg == "get_brand_wcpay_request:ok") { + // return; + // } + // } + // ); + // } + // if (typeof WeixinJSBridge == "undefined") { + // if (document.addEventListener) { + // document.addEventListener("WeixinJSBridgeReady", onBridgeReady, false); + // } else if (document.attachEvent) { + // document.attachEvent("WeixinJSBridgeReady", onBridgeReady); + // document.attachEvent("onWeixinJSBridgeReady", onBridgeReady); + // } + // } else { + // onBridgeReady(); + // } + }, []); + + //调起收银台 + useEffect(() => { + if (!jsapiParams) return; function onBridgeReady() { WeixinJSBridge.invoke( "getBrandWCPayRequest", { - appId: "wxc28fd8aaf31984b6", //公众号ID,由商户传入 - timeStamp: "1708438397", //时间戳,自1970年以来的秒数 - nonceStr: "jXEwa7uEExqFjjlByZivG623Akbzg3Cv", //随机串 - package: "prepay_id=wx2022131953635431d27a15e2c95e460000", - signType: "RSA", //微信签名方式: - paySign: - "0rODbhGbNidvOcdyC3+JW5pP6DEd+DZZs0Zg3oMvbQ1pC857YvISgrS8QM2yEZpJFWCnjmUmjyQijp3ZxfV5kqr4x8+l+cmNlc87sDtkXhn/pUNMSKtdt+X9KmOrykBjsLj7hztaPoFSwkGYpxHUrxwiooW+y3to5QqD550CaER7XhAAK5knItPy6pa8rGtYPpEpCn8OkcHHVkuGAcawej1a6MGw2byEv2eRusr1VbUVWENgu557qBdGA2NHoOHfRL3n/goOvt1KXTYRtb+BkwsssHJQb8gyuz0qBR/aNFcSUBb4eSBenWAm1L+Wp/ndxLMkxr1ixQblwEJ9I2NPZw==", //微信签名 + appId: jsapiParams.appId, //公众号ID,由商户传入 + timeStamp: jsapiParams.timeStamp, //时间戳,自1970年以来的秒数 + nonceStr: jsapiParams.nonceStr, //随机串 + package: jsapiParams.package, + signType: jsapiParams.signType, //微信签名方式: + paySign: jsapiParams.paySign, //微信签名 }, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { - // 使用以上方式判断前端返回,微信团队郑重提示: - //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 - setInfo("success"); + return; } } ); @@ -51,10 +111,10 @@ export default function InWeixin({ params }) { } else { onBridgeReady(); } - }, []); + }, [jsapiParams]); return ( -
+

微信安全支付 @@ -70,8 +130,6 @@ export default function InWeixin({ params }) {

正在跳转...

-

{code}

-

{info}

); } diff --git a/app/pay/page.jsx b/app/pay/page.jsx index 9f3bb6c..e7aad4d 100644 --- a/app/pay/page.jsx +++ b/app/pay/page.jsx @@ -91,7 +91,7 @@ export default function Pay() { }); return; } - setIsLoading(true); + const base = webviewBaseRequest(); const body = { ...base, @@ -100,6 +100,15 @@ export default function Pay() { pay_type: type, from: "app", }; + + //如果是微信jsapi支付直接跳转到中间页 + if (type === "wxpay_jsapi") { + router.push(`/pay/${encodeURIComponent(JSON.stringify(body))}`); + return; + } + + setIsLoading(true); + const signature = generateSignature(body); try { const response = await fetch( @@ -139,28 +148,6 @@ export default function Pay() { } }; - //跳转联系客服 - const handleContact = () => { - if (navigator.userAgent.includes("FromWebview")) { - window.ReactNativeWebView.postMessage( - JSON.stringify({ - type: "NAVIGATE", - data: { - page: "MessageDetail", - params: { - mid: 1, - }, - }, - }) - ); - } else { - Toast.show({ - content: "请下载app联系客服充值", - }); - } - document.getElementById("manual_pay_modal").close(); - }; - const PriceItem = ({ item }) => { const handleClickPrice = (item) => { setSelectedPrice(item); @@ -302,39 +289,20 @@ export default function Pay() {
- -
-

- 人工充值 -

-

- 请联系人工客服进行充值,单笔金额不低于500人民币 -

-
- -
- -
-
-
-
); }