forked from FinHubSA/open-payments-express
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen-payments.ts
More file actions
260 lines (215 loc) · 7.02 KB
/
open-payments.ts
File metadata and controls
260 lines (215 loc) · 7.02 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
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
import dotenv from "dotenv";
import {
type WalletAddress,
type AuthenticatedClient,
type Grant,
createAuthenticatedClient,
type PendingGrant,
isPendingGrant,
} from "@interledger/open-payments";
import { randomUUID } from "crypto";
import { type components } from "@interledger/open-payments/dist/openapi/generated/auth-server-types";
dotenv.config({ path: ".env" });
export async function getAuthenticatedClient() {
let walletAddress = process.env.OPEN_PAYMENTS_CLIENT_ADDRESS;
if (walletAddress && walletAddress.startsWith("$")) {
walletAddress = walletAddress.replace("$", "https://");
}
// TODO: create authenticated client object
const client: AuthenticatedClient | undefined = undefined;
return client;
}
export async function getWalletAddressInfo(
client: AuthenticatedClient,
walletAddress: string
): Promise<{ walletAddress: string; walletAddressDetails: WalletAddress }> {
if (walletAddress && walletAddress.startsWith("$")) {
walletAddress = walletAddress.replace("$", "https://");
}
const walletAddressDetails: WalletAddress = await client.walletAddress.get({
url: walletAddress,
});
console.log("<< Wallet address details");
console.log(walletAddressDetails);
return { walletAddress, walletAddressDetails };
}
/**
* The method requests a grant from the receivers auth server for creating an incoming payment grant
* After receiving the grant the incoming payment resource is created
*
* @param client
* @param value - payment amount to be made
* @param walletAddressDetails - wallet address details for the receiver
* @returns
*/
export async function createIncomingPayment(
client: AuthenticatedClient,
value: string,
walletAddressDetails: WalletAddress
) {
console.log(">> Creating Incoming Payment Resource");
console.log(walletAddressDetails);
// TODO: Request IP grant
const grant: PendingGrant | Grant | undefined = undefined;
if (grant && isPendingGrant(grant)) {
throw new Error("Expected non-interactive grant");
}
// TODO: create incoming payment
const incomingPayment: any = undefined;
console.log("<< Resource created");
console.log(incomingPayment);
return incomingPayment;
}
/**
* The method requests a grant to create a quote on the senders resource server
* The quote is then created on the senders resource server
*
* @param client
* @param incomingPaymentUrl - identifier for the incoming payment the quote is being created for
* @param walletAddressDetails - wallet address details for the sender
* @returns
*/
export async function createQuote(
client: AuthenticatedClient,
incomingPaymentUrl: string,
walletAddressDetails: WalletAddress
) {
console.log(">> Creating quoute");
console.log(walletAddressDetails);
// TODO: Request Quote grant
const grant: PendingGrant | Grant | undefined = undefined;
if (grant && isPendingGrant(grant)) {
throw new Error("Expected non-interactive grant");
}
// TODO: create quote
const quote: any = undefined;
console.log("<< Quote created");
console.log(quote);
return quote;
}
/**
* This method creates a pending grant which must be authorized by the user
* After it is authorized the continuation access token we receive can be used to get the actual OP creation grant
* Tells the client to go ask sender for approval and details of where to come back to continue the process
*
* @param client
* @param input - details from the quote
* @param walletAddressDetails - wallet address details for the sender
* @returns
*/
export async function createOutgoingPaymentPendingGrant(
client: AuthenticatedClient,
input: any,
walletAddressDetails: WalletAddress
): Promise<PendingGrant | undefined> {
console.log(">> Getting link to authorize outgoing payment grant request");
console.log(walletAddressDetails);
const dateNow = new Date().toISOString();
const debitAmount = input.debitAmount;
const receiveAmount = input.receiveAmount;
// TODO: Request outgoing payment pending grant
const grant: PendingGrant | Grant | undefined = undefined;
if (grant && !isPendingGrant(grant)) {
throw new Error("Expected interactive grant");
}
console.log("<< Pending outgoing grant obtained");
return grant;
}
/**
* This method will now get the grant if the user has given permission
* The grant is then used to create the outgoing payment
*
* @param client
* @param input
* @returns
*/
export async function createOutgoingPayment(
client: AuthenticatedClient,
input: any
) {
let walletAddress = input.senderWalletAddress;
if (walletAddress.startsWith("$"))
walletAddress = walletAddress.replace("$", "https://");
console.log(">> Creating outgoing payment");
console.log(input);
// TODO: Get the grant since it was still pending
const grant: PendingGrant | Grant | undefined = undefined;
console.log("<< Outgoing payment grant");
console.log(grant);
if (grant && isPendingGrant(grant)) {
throw new Error("Expected non-interactive grant");
}
// TODO: create outgoing payment
const outgoingPayment: any = undefined;
console.log("<< Outgoing payment created");
console.log(outgoingPayment);
return outgoingPayment;
}
/**
* This method creates an outgoing payment for a recurring payment
*
* @param client
* @param input
* @returns
*/
export async function processSubscriptionPayment(
client: AuthenticatedClient,
input: any
) {
// rotate the token
const token = await client.token.rotate({
url: input.manageUrl,
accessToken: input.previousToken,
});
if (!token.access_token) {
console.error("!! Failed to rotate token.");
}
console.log("<< Rotated Token ");
console.log(token.access_token);
const tokenAccessDetails = token.access_token.access as {
type: "outgoing-payment";
actions: ("create" | "read" | "read-all" | "list" | "list-all")[];
identifier: string;
limits?: components["schemas"]["limits-outgoing"];
}[];
const receiveAmount = (tokenAccessDetails[0].limits as any).receiveAmount
?.value;
const { walletAddressDetails: receiverWalletAddressDetails } =
await getWalletAddressInfo(client, input.receiverWalletAddress);
const {
walletAddress: senderWalletAddress,
walletAddressDetails: senderWalletAddressDetails,
} = await getWalletAddressInfo(
client,
tokenAccessDetails[0]?.identifier ?? ""
);
// create incoming payment
const incomingPayment = await createIncomingPayment(
client,
receiveAmount!,
receiverWalletAddressDetails
);
// create quote
const quote = await createQuote(
client,
incomingPayment.id,
senderWalletAddressDetails
);
// create outgoing payment
try {
const outgoingPayment = await client.outgoingPayment.create(
{
url: new URL(senderWalletAddress).origin,
accessToken: token.access_token.value, //OUTGOING_PAYMENT_ACCESS_TOKEN,
},
{
walletAddress: senderWalletAddress,
quoteId: quote.id, //QUOTE_URL,
}
);
return outgoingPayment;
} catch (error) {
console.log(error);
throw new Error("Error creating subscription outgoing payment");
}
}