11import { NextRequest , NextResponse } from "next/server" ;
22import type { Stripe } from "stripe" ;
33import { auth } from "../../../../auth" ;
4- import { checkUserPurchase } from "@/lib/supabase/utils" ;
5- import { CURRENT_VERSION , METADATA_KEYS , stripe } from "@/lib/stripe" ;
4+ // import { checkUserPurchase } from "@/lib/supabase/utils";
5+ import {
6+ CURRENT_VERSION ,
7+ METADATA_KEYS ,
8+ stripe ,
9+ } from "@/lib/stripe" ;
610
711// コンテンツ購入用のStripeセッションを作成
812async function createCheckoutSession (
@@ -22,32 +26,33 @@ async function createCheckoutSession(
2226 ? `${ APP_BASE_URL } /books/${ contentId } ?success=true`
2327 : `${ APP_BASE_URL } /posts/${ contentId } ?success=true` ;
2428
25- const sessionParams : Stripe . Checkout . SessionCreateParams = {
26- payment_method_types : [ "card" ] ,
27- line_items : [
28- {
29- price_data : {
30- currency : "jpy" ,
31- product_data : {
32- name : title ,
33- description : `コンテンツID: ${ contentId } ` ,
29+ const sessionParams : Stripe . Checkout . SessionCreateParams =
30+ {
31+ payment_method_types : [ "card" ] ,
32+ line_items : [
33+ {
34+ price_data : {
35+ currency : "jpy" ,
36+ product_data : {
37+ name : title ,
38+ description : `コンテンツID: ${ contentId } ` ,
39+ } ,
40+ unit_amount : price ,
3441 } ,
35- unit_amount : price ,
42+ quantity : 1 ,
3643 } ,
37- quantity : 1 ,
44+ ] ,
45+ mode : "payment" ,
46+ // webhookで使用するためのメタデータを記載
47+ metadata : {
48+ [ METADATA_KEYS . USER_IDENTIFIER ] : userIdentifier ,
49+ [ METADATA_KEYS . CONTENT_ID ] : contentId ,
50+ [ METADATA_KEYS . PRICE ] : price . toString ( ) ,
51+ [ METADATA_KEYS . VERSION ] : CURRENT_VERSION ,
3852 } ,
39- ] ,
40- mode : "payment" ,
41- // webhookで使用するためのメタデータを記載
42- metadata : {
43- [ METADATA_KEYS . USER_IDENTIFIER ] : userIdentifier ,
44- [ METADATA_KEYS . CONTENT_ID ] : contentId ,
45- [ METADATA_KEYS . PRICE ] : price . toString ( ) ,
46- [ METADATA_KEYS . VERSION ] : CURRENT_VERSION ,
47- } ,
48- success_url : successUrl ,
49- cancel_url : APP_BASE_URL ,
50- } ;
53+ success_url : successUrl ,
54+ cancel_url : APP_BASE_URL ,
55+ } ;
5156
5257 return stripe . checkout . sessions . create ( sessionParams ) ;
5358}
@@ -65,39 +70,51 @@ export async function POST(req: NextRequest) {
6570 const session = await auth ( ) ;
6671
6772 if ( ! session || ! session . user ) {
68- return new NextResponse ( "認証が必要です" , { status : 401 } ) ;
73+ return new NextResponse ( "認証が必要です" , {
74+ status : 401 ,
75+ } ) ;
6976 }
7077
71- const { contentId, price, title, contentType } : RequestData =
72- await req . json ( ) ;
78+ const {
79+ contentId,
80+ price,
81+ title,
82+ contentType,
83+ } : RequestData = await req . json ( ) ;
7384
7485 if ( ! contentId || ! price || ! title || ! contentType ) {
75- return new NextResponse ( "必要な情報が不足しています" , { status : 400 } ) ;
86+ return new NextResponse (
87+ "必要な情報が不足しています" ,
88+ { status : 400 }
89+ ) ;
7690 }
7791
7892 // ユーザー識別子を取得 (GitHubのID)
7993 const userIdentifier = session . user . id ;
8094
8195 if ( ! userIdentifier ) {
82- return new NextResponse ( "ユーザー識別子が取得できません" , {
83- status : 400 ,
84- } ) ;
96+ return new NextResponse (
97+ "ユーザー識別子が取得できません" ,
98+ {
99+ status : 400 ,
100+ }
101+ ) ;
85102 }
86103
87104 // すでに購入済みの場合、リダイレクトさせる
88- const existingPurchase = await checkUserPurchase ( userIdentifier , contentId ) ;
89- // 既存の購入記録があれば処理をスキップ
90- if ( existingPurchase ) {
91- console . log (
92- `既に購入済み: ユーザー=${ userIdentifier } , コンテンツ=${ contentId } `
93- ) ;
94- return NextResponse . json ( {
95- url :
96- contentType === "book"
97- ? `${ process . env . NEXT_PUBLIC_BASE_URL } /books/${ contentId } `
98- : `${ process . env . NEXT_PUBLIC_BASE_URL } /posts/${ contentId } ` ,
99- } ) ;
100- }
105+ // const existingPurchase = await checkUserPurchase(userIdentifier, contentId);
106+ // // 既存の購入記録があれば処理をスキップ
107+ // if (existingPurchase) {
108+ // console.log(
109+ // `既に購入済み: ユーザー=${userIdentifier}, コンテンツ=${contentId}`
110+ // );
111+ // return NextResponse.json({
112+ // url:
113+ // contentType === "book"
114+ // ? `${process.env.NEXT_PUBLIC_BASE_URL}/books/${contentId}`
115+ // : `${process.env.NEXT_PUBLIC_BASE_URL}/posts/${contentId}`,
116+ // });
117+ // }
101118
102119 // Stripeのセッションを作成
103120 const stripeSession = await createCheckoutSession (
@@ -115,6 +132,8 @@ export async function POST(req: NextRequest) {
115132 } ) ;
116133 } catch ( error ) {
117134 console . error ( "Checkout session error:" , error ) ;
118- return new NextResponse ( "内部エラーが発生しました" , { status : 500 } ) ;
135+ return new NextResponse ( "内部エラーが発生しました" , {
136+ status : 500 ,
137+ } ) ;
119138 }
120- }
139+ }
0 commit comments