forked from yoone/API
feat(webhook): 添加对shoppy平台webhook的支持
- 在site.entity.ts中添加webhookUrl字段 - 在auth.middleware.ts中添加/shoppy路由到白名单 - 在webhook.controller.ts中实现shoppy平台webhook处理逻辑
This commit is contained in:
parent
3d6f488a70
commit
c64da5879d
|
|
@ -14,9 +14,13 @@ import { SiteService } from '../service/site.service';
|
||||||
import { OrderService } from '../service/order.service';
|
import { OrderService } from '../service/order.service';
|
||||||
import { SiteApiService } from '../service/site-api.service';
|
import { SiteApiService } from '../service/site-api.service';
|
||||||
|
|
||||||
|
import {
|
||||||
|
UnifiedOrderDTO,
|
||||||
|
} from '../dto/site-api.dto';
|
||||||
|
|
||||||
@Controller('/webhook')
|
@Controller('/webhook')
|
||||||
export class WebhookController {
|
export class WebhookController {
|
||||||
private secret = 'YOONE24kd$kjcdjflddd';
|
private secret = '$kjYOONE24kdcdjflddd';
|
||||||
|
|
||||||
// 平台服务保留按需注入
|
// 平台服务保留按需注入
|
||||||
|
|
||||||
|
|
@ -193,4 +197,88 @@ export class WebhookController {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Post('/shoppy')
|
||||||
|
async handleShoppyWebhook(
|
||||||
|
@Body() body: any,
|
||||||
|
@Query('siteId') siteIdStr: string,
|
||||||
|
@Query('signature') signature: string,
|
||||||
|
@Headers() header: any
|
||||||
|
) {
|
||||||
|
const topic = header['x-oemsaas-event-type'];
|
||||||
|
// const source = header['x-oemsaas-shop-domain'];
|
||||||
|
const siteId = Number(siteIdStr);
|
||||||
|
const bodys = new UnifiedOrderDTO();
|
||||||
|
Object.assign(bodys, body);
|
||||||
|
// 从数据库获取站点配置
|
||||||
|
const site = await this.siteService.get(siteId, true);
|
||||||
|
|
||||||
|
// if (!site || !source?.includes(site.websiteUrl)) {
|
||||||
|
if (!site) {
|
||||||
|
console.log('domain not match');
|
||||||
|
return {
|
||||||
|
code: HttpStatus.BAD_REQUEST,
|
||||||
|
success: false,
|
||||||
|
message: 'domain not match',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!signature) {
|
||||||
|
return {
|
||||||
|
code: HttpStatus.BAD_REQUEST,
|
||||||
|
success: false,
|
||||||
|
message: 'Signature missing',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//shopyy 无法提供加密字段校验,注释校验逻辑
|
||||||
|
// const rawBody = this.ctx.request.rawBody;
|
||||||
|
// const hash = crypto
|
||||||
|
// .createHmac('sha256', this.secret)
|
||||||
|
// .update(rawBody)
|
||||||
|
// .digest('base64');
|
||||||
|
try {
|
||||||
|
if (this.secret === signature) {
|
||||||
|
switch (topic) {
|
||||||
|
case 'product.created':
|
||||||
|
case 'product.updated':
|
||||||
|
// 不再写入本地,平台事件仅确认接收
|
||||||
|
break;
|
||||||
|
case 'product.deleted':
|
||||||
|
// 不再写入本地,平台事件仅确认接收
|
||||||
|
break;
|
||||||
|
case 'orders/create':
|
||||||
|
case 'orders/update':
|
||||||
|
await this.orderService.syncSingleOrder(siteId, bodys);
|
||||||
|
break;
|
||||||
|
case 'orders/delete':
|
||||||
|
break;
|
||||||
|
case 'customer.created':
|
||||||
|
break;
|
||||||
|
case 'customer.updated':
|
||||||
|
break;
|
||||||
|
case 'customer.deleted':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log('Unhandled event:', topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
success: true,
|
||||||
|
message: 'Webhook processed successfully',
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
code: 403,
|
||||||
|
success: false,
|
||||||
|
message: 'Webhook verification failed',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ export class Site {
|
||||||
@Column({ name: 'webhook_url', length: 255, nullable: true })
|
@Column({ name: 'webhook_url', length: 255, nullable: true })
|
||||||
webhookUrl?: string;
|
webhookUrl?: string;
|
||||||
|
|
||||||
|
@Column({ name: 'webhook_url', length: 255, nullable: true })
|
||||||
|
webhookUrl: string;
|
||||||
|
|
||||||
@Column({ length: 255, nullable: true })
|
@Column({ length: 255, nullable: true })
|
||||||
consumerKey?: string;
|
consumerKey?: string;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue