124 lines
3.0 KiB
JavaScript
124 lines
3.0 KiB
JavaScript
const ALIPAY_PENDING_REFRESH_KEY = 'pending_alipay_payment_refresh';
|
|
|
|
export const PAYMENT_REFRESH_TARGETS = {
|
|
PACKAGE_ORDER: 'package-order',
|
|
ORDER_LIST: 'order-list',
|
|
MY_WALLET: 'my-wallet'
|
|
};
|
|
|
|
export function isValidWechatPayConfig(payConfig) {
|
|
return !!payConfig &&
|
|
typeof payConfig.package === 'string' &&
|
|
payConfig.package.startsWith('prepay_id=');
|
|
}
|
|
|
|
export function getAlipayPaymentUrl(paymentLink) {
|
|
if (!paymentLink) return '';
|
|
return paymentLink.copy_link || paymentLink.qr_link || '';
|
|
}
|
|
|
|
export function isValidAlipayPaymentLink(paymentLink) {
|
|
return !!getAlipayPaymentUrl(paymentLink);
|
|
}
|
|
|
|
export function markPendingPaymentRefresh(target) {
|
|
if (!target) return;
|
|
uni.setStorageSync(ALIPAY_PENDING_REFRESH_KEY, {
|
|
target,
|
|
timestamp: Date.now()
|
|
});
|
|
}
|
|
|
|
export function consumePendingPaymentRefresh(target) {
|
|
const pending = uni.getStorageSync(ALIPAY_PENDING_REFRESH_KEY);
|
|
if (!pending || pending.target !== target) {
|
|
return false;
|
|
}
|
|
|
|
uni.removeStorageSync(ALIPAY_PENDING_REFRESH_KEY);
|
|
return true;
|
|
}
|
|
|
|
export function openAlipayPayment(paymentLink, refreshTarget) {
|
|
return new Promise((resolve, reject) => {
|
|
const paymentUrl = getAlipayPaymentUrl(paymentLink);
|
|
|
|
if (!paymentUrl) {
|
|
reject(new Error('Invalid payment link'));
|
|
return;
|
|
}
|
|
|
|
markPendingPaymentRefresh(refreshTarget);
|
|
|
|
if (typeof window !== 'undefined' && window.location) {
|
|
window.location.href = paymentUrl;
|
|
resolve({ redirecting: true });
|
|
return;
|
|
}
|
|
|
|
uni.setClipboardData({
|
|
data: paymentUrl,
|
|
success: () => resolve({ copied: true }),
|
|
fail: () => reject(new Error('Unable to open payment link in this environment'))
|
|
});
|
|
});
|
|
}
|
|
|
|
export function wechatH5Pay(payConfig) {
|
|
return new Promise((resolve, reject) => {
|
|
if (!isValidWechatPayConfig(payConfig)) {
|
|
reject(new Error('Invalid payment params'));
|
|
return;
|
|
}
|
|
|
|
if (typeof WeixinJSBridge === 'undefined') {
|
|
reject(new Error('Please open in WeChat'));
|
|
return;
|
|
}
|
|
|
|
WeixinJSBridge.invoke('getBrandWCPayRequest', {
|
|
appId: payConfig.app_id,
|
|
timeStamp: payConfig.timestamp,
|
|
nonceStr: payConfig.nonce_str,
|
|
package: payConfig.package,
|
|
signType: payConfig.sign_type,
|
|
paySign: payConfig.pay_sign
|
|
}, function(res) {
|
|
if (res.err_msg === 'get_brand_wcpay_request:ok') {
|
|
resolve({
|
|
success: true,
|
|
message: 'Payment successful'
|
|
});
|
|
} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
|
|
reject({
|
|
cancelled: true,
|
|
message: 'Payment cancelled'
|
|
});
|
|
} else {
|
|
reject({
|
|
success: false,
|
|
message: 'Payment failed',
|
|
error: res
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export function showPaymentToast(success, message) {
|
|
uni.showToast({
|
|
title: message || (success ? 'Payment successful' : 'Payment failed'),
|
|
icon: success ? 'success' : 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
|
|
export function handlePaymentError(error) {
|
|
if (error.cancelled) {
|
|
showPaymentToast(false, 'Payment cancelled');
|
|
return;
|
|
}
|
|
|
|
showPaymentToast(false, error.message || 'Payment failed');
|
|
}
|