uniapp 苹果支付内购示例代码

2024-03-28 22:52

本文主要是介绍uniapp 苹果支付内购示例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

// #ifdef APPasync init() {uni.showLoading({title: '检测支付环境...'});try {// 初始化,获取iap支付通道await this._iap.init();// 从苹果服务器获取产品列表this.productList = await this._iap.getProduct();this.productList[0].checked = true;this.productId = this.productList[0].productid;console.log('this.productId',this.productId);console.log('this.productList',this.productList);} catch (e) {uni.showModal({title: "init",content: e.message,showCancel: false});} finally {uni.hideLoading();}if (this._iap.ready) {this.restore();}},async restore() {// 检查上次用户已支付且未关闭的订单,可能出现原因:首次绑卡,网络中断等异常// 在此处检查用户是否登陆uni.showLoading({title: '正在检测已支付且未关闭的订单...'});try {// 从苹果服务器检查未关闭的订单,可选根据 username 过滤,和调用支付时透传的值一致const transactions = await this._iap.restoreCompletedTransactions({username: this.userInfo.user_name});if (!transactions.length) {return;}// 开发者业务逻辑,从服务器获取当前用户未完成的订单列表,和本地的比较// 此处省略switch (transaction.transactionState) {case IapTransactionState.purchased:// 用户已付款,在此处请求开发者服务器,在服务器端请求苹果服务器验证票据//let result = await this.validatePaymentResult();// 验证通过,交易结束,关闭订单// if (result) {//   await this._iap.finishTransaction(transaction);// }break;case IapTransactionState.failed:// 关闭未支付的订单await this._iap.finishTransaction(transaction);break;default:break;}} catch (e) {uni.showModal({content: e.message,showCancel: false});} finally {uni.hideLoading();}},async payment() {if (this.loading == true) {return;}this.loading = true;uni.showLoading({title: '支付处理中...'});try {// 请求苹果支付const transaction = await this._iap.requestPayment({productid: this.productId,manualFinishTransaction: true,username: this.userInfo.user_name});console.log(transaction,'transaction');const res = await this.$service.getApplePaySign({receipt: transaction.transactionReceipt,id: this.detail.data[this.activeId].id,time: this.active})console.log(res,'借口返回');uni.showToast({icon:'none',title: res.msg})//支付成功} catch (e) {console.log(e,'e');uni.showModal({content: e.message,showCancel: false});} finally {this.loading = false;uni.hideLoading();}},

使用前先创建并引入以下文件:

// uni iapconst ProviderType = {IAP: 'iap'
}const IapTransactionState = {purchasing: "0", // A transaction that is being processed by the App Store.purchased: "1", // A successfully processed transaction.failed: "2", // A failed transaction.restored: "3", // A transaction that restores content previously purchased by the user.deferred: "4" // A transaction that is in the queue, but its final status is pending external action such as Ask to Buy.
};class Iap {_channel = null;_channelError = null;_productIds = [];_ready = false;constructor({products}) {this._productIds = products;}init() {return new Promise((resolve, reject) => {this.getChannels((channel) => {this._ready = true;resolve(channel);}, (err) => {reject(err);})})}getProduct(productIds) {return new Promise((resolve, reject) => {this._channel.requestProduct(productIds || this._productIds, (res) => {resolve(res);}, (err) => {reject(err);})});}requestPayment(orderInfo) {return new Promise((resolve, reject) => {uni.requestPayment({provider: 'appleiap',orderInfo: orderInfo,success: (res) => {resolve(res);},fail: (err) => {reject(err);}});});}restoreCompletedTransactions(username) {return new Promise((resolve, reject) => {this._channel.restoreCompletedTransactions({manualFinishTransaction: true,username}, (res) => {resolve(res);}, (err) => {reject(err);})});}finishTransaction(transaction) {return new Promise((resolve, reject) => {this._channel.finishTransaction(transaction, (res) => {resolve(res);}, (err) => {reject(err);});});}getChannels(success, fail) {if (this._channel !== null) {success(this._channel)return}if (this._channelError !== null) {fail(this._channelError)return}uni.getProvider({service: 'payment',success: (res) => {this._channel = res.providers.find((channel) => {return (channel.id === 'appleiap')})if (this._channel) {success(this._channel)} else {this._channelError = {errMsg: 'paymentContext:fail iap service not found'}fail(this._channelError)}}});}get channel() {return this._channel;}
}export {Iap,IapTransactionState
}

效果图:

 需要注意的是,这里是沙盒环境,只能使用虚拟账号进行支付:

可以在下面这个页面进行添加账号:

Apple 内购申请流程: 苹果支付内购申请-CSDN博客

参考:uniapp开发对接IOS应用内支付

其他解决方案:

uni-pay:uni-pay - DCloud 插件市场

这篇关于uniapp 苹果支付内购示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/856914

相关文章

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN