uniapp 苹果支付

苹果支付需要在苹果后台设置沙盒模拟账号,在手机上登录沙盒账号

在utils下创建js文件

//苹果支付

apple(payid, orderid , type) {
	/* 	1苹果支付码,字符串 
		2订单id后面抛回去 苹果支付不能获取支付结果,要在支付完调接口修改订单状态
		3类型,主要给自己判断在哪里支付的
	*/
	let that = this
	plus.payment.getChannels((channels) => {
		// console.log(channels);
		for (var i in channels) {
			var channel = channels[i];
			if (channel.id == 'appleiap') {
				// console.log("苹果")
				let iap = channel;
				//支付参数
				iap.requestOrder([payid], function(orderList) {
					//必须调用此方法才能进行 iap 支付 
					console.log('requestOrder success666: ' + JSON.stringify(orderList))
					that.applePay(orderList[0] , orderid , type)
				}, function(e) {
					// console.log('requestOrder failed: ' + JSON.stringify(e));
					// console.log('暂不支持苹果 iap 支付')
				});
			}
		}
	}, function(e) {
		console.log("获取iap支付通道失败:" + e.message);
	});
},
applePay(para , orderid , type) {
	setTimeout(()=>{
		uni.hideToast()
	},1000)
	console.log(para)
	uni.requestPayment({
		provider: 'appleiap',
		orderInfo: {
			productid: para.productid,
			username: orderid, // 用户标识
			optimize: true // 设置 optimize: true 解决丢单问题  
		},
		/* 
		orderInfo可传参数一览
		productid: (String 类型 )(必填) 商品的标识,你在苹果那里添加的商品标识 
		username: (String 类型 )(可选) 购买用户名称,我喜欢在这里传入订单号,这样支付成功时就可以获得订单号 
		quantity: (String 类型 )(可选) 商品数量,默认为 "1" 
		optimize:(Boolean)(可选) HX 3.1.10 版本新增参数,解决用户未绑定支付方式造成的丢单问题; 
		*/
		success: (es) => {
			console.log(es)
			if(type && type == 'wxCode'){
				//参数调接口改变订单状态
				uni.$emit('paymentSuccess', {
					TransactionID: es.transactionIdentifier,
					Payload: es.transactionReceipt,
					orderId: orderid,
					tips: '微信号'
				})
			}else if(type && type == 'vipPayment'){
				uni.$emit('paymentSuccess', {
					TransactionID: es.transactionIdentifier,
					Payload: es.transactionReceipt,
					orderId: orderid,
					tips: '置顶'
				})
			}else {
				uni.$emit('paymentSuccess', {
					TransactionID: es.transactionIdentifier,
					Payload: es.transactionReceipt,
					orderId: orderid,
				})
			}
			
		},
		fail: (err) => {
			console.log(err);
			com.msg('取消支付')
		},
		complete: () => {
			// console.log("payment结束");
		}
	})
},

在页面使用

// 1. 引入支付js文件
import payment from "@/utils/payment.js";

// 2. onLoad里 获取苹果支付回调,调接口
uni.$off('paymentSuccess').$on('paymentSuccess', function(res) {
	uni.showToast({
		icon: 'loading',
		duration: 3000
	})
	applePaycontactTeam(res).then(pay => {
		uni.hideToast()
		this.getdata() //重调渲染页面
	})
}.bind(this))

// 3. 接口内调苹果支付 很慢,加一个加载效果
uni.showToast({
	title: '正在连接App Store,请稍等',
	icon: 'loading',
	duration: 10000
})
payment.apple(this.payPrice.encoding, this.orderId)
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值