From f385e3590677debbd08fb3dbc4fa0cd6b3356402 Mon Sep 17 00:00:00 2001 From: zhaofengliang Date: Thu, 18 Jul 2024 16:29:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20virtualPayment=20=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=A4=BA=E4=BE=8B=EF=BC=8C=E6=94=AF=E6=8C=81=E9=80=9A?= =?UTF-8?q?=E8=BF=87virtualPaymentContext=E5=AF=B9=E8=B1=A1=E8=B0=83?= =?UTF-8?q?=E7=94=A8api=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../API/virtual-payment/virtual-payment.uvue | 218 +++++++++--------- 1 file changed, 114 insertions(+), 104 deletions(-) diff --git a/pages/API/virtual-payment/virtual-payment.uvue b/pages/API/virtual-payment/virtual-payment.uvue index bc76d614..3e531fe6 100644 --- a/pages/API/virtual-payment/virtual-payment.uvue +++ b/pages/API/virtual-payment/virtual-payment.uvue @@ -1,39 +1,40 @@ @@ -42,85 +43,91 @@ export default { data() { return { - productList: [] as PayItem[] + productList: [] as PayItem[], + virtualPaymentContext: Object } }, onLoad: function () { - this.productList.push({ - name: '消耗性产品:个人赞助1元,购买数量=5', - id: this.isDebug() ? "uniappx.consumable.sponsor_1" : "uniappx.consumable.sponsor1", - quantity: 5 - } as PayItem); - - this.productList.push({ - name: '消耗性产品:金牌赞助50元', - id: this.isDebug() ? "uniappx.consumable.sponsor_50" : "uniappx.consumable.sponsor50", - quantity: 1 - } as PayItem); - - this.productList.push({ - name: '非消耗性产品: 赞助特效1元', - id: this.isDebug() ? "uniappx.nonconsumable.sponsorskin_1" : "uniappx.nonconsumable.sponsorskin1" - } as PayItem); - - this.productList.push({ - name: '自动续期订阅产品:每月定期赞助1元', - id: this.isDebug() ? "uniappx.autorenewable.monthly_1" : "uniappx.autorenewable.monthly1" - } as PayItem); - - this.productList.push({ - name: '非自动续期订阅产品:月赞助1元', - id: this.isDebug() ? "uniappx.nonrenewable.monthly_1" : "uniappx.nonrenewable.monthly1" - } as PayItem); - - this.productList.push({ - name: '测试不存在的产品', - id: "uniappx.nonrenewable.none" - } as PayItem); + this.virtualPaymentContext = uni.createVirtualPaymentContext() + this.initProductList() }, - methods: { - getPackageName() : string { - const res = uni.getAppBaseInfo(); - let packageName : string = "" - - // #ifdef APP-ANDROID - packageName = res.packageName - // #endif - - // #ifdef APP-IOS - packageName = res.bundleId - // #endif - - return packageName - }, - isDebug() : boolean { - if (this.getPackageName() == 'io.dcloud.uniappx') { - return true - } - return false - }, - isProd() : boolean { - if (this.getPackageName() == 'io.dcloud.hellouniappx') { - return true - } - return false - }, - isCustom() : boolean { - if (this.isDebug() == false && this.isProd() == false) { - return true - } - return false + methods: { + initProductList() { + this.productList.push({ + name: '消耗性产品:个人赞助1元, 数量=5', + id: this.isDebug() ? "uniappx.consumable.sponsor_1" : "uniappx.consumable.sponsor1", + quantity: 5 + } as PayItem); + + this.productList.push({ + name: '消耗性产品:金牌赞助50元', + id: this.isDebug() ? "uniappx.consumable.sponsor_50" : "uniappx.consumable.sponsor50", + quantity: 1 + } as PayItem); + + this.productList.push({ + name: '非消耗性产品: 赞助特效1元', + id: this.isDebug() ? "uniappx.nonconsumable.sponsorskin_1" : "uniappx.nonconsumable.sponsorskin1" + } as PayItem); + + this.productList.push({ + name: '自动续期订阅产品:每月定期赞助1元', + id: this.isDebug() ? "uniappx.autorenewable.monthly_1" : "uniappx.autorenewable.monthly1" + } as PayItem); + + this.productList.push({ + name: '非自动续期订阅产品:月赞助1元', + id: this.isDebug() ? "uniappx.nonrenewable.monthly_1" : "uniappx.nonrenewable.monthly1" + } as PayItem); + + this.productList.push({ + name: '测试不存在的产品', + id: "uniappx.nonrenewable.none" + } as PayItem); + }, + getPackageName() : string { + const res = uni.getAppBaseInfo(); + let packageName : string = "" + + // #ifdef APP-ANDROID + packageName = res.packageName + // #endif + + // #ifdef APP-IOS + packageName = res.bundleId + // #endif + + return packageName + }, + isDebug() : boolean { + if (this.getPackageName() == 'io.dcloud.uniappx') { + return true + } + return false + }, + isProd() : boolean { + if (this.getPackageName() == 'io.dcloud.hellouniappx') { + return true + } + return false + }, + isCustom() : boolean { + if (this.isDebug() == false && this.isProd() == false) { + return true + } + return false }, requestVirtualPayment(e : PayItem) { uni.showLoading({ title: "", mask: true }); + uni.requestVirtualPayment({ apple: { productId: e.id, appAccountToken: "orderId+accountId", - quantity: e.quantity ?? 1 + quantity: e.quantity ?? 1, }, success: (res) => { uni.hideLoading() @@ -128,7 +135,7 @@ //TODO: 开发者server验证逻辑 //经过开发者server验证成功后请结束该交易 - uni.finishedTransaction({ + this.virtualPaymentContext.finishTransaction({ transaction: res.apple, success: (r) => { console.log("关单成功, 该productId= " + res.apple?.payment.productId) @@ -157,7 +164,8 @@ title: "", mask: true }); - uni.restoreCompletedTransactions({ + + this.virtualPaymentContext.restoreCompletedTransactions({ success: (res) => { uni.hideLoading() console.log("restore成功的交易个数:" + res.transactions.length) @@ -187,16 +195,18 @@ title: "", mask: true }); - uni.getUnfinishedTransactions({ + + this.virtualPaymentContext.getUnfinishedTransactions({ success: (res) => { uni.hideLoading() console.log("获取未结束的订单列表个数:" + res.transactions.length) res.transactions.forEach(transaction => { + console.log("getUnfinishedTransactions成功的交易productId= " + transaction.payment.productId) //TODO: 开发者server验证逻辑 //经过开发者server验证成功后请结束该交易 - uni.finishedTransaction({ + this.virtualPaymentContext.finishTransaction({ transaction: transaction, success: (r) => { console.log("关单成功, 该productId= " + transaction.payment.productId) -- GitLab