forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-payment.ts
More file actions
168 lines (146 loc) · 4.63 KB
/
Copy pathuse-payment.ts
File metadata and controls
168 lines (146 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import i18next from 'i18next'
import { useState, useCallback } from 'react'
import { toast } from 'sonner'
import {
calculateAmount,
calculateStripeAmount,
calculateWaffoAmount,
calculateWaffoPancakeAmount,
requestPayment,
requestStripePayment,
isApiSuccess,
} from '../api'
import {
isStripePayment,
isWaffoPayment,
isWaffoPancakePayment,
submitPaymentForm,
} from '../lib'
import type { AmountRequest, AmountResponse } from '../types'
// ============================================================================
// Payment Hook
// ============================================================================
type AmountCalculator = (request: AmountRequest) => Promise<AmountResponse>
export interface PaymentAmountCalculators {
regular: AmountCalculator
stripe: AmountCalculator
waffo: AmountCalculator
waffoPancake: AmountCalculator
}
const defaultPaymentAmountCalculators: PaymentAmountCalculators = {
regular: calculateAmount,
stripe: calculateStripeAmount,
waffo: calculateWaffoAmount,
waffoPancake: calculateWaffoPancakeAmount,
}
export async function requestPaymentAmount(
topupAmount: number,
paymentType: string,
calculators: PaymentAmountCalculators = defaultPaymentAmountCalculators
): Promise<number> {
let calculator = calculators.regular
if (isStripePayment(paymentType)) {
calculator = calculators.stripe
} else if (isWaffoPayment(paymentType)) {
calculator = calculators.waffo
} else if (isWaffoPancakePayment(paymentType)) {
calculator = calculators.waffoPancake
}
const response = await calculator({ amount: topupAmount })
if (!isApiSuccess(response) || !response.data) {
return 0
}
return Number.parseFloat(response.data)
}
export function usePayment() {
const [amount, setAmount] = useState<number>(0)
const [calculating, setCalculating] = useState(false)
const [processing, setProcessing] = useState(false)
// Calculate payment amount
const calculatePaymentAmount = useCallback(
async (topupAmount: number, paymentType: string) => {
try {
setCalculating(true)
const calculatedAmount = await requestPaymentAmount(
topupAmount,
paymentType
)
setAmount(calculatedAmount)
return calculatedAmount
} catch {
setAmount(0)
return 0
} finally {
setCalculating(false)
}
},
[]
)
// Process payment
const processPayment = useCallback(
async (topupAmount: number, paymentType: string) => {
try {
setProcessing(true)
const isStripe = isStripePayment(paymentType)
const amount = Math.floor(topupAmount)
const response = isStripe
? await requestStripePayment({
amount,
payment_method: 'stripe',
})
: await requestPayment({
amount,
payment_method: paymentType,
})
if (!isApiSuccess(response)) {
toast.error(response.message || i18next.t('Payment request failed'))
return false
}
// Handle Stripe payment
if (isStripe && response.data?.pay_link) {
window.open(response.data.pay_link as string, '_blank')
toast.success(i18next.t('Redirecting to payment page...'))
return true
}
// Handle non-Stripe payment
if (!isStripe && response.data) {
const url = (response as unknown as { url?: string }).url
if (url) {
submitPaymentForm(url, response.data)
toast.success(i18next.t('Redirecting to payment page...'))
return true
}
}
return false
} catch {
toast.error(i18next.t('Payment request failed'))
return false
} finally {
setProcessing(false)
}
},
[]
)
return {
amount,
calculating,
processing,
calculatePaymentAmount,
processPayment,
setAmount,
}
}