uniapp-app/utils/payment.js
2025-08-25 14:17:06 +08:00

99 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// utils/payment.js
// 统一的支付方法
export function requestPayment(orderInfo, successCallback, failCallback) {
// orderInfo 一般由后端返回,包括平台所需的支付参数
// 比如 { "timeStamp": "", "nonceStr": "", "package": "", "signType": "RSA", "paySign": "" } (微信)
// 或者 { "orderInfo": "xxx" } (支付宝)
// #ifdef MP-WEIXIN
console.log("MP-WEIXIN == MP-WEIXIN == MP-WEIXIN == MP-WEIXIN")
// 微信小程序支付
uni.requestPayment({
provider: 'wxpay',
timeStamp: orderInfo.timeStamp,
nonceStr: orderInfo.nonceStr,
package: 'prepay_id='+orderInfo.prepayId,
signType: orderInfo.signType || 'RSA',
paySign: orderInfo.paySign,
success: (res) => {
console.log('微信支付成功', res);
successCallback && successCallback(res);
},
fail: (err) => {
console.error('微信支付失败', err);
failCallback && failCallback(err);
}
});
// #endif
// #ifdef MP-ALIPAY
console.log("MP-ALIPAY == MP-ALIPAY == MP-ALIPAY == MP-ALIPAY")
// 支付宝小程序支付
uni.requestPayment({
provider: 'alipay',
orderInfo: orderInfo.orderInfo, // 需要的是支付宝的 orderString
success: (res) => {
console.log('支付宝支付成功', res);
successCallback && successCallback(res);
},
fail: (err) => {
console.error('支付宝支付失败', err);
failCallback && failCallback(err);
}
});
// #endif
// #ifdef APP-PLUS
console.log("APP-PLUS == APP-PLUS==APP-PLUS== APP-PLUS")
// App 端支付(支持微信/支付宝/ApplePay等
// 通常也是调用 uni.requestPayment通过 provider 区分
const platform = uni.getSystemInfoSync().platform; // iOS or Android
let provider = '';
if (platform === 'ios') {
// 可以根据业务需求选择微信或 Apple Pay
provider = 'wxpay'; // 或者 'wxpay',但 App 微信支付一般用 'wxpay'
} else {
// Android 通常用支付宝或微信
// 你可以根据后端返回的 provider 决定,比如 orderInfo.provider
provider = orderInfo.provider || 'wxpay'; // 或 alipay
}
uni.requestPayment({
provider: provider, // 可选值wxpay、alipay、appleiap 等
orderInfo: orderInfo.orderInfo, // 支付宝传 orderInfo微信可能传其它参数
timeStamp: orderInfo.timestamp,
nonceStr: orderInfo.noncestr,
package: orderInfo.package_str,
signType: orderInfo.signType || 'RSA',
paySign: orderInfo.sign,
success: (res) => {
console.log(`${provider} 支付成功`, res);
successCallback && successCallback(res);
},
fail: (err) => {
console.error(`${provider} 支付失败`, err);
failCallback && failCallback(err);
}
});
// #endif
// #ifdef H5
console.log("H5 == H5==H5==H5==H5")
// H5 支付:通常跳转到第三方支付页面(如微信扫码、支付宝跳转)
// 由于 H5 无法直接调起微信支付(除非在微信内置浏览器),所以一般后端会返回支付链接,前端跳转
if (orderInfo.payUrl) {
// 比如支付宝的支付链接,或者微信的二维码链接
window.location.href = orderInfo.payUrl;
} else {
uni.showToast({
title: 'H5暂不支持直接支付请使用其他端',
icon: 'none'
});
}
// #endif
// 其它平台(如 QuickApp、钉钉等可根据需要扩展
}