aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/buy/+page.server.js
blob: b97ac88bbf6b91b1aa02b1b6d2d53c67a08b2fa8 (plain) (blame)
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
import { env } from '$env/dynamic/private';
import { error, fail, redirect } from '@sveltejs/kit';
import { post, auth } from '$lib/utils';
import { createTicket } from '$lib/ticket';

export let load = async ({ cookies }) => {
	let invoice = await post('/invoice', { invoice: { type: 'bitcoin' }}, auth(cookies));
	let { hash } = invoice;
  return { address: hash };
}

export const actions = {
	default: async ({ cookies, request }) => {
		let form = await request.formData();

		let amount = parseFloat(form.get('amount'));
		if (!amount || amount < 0 || amount > 500) return fail(400, { message: 'Invalid amount' });

		await post(
			'/buy',
			{
				amount: form.get('amount'),
				number: form.get('number').replace(/\s+/g, ''),
				month: form.get('expiry').slice(0, 2),
				year: form.get('expiry').slice(-2),
				cvc: form.get('cvc')
			},
			auth(cookies)
		);

		throw redirect(307, `/${form.get('username')}/payments`);
	}
};