add google in-app purchase

This commit is contained in:
CounterFire2023 2023-07-09 19:19:57 +08:00
parent 2e1c76976d
commit 676e1176de
5 changed files with 98 additions and 56 deletions

19
src/JCWallet.d.ts vendored
View File

@ -31,20 +31,11 @@ declare namespace jsb {
export function hashSvrPass(pass: string): string; export function hashSvrPass(pass: string): string;
export function walletEncrypt(str: string): string; export function walletEncrypt(str: string): string;
export function walletDecrypt(str: string): string; export function walletDecrypt(str: string): string;
export function walletSecKey( export function beginBuy(id: number, productId: string, orderId: string): string;
id: string, export function queryPurchase(id: number): string;
openid: string, export function queryProducts(id: number, str: string): string;
key_master: string, export function walletSecKey(id: string, openid: string, key_master: string, salt: string, pass: string): string;
salt: string, export function prepareWallet(id: string, openid: string, key_master: string, salt: string, pass: string): string;
pass: string
): string;
export function prepareWallet(
id: string,
openid: string,
key_master: string,
salt: string,
pass: string
): string;
} }
declare let window: Window; declare let window: Window;

View File

@ -1,17 +1,22 @@
import { WALLET_API_HOST } from "../config/constants"; import { PAY_API_HOST } from '../config/constants';
import { GET_JSON, POST_JSON } from "../lib/Http"; import { GET_JSON, POST_JSON } from '../lib/Http';
export function reqAlchemyPrePay(data: any) { 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); return POST_JSON(url, data);
} }
export function queryTokenUsdPrice(eth: string, chain: string, env: string) { 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 }); return POST_JSON(url, { token: eth, chain, env });
} }
export function queryFiatList() { 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); return GET_JSON(url);
} }
export function verifyGooglePay(data: any) {
const url = `${PAY_API_HOST}/pay/google/verify`;
return POST_JSON(url, data);
}

View File

@ -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://wallet.cebggame.com";
export const WALLET_API_HOST = "https://pay.cebggame.com"; // export const WALLET_API_HOST = 'http://192.168.100.183:3007';
// export const WALLET_API_HOST = "http://192.168.100.185: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; export const MAX_TRY_COUNT = 6;
@ -10,12 +12,12 @@ export const MAX_UPLOAD_COUNT = 10;
export const TX_CONFIRM_BLOCKS = 6; 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 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; export const GAS_BOOST = 1.2;

View File

@ -1,6 +1,6 @@
import { payloadId } from "@walletconnect/utils"; import { payloadId } from '@walletconnect/utils';
import { createWalletEvents } from "../common/WalletEvent"; import { createWalletEvents } from '../common/WalletEvent';
import { singleton } from "../decorator/singleton.decorator"; import { singleton } from '../decorator/singleton.decorator';
@singleton @singleton
export class NativeSvr { export class NativeSvr {
@ -75,4 +75,22 @@ export class NativeSvr {
jsb.scanQRCode(id, title); jsb.scanQRCode(id, title);
return this._subscribeToCallResponse(id); 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);
}
} }

View File

@ -1,10 +1,8 @@
import { import { queryFiatList, queryTokenUsdPrice, reqAlchemyPrePay, verifyGooglePay } from '../api/PayApi';
queryFiatList, import { ZError } from '../common/ZError';
queryTokenUsdPrice, import { singleton } from '../decorator/singleton.decorator';
reqAlchemyPrePay, import { IPayData } from '../types/data.types';
} from "../api/PayApi"; import { NativeSvr } from './NativeSvr';
import { singleton } from "../decorator/singleton.decorator";
import { IPayData } from "../types/data.types";
@singleton @singleton
export class PaySvr { export class PaySvr {
@ -24,13 +22,13 @@ export class PaySvr {
throw new Error(res.errmsg); throw new Error(res.errmsg);
} }
if (!res.data) { if (!res.data) {
throw new Error("No data returned"); throw new Error('No data returned');
} }
const url = res.data.url; const url = res.data.url;
if (!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.showWebPage(url);
// jsb.openURL(url); // jsb.openURL(url);
return res.data; return res.data;
@ -40,34 +38,26 @@ export class PaySvr {
const key = `gasprice_${chainId}`; const key = `gasprice_${chainId}`;
if (!this.priceMap.has(key)) { if (!this.priceMap.has(key)) {
let price = await new jc.wallet.web3.eth.getGasPrice(); let price = await new jc.wallet.web3.eth.getGasPrice();
this.priceMap.set(key, price + ""); this.priceMap.set(key, price + '');
} else { } else {
setImmediate(async () => { setImmediate(async () => {
let price = await new jc.wallet.web3.eth.getGasPrice(); 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); return this.priceMap.get(key);
} }
private async updateTokenPrice( private async updateTokenPrice(chain: string, tokenName: string, env: string) {
chain: string,
tokenName: string,
env: string
) {
const key = `crypto_usd_${chain}_${tokenName}_${env}`; const key = `crypto_usd_${chain}_${tokenName}_${env}`;
let priceData = await queryTokenUsdPrice(tokenName, chain, 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; let price = priceData.data.price;
this.priceMap.set(key, price + ""); this.priceMap.set(key, price + '');
} }
public async queryTokenPrice( public async queryTokenPrice(chain: string, tokenName: string, env: string = 'release') {
chain: string,
tokenName: string,
env: string = "release"
) {
chain = chain.toUpperCase(); chain = chain.toUpperCase();
tokenName = tokenName.toUpperCase(); tokenName = tokenName.toUpperCase();
const key = `crypto_usd_${chain}_${tokenName}_${env}`; const key = `crypto_usd_${chain}_${tokenName}_${env}`;
@ -75,22 +65,58 @@ export class PaySvr {
try { try {
await this.updateTokenPrice(chain, tokenName, env); await this.updateTokenPrice(chain, tokenName, env);
} catch (err) { } catch (err) {
console.log("ETH price err", err); console.log('ETH price err', err);
} }
} else { } else {
setImmediate(async () => { setImmediate(async () => {
try { try {
await this.updateTokenPrice(chain, tokenName, env); await this.updateTokenPrice(chain, tokenName, env);
} catch (err) { } 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); return this.priceMap.get(key);
} }
public async fetchFiatList() { public async fetchFiatList() {
return queryFiatList(); 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
} }