Skip to content

Commit

Permalink
Merge pull request #25 from notadd/develop
Browse files Browse the repository at this point in the history
 chore(release): publish v0.5.9
  • Loading branch information
dzzzzzy authored Jan 3, 2019
2 parents 7589e38 + 0d71b80 commit 408f2a7
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 157 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ export class TestSign {

*TODO*

### 随机字符生成工具

支付插件内使用了 [nanoid](https://github.com/ai/nanoid) 作为随机字符生成工具,开发者可以通过注入 `RandomUtil`,即可使用该工具。

```typescript
import { Injectable, Inject } from '@nestjs/common';
import { RandomUtil } from '@notadd/addon-pay';

@Injectable()
export class TestSign {
constructor(@Inject(RandomUtil) private readonly randomUtil: RandomUtil) { }

async testRandomStr() {
/**
* genRandomStr(pool?: string, length?: number) 接受两个参数:
*
* pool 随机字符池,默认:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
*
* length 随机字符长度,默认:32
*/
const randomStr = await this.randomUtil.genRandomStr();
}
}
```

## 贡献说明

我们欢迎 Nest.js 使用者来参与这个插件的开发,作为一个贡献者,请您遵循以下原则:
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nt-addon-pay",
"version": "0.5.8",
"version": "0.5.9",
"description": "The pay addon for notadd application",
"scripts": {
"start": "ts-node -r tsconfig-paths/register starter/main.ts",
Expand All @@ -15,26 +15,26 @@
"author": "notadd",
"license": "Apache-2.0",
"dependencies": {
"chance": "^1.0.16",
"nanoid": "^2.0.0",
"xml2js": "^0.4.19"
},
"devDependencies": {
"@nestjs/common": "^5.4.0",
"@nestjs/core": "^5.4.0",
"@nestjs/testing": "^5.4.0",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.3.3",
"typescript": "^3.1.3",
"@types/chance": "^1.0.1",
"@types/jest": "^23.3.9",
"@types/node": "^10.12.0",
"@nestjs/common": "^5.5.0",
"@nestjs/core": "^5.5.0",
"@nestjs/testing": "^5.5.0",
"@types/jest": "^23.3.11",
"@types/nanoid": "^1.2.0",
"@types/node": "^10.12.18",
"@types/xml2js": "^0.4.3",
"jest": "^23.6.0",
"nodemon": "^1.18.4",
"ts-jest": "^23.10.4",
"nodemon": "^1.18.9",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.3.3",
"ts-jest": "^23.10.5",
"ts-node": "^7.0.1",
"tsconfig-paths": "^3.6.0",
"tslint": "^5.11.0"
"tsconfig-paths": "^3.7.0",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"jest": {
"moduleFileExtensions": [
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './pay.addon';
export * from './modules/wechat';
export * from './modules/wechat';
export * from './shared/utils/random.util';
4 changes: 2 additions & 2 deletions src/modules/wechat/services/transfer.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import * as crypto from 'crypto';
import { publicEncrypt } from 'crypto';
import * as fs from 'fs';
import * as https from 'https';
import * as path from 'path';
Expand Down Expand Up @@ -96,7 +96,7 @@ export class WeChatTransferService {
*/
private async encryptStr(str: string) {
const pubKey = await this.getPublicKey();
return crypto.publicEncrypt(pubKey, Buffer.from(str)).toString('base64');
return publicEncrypt(pubKey, Buffer.from(str)).toString('base64');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/modules/wechat/utils/notify-parser.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import * as crypto from 'crypto';
import { createDecipheriv, createHash } from 'crypto';
import { IncomingMessage } from 'http';

import { WeChatPayConfig } from '../../../common';
Expand Down Expand Up @@ -58,9 +58,9 @@ export class WeChatNotifyParserUtil {
try {
const secretKey = this.config.secretKey;
const cryptedBase64Str = Buffer.from((result as any).req_info).toString('base64');
const secretKeyMD5 = crypto.createHash('md5').update(secretKey).digest('hex').toLocaleLowerCase();
const secretKeyMD5 = createHash('md5').update(secretKey).digest('hex').toLocaleLowerCase();

const decipher = crypto.createDecipheriv('aes-256-ecb', secretKeyMD5, '');
const decipher = createDecipheriv('aes-256-ecb', secretKeyMD5, '');
const decryptedStr = Buffer.concat([decipher.update(cryptedBase64Str, 'base64'), decipher.final()]).toString();

Object.assign(result, JSON.parse(decryptedStr));
Expand Down
2 changes: 1 addition & 1 deletion src/modules/wechat/utils/request.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class WeChatRequestUtil {
params.mch_id = this.config.mch_id;
}

params.nonce_str = this.randomUtil.genRandomStr();
params.nonce_str = await this.randomUtil.genRandomStr();

let signType: 'MD5' | 'HMAC-SHA256';
if (params.sign_type && params.sign_type === 'no_sign_type') {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/wechat/utils/sign.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import * as crypto from 'crypto';
import { createHash, createHmac } from 'crypto';

import { WeChatPayConfig } from '../../../common';
import { WeChatPayConfigProvider } from '../constants/wechat.constant';
Expand Down Expand Up @@ -29,8 +29,8 @@ export class WeChatSignUtil {
}
let signStr = paramArr.join('&');
if (signType && signType === 'HMAC-SHA256') {
return crypto.createHmac('sha256', secretKey).update(signStr).digest('hex').toUpperCase();
return createHmac('sha256', secretKey).update(signStr).digest('hex').toUpperCase();
}
return crypto.createHash('md5').update(signStr += `&key=${secretKey}`).digest('hex').toUpperCase();
return createHash('md5').update(signStr += `&key=${secretKey}`).digest('hex').toUpperCase();
}
}
2 changes: 1 addition & 1 deletion src/pay.addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PayAddon {
return {
module: PayAddon,
imports: [SharedModule, WeChatPayModule.forRoot(config.wechatConfig), AliPayModule],
exports: [WeChatPayModule, AliPayModule]
exports: [WeChatPayModule, AliPayModule, SharedModule]
};
}

Expand Down
19 changes: 16 additions & 3 deletions src/shared/utils/random.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import * as chance from 'chance';
import nanoid from 'nanoid/async/generate';

/**
* 随机数工具
Expand All @@ -8,8 +8,21 @@ import * as chance from 'chance';
export class RandomUtil {
/**
* 生成32位随机字符串
*
* @param pool 随机字符池,默认:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
*
* @param length 随机字符长度,默认:32
*/
genRandomStr() {
return chance().string({ length: 32, pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' });
async genRandomStr(pool?: string, length?: number): Promise<string> {
if (pool && !length) {
return await nanoid(pool, 32);
}
if (!pool && length) {
return await nanoid('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', length);
}
if (pool && length) {
return await nanoid(pool, length);
}
return await nanoid('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 32);
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"esModuleInterop": true,
"noImplicitAny": false,
"removeComments": false,
"sourceMap": false,
Expand Down
5 changes: 5 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"extends": [
"tslint:recommended"
],
"linterOptions": {
"exclude": [
"node_modules/**"
]
},
"jsRules": {
"no-unused-expression": true
},
Expand Down
Loading

0 comments on commit 408f2a7

Please sign in to comment.