recurr.
Next.js

createBillingRouter

Tüm billing endpoint'lerini tek bir Next.js route handler ile yönetin

Kullanım

app/api/billing/[...bp]/route.ts
import { createBillingRouter } from '@vinenastudio/recurr/nextjs'
import { adapter, subscriptionManager, paytrClient, management } from '@/lib/subscriptions'

const router = createBillingRouter({
  client: paytrClient,
  adapter,
  subscriptionManager,
  secret: process.env.CRON_SECRET!,
  appUrl: process.env.NEXT_PUBLIC_APP_URL!,
  successPath: '/billing/success',
  management,                    // opsiyonel — yönetim panelini korumak için
  onPaymentSuccess: async ({ subscriber, payment }) => {
    await sendWelcomeEmail(subscriber.email)
  },
  onPaymentFailure: async ({ subscriber, payment, reason }) => {
    await sendPaymentFailedEmail(subscriber?.email, reason)
  },
  onSubscriptionCancelled: async ({ subscriber }) => {
    await revokeAccess(subscriber.email)
  },
})

export const GET = router.GET
export const POST = router.POST

Endpoint Referansı

MethodSegmentAuthAçıklama
GET/plansTüm planları JSON olarak döner
POST/plansCookieYeni plan oluşturur (yönetim)
POST/checkout{ planId, email } → Direct API form alanları
POST/webhookPayTR webhook handler (daima "OK" döner)
GET/cronBearerVadesi gelen ödemeleri işler
GET/subscriber?email= → abone durumu
POST/cancel{ email } → aboneliği iptal et
GET/subscribersCookieTüm aboneler (yönetim)
GET/paymentsCookieTüm ödemeler (yönetim)
POST/management-login{ username, password }bp_mgmt cookie
POST/management-logoutbp_mgmt cookie'yi temizler

Callbacks

CallbackNe Zaman Tetiklenir
onPaymentSuccessWebhook ile başarılı ödeme onaylandığında
onPaymentFailureÖdeme başarısız olduğunda (senkron veya webhook)
onSubscriptionCancelledMaksimum yeniden deneme aşıldığında otomatik iptal

Webhook handler her zaman "OK" plain text döndürmek zorundadır. PayTR bu yanıtı almazsa webhook'u yeniden gönderir.

On this page