Solana KitSolana Kit Docs
openbudgetfun/solana_kit 999999

Subscription plans#

Subscription plans let a merchant publish reusable terms. A subscriber accepts a plan with getSubscribeInstruction, which creates a subscription delegation account tied to the accepted terms.

import 'package:solana_kit/solana_kit.dart';
import 'package:solana_kit_subscriptions/solana_kit_subscriptions.dart';

Future<void> main() async {
  const emptyAddress = Address('11111111111111111111111111111111');
  const merchant = Address('11111111111111111111111111111112');
  const planPda = Address('11111111111111111111111111111113');
  const tokenMint = Address('So11111111111111111111111111111111111111112');

  final createPlanInstruction = getCreatePlanInstruction(
    programAddress: subscriptionsProgramAddress,
    merchant: merchant,
    planPda: planPda,
    tokenMint: tokenMint,
    systemProgram: systemProgramAddress,
    tokenProgram: tokenProgramAddress,
    planData: PlanData(
      planId: BigInt.from(1),
      mint: tokenMint,
      terms: PlanTerms(
        amount: BigInt.from(5_000_000),
        periodHours: BigInt.from(24 * 30),
        createdAt: BigInt.from(DateTime.now().millisecondsSinceEpoch ~/ 1000),
      ),
      endTs: BigInt.zero,
      destinations: const [emptyAddress, emptyAddress, emptyAddress, emptyAddress],
      pullers: const [emptyAddress, emptyAddress, emptyAddress, emptyAddress],
      metadataUri: 'https://example.com/subscription-plan.json',
    ),
  );

  print(createPlanInstruction.accounts!.length);
}

After the plan exists, derive the subscription delegation PDA and call getSubscribeInstruction. Use getTransferSubscriptionInstruction for billing, getCancelSubscriptionInstruction for subscriber cancellation, and getResumeSubscriptionInstruction to resume a paused subscription.