1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
| const googlePay = uni.requireNativePlugin('GT-GooglePay-V5__PayModule');
export default { data() { return { prdList: [], payList: [], isGoogleConnect: false, isPaying: false, google_sku_id: 'app_appname_companyname_coin10000' } }, onUnload() { uni.setStorageSync('pli', []) uni.hideLoading() }, methods: { doInit() { googlePay.doInit({}, e => { if (e.code == 200) { console.log('连接谷歌成功'); this.isGoogleConnect = true this.doQueryPurchases((res) => { this.doQuerySku() }) } else { console.log('连接谷歌失败'); uni.hideLoading() that.showToast('连接谷歌失败'); this.isPaying = false } }); }, doQueryPurchases(callback) { let that = this; uni.setStorageSync('pli', []) googlePay.doQueryPurchases( 'inapp', e => { if (e.code == 200) { if (e.purchasesList == null || e.purchasesList.length == 0) { callback() } else { that.payList = e.purchasesList that.verifyOrder(callback) } } else { console.log('google queryPurchases失败'); } } ); }, verifyOrder(callback) { const promises = [] this.payList.map((item, index) => { promises.push(this.verifyOrderRequest(JSON.parse(item.originalJson))) }) Promise.allSettled(promises).then(res => { if (promises.length === res.length) { callback && callback(res) } }) }, verifyOrderRequest(data) { return new Promise((resolve, reject) => { const storageList = uni.getStorageSync('pli') || []; const curToken = data.purchaseToken if (storageList.includes(curToken)) { uni.hideLoading() return false } uni.setStorageSync('pli', [...storageList, curToken]) uni.request({ url: '/api/paygoogle', data, method: 'POST', success: res => { if (res.data.code === 1) { resolve(data) } else { const newStorageList = uni.getStorageSync('pli') || [] const index = newStorageList.findIndex(item => item === curToken) if (index) { newStorageList.splice(index, 1) uni.setStorageSync('pli', newStorageList) } if (res.data.code === 0) { reject(res.data.msg) } else { reject('服务器验证出错,请联系管理员') } } }, fail: (data, code) => { uni.showToast({ icon: "none", title: '服务器验证出错,请联系管理员' }) } }); }) },
doQuerySku() { let that = this; googlePay.doQuerySku({ inapp: [this.google_sku_id], }, e => { if (e.code == 200) { that.prdList = e.list; that.createOrder() } else { uni.hideLoading() that.showToast('未查询到应用内商品'); console.log('未查询到应用内商品'); this.isPaying = false } } ); }, createOrder() { uni.request({ url: '/api/payGoogle/sumbitOrder', data: { google_sku_id: this.google_sku_id, }, method: 'POST', success: res => { uni.hideLoading() if (res.data.code === 1) { const orderSn = res.data.data.order_id this.doPayAll(orderSn) } else { uni.showToast({ icon: "none", title: '创建订单失败' }) this.isPaying = false } }, fail: (data, code) => { uni.hideLoading() this.isPaying = false uni.showToast({ icon: "none", title: '网络出错' }) } }); },
doPayAll(orderSn) { const that = this if (!orderSn) { that.showToast('未创建订单,请重试'); return } console.log('do pay function.'); if (that.prdList == null || that.prdList.length == 0) { that.showToast('产品列表为空,不可支付'); return; } googlePay.doPayAll({ productId: that.prdList[0].productId, offerToken: '', accountId: orderSn, profileId: "", }, e => { if (e.code == 200) { if (e && e.data) { that.payList = e.data; that.verifyOrder((res) => { const hasCurrentSuccessProList = res.find(item => item.status === 'fulfilled' && item.value.productId === that.curSkuSelected) if (hasCurrentSuccessProList) { uni.navigateBack() setTimeout(() => { that.showToast('购买成功') }, 2000) } else { const fail = res.find(item => item.status === 'rejected') fail && that.showToast(fail.reason) } this.isPaying = false }) } } else { if (e.errorCode === 7) { that.doQueryPurchases() } else if (e.errorCode !== 1) { console.log('支付失败: ', e); that.showToast("支付失败: " + e.data); } this.isPaying = false } }); }, } }
|