Skip to content

Commit

Permalink
feat: add payment type to sheet (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverrejb committed May 25, 2024
1 parent 851dee1 commit a8021ec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/lib/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export async function encryptFormData(formData) {
const email = formData.get("email").toString();
const membershipType = formData.get("membershipType").toString();
const id = formData.get("id").toString();
const paymentType = formData.get("paymentType").toString();

const json = JSON.stringify({ id, name, email, membershipType });
const json = JSON.stringify({ id, name, email, membershipType, paymentType });

const encoder = new TextEncoder();
const encryptionKey = encoder.encode(ENCRYPTION_KEY);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/utils/googleSheets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { google } from "googleapis";
import { GOOGLE_SHEETS_KEY, GOOGLE_SHEETS_EMAIL, GOOGLE_SHEETS_ID } from '$env/static/private';
import type { PaymentType } from "./vipps";

export async function saveMemberToGoogleSheet(formData: FormData) {
const name = formData.get("name").toString();
Expand Down Expand Up @@ -55,7 +56,7 @@ export async function saveMemberToGoogleSheet(formData: FormData) {
}


export async function addPaymentDetailsToRegistration(id: number, pspReference: string) {
export async function addPaymentDetailsToRegistration(id: number, pspReference: string, paymentType: PaymentType) {
let jwtClient = new google.auth.JWT(
GOOGLE_SHEETS_EMAIL,
null,
Expand Down Expand Up @@ -92,6 +93,7 @@ export async function addPaymentDetailsToRegistration(id: number, pspReference:
if (row) {
row.push(pspReference);
row.push(new Date());
row.push(paymentType);
// Update the sheet
await sheets.spreadsheets.values.update({
auth: jwtClient,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/membership/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const actions = {
payWithVipps: async (event) => {
const formData = await event.request.formData();
formData.append("id", crypto.randomUUID());
formData.append("paymentType", PaymentType.Vipps);
saveMemberToGoogleSheet(formData);
const encryptedFormData = await encryptFormData(formData);

Expand All @@ -25,6 +26,7 @@ export const actions = {
payWithCard: async (event) => {
const formData = await event.request.formData();
formData.append("id", crypto.randomUUID());
formData.append("paymentType", PaymentType.Card);
saveMemberToGoogleSheet(formData);
const encryptedFormData = await encryptFormData(formData);

Expand Down
4 changes: 2 additions & 2 deletions src/routes/membership/registrationComplete/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function load({ url }) {

try {
const decryptedJson = await decryptFormData(encryptedData);
const { id, name, email, membershipType } = decryptedJson;
const { id, name, paymentType } = decryptedJson;
const vippsToken = await getVippsAccessToken();
const paymentStatus = await getPaymentStatus(id, vippsToken.access_token);
const pspReference = paymentStatus.pspReference;
Expand All @@ -36,7 +36,7 @@ export async function load({ url }) {
await new Promise(resolve => setTimeout(resolve, 3000));
console.info(`Capturing payment for ${name} with id ${id}`);
await capturePayment(id, amount, vippsToken.access_token);
await addPaymentDetailsToRegistration(id, pspReference);
await addPaymentDetailsToRegistration(id, pspReference, paymentType);
}

return { name };
Expand Down

0 comments on commit a8021ec

Please sign in to comment.