提交 058e1ae0 编写于 作者: zhaofengliang920817's avatar zhaofengliang920817

iOS|添加苹果内购示例页面

上级 c317a117
......@@ -970,6 +970,16 @@
"enablePullDownRefresh": false
}
},
// #ifndef WEB
{
"path": "pages/API/virtual-payment/virtual-payment",
"group": "1,14",
"style": {
"navigationBarTitleText": "virtualPayment | 苹果内购",
"enablePullDownRefresh": false
}
},
// #endif
{
"path": "pages/API/request-payment/request-payment/order-detail",
"style": {
......
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;">
<!-- #endif -->
<page-head title="苹果内购支付"></page-head>
<view style="padding-left: 20px; padding-right: 20px;">
<text>
requestVirtualPayment api 适用于消耗性类型、非消耗性类型、自动续期订阅类型、非自动续期订阅类型产品的购买。\n\n
1. 消耗性类型:该类型的产品可以设置购买数量,默认是1,最大值是10; \n
2. 非消耗性类型、自动续期订阅类型、非自动续期订阅类型: 这些型的产品购买数量设置无效,数量只能是1。
</text>
<button style="margin-top: 20px;" type="primary" v-for="(item,index) in productList" :key="index"
@click="requestVirtualPayment(item)">{{item.name}}</button>
<text>
\nrestoreCompletedTransactions api 适用于非消耗性类型、自动续期订阅类型、非自动续期订阅类型产品的购买。\n\n
1. 非消耗性类型: 返回每个已购买的非消耗性类型产品的购买记录;\n
2. 自动续期订阅类型: 返回每个已购买的自动续期订阅类型产品的最新购买记录;\n
3. 非自动续期订阅类型: 返回每个已购买的非自动续期订阅类型产品的最新购买记录。\n
Note: 不能用来恢复消耗性类型的购买记录。
</text>
<button style="margin-top: 20px;" type="primary" @click="restoreCompletedTransactions">恢复购买订单列表</button>
<text>
\ngetUnfinishedTransactions api 适用于获取未完成的各种类型产品的购买记录(用来防止丢单)。\n\n
1. 比如用户点击购买已经付款成功,但因网络、手机没电关机等特殊情况,Apple IAP 没有返回客户端对应的购买凭证,从而导致无法finish该交易导致的丢单,可以在需要的地方调用该api获得此类未finished的记录; \n
2. 针对消耗性类型产品交易,只能通过该api获取未finished的交易,防止丢单;\n
3. 对于其他类型产品未finished交易, 不仅可以通过该api获取,也可以通过restoreCompletedTransactions api(也可以获取已经finished的交易)获取;
</text>
<button style="margin-top: 20px; margin-bottom: 50px;" type="primary" @click="getUnfinishedTransactions">获取未结束的订单列表</button>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script>
export type PayItem = { id : string, name : string, quantity ?: number }
export default {
data() {
return {
productList: [] as PayItem[]
}
},
onLoad: function () {
this.productList.push({
name: '消耗性产品:个人赞助1元',
id: "io.dcloud.uniappx.consumable.sponsor1",
quantity: 5
} as PayItem);
this.productList.push({
name: '消耗性产品:金牌赞助50元',
id: "io.dcloud.uniappx.consumable.sponsor50",
quantity: 1
} as PayItem);
this.productList.push({
name: '非消耗性产品: 赞助特效1元',
id: "io.dcloud.uniappx.nonconsumable.skin"
} as PayItem);
this.productList.push({
name: '自动续期订阅产品:每月定期赞助1元',
id: "io.dcloud.uniappx.autorenewable.monthly"
} as PayItem);
this.productList.push({
name: '非自动续期订阅产品:月赞助1元',
id: "io.dcloud.uniappx.nonrenewable.week"
} as PayItem);
},
methods: {
requestVirtualPayment(e : PayItem) {
uni.showLoading({
title: "",
mask: true
});
uni.requestVirtualPayment({
apple: {
productId: e.id,
appAccountToken: "orderId+accountId",
quantity: e.quantity ?? 1
},
success: (res) => {
uni.hideLoading()
console.log("购买成功:该productId= " + res.apple?.payment.productId)
//TODO: 开发者server验证逻辑
//经过开发者server验证成功后请结束该交易
uni.finishedTransaction({
transaction: res.apple,
success: (r) => {
console.log("关单成功, 该productId= " + res.apple?.payment.productId)
},
fail: (e) => {
console.log("关单失败, 该productId= " + res.apple?.payment.productId)
}
})
uni.showToast({
title: "购买成功:" + res.apple?.payment.productId,
icon: 'error'
})
},
fail: (e) => {
uni.hideLoading()
console.log("购买失败:errSubject= " + e.errSubject + ", errCode= " + e.errCode + ", errMsg= " + e.errMsg)
uni.showToast({
title: "购买失败:errCode=" + e.errCode,
icon: 'error'
})
}
})
},
restoreCompletedTransactions() {
uni.showLoading({
title: "",
mask: true
});
uni.restoreCompletedTransactions({
success: (res) => {
uni.hideLoading()
console.log("restore成功的交易个数:" + res.transactions.length)
res.transactions.forEach(transaction => {
//TODO: 开发者server验证逻辑
console.log("restore成功的交易productId= " + transaction.payment.productId)
})
uni.showToast({
title: "restore成功的交易个数:" + res.transactions.length,
icon: 'error'
})
},
fail: (e) => {
uni.hideLoading()
console.log("restore失败:errSubject= " + e.errSubject + ", errCode= " + e.errCode + ", errMsg= " + e.errMsg)
uni.showToast({
title: "restore失败:errCode=" + e.errCode,
icon: 'error'
})
}
})
},
getUnfinishedTransactions() {
uni.showLoading({
title: "",
mask: true
});
uni.getUnfinishedTransactions({
success: (res) => {
hideLoading()
console.log("获取未结束的订单列表个数:" + res.transactions.length)
res.transactions.forEach(transaction => {
//TODO: 开发者server验证逻辑
//经过开发者server验证成功后请结束该交易
uni.finishedTransaction({
transaction: transaction,
success: (r) => {
console.log("关单成功, 该productId= " + transaction.payment.productId)
},
fail: (e) => {
console.log("关单失败, 该productId= " + transaction.payment.productId)
}
})
})
uni.showToast({
title: "获取未结束的订单列表个数:" + res.transactions.length,
icon: 'error'
})
},
fail: (e) => {
uni.hideLoading()
console.log("获取未结束的订单列表失败:errSubject= " + e.errSubject + ", errCode= " + e.errCode + ", errMsg= " + e.errMsg)
uni.showToast({
title: "获取未结束的订单列表失败:errCode= " + e.errCode,
icon: 'error'
})
}
})
}
}
}
</script>
<style>
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册