-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
Showing
5 changed files
with
176 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { json } from '@remix-run/cloudflare'; | ||
import { db } from '~/lib/db.server'; | ||
import { requireUserId } from '~/lib/session.server'; | ||
|
||
export async function action({ request }) { | ||
const userId = await requireUserId(request); | ||
const { planId, billingCycle } = await request.json(); | ||
|
||
try { | ||
// 开始数据库事务 | ||
await db.transaction(async (trx) => { | ||
// 获取订阅计划详情 | ||
const plan = await trx('subscription_plans').where('_id', planId).first(); | ||
if (!plan) { | ||
throw new Error('Invalid subscription plan'); | ||
} | ||
|
||
// 计算实际价格和代币数量 | ||
const price = billingCycle === 'yearly' ? plan.price * 10 : plan.price; | ||
const tokens = billingCycle === 'yearly' ? plan.tokens * 12 : plan.tokens; | ||
|
||
// 创建交易记录 | ||
await trx('user_transactions').insert({ | ||
user_id: userId, | ||
type: 'subscription', | ||
plan_id: planId, | ||
amount: price, | ||
tokens: tokens, | ||
status: 'completed', // 假设支付已完成 | ||
payment_method: 'credit_card', // 假设使用信用卡支付 | ||
transaction_id: `sub_${Date.now()}`, // 生成一个简单的交易ID | ||
}); | ||
|
||
// 这里可以添加更多逻辑,如更新用户的订阅状态等 | ||
}); | ||
|
||
return json({ success: true, message: '订阅购买成功' }); | ||
} catch (error) { | ||
console.error('Error purchasing subscription:', error); | ||
return json({ error: 'Failed to purchase subscription' }, { status: 500 }); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { json } from '@remix-run/cloudflare'; | ||
import { db } from '~/lib/db.server'; | ||
|
||
export async function loader() { | ||
try { | ||
const plans = await db.select().from('subscription_plans').where('is_active', true); | ||
return json(plans); | ||
} catch (error) { | ||
console.error('Error fetching subscription plans:', error); | ||
return json({ error: 'Failed to fetch subscription plans' }, { status: 500 }); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { json } from '@remix-run/cloudflare'; | ||
import { db } from '~/lib/db.server'; | ||
import { requireUserId } from '~/lib/session.server'; | ||
|
||
export async function loader({ request }) { | ||
const userId = await requireUserId(request); | ||
try { | ||
const userSubscription = await db.select( | ||
'subscription_plans.*', | ||
'user_transactions.tokens as tokensLeft', | ||
db.raw('DATE_ADD(user_transactions._create, INTERVAL 1 MONTH) as nextReloadDate') | ||
) | ||
.from('user_transactions') | ||
.join('subscription_plans', 'user_transactions.plan_id', 'subscription_plans._id') | ||
.where('user_transactions.user_id', userId) | ||
.orderBy('user_transactions._create', 'desc') | ||
.first(); | ||
|
||
return json(userSubscription); | ||
} catch (error) { | ||
console.error('Error fetching user subscription:', error); | ||
return json({ error: 'Failed to fetch user subscription' }, { status: 500 }); | ||
} | ||
} |