Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 10 additions & 5 deletions app/api/subscriptions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ export async function POST(request: Request) {
const { amount, nostrWalletConnectUrl, sleepDuration, cronExpression } =
createSubscriptionRequest;

// Validate amount based on currency type
const isSatsCurrency = createSubscriptionRequest.currency === SATS_CURRENCY;
const numAmount = isSatsCurrency ? parseInt(amount) : parseFloat(amount);

if (
(!sleepDuration && !cronExpression) ||
!isValidPositiveValue(parseInt(amount)) ||
!isValidPositiveValue(numAmount) ||
!isValidNostrWalletConnectUrl(nostrWalletConnectUrl)
) {
return new Response("One or more invalid subscription fields", {
Expand Down Expand Up @@ -70,7 +74,8 @@ export async function POST(request: Request) {
}
}

let satoshi = +amount;
// Use the correctly parsed amount based on currency type
let satoshi = numAmount;
if (
createSubscriptionRequest.currency &&
createSubscriptionRequest.currency !== SATS_CURRENCY
Expand All @@ -87,7 +92,7 @@ export async function POST(request: Request) {
}

satoshi = await fiat.getSatoshiValue({
amount: createSubscriptionRequest.amount,
amount: parseFloat(createSubscriptionRequest.amount),
currency: createSubscriptionRequest.currency,
});
}
Expand All @@ -105,8 +110,8 @@ export async function POST(request: Request) {

const subscription = await prismaClient.subscription.create({
data: {
amount: parseInt(createSubscriptionRequest.amount),
currency: createSubscriptionRequest.currency,
amount: Math.round(satoshi),
currency: SATS_CURRENCY, // Always store as SATS since amount is in sats
recipientLightningAddress:
createSubscriptionRequest.recipientLightningAddress,
message: createSubscriptionRequest.message,
Expand Down
11 changes: 8 additions & 3 deletions app/confirm/components/SubscriptionSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { formatDistance, add, max } from "date-fns";
import ms from "ms";
import { MAX_RETRIES } from "lib/constants";
import { MAX_RETRIES, SATS_CURRENCY } from "lib/constants";

type SubscriptionSummaryProps = {
values: {
Expand Down Expand Up @@ -36,7 +36,9 @@ export function SubscriptionSummary({
right={
<div>
<span className="mono">
{parseInt(values.amount).toLocaleString()}
{values.currency === SATS_CURRENCY
? parseInt(values.amount).toLocaleString()
: parseFloat(values.amount).toLocaleString()}
</span>{" "}
{values.currency}
</div>
Expand Down Expand Up @@ -145,7 +147,10 @@ export function SubscriptionSummary({
<SubscriptionSummaryItem
left={`Total ${values.currency} sent`}
right={`${
(values.numSuccessfulPayments || 0) * parseInt(values.amount)
(values.numSuccessfulPayments || 0) *
(values.currency === SATS_CURRENCY
? parseInt(values.amount)
: parseFloat(values.amount))
}`}
/>
)}
Expand Down
22 changes: 19 additions & 3 deletions app/create/components/CreateSubscriptionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,25 @@ export function CreateSubscriptionForm() {
<input
{...register("amount", {
validate: (value) => {
return !isValidPositiveValue(parseInt(value))
? "Please enter a positive value"
: undefined;
// Use parseInt for sats (no decimals), parseFloat for fiat currencies
const numValue =
watchedCurrency === SATS_CURRENCY
? parseInt(value)
: parseFloat(value);

if (!isValidPositiveValue(numValue)) {
return "Please enter a positive value";
}

// Additional validation for sats to prevent decimals
if (
watchedCurrency === SATS_CURRENCY &&
value.includes(".")
) {
return "Millisatoshis (msat) are not supported. Please enter a whole number of sats.";
}

return undefined;
},
})}
className="zp-input flex-1"
Expand Down