From 4db8ded1ab91b1670b56e34199cebf2edf0144d4 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 10 Apr 2024 13:27:58 -0700 Subject: rename pay route --- .../pay/[username]/[...amount]/+page.server.js | 50 ++++++++++ .../(app)/pay/[username]/[...amount]/+page.svelte | 108 +++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/routes/(app)/pay/[username]/[...amount]/+page.server.js create mode 100644 src/routes/(app)/pay/[username]/[...amount]/+page.svelte (limited to 'src/routes/(app)/pay') diff --git a/src/routes/(app)/pay/[username]/[...amount]/+page.server.js b/src/routes/(app)/pay/[username]/[...amount]/+page.server.js new file mode 100644 index 00000000..884e3eee --- /dev/null +++ b/src/routes/(app)/pay/[username]/[...amount]/+page.server.js @@ -0,0 +1,50 @@ +import { error, fail, redirect } from "@sveltejs/kit"; +import { types, sats, fd, auth, get, post } from "$lib/utils"; + +export async function load({ cookies, params, parent }) { + let subject = await get(`/users/${params.username}`); + + let { rates, user } = await parent(); + let { username } = params; + let [amount, currency] = params.amount.split("/"); + + let rate; + if (amount) { + rate = rates[currency ? currency.toUpperCase() : subject.currency]; + if (!rate) error(500, "Invalid currency symbol"); + if (currency) amount = (amount * sats) / rate; + + let { id } = await post( + "/invoice", + { + invoice: { + amount, + currency: currency === subject.currency ? currency : undefined, + prompt: false, + rate: currency === subject.currency ? rate : undefined, + type: types.lightning, + }, + user: { username }, + }, + auth(cookies) + ); + + redirect(307, `/${username}/invoice/${id}`); + } + + return { amount, rate, subject }; +} + +export const actions = { + default: async ({ cookies, request }) => { + try { + let body = await fd(request); + await post("/payments", body, auth(cookies)); + } catch (e) { + console.log(e); + return fail(400, { message: e.message }); + } + + redirect(307, "/sent"); + }, +}; diff --git a/src/routes/(app)/pay/[username]/[...amount]/+page.svelte b/src/routes/(app)/pay/[username]/[...amount]/+page.svelte new file mode 100644 index 00000000..ab8a3add --- /dev/null +++ b/src/routes/(app)/pay/[username]/[...amount]/+page.svelte @@ -0,0 +1,108 @@ + + + + +{#if form?.message} +
+ {form.message} +
+{/if} + +
+ + +
+ + + + + +
+ {#if amount} + + {:else} + + {/if} +
+
+
-- cgit v1.2.3