11import { paymentService } from '../services/payment-service.js' ;
22import { errorUtils } from '../utils/error-utils.js' ;
3+ import { supabase } from '../utils/supabase.js' ;
4+ import {
5+ getBitcoinAddressBalance ,
6+ getEthereumAddressBalance ,
7+ getSolanaAddressBalance ,
8+ getUsdcAddressBalance
9+ } from '../utils/tatum.js' ;
310
411/**
512 * Route handler for creating a subscription
@@ -125,16 +132,25 @@ export async function paymentCallbackHandler(c) {
125132 * @param {Object } c - Hono context
126133 * @returns {Response } - JSON response with subscription status
127134 */
135+ /**
136+ * Route handler for checking subscription status and verifying cryptocurrency payments
137+ * @param {Object } c - Hono context
138+ * @returns {Response } - JSON response with subscription status
139+ */
128140export async function subscriptionStatusHandler ( c ) {
129141 try {
130- const { email } = await c . req . json ( ) ;
142+ // Get request body or parameters
143+ const body = await c . req . json ( ) ;
144+ const { id, email } = body ;
131145
132- if ( ! email ) {
133- return c . json ( { error : 'Email is required' } , 400 ) ;
146+ if ( ! id && ! email ) {
147+ return c . json ( { error : 'Subscription ID or email is required' } , 400 ) ;
134148 }
135149
136- // Get subscription details
137- const subscription = await paymentService . getSubscription ( email ) ;
150+ // Get subscription details from the database
151+ const subscription = id
152+ ? await paymentService . getSubscriptionById ( id )
153+ : await paymentService . getSubscription ( email ) ;
138154
139155 if ( ! subscription ) {
140156 return c . json ( {
@@ -143,12 +159,90 @@ export async function subscriptionStatusHandler(c) {
143159 } ) ;
144160 }
145161
146- // Check if subscription is active
147- const isActive = subscription . status === 'active' &&
162+ console . log ( 'Retrieved subscription:' , JSON . stringify ( subscription ) ) ;
163+
164+ // Check cryptocurrency balance using Tatum API to verify payment
165+ let paymentVerified = false ;
166+ let balance = null ;
167+ let requiredAmount = subscription . crypto_amount || 0 ;
168+
169+ try {
170+ const paymentAddress = subscription . payment_address ;
171+ const cryptoCurrency = subscription . crypto_currency ;
172+
173+ console . log ( `Verifying payment for ${ cryptoCurrency } at address ${ paymentAddress } ` ) ;
174+ console . log ( `Required amount: ${ requiredAmount } ${ cryptoCurrency } ` ) ;
175+
176+ // Check balance for the appropriate cryptocurrency using Tatum API
177+ switch ( cryptoCurrency ) {
178+ case 'btc' :
179+ balance = await getBitcoinAddressBalance ( paymentAddress ) ;
180+ break ;
181+ case 'eth' :
182+ balance = await getEthereumAddressBalance ( paymentAddress ) ;
183+ break ;
184+ case 'sol' :
185+ balance = await getSolanaAddressBalance ( paymentAddress ) ;
186+ break ;
187+ case 'usdc' :
188+ balance = await getUsdcAddressBalance ( paymentAddress ) ;
189+ break ;
190+ default :
191+ throw new Error ( `Unsupported cryptocurrency: ${ cryptoCurrency } ` ) ;
192+ }
193+
194+ console . log ( `Retrieved balance from Tatum:` , balance ) ;
195+
196+ // Parse the balance based on cryptocurrency type
197+ let actualBalance = 0 ;
198+ if ( cryptoCurrency === 'btc' ) {
199+ actualBalance = parseFloat ( balance . incoming ) - parseFloat ( balance . outgoing ) ;
200+ } else if ( cryptoCurrency === 'eth' || cryptoCurrency === 'usdc' ) {
201+ actualBalance = parseFloat ( balance . balance ) ;
202+ } else if ( cryptoCurrency === 'sol' ) {
203+ actualBalance = parseFloat ( balance . balance ) ;
204+ }
205+
206+ console . log ( `Actual balance: ${ actualBalance } ${ cryptoCurrency } ` ) ;
207+
208+ // Verify if payment meets or exceeds the required amount
209+ if ( actualBalance >= requiredAmount ) {
210+ paymentVerified = true ;
211+ console . log ( `Payment verified: ${ actualBalance } ${ cryptoCurrency } >= ${ requiredAmount } ${ cryptoCurrency } ` ) ;
212+
213+ // Update subscription status to active if payment is verified
214+ if ( subscription . status !== 'active' ) {
215+ await supabase
216+ . from ( 'subscriptions' )
217+ . update ( {
218+ status : 'active' ,
219+ updated_at : new Date ( ) . toISOString ( ) ,
220+ last_payment_date : new Date ( ) . toISOString ( )
221+ } )
222+ . eq ( 'id' , subscription . id ) ;
223+
224+ console . log ( `Subscription ${ subscription . id } status updated to active` ) ;
225+
226+ // Update the subscription object to reflect the changes
227+ subscription . status = 'active' ;
228+ subscription . updated_at = new Date ( ) . toISOString ( ) ;
229+ subscription . last_payment_date = new Date ( ) . toISOString ( ) ;
230+ }
231+ } else {
232+ console . log ( `Payment verification failed: ${ actualBalance } ${ cryptoCurrency } < ${ requiredAmount } ${ cryptoCurrency } ` ) ;
233+ }
234+ } catch ( verificationError ) {
235+ console . error ( 'Error verifying payment:' , verificationError ) ;
236+ }
237+
238+ // Check if subscription is active based on status and expiration date
239+ const isActive = ( subscription . status === 'active' || paymentVerified ) &&
148240 new Date ( subscription . expiration_date ) > new Date ( ) ;
149241
150242 return c . json ( {
151243 has_subscription : isActive ,
244+ payment_verified : paymentVerified ,
245+ balance : balance ,
152246 subscription : {
153247 id : subscription . id ,
154248 email : subscription . email ,
@@ -159,10 +253,16 @@ export async function subscriptionStatusHandler(c) {
159253 start_date : subscription . start_date ,
160254 expiration_date : subscription . expiration_date ,
161255 payment_method : subscription . payment_method ,
256+ payment_address : subscription . payment_address ,
257+ crypto_amount : subscription . crypto_amount ,
258+ crypto_currency : subscription . crypto_currency ,
259+ exchange_rate_usd : subscription . exchange_rate_usd ,
260+ last_payment_date : subscription . last_payment_date ,
162261 is_active : isActive
163262 }
164263 } ) ;
165264 } catch ( error ) {
265+ console . error ( 'Error in subscription status handler:' , error ) ;
166266 return errorUtils . handleError ( error , c ) ;
167267 }
168268}
0 commit comments