// A purchase may be paid but queued. Only its actual service interval grants access. export function activePass(order, now=Date.now()) { return Boolean(order && order.status==='paid' && Number.isSafeInteger(order.starts) && Number.isSafeInteger(order.ends) && order.starts<=now && now? ORDER BY ends DESC LIMIT 1").get(uid,now,now); return activePass(row,now)?row:null; } export function cachedAccess(cached, now=Date.now()) { return Boolean(cached?.value && now-cached.at<60000 && (cached.value.until==null || now/^pay_[A-Za-z0-9]+$/.test(p.id||'') && p.order_id===local.id && p.status==='captured' && p.amount===local.amount && p.currency==='INR' && !(p.amount_refunded>0)) || null; } // A refund ends a one-time pass only when Razorpay's own payment record (fetched, never the webhook body) shows the // pass's payment fully refunded. Partial refunds keep the pass. export function refundRevokes(local, payment) { return Boolean(local && local.status==='paid' && payment && payment.id===local.payment && payment.order_id===local.id && (payment.status==='refunded' || payment.amount_refunded>=local.amount)); } // A subscription gives access while Razorpay says it is active (or pending a retry). Once it is finished, access lasts // to the end of the paid period, unless it was ended earlier on purpose (cancelled now, e.g. after a refund: ended_at). export function subscriptionPaidUp(sub, now=Date.now()) { if(['active','pending'].includes(sub?.status))return true; if(!['cancelled','completed','expired'].includes(sub?.status) || !(sub.paid_count>0))return false; return Math.min(sub.current_end||0, sub.ended_at||Infinity)*1000>now; } // Still renews: not finished, and no cancellation scheduled (by Razorpay or recorded by us). export function renews(sub, row={}) { return Boolean(sub && !['cancelled','completed','expired'].includes(sub.status) && !sub.has_scheduled_changes && !row.cancel_at); } // When a plan switch or a resubscription starts: the end of the current paid period (Unix seconds). Null when that is // under 10 minutes away or already past, so the buyer is asked to wait for the renewal instead of racing it. export function successorStart(sub, now=Date.now()) { const end=Number(sub?.current_end); return Number.isSafeInteger(end) && end*1000-now>10*60e3 ? end : null; }