Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ZapPlanner Subscription Budget Calculation and Interval Enforcement (Fixes #60) #61

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions app/confirm/components/ConfirmSubscriptionForm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use client";

import { useForm } from "react-hook-form";
import { CreateSubscriptionRequest } from "types/CreateSubscriptionRequest";
import { useRouter } from "next/navigation";
import { CreateSubscriptionResponse } from "types/CreateSubscriptionResponse";
import React from "react";
import React, { useEffect, useState } from "react";
import { webln } from "@getalby/sdk";
import { UnconfirmedSubscription } from "types/UnconfirmedSubscription";
import { isValidNostrConnectUrl } from "lib/validation";
Expand Down Expand Up @@ -42,7 +40,7 @@ export function ConfirmSubscriptionForm({
},
});

const [isNavigating, setNavigating] = React.useState(false);
const [isNavigating, setNavigating] = useState(false);
const { push } = useRouter();
const hasLinkedWallet = !!watch("nostrWalletConnectUrl");

Expand Down Expand Up @@ -73,19 +71,47 @@ export function ConfirmSubscriptionForm({
}
};

const getSatoshisForInterval = (interval: string) => {
// Calculate the number of satoshis based on the desired interval
switch (interval) {
case "daily":
return 1; // Adjust this as needed
case "weekly":
return 7; // Adjust this as needed
case "monthly":
return 30; // Adjust this as needed
default:
return 1; // Default to daily
}
};

const onSubmit = handleSubmit(async (data) => {
if (!data.nostrWalletConnectUrl) {
toast.error("Please link your wallet");
return;
}

const selectedInterval = data.sleepDuration;

// Enforce that the interval should not be more than 1 year (365 days)
if (selectedInterval === "yearly") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the duration in milliseconds. Otherwise this will let through e.g. 100 weeks or 1000000000 hours

toast.error("Interval cannot be more than 1 year");
return;
}

// Calculate the number of satoshis based on the desired interval
const satoshis = getSatoshisForInterval(selectedInterval);

data.amount = satoshis;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this is incorrect. We need to pass a max_amount field to https://github.com/getAlby/nostr-wallet-connect based on the amount and interval the user sets in ZapPlanner, not change the amount for the subscription. Please see the original ticket: #60


const subscriptionId = await createSubscription(data);
if (subscriptionId) {
toast.success("Recurring payment created");
setNavigating(true);
push(
`/subscriptions/${subscriptionId}${
returnUrl ? `?returnUrl=${returnUrl}` : ""
}`,
}`
);
}
});
Expand Down Expand Up @@ -255,7 +281,7 @@ export function ConfirmSubscriptionForm({
}

async function createSubscription(
createSubscriptionRequest: CreateSubscriptionRequest,
createSubscriptionRequest: CreateSubscriptionRequest
): Promise<string | undefined> {
const res = await fetch("/api/subscriptions", {
method: "POST",
Expand All @@ -267,7 +293,6 @@ async function createSubscription(
toast.error(res.status + " " + res.statusText);
return undefined;
}
const createSubscriptionResponse =
(await res.json()) as CreateSubscriptionResponse;
const createSubscriptionResponse = (await res.json()) as CreateSubscriptionResponse;
return createSubscriptionResponse.subscriptionId;
}