-
Notifications
You must be signed in to change notification settings - Fork 0
/
exWirePayout.ts
87 lines (77 loc) · 2.26 KB
/
exWirePayout.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
import { Circle, CircleEnvironments } from "@circle-fin/circle-sdk";
import { v4 as uuidv4 } from "uuid";
// Create a file named .env within the root of the project that has one line item with you API Key like so
// API_KEY="???"
import * as dotenv from "dotenv";
dotenv.config();
// Initialize API driver
const circle = new Circle(
process.env.API_KEY as string,
CircleEnvironments.sandbox
);
function delay(time: number) {
return new Promise((resolve) => setTimeout(resolve, time));
}
// POST: /v1/businessAccount/banks/wires
async function createWireAccount() {
const createWireAccountRes = await circle.wires.createWireAccount({
idempotencyKey: uuidv4(),
accountNumber: "12340011",
routingNumber: "121000248",
billingDetails: {
name: "John Smith",
city: "Boston",
country: "US",
line1: "100 Money Street",
district: "MA",
postalCode: "01234",
},
bankAddress: { country: "US" },
});
return createWireAccountRes.data.data?.id;
}
// GET: /v1//banks/wires/{id}
async function wireAccountReady(id: any) {
let wireAccount = await circle.wires.getWireAccount(id);
// Polling
do {
await delay(500);
wireAccount = await circle.wires.getWireAccount(id);
} while (wireAccount.data.data?.status !== "complete");
return wireAccount.data.data?.id;
}
// GET: /v1/configuration
async function getWalletMasterId() {
const getWalletMasterIdRes = await circle.management.getAccountConfig();
return getWalletMasterIdRes.data.data?.payments?.masterWalletId;
}
// POST: /v1/payouts
async function createPayout(id: any, walletMasterId: any) {
const creatWireDepositRes = await circle.payouts.createPayout({
idempotencyKey: uuidv4(),
source: {
type: "wallet",
id: walletMasterId,
},
destination: {
type: "wire",
id: id,
},
amount: {
amount: "3.14",
currency: "USD",
},
metadata: {
beneficiaryEmail: "[email protected]",
},
});
return creatWireDepositRes.data.data;
}
async function wirePayout() {
const wireAccountId = await createWireAccount();
const walletMasterId = await getWalletMasterId();
await wireAccountReady(wireAccountId);
const payout = await createPayout(wireAccountId, walletMasterId);
console.log(payout);
}
wirePayout();