uniapp-app苹果支付

在做苹果支付之前首先要有苹果开发者账号,在测试的时候需要在开发者中心设置沙盒账号,app未上架之前正式环境测试有问题,一般沙盒环境测试正常,就没有问题。
苹果支付需要苹果产品id,在苹果开发者官网设置

页面样式根据项目原型需求自己定义就好,这里就不做展示了

1.首先根据plus.payment.getChannels()验证是否有苹果支付功能,同时获取支付通道当中的产品列表,拿到里面平台申请拿到的内购产品i的id,后续的支付需要苹果产品id才能走下去。

created() {
	this.iphonepay()
},
methods: {
	// 验证是否有苹果支付功能
	iphonepay() {
		let that = this
		plus.payment.getChannels((res) => {
			let channel = res.find(i => i.id === 'appleiap')
			that.iapChannel = channel ? channel : null
			that.requestOrder()
		})
	},
	//获取支付通道 使用所有的产品列表去检测 that.productIds 这个自己定义获取
	requestOrder() {
		let that = this
		// this.productIds 是平台申请拿到的内购商品的id 
		that.iapChannel.requestOrder(that.productIds, function(event) {
			console.log(event)
			if(!that.list[0].productid){
				event.forEach((v,i)=>{
					that.list.map((x,y)=>{
						if(v.price==x.price){
							return x.productid = v.productid
						}
					})
				})
			}
		}, function(erroemsg) {
			uni.hideLoading()
			uni.showToast({
				title: "获取支付通道失败:" + errormsg.message,
				icon: 'none'
			})
		})
	},
},

2.第二部就和普通的支付差不多了,都是根据后端的提供接口,来获取订单ID,但是不同的是这里需要上传商品id是我们之前从支付通道当中取到的苹果商品id

Recharge() {
	uni.showLoading({
		title: '支付加载中',
		mask: true
	});
	if (this.value == "appleiap") {
		// 生成订单接口
		this.$req(api.Recharge, {
			payType: 6,//支付类型 后端定义
			salePointTypeId: this.list[this.tabIndex].salePointTypeId //苹果商品id
		}, 'POST', ).then(res => {
			if (res.code == 200) {
				this.orderid = res.data //订单id
				// 苹果支付
				this.topays(this.list[this.tabIndex].productid)
			} else {
				uni.hideLoading();
				uni.showToast({
					title: res.msg,
					duration: 1000,
					icon: 'none'
				});
			}
		})
	}
}

然后就是通过plus.payment.request()方法根据苹果产品id吊起苹果手机的支付功能,支付完成后需要调用向苹果中心验证支付成功的接口,有后端提供

//苹果支付
topays(id) {
	const that = this
	uni.showLoading({
		title: '充值中请勿离开',
		mask: true
	})
	plus.payment.request(that.iapChannel, {
		productid: id,//苹果产品id 必填
		username: '购买上架点',//支付功能名称,不必填
		optimize: true // 支付时设置 optimize: true  
	}, function(res) {
		// 支付信息
		const receipt = res.transactionReceipt
		// 后端向苹果中心验证支付成功的接口
		that.$req(api.setIapCertificate, {
			receipt,
			chooseEnv: false,
			orderNo: that.orderid, // 这里就算第二步里获取的订单ID
			totalAmount: that.list[that.tabIndex].price
		}, 'GET').then(ress => {
			uni.hideLoading()
			if (ress.code == 200) {
			    uni.showToast({
				   title: '充值成功'
				})
			} else {
				uni.showToast({
					title: ress.msg,
					duration: 1000,
					icon: 'none'
				});
			}

		})
	}, function(err) {
		console.log(err)
		uni.hideLoading()
		uni.showModal({
			content: "支付失败",
			showCancel: false
		})
	})
},

到这里苹果支付功能就算完成了,剩下就算处理支付成功后的逻辑处理,根据需求就行处理整体的流程不算复杂。整体的难点就是如果是第一次做测试环境的配置和苹果产品id的配置可能有点迷糊,最好是有个运维配合配置这些数据
uniapp中配置App微信支付,有两种方式可供选择:微信支付原生方式和uniappunipay方式(unicloud)。 1. 使用微信支付原生方式: - 首先,需要在微信开放平台注册一个应用,并获取到应用的AppID和AppSecret。 - 然后,在uniapp的manifest.json文件中配置微信支付相关的权限和参数,比如AppID和支付回调地址。 - 接下来,在uniapp的代码中使用wx.requestPayment()函数调用微信支付接口,并传入相关参数,如商品信息、订单号、支付金额等。 - 最后,根据微信支付结果进行相应的处理,比如支付成功后的跳转页面或展示支付成功的提示信息等。 2. 使用uniappunipay方式(unicloud): - 首先,需要在unicloud的控制台中进行微信支付的配置,包括AppID、AppSecret、商户号等信息。 - 接着,在uniapp的代码中使用unipay的API调用微信支付接口,传入相关参数,如商品信息、订单号、支付金额等。 - 最后,根据微信支付结果进行相应的处理,比如支付成功后的跳转页面或展示支付成功的提示信息等。 需要注意的是,对于uniapp配置微信支付,具体的实现方式还需要根据具体的项目需求和开发框架进行调整和配置。详细的配置步骤和代码示例可以参考uniappunicloud的官方文档,以及相关的开发者社区和论坛。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [uniapp微信支付方案](https://blog.csdn.net/weixin_43108539/article/details/124691959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [2023年包更原生安卓影视app苹果CMS对接,原生JAVA影视APP源码支持PC+WAP+APP](https://download.csdn.net/download/weixin_36643308/88220311)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值