-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-client.ts
341 lines (324 loc) · 10.2 KB
/
main-client.ts
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import {
Circle,
CardCreationRequest,
Card,
PaymentCreationRequestVerificationEnum,
PublicKey,
} from "@circle-fin/circle-sdk";
import { ApiError } from "../shared/error";
import { CIRCLE_API_KEY, CIRCLE_ENVIRONMENT, CIRCLE_MASTER_WALLET, SERVER_ENV } from "../constants";
import { CircleClient, CircleDepositArgs } from "./client";
import { v4 as uuid } from "uuid";
import { pgpEncrypt } from "./open-pgp";
import { CircleCardId, ServerEnv } from "../types/types";
/**
* Implemntation of the Circle client that uses the Circle SDK
*/
export class CircleMainClient implements CircleClient {
private readonly sdk: Circle;
/* This is a private constructor so that we can only create initiate new instances of this client */
private constructor(sdk: Circle) {
this.sdk = sdk;
}
/**
*
* @returns a new instance of the CircleMainClient with the default Circle API key and environment
*/
public static ofDefaults(): CircleMainClient {
return new CircleMainClient(new Circle(CIRCLE_API_KEY, CIRCLE_ENVIRONMENT));
}
/**
*
* Generates and adds a new card to the Circle account
* @returns CircleCardId of the card that was added
*/
private async addRandomCard(): Promise<CircleCardId> {
if (SERVER_ENV === ServerEnv.PROD) {
throw new Error("Only allowed in dev environments.");
}
return await this.addCreditCard(exampleCards[Math.floor(Math.random() * exampleCards.length)]);
}
// Reference Implementation: https://github.com/circlefin/payments-sample-app/blob/78e3d1b5b3b548775e755f1b619720bcbe5a8789/pages/flow/charge/index.vue
private async addCreditCard(args: CardDetails): Promise<CircleCardId> {
const publicKey: PublicKey = await this.getCircleRsaKey();
const verificationDetails: CardVerificationDetails = {
number: args.cardNumber,
cvv: args.cvv
};
const encryptedData = await pgpEncrypt(verificationDetails, publicKey);
const { encryptedMessage, keyId } = encryptedData;
const payload: CardCreationRequest = {
idempotencyKey: uuid(),
expMonth: parseInt(args.expiry.month),
expYear: parseInt(args.expiry.year),
keyId: keyId,
encryptedData: encryptedMessage,
billingDetails: {
name: args.name,
city: args.city,
country: args.country,
line1: args.line1,
line2: args.line2,
postalCode: args.postalCode,
district: args.district
},
metadata: {
email: args.email,
phoneNumber: args.phoneNumber,
//TODO copied from example
sessionId: 'xxx',
ipAddress: '172.33.222.1',
},
}
let cardResponse = await this.sdk.cards.createCard(payload);
const cardId: string | undefined = cardResponse.data.data?.id;
if (cardId === undefined) {
//TODO better error
throw ApiError.generalServerError(`Couldnt make card: status: ${cardResponse.statusText}, code: ${cardResponse.data.data?.errorCode}`);
}
return cardId;
}
public async fetchCard(id: string): Promise<Card> {
const response = await this.sdk.cards.getCard(id);
const card: Card | undefined = response.data.data;
if (card === undefined) {
throw ApiError.noCardFound();
}
return card;
}
public async depositUsdc(args: CircleDepositArgs): Promise<void> {
//TODO replace when going to prod
const cardId: string = await this.addRandomCard();
const cardCvv: string = "123"; // all the example cards have this cvv
// https://developers.circle.com/developer/reference/createpayment
const publicKey: PublicKey = await this.getCircleRsaKey();
const verificationDetails: Partial<CardVerificationDetails> = {
cvv: cardCvv
};
const encryptedData = await pgpEncrypt(verificationDetails, publicKey);
const { encryptedMessage, keyId } = encryptedData;
const transactionId: string = uuid();
const paymentResponse = await this.sdk.payments.createPayment({
idempotencyKey: transactionId,
amount: {
amount: args.amount.toFixed(2),
currency: "USD"
},
verification: PaymentCreationRequestVerificationEnum.Cvv,
metadata: {
email: args.member,
sessionId: 'xxx',
ipAddress: '172.33.222.1'
},
source: {
id: cardId,
type: "card",
},
description: "Deposit to Tap account.",
encryptedData: encryptedMessage,
keyId: keyId
});
if (paymentResponse.status < 400) {
try {
const transferResponse = await this.sdk.transfers.createTransfer({
idempotencyKey: uuid(),
source: {
type: "wallet",
id: CIRCLE_MASTER_WALLET
},
destination: {
chain: "SOL",
address: args.destinationAtaString,
type: "blockchain",
},
amount: {
amount: `${args.amount}`,
currency: "USD"
}
});
if (transferResponse.status >= 400) {
throw ApiError.generalServerError("Unable to transfer funds to user.");
}
} catch (e) {
throw ApiError.generalServerError("Unable to transfer funds to user.");
}
}
}
private async getCircleRsaKey(): Promise<PublicKey> {
const publicKey = (await this.sdk.encryption.getPublicKey()).data.data;
if (publicKey === undefined) {
throw ApiError.generalServerError("Unable to get Circle credentials.");
}
return publicKey;
}
}
interface CardVerificationDetails {
/* numbers only, no spaces or dashes */
number: string;
/* secure code on the back of the card */
cvv: string;
}
interface CardDetails {
/* numbers only, no spaces or dashes */
cardNumber: string;
/* secure code on the back of the card */
cvv: string;
/* card expiry date */
expiry: {
/* 2 digit month */
month: string;
/* 4 digit year */
year: string;
},
name: string;
country: string;
district?: string;
line1: string;
line2?: string;
city: string;
postalCode: string;
phoneNumber: string;
email: string;
}
/**
* Example cards for testing
* Ref Implementation: https://github.com/circlefin/payments-sample-app/blob/78e3d1b5b3b548775e755f1b619720bcbe5a8789/lib/cardTestData.ts
*/
const exampleCards: CardDetails[] = [
{
cardNumber: '4007400000000007',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0001',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]'
},
{
cardNumber: '4007410000000006',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0002',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
{
cardNumber: '4200000000000000',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0003',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
{
cardNumber: '4757140000000001',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0004',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
{
cardNumber: '5102420000000006',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0005',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
{
cardNumber: '5173375000000006',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0006',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
{
cardNumber: '5555555555554444',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0007',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
{
cardNumber: '378282246310005',
cvv: '123',
expiry: {
month: '01',
year: '2025',
},
name: 'Customer 0009',
country: 'US',
district: 'MA',
line1: 'Test',
line2: '',
city: 'Test City',
postalCode: '11111',
phoneNumber: '+12025550180',
email: '[email protected]',
},
];