更新记录
1.0.4(2025-08-12)
- acknowledge改为传json字符串
1.0.3(2025-08-12)
- 支付结果添加purchaseToken
1.0.2(2025-08-12)
- 修复query结果缓存
平台兼容性
uni-app(4.63)
Vue2 | Vue2插件版本 | Vue3 | Vue2插件版本 | Chrome | Safari | app-vue | app-nvue | Android | Android插件版本 | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|---|---|---|
√ | 1.0.0 | √ | 1.0.0 | × | × | × | × | 5.0 | 1.0.0 | × | × |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × | × | × |
uni-app x(4.63)
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | - | - | - | - |
sn-uts-googlepay
Google Play 内购 UTS 插件,支持一次性商品和订阅商品的购买、查询、确认等操作。
原生插件的uts版本
环境兼容性
uni-app | uni-app x |
---|---|
√ | √ |
功能特性
- ✅ 支持一次性商品 (INAPP) 和订阅商品 (SUBS)
- ✅ 自动重连和连接状态管理
- ✅ 商品查询和购买流程
- ✅ 交易确认和消耗
- ✅ 订阅更新和替换
- ✅ 外部优惠支持
- ✅ 应用内消息显示
- ✅ 完整的错误处理和回调机制
安装配置
1. 安装插件
插件市场导入插件
2. 配置 Android 权限
插件已经包含,不用处理
3. 配置 Google Play Console
- 在 Google Play Console 中创建应用
- 配置内购商品和订阅
- 上传 APK 并发布到测试轨道
使用说明
1. 初始化客户端
import { getGooglePayClient } from '@/uni_modules/sn-uts-googlepay'
const googlePayClient = getGooglePayClient()
// 监听状态变化
googlePayClient.onClientStateChange((result) => {
console.log('状态变化:', result.code, result.data.msg)
})
// 初始化配置
googlePayClient.initClient({
autoReconnect: true, // 是否自动重连
enableAutoServiceReconnection: true, // 启用自动服务重连 (Android 8.0+)
enableExternalOffer: false, // 是否启用外部优惠
enableAlternativeBillingOnly: false // 是否仅使用备选结算
})
2. 查询商品信息
// 查询一次性商品
googlePayClient.queryInappSku(['product_id_1', 'product_id_2'], (result) => {
if (result.code === 0) {
console.log('商品信息:', result.data.list)
} else {
console.error('查询失败:', result.data.msg)
}
})
// 查询订阅商品
googlePayClient.querySubsSku(['subscription_id_1'], (result) => {
if (result.code === 0) {
console.log('订阅信息:', result.data.list)
}
})
3. 发起购买
googlePayClient.pay({
productId: 'product_id_1',
offerToken: 'offer_token', // 可选:优惠令牌
accountId: 'user_account_id', // 可选:用户账户ID
profileId: 'user_profile_id', // 可选:用户配置文件ID
oldPurchaseToken: 'old_token', // 可选:旧购买令牌(订阅更新)
replacementMode: 1, // 可选:替换模式
prorationMode: 1, // 可选:按比例计费模式
externalTransactionId: 'ext_id' // 可选:外部交易ID
}, (result) => {
if (result.code === 0) {
console.log('购买成功:', result.data)
} else {
console.error('购买失败:', result.code, result.data.msg)
}
})
4. 确认交易
// 消耗型商品确认
googlePayClient.consume({
purchaseToken: purchaseData.purchaseToken
}, (result) => {
if (result.code === 0) {
console.log('消耗成功')
}
})
// 非消耗型商品或订阅确认
googlePayClient.acknowledge({
originalJson: purchaseData.originalJson, // 购买原始数据,json字符串,取自pay返回的originalJson
signature: purchaseData.signature // 购买签名,取自pay返回的signature
}, (result) => {
if (result.code === 0) {
console.log('确认成功')
}
})
5. 查询购买记录
// 查询一次性商品购买记录
googlePayClient.queryPurchases('inapp', (result) => {
if (result.code === 0) {
console.log('购买记录:', result.data.purchasesList)
}
})
// 查询订阅购买记录
googlePayClient.queryPurchases('subs', (result) => {
if (result.code === 0) {
console.log('订阅记录:', result.data.purchasesList)
}
})
6. 显示应用内消息
googlePayClient.showInAppMessages({
categoryId: 2 // 消息类别ID,int UNKNOWN_IN_APP_MESSAGE_CATEGORY_ID = 0; int TRANSACTIONAL = 2;
}, (result) => {
if (result.code === 0) {
console.log('消息显示成功')
}
})
7. 连接状态管理
// 获取连接状态: int DISCONNECTED = 0; int CONNECTING = 1; int CONNECTED = 2; int CLOSED = 3;
const state = googlePayClient.getConnectionState()
console.log('连接状态:', state)
// 检查功能支持
const supported = googlePayClient.isFeatureSupported()
console.log('功能支持:', supported)
// 手动重连
googlePayClient.reconnect()
// 结束连接
googlePayClient.endConnection()
完整示例
<template>
<view class="container">
<button @click="initGooglePay">初始化</button>
<button @click="queryProducts">查询商品</button>
<button @click="purchaseProduct">购买商品</button>
<button @click="queryPurchases">查询购买记录</button>
</view>
</template>
<script>
import { getGooglePayClient } from '@/uni_modules/sn-uts-googlepay'
export default {
data() {
return {
googlePayClient: null,
products: []
}
},
onLoad() {
this.googlePayClient = getGooglePayClient()
},
methods: {
// 初始化
initGooglePay() {
this.googlePayClient.initClient({
autoReconnect: true,
enableAutoServiceReconnection: true
})
},
// 查询商品
queryProducts() {
this.googlePayClient.queryInappSku(['test_product'], (result) => {
if (result.code === 0) {
this.products = result.data.list
console.log('商品信息:', this.products)
}
})
},
// 购买商品
purchaseProduct() {
if (this.products.length === 0) {
uni.showToast({ title: '请先查询商品', icon: 'none' })
return
}
this.googlePayClient.pay({
productId: this.products[0].productId
}, (result) => {
if (result.code === 0) {
uni.showToast({ title: '购买成功', icon: 'success' })
} else {
uni.showToast({ title: '购买失败', icon: 'none' })
}
})
},
// 查询购买记录
queryPurchases() {
this.googlePayClient.queryPurchases('inapp', (result) => {
if (result.code === 0) {
console.log('购买记录:', result.data.purchasesList)
}
})
}
}
}
</script>
注意事项
1. 开发环境要求
- Android SDK 版本:API 21+ (Android 5.0+)
- Google Play Services 版本:最新版本
- 测试设备必须安装 Google Play Store
- 开发账号需要加入 Google Play 开发者计划
2. 测试注意事项
- 测试账号:使用 Google Play Console 中添加的测试账号
- 测试轨道:将应用发布到内部测试或封闭测试轨道
- 商品配置:确保商品状态为"活跃"且价格已设置
- 签名验证:使用正确的签名密钥进行测试
3. 生产环境要求
- 应用必须通过 Google Play 审核并发布
- 商品必须通过 Google Play 审核
- 遵守 Google Play 开发者政策
- 实现适当的服务器端验证
4. 常见问题
Q: 购买回调没有触发?
A: 检查是否正确设置了 PurchasesUpdatedListener
,确保在 initClient
后调用
Q: 查询商品返回空列表? A: 检查商品ID是否正确,商品是否在 Google Play Console 中配置为活跃状态
Q: 购买失败提示"商品未找到"?
A: 确保在购买前已经调用了 querySku
方法获取商品详情
Q: 连接状态总是断开? A: 检查设备网络连接,确保 Google Play Services 正常运行
5. 安全建议
- 始终在服务器端验证购买
- 不要在前端存储敏感的用户信息
- 实现适当的错误处理和重试机制
- 定期检查 Google Play 开发者政策更新
6. 性能优化
- 避免频繁调用
querySku
- 合理使用自动重连功能
- 及时释放不需要的回调引用
- 在适当的生命周期中管理连接状态