Repository navigation
Expand file tree
/
Copy pathpayment-processor.contract.spec.ts
More file actions
223 lines (203 loc) · 6.49 KB
/
Copy pathpayment-processor.contract.spec.ts
File metadata and controls
223 lines (203 loc) · 6.49 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { HttpService } from "@nestjs/axios";
import { ConfigService } from "@nestjs/config";
import { Account, Keypair, Networks } from "@stellar/stellar-sdk";
import { of } from "rxjs";
import {
CreatedPayment,
IPaymentProcessor,
PaymentRequest,
PaymentStatus,
} from "../../interfaces/payment-processor.interface";
import { GrantfoxAdapter } from "../grantfox/grantfox.adapter";
import { StellarAdapter } from "../stellar/stellar.adapter";
import { SandboxPaymentAdapter } from "src/sandbox/adapters/sandbox-payment.adapter";
import { SandboxConfigService } from "src/sandbox/sandbox.config";
/**
* Contract-compliance suite. Every adapter is driven through the full
* create → sign → submit → getStatus → refund lifecycle against *mocked*
* network clients, and each step's result is type/shape-checked against
* {@link IPaymentProcessor}. Adding a new adapter here is the one place that
* proves it honours the contract.
*/
const VALID_STATUSES = new Set<string>(Object.values(PaymentStatus));
interface AdapterCase {
name: string;
build: () => IPaymentProcessor;
request: PaymentRequest;
}
function stellarCase(): AdapterCase {
const signingKp = Keypair.random();
const payerKp = Keypair.random();
const destKp = Keypair.random();
const build = (): IPaymentProcessor => {
const call = jest
.fn()
.mockResolvedValue({ hash: "TXHASH", successful: true });
const server = {
loadAccount: jest
.fn()
.mockResolvedValue(new Account(payerKp.publicKey(), "100")),
submitTransaction: jest
.fn()
.mockResolvedValue({ hash: "TXHASH", successful: true }),
transactions: jest
.fn()
.mockReturnValue({ transaction: jest.fn().mockReturnValue({ call }) }),
payments: jest.fn().mockReturnValue({
forTransaction: jest.fn().mockReturnValue({
call: jest.fn().mockResolvedValue({
records: [
{
type: "payment",
amount: "10",
from: payerKp.publicKey(),
to: signingKp.publicKey(),
asset_type: "native",
},
],
}),
}),
}),
};
const config = {
get: jest.fn((key: string, def?: unknown) =>
key === "STELLAR_NETWORK_PASSPHRASE"
? Networks.TESTNET
: key === "STELLAR_SIGNING_SECRET"
? signingKp.secret()
: def,
),
} as unknown as ConfigService;
return new StellarAdapter(server as any, config);
};
return {
name: "StellarAdapter",
build,
request: {
amount: "10",
currency: "XLM",
destination: destKp.publicKey(),
source: payerKp.publicKey(),
idempotencyKey: "idem-stellar",
},
};
}
function grantfoxCase(): AdapterCase {
const build = (): IPaymentProcessor => {
const http = {
post: jest.fn((url: string) =>
of(
url.endsWith("/refund")
? { data: { refundId: "rf_1", status: "refunded", amount: "10" } }
: { data: { id: "gf_1", status: "processing" } },
),
),
get: jest.fn(() =>
of({
data: {
id: "gf_1",
status: "confirmed",
transactionHash: "0xabc",
amount: "10",
},
}),
),
};
const config = {
get: jest.fn((key: string, def?: unknown) => {
const env: Record<string, string> = {
GRANTFOX_API_URL: "https://api.grantfox.example",
GRANTFOX_API_KEY: "key",
};
return env[key] ?? def;
}),
} as unknown as ConfigService;
return new GrantfoxAdapter(http as unknown as HttpService, config);
};
return {
name: "GrantfoxAdapter",
build,
request: {
amount: "10",
currency: "USD",
destination: "acct_1",
idempotencyKey: "idem-grantfox",
},
};
}
function sandboxCase(): AdapterCase {
const build = (): IPaymentProcessor => {
const config = {
get: (key: string, def?: unknown) =>
key === "SANDBOX_MODE" ? "true" : def,
} as unknown as ConfigService;
return new SandboxPaymentAdapter(new SandboxConfigService(config));
};
return {
name: "SandboxPaymentAdapter",
build,
request: {
amount: "10",
currency: "XLM",
destination: "GBSANDBOXDESTINATION",
source: "GBSANDBOXSOURCE",
idempotencyKey: "idem-sandbox",
},
};
}
const cases = [stellarCase(), grantfoxCase(), sandboxCase()];
describe.each(cases)("IPaymentProcessor contract: $name", (testCase) => {
let adapter: IPaymentProcessor;
beforeEach(() => {
adapter = testCase.build();
});
it("exposes the full interface surface", () => {
expect(typeof adapter.name).toBe("string");
expect(typeof adapter.displayName).toBe("string");
expect(adapter.capabilities).toEqual(
expect.objectContaining({
supportsPartialRefund: expect.any(Boolean),
requiresClientSideSigning: expect.any(Boolean),
currencies: expect.any(Array),
}),
);
for (const method of [
"initialize",
"createPayment",
"signTransaction",
"submitTransaction",
"getStatus",
"refund",
] as const) {
expect(typeof adapter[method]).toBe("function");
}
});
it("runs a create → sign → submit → status → refund round-trip with typed results", async () => {
const created: CreatedPayment = await adapter.createPayment(
testCase.request,
);
expect(typeof created.paymentId).toBe("string");
expect(VALID_STATUSES.has(created.status)).toBe(true);
const signed = await adapter.signTransaction(created);
expect(signed.paymentId).toBe(created.paymentId);
expect(typeof signed.signedPayload).toBe("string");
const submitted = await adapter.submitTransaction(signed);
expect(typeof submitted.transactionHash).toBe("string");
expect(VALID_STATUSES.has(submitted.status)).toBe(true);
const status = await adapter.getStatus(submitted.transactionHash);
expect(VALID_STATUSES.has(status.status)).toBe(true);
const refund = await adapter.refund({
paymentId: submitted.transactionHash,
idempotencyKey: "refund-key",
});
expect(typeof refund.refundId).toBe("string");
expect(typeof refund.refundedAmount).toBe("string");
expect([
PaymentStatus.REFUNDED,
PaymentStatus.PARTIALLY_REFUNDED,
]).toContain(refund.status);
});
it("initialize() resolves without throwing", async () => {
await expect(adapter.initialize()).resolves.toBeUndefined();
});
});