|
@@ -10,6 +10,7 @@ import {
|
|
|
Provide,
|
|
|
Put,
|
|
|
Query,
|
|
|
+ Controller
|
|
|
} from '@midwayjs/decorator';
|
|
|
import { BaseController, CoolController } from '@cool-midway/core';
|
|
|
import { Context } from 'vm';
|
|
@@ -17,6 +18,7 @@ import { IndividualEntity } from '../../payment/entity/individual';
|
|
|
import { NoahPayAdapter } from '../../payment/adapter/noah.adapter';
|
|
|
import { NoahOpenService } from '../service/open_v4';
|
|
|
import { OrderType } from '../entity/open_payment_order';
|
|
|
+import * as crypto from 'crypto';
|
|
|
|
|
|
/**
|
|
|
* 客户管理
|
|
@@ -163,6 +165,18 @@ export class OpenApiControllerV4 extends BaseController {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 模拟用户触发法币转数字货币自动划转
|
|
|
+ */
|
|
|
+ @Post('/v1/sandbox/fiat-deposit/simulate', { summary: '申请收款账户' })
|
|
|
+ async fiatDepositSimulate(@Body() params: any) {
|
|
|
+ const method = this.ctx.method;
|
|
|
+ const path = this.ctx.path;
|
|
|
+ const res = await this.noahPayAdapter.request(method, path, params);
|
|
|
+ this.noahOpenService.save_user_account(res, params);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询订单列表
|
|
|
*/
|
|
@@ -202,38 +216,91 @@ export class OpenApiControllerV4 extends BaseController {
|
|
|
|
|
|
|
|
|
// webhook
|
|
|
+
|
|
|
+ // NOAH 提供的公钥(根据环境选择 Sandbox 或 Production 公钥)
|
|
|
+ private publicKey = `-----BEGIN PUBLIC KEY-----
|
|
|
+ MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEm8yBiD+kmVJ1Xc9sfRkDx0yo9+u8yiAD
|
|
|
+ PngI20KoEswz0gflp8o/z66Abqz/m9A1CBecixWdeT72pA8NZBJI6L6Osd8RV+yx
|
|
|
+ QArxeGKEVX/2QNrfPqeAKODHT5LdStGT
|
|
|
+ -----END PUBLIC KEY-----`;
|
|
|
+
|
|
|
+ // 验证 Webhook 签名
|
|
|
+ private verifySignature(rawBody: string, signatureHeader: string): boolean {
|
|
|
+ try {
|
|
|
+ const verifier = crypto.createVerify('SHA384');
|
|
|
+ verifier.update(rawBody);
|
|
|
+ verifier.end();
|
|
|
+ return verifier.verify(this.publicKey, signatureHeader, 'base64');
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error verifying signature:', error);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* FiatDeposit 通知您客户已向其分配的银行账户号码发送了法币付款。
|
|
|
*/
|
|
|
@Post('/v1/webhooks/fiat/deposit', { summary: 'Webhook 发送法币收款' })
|
|
|
- async webhooksFiatDeposit(@Body(ALL) params: any) {
|
|
|
+ async webhooksFiatDeposit(@Body() body: any, ctx: any) {
|
|
|
// Access the request method and path
|
|
|
const method = this.ctx.method;
|
|
|
const path = this.ctx.path;
|
|
|
|
|
|
console.log('Request Method:webhooks/fiat/deposit-=-=', method);
|
|
|
console.log('Request Path:webhooks/fiat/deposit-=-=', path);
|
|
|
- console.log('Request params:webhooks/fiat/deposit-=-=', params);
|
|
|
+ console.log('Request params:webhooks/fiat/deposit-=-=', body);
|
|
|
+ // 获取原始请求体和签名头
|
|
|
+ const rawBody = JSON.stringify(body); // 原始请求体
|
|
|
+ const signatureHeader = ctx.headers['webhook-signature']; // 签名头
|
|
|
+
|
|
|
+ // 验证签名
|
|
|
+ if (!signatureHeader || !this.verifySignature(rawBody, signatureHeader)) {
|
|
|
+ console.log('Invalid webhook signature');
|
|
|
+ ctx.status = 400;
|
|
|
+ return { message: 'Invalid signature' };
|
|
|
+ }
|
|
|
|
|
|
- // console.log(typeof params)
|
|
|
- return
|
|
|
+ // 处理 Webhook 数据
|
|
|
+ const event = body;
|
|
|
+ console.log('Received webhook event webhooksFiatDeposit:', event);
|
|
|
+
|
|
|
+ // 返回 200 状态码确认接收
|
|
|
+ ctx.status = 200;
|
|
|
+ return { message: 'Webhook received' };
|
|
|
}
|
|
|
/**
|
|
|
* Transaction 通知您法币已被转换为您选择的加密货币,并记入您的 NOAH 账户。
|
|
|
* 通知您资金已提现到客户的 DestinationAddress
|
|
|
*/
|
|
|
@Post('/v1/webhooks/transaction', { summary: 'Webhook 发送法币收款' })
|
|
|
- async webhooksTransaction(@Body(ALL) params: any) {
|
|
|
+ async webhooksTransaction(@Body() body: any, ctx: any) {
|
|
|
// Access the request method and path
|
|
|
const method = this.ctx.method;
|
|
|
const path = this.ctx.path;
|
|
|
|
|
|
console.log('Request Method:webhooks/transaction-=-', method);
|
|
|
console.log('Request Path:webhooks/transaction-=-=', path);
|
|
|
- console.log('Request params:webhooks/transaction-=-', params);
|
|
|
-
|
|
|
- // console.log(typeof params)
|
|
|
- return
|
|
|
+ console.log('Request params:webhooks/transaction-=-', body);
|
|
|
+
|
|
|
+ // 获取原始请求体和签名头
|
|
|
+ const rawBody = JSON.stringify(body); // 原始请求体
|
|
|
+ const signatureHeader = ctx.headers['webhook-signature']; // 签名头
|
|
|
+
|
|
|
+ // 验证签名
|
|
|
+ if (!signatureHeader || !this.verifySignature(rawBody, signatureHeader)) {
|
|
|
+ console.log('Invalid webhook signature');
|
|
|
+ ctx.status = 400;
|
|
|
+ return { message: 'Invalid signature' };
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理 Webhook 数据
|
|
|
+ const event = body;
|
|
|
+ console.log('Received webhook event webhooksTransaction:', event);
|
|
|
+
|
|
|
+ // 返回 200 状态码确认接收
|
|
|
+ ctx.status = 200;
|
|
|
+ return { message: 'Webhook received' };
|
|
|
}
|
|
|
// /**
|
|
|
// * FiatDeposit 通知您客户已向其分配的银行账户号码发送了法币付款。
|