-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8a2e20
commit bd3c667
Showing
6 changed files
with
48 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,34 @@ | ||
import { HttpClient, HttpClientRequest, HttpClientResponse } from '@effect/platform' | ||
import { Schema } from '@effect/schema' | ||
import { Effect, Schedule, pipe } from 'effect' | ||
import type { NextRequest } from 'next/server' | ||
|
||
const Order = Schema.Struct({ | ||
data: Schema.optional( | ||
Schema.Struct({ | ||
attributes: Schema.Struct({ | ||
refunded: Schema.Boolean, | ||
}), | ||
}), | ||
), | ||
}) | ||
|
||
const { FIGMA_KIT_URL, LEMON_SQUEEZY_API_KEY } = process.env | ||
|
||
export const GET = async (req: NextRequest) => { | ||
const { searchParams } = new URL(req.url) | ||
const orderId = searchParams.get('order_id') | ||
|
||
if (!orderId) { | ||
return Response.redirect('https://park-ui.com') | ||
} | ||
|
||
try { | ||
const response = await fetch(`https://api.lemonsqueezy.com/v1/orders/${orderId}`, { | ||
headers: { | ||
Accept: 'application/vnd.api+json', | ||
'Content-Type': 'application/vnd.api+json', | ||
Authorization: `Bearer ${LEMON_SQUEEZY_API_KEY}`, | ||
}, | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error('Network response was not ok') | ||
} | ||
|
||
const data = await response.json() | ||
|
||
const isValid = data.data?.attributes?.refunded === false | ||
|
||
const isValid = await Effect.runPromise( | ||
pipe( | ||
Effect.fromNullable(searchParams.get('order_id')), | ||
Effect.flatMap((orderId) => | ||
pipe( | ||
HttpClientRequest.get(`https://api.lemonsqueezy.com/v1/orders/${orderId}`, { | ||
headers: { | ||
Accept: 'application/vnd.api+json', | ||
'Content-Type': 'application/vnd.api+json', | ||
Authorization: `Bearer ${LEMON_SQUEEZY_API_KEY}`, | ||
}, | ||
}), | ||
HttpClient.fetchOk, | ||
HttpClientResponse.schemaBodyJsonScoped(Order), | ||
Effect.timeout('1 seconds'), | ||
Effect.retry(Schedule.exponential(200).pipe(Schedule.compose(Schedule.recurs(3)))), | ||
), | ||
), | ||
Effect.map((data) => !data.data?.attributes.refunded), | ||
Effect.catchAll(() => Effect.succeed(false)), | ||
), | ||
) | ||
|
||
return Response.redirect(isValid ? FIGMA_KIT_URL : 'https://park-ui.com') | ||
return Response.redirect(isValid ? FIGMA_KIT_URL : 'https://park-ui.com') | ||
} catch (error) { | ||
return Response.redirect('https://park-ui.com') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters