|
1 | 1 | "use server"; |
| 2 | +import type { Address } from "thirdweb"; |
2 | 3 | import { getAuthToken } from "@/api/auth-token"; |
3 | 4 | import { NEXT_PUBLIC_THIRDWEB_BRIDGE_HOST } from "@/constants/public-envs"; |
4 | 5 |
|
@@ -96,6 +97,139 @@ export async function deleteWebhook(props: { |
96 | 97 | return; |
97 | 98 | } |
98 | 99 |
|
| 100 | +type PaymentLink = { |
| 101 | + id: string; |
| 102 | + link: string; |
| 103 | + title: string; |
| 104 | + imageUrl: string; |
| 105 | + createdAt: string; |
| 106 | + updatedAt: string; |
| 107 | + destinationToken: { |
| 108 | + chainId: number; |
| 109 | + address: Address; |
| 110 | + symbol: string; |
| 111 | + name: string; |
| 112 | + decimals: number; |
| 113 | + iconUri: string; |
| 114 | + }; |
| 115 | + receiver: Address; |
| 116 | + amount: bigint; |
| 117 | +}; |
| 118 | + |
| 119 | +export async function getPaymentLinks(props: { |
| 120 | + clientId: string; |
| 121 | + teamId: string; |
| 122 | +}): Promise<Array<PaymentLink>> { |
| 123 | + const authToken = await getAuthToken(); |
| 124 | + const res = await fetch(`${UB_BASE_URL}/v1/developer/links`, { |
| 125 | + headers: { |
| 126 | + Authorization: `Bearer ${authToken}`, |
| 127 | + "Content-Type": "application/json", |
| 128 | + "x-client-id": props.clientId, |
| 129 | + "x-team-id": props.teamId, |
| 130 | + }, |
| 131 | + method: "GET", |
| 132 | + }); |
| 133 | + |
| 134 | + if (!res.ok) { |
| 135 | + const text = await res.text(); |
| 136 | + throw new Error(text); |
| 137 | + } |
| 138 | + |
| 139 | + const json = (await res.json()) as { |
| 140 | + data: Array<PaymentLink & { amount: string }>; |
| 141 | + }; |
| 142 | + return json.data.map((link) => ({ |
| 143 | + id: link.id, |
| 144 | + link: link.link, |
| 145 | + title: link.title, |
| 146 | + imageUrl: link.imageUrl, |
| 147 | + createdAt: link.createdAt, |
| 148 | + updatedAt: link.updatedAt, |
| 149 | + destinationToken: { |
| 150 | + chainId: link.destinationToken.chainId, |
| 151 | + address: link.destinationToken.address, |
| 152 | + symbol: link.destinationToken.symbol, |
| 153 | + name: link.destinationToken.name, |
| 154 | + decimals: link.destinationToken.decimals, |
| 155 | + iconUri: link.destinationToken.iconUri, |
| 156 | + }, |
| 157 | + receiver: link.receiver, |
| 158 | + amount: BigInt(link.amount), |
| 159 | + })); |
| 160 | +} |
| 161 | + |
| 162 | +export async function createPaymentLink(props: { |
| 163 | + clientId: string; |
| 164 | + teamId: string; |
| 165 | + title: string; |
| 166 | + imageUrl?: string; |
| 167 | + intent: { |
| 168 | + destinationChainId: number; |
| 169 | + destinationTokenAddress: Address; |
| 170 | + receiver: Address; |
| 171 | + amount: bigint; |
| 172 | + purchaseData?: unknown; |
| 173 | + }; |
| 174 | +}) { |
| 175 | + const authToken = await getAuthToken(); |
| 176 | + |
| 177 | + const res = await fetch(`${UB_BASE_URL}/v1/developer/links`, { |
| 178 | + body: JSON.stringify({ |
| 179 | + title: props.title, |
| 180 | + imageUrl: props.imageUrl, |
| 181 | + intent: { |
| 182 | + destinationChainId: props.intent.destinationChainId, |
| 183 | + destinationTokenAddress: props.intent.destinationTokenAddress, |
| 184 | + receiver: props.intent.receiver, |
| 185 | + amount: props.intent.amount.toString(), |
| 186 | + purchaseData: props.intent.purchaseData, |
| 187 | + }, |
| 188 | + }), |
| 189 | + headers: { |
| 190 | + Authorization: `Bearer ${authToken}`, |
| 191 | + "Content-Type": "application/json", |
| 192 | + "x-client-id": props.clientId, |
| 193 | + "x-team-id": props.teamId, |
| 194 | + }, |
| 195 | + method: "POST", |
| 196 | + }); |
| 197 | + |
| 198 | + if (!res.ok) { |
| 199 | + const text = await res.text(); |
| 200 | + throw new Error(text); |
| 201 | + } |
| 202 | + |
| 203 | + return; |
| 204 | +} |
| 205 | + |
| 206 | +export async function deletePaymentLink(props: { |
| 207 | + clientId: string; |
| 208 | + teamId: string; |
| 209 | + paymentLinkId: string; |
| 210 | +}) { |
| 211 | + const authToken = await getAuthToken(); |
| 212 | + const res = await fetch( |
| 213 | + `${UB_BASE_URL}/v1/developer/links/${props.paymentLinkId}`, |
| 214 | + { |
| 215 | + headers: { |
| 216 | + Authorization: `Bearer ${authToken}`, |
| 217 | + "Content-Type": "application/json", |
| 218 | + "x-client-id": props.clientId, |
| 219 | + "x-team-id": props.teamId, |
| 220 | + }, |
| 221 | + method: "DELETE", |
| 222 | + }, |
| 223 | + ); |
| 224 | + |
| 225 | + if (!res.ok) { |
| 226 | + const text = await res.text(); |
| 227 | + throw new Error(text); |
| 228 | + } |
| 229 | + |
| 230 | + return; |
| 231 | +} |
| 232 | + |
99 | 233 | export type Fee = { |
100 | 234 | feeRecipient: string; |
101 | 235 | feeBps: number; |
@@ -195,30 +329,28 @@ export type Payment = { |
195 | 329 | export async function getPayments(props: { |
196 | 330 | clientId: string; |
197 | 331 | teamId: string; |
| 332 | + paymentLinkId?: string; |
198 | 333 | limit?: number; |
199 | 334 | offset?: number; |
200 | 335 | }) { |
201 | 336 | const authToken = await getAuthToken(); |
202 | 337 |
|
203 | 338 | // Build URL with query parameters if provided |
204 | | - let url = `${UB_BASE_URL}/v1/developer/payments`; |
205 | | - const queryParams = new URLSearchParams(); |
| 339 | + const url = new URL(`${UB_BASE_URL}/v1/developer/payments`); |
206 | 340 |
|
207 | 341 | if (props.limit) { |
208 | | - queryParams.append("limit", props.limit.toString()); |
| 342 | + url.searchParams.append("limit", props.limit.toString()); |
209 | 343 | } |
210 | 344 |
|
211 | 345 | if (props.offset) { |
212 | | - queryParams.append("offset", props.offset.toString()); |
| 346 | + url.searchParams.append("offset", props.offset.toString()); |
213 | 347 | } |
214 | 348 |
|
215 | | - // Append query params to URL if any exist |
216 | | - const queryString = queryParams.toString(); |
217 | | - if (queryString) { |
218 | | - url = `${url}?${queryString}`; |
| 349 | + if (props.paymentLinkId) { |
| 350 | + url.searchParams.append("paymentLinkId", props.paymentLinkId); |
219 | 351 | } |
220 | 352 |
|
221 | | - const res = await fetch(url, { |
| 353 | + const res = await fetch(url.toString(), { |
222 | 354 | headers: { |
223 | 355 | Authorization: `Bearer ${authToken}`, |
224 | 356 | "Content-Type": "application/json", |
|
0 commit comments