vending-machine/mobile/utils/payment.js
2026-04-08 20:45:41 +08:00

131 lines
3.5 KiB
JavaScript
Raw Permalink 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.

/**
* 支付封装工具
* 统一支付接口内部根据平台自动选择Google Pay或Apple Pay
*/
/**
* 获取当前设备平台
* @returns {string} 'android' | 'ios' | 'unknown'
*/
export function getPlatform() {
try {
const systemInfo = uni.getSystemInfoSync()
const platform = (systemInfo.platform || '').toLowerCase()
if (platform === 'android') return 'android'
if (platform === 'ios') return 'ios'
return 'unknown'
} catch (e) {
return 'unknown'
}
}
/**
* 根据平台选择支付渠道
* @param {string} platform - 设备平台
* @returns {string} 'google_pay' | 'apple_pay' | 'unsupported'
*/
export function resolvePaymentChannel(platform) {
if (platform === 'android') return 'google_pay'
if (platform === 'ios') return 'apple_pay'
return 'unsupported'
}
/**
* 调用Google Pay支付
* @param {object} params - 支付参数
* @returns {Promise<{success: boolean, receipt: string}>}
*/
async function callGooglePay(params) {
return new Promise((resolve, reject) => {
// 通过UniApp原生插件调用Google Pay
// #ifdef APP-PLUS
const googlePay = uni.requireNativePlugin('GooglePay')
googlePay.pay({
productId: params.productId,
price: params.price,
type: params.type
}, (res) => {
if (res.code === 0) {
resolve({ success: true, receipt: res.receipt })
} else if (res.code === -1) {
// 用户取消支付
reject(new Error('cancelled'))
} else {
reject(new Error(res.message || '支付失败'))
}
})
// #endif
})
}
/**
* 调用Apple Pay支付
* @param {object} params - 支付参数
* @returns {Promise<{success: boolean, receipt: string}>}
*/
async function callApplePay(params) {
return new Promise((resolve, reject) => {
// 通过UniApp原生插件调用Apple Pay
// #ifdef APP-PLUS
const applePay = uni.requireNativePlugin('ApplePay')
applePay.pay({
productId: params.productId,
price: params.price,
type: params.type
}, (res) => {
if (res.code === 0) {
resolve({ success: true, receipt: res.receipt })
} else if (res.code === -1) {
// 用户取消支付
reject(new Error('cancelled'))
} else {
reject(new Error(res.message || '支付失败'))
}
})
// #endif
})
}
/**
* 统一支付接口内部根据平台选择Google Pay或Apple Pay
* @param {object} params
* @param {string} params.productId - 商品ID
* @param {number} params.price - 价格
* @param {string} params.type - 'purchase' | 'subscribe'
* @returns {Promise<{success: boolean, receipt: string}>}
*/
export async function pay(params) {
const platform = getPlatform()
const channel = resolvePaymentChannel(platform)
if (channel === 'unsupported') {
uni.showToast({ title: '当前设备不支持该支付方式', icon: 'none' })
throw new Error('当前设备不支持该支付方式')
}
try {
if (channel === 'google_pay') {
return await callGooglePay(params)
} else {
return await callApplePay(params)
}
} catch (e) {
if (e.message === 'cancelled') {
// 用户取消,不做额外提示
throw e
}
uni.showToast({ title: '支付失败,请重试', icon: 'none' })
throw e
}
}
/**
* 检查支付环境是否可用
* @returns {Promise<boolean>}
*/
export async function isPaymentAvailable() {
const platform = getPlatform()
const channel = resolvePaymentChannel(platform)
return channel !== 'unsupported'
}