1/* 2* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16import cryptoFramework from '@ohos.security.cryptoFramework'; 17import { PayServer } from './PayServer'; 18import util from '@ohos.util'; 19import resourceManager from '@ohos.resourceManager'; 20 21interface ProductInfo { 22 id: string, 23 name: string, 24 price: string, 25 img: string, 26} 27 28enum ErrorCode { 29 PLACE_ORDER_BODY_ERROR = 400, 30 PLACE_ORDER_ERROR = 401, 31} 32 33const TAG = 'MerchantServer'; 34const PRICE = 100; 35const TEXT_ENCODER = new util.TextEncoder(); 36const BASE64 = new util.Base64Helper(); 37const PLATFORM_NAME = '测试'; 38const SIGNER_INPUT = { data: TEXT_ENCODER.encodeInto('This is a test') }; 39const MERCHANT_PRI_KEY = 'MDECAQEEILoJbGqF0n4C+5KzSc8l6UFli3PAaH2+QYbe5sAxqCquoAoGCCqGSM49AwEH'; 40const PAY_PUB_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDFl9UiVDwrIUtTSFVaapGgH7v22lBqnm7kqOjUdNCaJE+eBVhCVT2K3KykPuE4xgtKej2ry+2Hme5MQaZZx7pxNkXE6P69y3m2Vbp1KnFgsXtWYldGA/vrmbXoDhKU4HdsPC4m+9vsELevFjuNyONMMMklF/l+3RdZ0gzRgOmJwIDAQAB'; 41const TEST_PRODUCTS: ProductInfo[] = Array.from({ length: 10 }, (_, i) => ({ 42 id: `${i + PRICE}`, 43 name: '80包手帕纸卫生抽纸家用', 44 price: '¥ 3.90', 45 img: 'app.media.icon' 46})); 47 48 49export class MerchantServer { 50 private merchantKey: cryptoFramework.KeyPair; 51 private payKey: cryptoFramework.KeyPair; 52 private static instance: MerchantServer; 53 54 private constructor() { 55 } 56 57 public static getInstance(): MerchantServer { 58 if (!MerchantServer.instance) { 59 MerchantServer.instance = new MerchantServer(); 60 } 61 return MerchantServer.instance; 62 } 63 64 public async placeAnOrder(body: ProductInfo): Promise<string> { 65 if (!body.id || !body.name || !body.price) { 66 throw { 67 code: ErrorCode.PLACE_ORDER_BODY_ERROR, 68 }; 69 } 70 let isOrderRight = false; 71 for (let product of TEST_PRODUCTS) { 72 if (product.id === body.id && product.name === body.name && product.price === body.price) { 73 isOrderRight = true; 74 } 75 } 76 if (!isOrderRight) { 77 throw { 78 code: ErrorCode.PLACE_ORDER_ERROR, 79 }; 80 } 81 82 // 下单信息加密传输给支付服务器,生成支付订单 83 let temStr = `platformName=${PLATFORM_NAME}&totalPrice=${body.price}&productName=${body.name}&productId=${body.id}`; 84 if (!this.payKey) { 85 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2'); 86 this.payKey = await rsaGenerator.convertKey({ data: BASE64.decodeSync(PAY_PUB_KEY) }, null); 87 } 88 let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); 89 await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, this.payKey.pubKey, null); 90 let dataBlob = await cipher.doFinal({ data: TEXT_ENCODER.encodeInto(temStr) }); 91 let str: string = BASE64.encodeToStringSync(dataBlob.data); 92 let order = await PayServer.getInstance().generatePayOrder(str); 93 AppStorage.setOrCreate("PayServer", PayServer.getInstance()); 94 if (!this.merchantKey) { 95 let eccGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 96 this.merchantKey = await eccGenerator.convertKey(null, { data: BASE64.decodeSync(MERCHANT_PRI_KEY) }); 97 } 98 let signer = cryptoFramework.createSign('ECC256|SHA256'); 99 await signer.init(this.merchantKey.priKey); 100 let signMessageBlob = await signer.sign(SIGNER_INPUT); 101 let result: string = order + '&signer=' + BASE64.encodeToStringSync(signMessageBlob.data); 102 return result; 103 } 104 105 public getProducts(): ProductInfo[] { 106 return TEST_PRODUCTS; 107 } 108} 109