Repository navigation
Expand file tree
/
Copy pathPaymentBackend.ts
More file actions
133 lines (115 loc) · 3.76 KB
/
Copy pathPaymentBackend.ts
File metadata and controls
133 lines (115 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import express from "express";
import * as helper from "encrypted-nestjs";
import * as nest from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import payments from "./api";
import FakeIamport from "fake-iamport-server";
import FakeToss from "fake-toss-payments-server";
import { PaymentConfiguration } from "./PaymentConfiguration";
import { PaymentGlobal } from "./PaymentGlobal";
/**
* 통합 결제 백엔드 서버.
*
* @author Jeongho Nam - https://github.com/samchon
*/
export class PaymentBackend
{
private application_?: nest.INestApplication;
private is_closing_: boolean = false;
private fake_servers_: IFakeServer[] = [];
/**
* 서버 개설하기.
*
* 통합 결제 백엔드 서버를 개설한다. 이 때 개설되는 서버의 종류는
* {@link PaymentGlobal.mode} 를 따르며, 만일 개설되는 서버가 테스트 자동화 프로그램에
* 의하여 시작된 것이라면 ({@link PaymentGlobal.testing} 값이 true), 이와 연동하게 될
* 가짜 PG 결제사 서버들도 함께 개설한다.
*/
public async open(): Promise<void>
{
//----
// OPEN THE BACKEND SERVER
//----
// MOUNT CONTROLLERS
this.application_ = await NestFactory.create
(
await helper.EncryptedModule.dynamic
(
__dirname + "/controllers",
PaymentConfiguration.ENCRYPTION_PASSWORD
),
{ logger: false }
);
// CONFIGURATIONS
this.is_closing_ = false;
this.application_.enableCors();
this.application_.use(this.middleware.bind(this));
// DO OPEN
await this.application_.listen(PaymentConfiguration.API_PORT);
// CONFIGURE FAKE SERVERS IF TESTING
if (PaymentGlobal.testing === true)
{
// OPEN FAKE SERVERS
this.fake_servers_ = [
new FakeIamport.FakeIamportBackend(),
new FakeToss.FakeTossBackend()
];
for (const s of this.fake_servers_)
await s.open();
const host: string = `http://127.0.0.1:${PaymentConfiguration.API_PORT}`;
// CONFIGURE WEBHOOK URLS
FakeIamport.FakeIamportConfiguration.WEBHOOK_URL = `${host}${payments.functional.webhooks.iamport.PATH}`;
FakeToss.TossFakeConfiguration.WEBHOOK_URL = `${host}${payments.functional.webhooks.toss.PATH}`;
}
//----
// POST-PROCESSES
//----
// INFORM TO THE PM2
if (process.send)
process.send("ready");
// WHEN KILL COMMAND COMES
process.on("SIGINT", async () =>
{
this.is_closing_ = true;
await this.close();
process.exit(0);
});
}
/**
* 개설한 서버 닫기.
*/
public async close(): Promise<void>
{
if (this.application_ === undefined)
return;
// DO CLOSE
await this.application_.close();
delete this.application_;
// EXIT FROM THE CRITICAL-SERVER
if (await PaymentGlobal.critical.is_loaded() === true)
{
const critical = await PaymentGlobal.critical.get();
await critical.close();
}
// CLOSE FAKE SERVERS
for (const s of this.fake_servers_)
await s.close();
this.fake_servers_ = [];
}
private middleware
(
_request: express.Request,
response: express.Response,
next: Function
): void
{
if (this.is_closing_ === true)
response.set("Connection", "close");
next();
}
}
interface IFakeServer
{
open(): Promise<void>;
close(): Promise<void>;
}