uni.getProvider({
service: 'payment',
success: (res) => {
const iapChannel = res.providers.find((channel) => {
return (channel.id === 'appleiap')
})
}
});
getProduct(productIds) {
return new Promise((resolve, reject) => {
this._channel.requestProduct(
productIds || this._productIds,
(res) => {
resolve(res);
},
(err) => {
reject(err);
}
);
});
}
async restore() {
// 检查上次用户已支付且未关闭的订单,可能出现原因:首次绑卡,网络中断等异常
let _this = this
try {
// 从苹果服务器检查未关闭的订单,可选根据 username 过滤,和调用支付时透传的值一致
const transactions = await this.restoreCompletedTransactions({});
if (!transactions.length) {
return;
}
for (const transaction of transactions) {
switch (transaction.transactionState) {
case IapTransactionState.purchased:
//这里写上传数据到服务器的代码
console.log('成功');
_this.finishTransaction(transaction);
}
break;
case IapTransactionState.failed:
// 关闭未支付的订单
_this.finishTransaction(transaction);
break;
default:
break;
}
};
} catch (e) {
uni.showModal({
content: e.message,
showCancel: false
});
} finally {
}
}
requestPayment(orderInfo) {
return new Promise((resolve, reject) => {
uni.requestPayment({
provider: 'appleiap',
orderInfo: orderInfo,
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
}
});
});
}
前端传过来transactionReceipt以及透传参数(一般是自己的订单号), 我们拿着transactionReceipt请求苹果的服务器即可,请求苹果服务器会返回该支付的一切信息,包括支付订单号,支付时间,支付数量等
