Base 구독을 통해 자동 USDC 결제를 수락하여 예측 가능한 반복 수익 흐름을 구축할 수 있습니다. SaaS 플랫폼, 콘텐츠 구독 서비스, 또는 정기 결제가 필요한 비즈니스 모델을 운영하든 관계없이, Base 구독은 가맹점 수수료 없이 원활한 솔루션을 제공합니다.주요 기능:
import { base } from '@base-org/account/node';// Backend setup (Node.js only)// Set CDP credentials as environment variables:// CDP_API_KEY_ID, CDP_API_KEY_SECRET, CDP_WALLET_SECRET// PAYMASTER_URL (recommended for gasless transactions)async function setupSubscriptionWallet() { try { // Create or retrieve your subscription owner wallet (CDP smart wallet) const wallet = await base.subscription.getOrCreateSubscriptionOwnerWallet({ walletName: 'my-app-subscriptions' // Optional: customize wallet name }); console.log('✅ Subscription owner wallet ready!'); console.log(`Smart Wallet Address: ${wallet.address}`); console.log(`Wallet Name: ${wallet.walletName}`); // Make this address available to your frontend // Option 1: Store in database/config // Option 2: Expose via API endpoint // Option 3: Set as public environment variable (e.g., NEXT_PUBLIC_SUBSCRIPTION_OWNER) return wallet; } catch (error) { console.error('Failed to setup wallet:', error.message); throw error; }}// Run once at application startupsetupSubscriptionWallet();// Optional: Provide an API endpoint for the frontend to fetch the addressexport async function getSubscriptionOwnerAddress() { const wallet = await base.subscription.getOrCreateSubscriptionOwnerWallet(); return wallet.address;}
백엔드 전용: 이 설정은 CDP 자격 증명을 가진 Node.js 백엔드에서 실행됩니다. 결과 지갑 주소는 공개적이며 subscribe() 호출 시 사용하기 위해 프론트엔드에 안전하게 공유할 수 있습니다.
기본적으로 청구된 USDC는 구독 소유자 지갑에 남아 있습니다. 선택적으로 recipient 주소를 지정하여 자금을 다른 주소로 자동 이체할 수 있습니다:
기본값 (소유자 지갑 보관)
트레저리 지갑으로 전송
동적 수신자
// Funds stay in the subscription owner walletconst result = await base.subscription.charge({ id: subscriptionId, amount: 'max-remaining-charge', testnet: false});// USDC is now in your CDP smart wallet// Access it later or transfer as needed
// Automatically send to your treasury walletconst result = await base.subscription.charge({ id: subscriptionId, amount: 'max-remaining-charge', recipient: '0xYourTreasuryAddress', testnet: false});// USDC is sent directly to the recipient addressconsole.log(`Sent ${result.amount} to ${result.recipient}`);
// Send to different addresses based on subscription typeasync function chargeWithRecipient(subscriptionId: string, plan: string) { const recipients = { premium: '0xPremiumTreasuryAddress', basic: '0xBasicTreasuryAddress', enterprise: '0xEnterpriseTreasuryAddress' }; return await base.subscription.charge({ id: subscriptionId, amount: 'max-remaining-charge', recipient: recipients[plan], testnet: false });}
트랜잭션 실행에 대한 수동 제어가 필요하거나 기존 지갑 인프라와 통합하려는 개발자를 위해 하위 수준의 유틸리티를 사용할 수 있습니다:
prepareCharge - 수동 청구 실행
CDP 지갑을 사용할 수 없는 경우, prepareCharge()는 수동으로 실행할 수 있는 호출 데이터를 제공합니다:
import { base } from '@base-org/account';// Prepare charge call dataconst chargeCalls = await base.subscription.prepareCharge({ id: subscriptionId, amount: 'max-remaining-charge', testnet: false});// Execute with your own wallet infrastructure// (requires custom wallet client setup)
자세한 내용은 이 섹션의 prepareCharge() 예시를 참고하세요.
prepareRevoke - 수동 취소 실행
마찬가지로, prepareRevoke()는 취소 호출 데이터를 제공합니다:
import { base } from '@base-org/account';// Prepare revoke call dataconst revokeCall = await base.subscription.prepareRevoke({ id: subscriptionId, testnet: false});// Execute with your own wallet infrastructure