From 676e1176dee28067560ab8009d9dad19baaf37a6 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:19:57 +0800 Subject: [PATCH] add google in-app purchase --- src/JCWallet.d.ts | 19 +++------ src/api/PayApi.ts | 15 ++++--- src/config/constants.ts | 14 ++++--- src/services/NativeSvr.ts | 24 ++++++++++-- src/services/PaySvr.ts | 82 ++++++++++++++++++++++++++------------- 5 files changed, 98 insertions(+), 56 deletions(-) diff --git a/src/JCWallet.d.ts b/src/JCWallet.d.ts index 94bbdf5..004acdd 100644 --- a/src/JCWallet.d.ts +++ b/src/JCWallet.d.ts @@ -31,20 +31,11 @@ declare namespace jsb { export function hashSvrPass(pass: string): string; export function walletEncrypt(str: string): string; export function walletDecrypt(str: string): string; - export function walletSecKey( - id: string, - openid: string, - key_master: string, - salt: string, - pass: string - ): string; - export function prepareWallet( - id: string, - openid: string, - key_master: string, - salt: string, - pass: string - ): string; + export function beginBuy(id: number, productId: string, orderId: string): string; + export function queryPurchase(id: number): string; + export function queryProducts(id: number, str: string): string; + export function walletSecKey(id: string, openid: string, key_master: string, salt: string, pass: string): string; + export function prepareWallet(id: string, openid: string, key_master: string, salt: string, pass: string): string; } declare let window: Window; diff --git a/src/api/PayApi.ts b/src/api/PayApi.ts index 5b31ff6..104de18 100644 --- a/src/api/PayApi.ts +++ b/src/api/PayApi.ts @@ -1,17 +1,22 @@ -import { WALLET_API_HOST } from "../config/constants"; -import { GET_JSON, POST_JSON } from "../lib/Http"; +import { PAY_API_HOST } from '../config/constants'; +import { GET_JSON, POST_JSON } from '../lib/Http'; export function reqAlchemyPrePay(data: any) { - const url = `${WALLET_API_HOST}/pay/alchemy/buy`; + const url = `${PAY_API_HOST}/pay/alchemy/buy`; return POST_JSON(url, data); } export function queryTokenUsdPrice(eth: string, chain: string, env: string) { - const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`; + const url = `${PAY_API_HOST}/pay/alchemy/crypto_price`; return POST_JSON(url, { token: eth, chain, env }); } export function queryFiatList() { - const url = `${WALLET_API_HOST}/pay/alchemy/fait_list`; + const url = `${PAY_API_HOST}/pay/alchemy/fait_list`; return GET_JSON(url); } + +export function verifyGooglePay(data: any) { + const url = `${PAY_API_HOST}/pay/google/verify`; + return POST_JSON(url, data); +} diff --git a/src/config/constants.ts b/src/config/constants.ts index 66a39d1..eb5c4d4 100644 --- a/src/config/constants.ts +++ b/src/config/constants.ts @@ -1,8 +1,10 @@ -export const WALLET_STORAGE_KEY_NAME = "jc_wallet_data"; +export const WALLET_STORAGE_KEY_NAME = 'jc_wallet_data'; // export const WALLET_API_HOST = "https://wallet.cebggame.com"; -export const WALLET_API_HOST = "https://pay.cebggame.com"; -// export const WALLET_API_HOST = "http://192.168.100.185:3007"; +// export const WALLET_API_HOST = 'http://192.168.100.183:3007'; +// export const PAY_API_HOST = 'http://192.168.100.183:3007'; +export const WALLET_API_HOST = 'https://pay.cebggame.com'; +export const PAY_API_HOST = 'https://pay.cebggame.com/v1'; export const MAX_TRY_COUNT = 6; @@ -10,12 +12,12 @@ export const MAX_UPLOAD_COUNT = 10; export const TX_CONFIRM_BLOCKS = 6; -export const NATIVE_PK_PREFIX = "0x000000000000000000000000"; +export const NATIVE_PK_PREFIX = '0x000000000000000000000000'; -export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; +export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; export const AVAILABLE_CHAINS = [80001, 421613, 137, 42161]; -export const BASE_TOKEN_URI = "https://market.cebg.games/api/nft/info/"; +export const BASE_TOKEN_URI = 'https://market.cebg.games/api/nft/info/'; export const GAS_BOOST = 1.2; diff --git a/src/services/NativeSvr.ts b/src/services/NativeSvr.ts index 1e44ad2..469a1e2 100644 --- a/src/services/NativeSvr.ts +++ b/src/services/NativeSvr.ts @@ -1,6 +1,6 @@ -import { payloadId } from "@walletconnect/utils"; -import { createWalletEvents } from "../common/WalletEvent"; -import { singleton } from "../decorator/singleton.decorator"; +import { payloadId } from '@walletconnect/utils'; +import { createWalletEvents } from '../common/WalletEvent'; +import { singleton } from '../decorator/singleton.decorator'; @singleton export class NativeSvr { @@ -75,4 +75,22 @@ export class NativeSvr { jsb.scanQRCode(id, title); return this._subscribeToCallResponse(id); } + + public buyProduct(productId: string, orderId: string) { + let id = payloadId(); + jsb.beginBuy(id, productId, orderId); + return this._subscribeToCallResponse(id); + } + + public queryPurchase() { + let id = payloadId(); + jsb.queryPurchase(id); + return this._subscribeToCallResponse(id); + } + + public queryProducts(productIds: string) { + let id = payloadId(); + jsb.queryProducts(id, productIds); + return this._subscribeToCallResponse(id); + } } diff --git a/src/services/PaySvr.ts b/src/services/PaySvr.ts index 2c3c10a..acb57d4 100644 --- a/src/services/PaySvr.ts +++ b/src/services/PaySvr.ts @@ -1,10 +1,8 @@ -import { - queryFiatList, - queryTokenUsdPrice, - reqAlchemyPrePay, -} from "../api/PayApi"; -import { singleton } from "../decorator/singleton.decorator"; -import { IPayData } from "../types/data.types"; +import { queryFiatList, queryTokenUsdPrice, reqAlchemyPrePay, verifyGooglePay } from '../api/PayApi'; +import { ZError } from '../common/ZError'; +import { singleton } from '../decorator/singleton.decorator'; +import { IPayData } from '../types/data.types'; +import { NativeSvr } from './NativeSvr'; @singleton export class PaySvr { @@ -24,13 +22,13 @@ export class PaySvr { throw new Error(res.errmsg); } if (!res.data) { - throw new Error("No data returned"); + throw new Error('No data returned'); } const url = res.data.url; if (!url) { - throw new Error("No url returned"); + throw new Error('No url returned'); } - console.log("pay url::", url); + console.log('pay url::', url); // jsb.showWebPage(url); // jsb.openURL(url); return res.data; @@ -40,34 +38,26 @@ export class PaySvr { const key = `gasprice_${chainId}`; if (!this.priceMap.has(key)) { let price = await new jc.wallet.web3.eth.getGasPrice(); - this.priceMap.set(key, price + ""); + this.priceMap.set(key, price + ''); } else { setImmediate(async () => { let price = await new jc.wallet.web3.eth.getGasPrice(); - this.priceMap.set(key, price + ""); + this.priceMap.set(key, price + ''); }); } - console.log("gas price", this.priceMap.get(key)); + console.log('gas price', this.priceMap.get(key)); return this.priceMap.get(key); } - private async updateTokenPrice( - chain: string, - tokenName: string, - env: string - ) { + private async updateTokenPrice(chain: string, tokenName: string, env: string) { const key = `crypto_usd_${chain}_${tokenName}_${env}`; let priceData = await queryTokenUsdPrice(tokenName, chain, env); - console.log("ETH price data", JSON.stringify(priceData)); + console.log('ETH price data', JSON.stringify(priceData)); let price = priceData.data.price; - this.priceMap.set(key, price + ""); + this.priceMap.set(key, price + ''); } - public async queryTokenPrice( - chain: string, - tokenName: string, - env: string = "release" - ) { + public async queryTokenPrice(chain: string, tokenName: string, env: string = 'release') { chain = chain.toUpperCase(); tokenName = tokenName.toUpperCase(); const key = `crypto_usd_${chain}_${tokenName}_${env}`; @@ -75,22 +65,58 @@ export class PaySvr { try { await this.updateTokenPrice(chain, tokenName, env); } catch (err) { - console.log("ETH price err", err); + console.log('ETH price err', err); } } else { setImmediate(async () => { try { await this.updateTokenPrice(chain, tokenName, env); } catch (err) { - console.log("ETH price err", err); + console.log('ETH price err', err); } }); } - console.log("ETH price", this.priceMap.get(key)); + console.log('ETH price', this.priceMap.get(key)); return this.priceMap.get(key); } public async fetchFiatList() { return queryFiatList(); } + + // begin of google pay + public async queryGoogleProducts(productIds: string[]) { + let str = await new NativeSvr().queryProducts(productIds.join(',')); + return str; + } + + public async queryGooglePurchases() { + let result = await new NativeSvr().queryPurchase(); + let data = JSON.parse(result + ''); + console.log('query purchase result:: ' + data); + if (data.length === 0) { + throw new ZError(10, 'no records'); + } + let res = await verifyGooglePay({ list: data }); + if (res.errcode) { + throw new ZError(res.errcode, res.errmsg); + } + return res.data; + } + + public async buyGoogleProduct(productId: string, orderId: string) { + let result = await new NativeSvr().buyProduct(productId, orderId); + let data = JSON.parse(result + ''); + console.log('native buy result:: ' + data); + if (data.length === 0) { + throw new ZError(10, 'no records'); + } + let res = await verifyGooglePay({ list: data }); + if (res.errcode) { + throw new ZError(res.errcode, res.errmsg); + } + return res.data; + } + + // end of google pay }