Files
2026-09-23 10:29:41 +08:00

167 lines
5.4 KiB
JavaScript

import { appDypayFun } from './douyin-pay.js';
import { jumpToPaymentSuccess, jumpToPaymentFailure } from './payment-navigation.js';
import { feedback } from "@/api/payment.js";
/**
* 微信支付
* @param {Object} alipay 微信支付的参数
* @param {Object} orderId 支付的订单ID
* @param {Object} orderNum 支付的订单编号
*/
export function appWxpayFun(alipay, orderId, orderNum, customSuccessUrl) {
const type = alipay.type;
const orderStr = alipay.orderStr;
console.log("微信支付---", alipay, type);
if (type === "app") {
// app
const orderInfo = {
appid: alipay.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
noncestr: alipay.nonceStr, // 随机字符串
package: "Sign=WXPay", // 固定值
partnerid: alipay.partnerId, // 微信支付商户号
prepayid: alipay.prepayId, // 统一下单订单号
timestamp: alipay.timeStamp, // 时间戳(单位:秒)
sign: alipay.sign, // 签名,这里用的 MD5/RSA 签名
};
uni.requestPayment({
provider: "wxpay",
orderInfo: orderInfo,
success: function (res) {
console.log("success:" + res);
payFeedbackFun(orderId, orderNum, 1);
jumpWayOk(customSuccessUrl);
},
fail: function (err) {
console.log("fail:", err);
jumpWayError();
},
});
} else if (type === "wechatMiniProgram") {
const orderInfo = {
// "appid": alipay.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
nonceStr: alipay.nonceStr, // 微信支付商户号
package: alipay.packageValue, // 统一下单订单号
timeStamp: alipay.timeStamp, // 时间戳(单位:秒)
signType: "RSA",
paySign: alipay.sign, // 签名,这里用的 MD5/RSA 签名
};
console.log("微信小程序支付---", orderInfo);
uni.requestPayment({
...orderInfo,
success: function (res) {
console.log("success:" + res);
payFeedbackFun(orderId, orderNum, 1);
jumpWayOk(customSuccessUrl);
},
fail: function (err) {
console.log("fail:", err);
jumpWayError();
},
});
} else if (type === "h5") {
// h5
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?link=${encodeURIComponent(
orderStr
)}&orderNum=` + orderNum, // 假设有一个专门的支付页面来处理支付链接
});
}
}
/**
* 支付宝支付
* @param {Object} alipay 微信支付的参数
* @param {Object} orderId 支付的订单ID
* @param {Object} orderNum 支付的订单编号
* @param {string} customSuccessUrl 自定义成功跳转页面
*/
export function zfbPayFun(alipay, orderId, orderNum, customSuccessUrl) {
console.log("alipay", alipay);
const type = alipay.type;
const orderStr = alipay.orderStr;
if (type === "h5") {
// h5
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?link=${encodeURIComponent(
orderStr
)}&orderNum=` + orderNum, // 假设有一个专门的支付页面来处理支付链接
});
} else if (type === "app") {
// app
uni.requestPayment({
provider: "alipay",
orderInfo: orderStr,
success: function (res) {
console.log("zfbPayFun res", res);
payFeedbackFun(orderId, orderNum, 1);
jumpWayOk(customSuccessUrl);
},
fail: function (err) {
console.error("支付宝支付error", err);
jumpWayError();
},
});
}
// return new Promise((resolve, reject) => {
// resolve(res);
// // reject(err);
// });
}
/**
* Compatibility entry for callers passing ApiResult.data.douyin separately.
* Native SDK invocation and authoritative result query are handled together.
*/
export function douyinPayFun(douyin, orderId, orderNum) {
return appDypayFun({ douyin, orderId, orderNum });
}
export const dyPayFun = douyinPayFun;
export const appDyPayFun = douyinPayFun;
export const appDouyinPayFun = douyinPayFun;
// 跳转到支付成功页面
export function jumpWayOk(customUrl) {
if (customUrl) {
const pages = typeof getCurrentPages === "function" ? getCurrentPages() : [];
if (pages[pages.length - 1]?.route === customUrl.replace(/^\//, "")) return Promise.resolve(true);
return new Promise((resolve) => {
uni.redirectTo({
url: customUrl,
success: () => resolve(true),
fail: () => {
uni.navigateTo({ url: customUrl, success: () => resolve(true), fail: () => resolve(false) });
},
});
});
}
return jumpToPaymentSuccess();
}
// 跳转到支付失败页面
export function jumpWayError() {
return jumpToPaymentFailure();
}
/**
* 支付结果回调
* @param {Object} orderId 支付订单ID
* @param {Object} orderNum 支付订单编号
* @param {Object} status 反馈结果【0:失败,1:成功】
*/
async function payFeedbackFun(orderId, orderNum, status) {
const params = {
orderId,
orderNum,
status,
};
console.log("params", params);
const response = await feedback(params);
console.log("payFeedbackFun response", response);
if (response && response.bizcode !== 100) {
console.error("反馈结果", response);
}
}