xiongzhu 3 lat temu
rodzic
commit
20f0fa2f8c

+ 14 - 0
src/weixin/dto/notify.dto.ts

@@ -0,0 +1,14 @@
+export class WeixinNotifyDto {
+    id: string
+    create_time?: Date
+    resource_type?: string
+    event_type?: string
+    summary?: string
+    resource?: {
+        original_type?: string
+        algorithm?: string
+        ciphertext?: string
+        associated_data?: string
+        nonce?: string
+    }
+}

+ 7 - 3
src/weixin/weixin.controller.ts

@@ -1,7 +1,11 @@
-import { Body, Controller, Get, HttpCode, Logger, Param, Post, Query } from '@nestjs/common'
+import { Body, Controller, Get, HttpCode, Post, Query, Headers } from '@nestjs/common'
 import { WeixinService } from './weixin.service'
 import { Public } from 'src/auth/public.decorator'
+import { WeixinNotifyDto } from './dto/notify.dto'
+import { ApiTags } from '@nestjs/swagger'
+import { Request } from 'express'
 
+@ApiTags('weixin')
 @Controller('/weixin')
 export class WeixinController {
     constructor(private readonly weixinService: WeixinService) {}
@@ -43,7 +47,7 @@ export class WeixinController {
     @Public()
     @Post('/notify')
     @HttpCode(200)
-    public async notify(@Body() body) {
-        Logger.log(body, 'weixin notify')
+    public async notify(@Headers() headers, @Body() body: WeixinNotifyDto) {
+        await this.weixinService.notify(headers, body)
     }
 }

+ 12 - 1
src/weixin/weixin.service.ts

@@ -22,6 +22,7 @@ import { LessThan, MoreThan, Not, Repository } from 'typeorm'
 import { addSeconds } from 'date-fns'
 import * as fs from 'node:fs'
 import { JsapiTicketCache } from './entities/jsapi-ticket-cache.entity'
+import { WeixinNotifyDto } from './dto/notify.dto'
 
 @Injectable()
 export class WeixinService {
@@ -205,5 +206,15 @@ export class WeixinService {
         }
     }
 
-    async notify(data: any) {}
+    async notify(headers, data: WeixinNotifyDto) {
+        let verifySignature: boolean = PayKit.verifySign(headers, JSON.stringify(data), this.platformPlublicKey)
+        Logger.log(verifySignature, '验证签名')
+        let decrypt = PayKit.aes256gcmDecrypt(
+            this.weixinConfiguration.mchKey,
+            data.resource.nonce,
+            data.resource.associated_data,
+            data.resource.ciphertext
+        )
+        Logger.log(decrypt, '解密数据')
+    }
 }