コンテンツにスキップ
アカウントを作成
または
サインイン
Stripe ドキュメントのロゴ
/
AI に質問する
アカウントを作成
サインイン
始める
支払い
売上
プラットフォームおよびマーケットプレイス
資金管理
開発者向けリソース
概要
Billing
概要Billing API について
サブスクリプション
    概要
    サブスクリプションの仕組み
    始める
    クイックスタート
    連携の計画を立てる
    連携機能の構築
    ユースケース
    サブスクリプションについて
    請求モードを有効にする
    サブスクリプションイベントの定義
    エンタイトルメント
    サブスクリプションの請求書
    サブスクリプションのスケジュール
    継続的な料金体系モデル
    強力な顧客認証 (SCA)
    サブスクリプションを設定する
    請求回収方法の設定
    料金表を埋め込む
    数量の設定
    請求サイクルの設定
    サブスクリプションを管理
    サブスクリプションを Stripe に移行する
    複数のアイテムに登録
    サブスクリプションの遡及適用
    トライアル期間を設定
    後払い支払いによるサブスクリプションの処理
    クーポンを適用
    サブスクリプションの修正
    サブスクリプションの決済方法の管理
    アナリティクス
    Manage subscriptions on iOS
Invoicing
従量課金
見積もり
顧客管理
Billing と他のプロダクトの連携
売上回収
オートメーション
実装内容をテストする
税金
概要
Stripe tax を使用
法規制の遵守・対応管理
レポート機能
概要
レポートの選択
レポートを設定
Reports API
複数のアカウントのレポート
収益認識
データ
概要スキーマ
カスタムレポート
Data Pipeline
データ管理
ホーム売上Subscriptions

注

このページはまだ日本語ではご利用いただけません。より多くの言語で文書が閲覧できるように現在取り組んでいます。準備が整い次第、翻訳版を提供いたしますので、もう少しお待ちください。

Manage subscriptions on iOS非公開プレビュー

Accept recurring payments and manage entitlements with the BillingSDK for iOS.

Accept subscription payments, let customers manage their subscriptions, and manage Entitlements directly in your iOS app with the BillingSDK for iOS. The SDK provides prebuilt UI components to display buy buttons, present the customer portal, and check entitlements to gate premium features.

Before you begin

To use the BillingSDK for iOS, you’ll need:

  • A backend server to create Customer Sessions
  • A Stripe account with access to the private preview
  • iOS 15.0+ and macOS 12.0+
  • Xcode 15.0+

What you’ll build

This guide shows you how to:

  • Set up a server endpoint to create Customer Sessions
  • Install and configure the BillingSDK for iOS
  • Display buy buttons for subscription purchases
  • Check entitlements to gate premium features
  • Present the customer portal for subscription management
  • Handle errors and manage session state

You can find a complete example app in the BillingSDK for iOS repository.

Set up your backend
Server

Create a server endpoint that generates Customer Sessions for authenticated users. These sessions securely authenticate your iOS app with Stripe’s billing APIs.

Create a customer session endpoint

The endpoint should:

  1. Verify that the user is authenticated
  2. Create or retrieve the Stripe Customer ID for the user
  3. Create a Customer Session with the required components enabled
  4. Return the session details to your app

Here’s an example implementation:

server.js
import 'dotenv/config'; import express from 'express'; import Stripe from 'stripe'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || '', { apiVersion: '2025-07-30.basil', }); const app = express(); app.use(express.json()); app.post('/authenticate', async (req, res) => { try { // Replace with your auth; return 401 if the user is not logged in const user = authUser(req); if (!user) { res.status(401).json({ error: { type: 'no_session_present' } }); return; } const customerSession = await stripe.customerSessions.create({ customer: user.stripeCustomerId, components: { buy_button: { enabled: true }, active_entitlements: { enabled: true }, customer_portal: { enabled: true }, } as any, }); res.json({ clientSecret: customerSession.client_secret, expiresAt: customerSession.expires_at, customer: customerSession.customer as string, }); } catch (err) { res.status(500).json({ error: { message: 'Internal server error' } }); } }); app.listen(3000);

Response format

The endpoint must return these fields for successful authentication:

FieldTypeDescription
clientSecretstringThe Customer Session secret used to authenticate the SDK
expiresAtnumberExpiration timestamp in seconds since epoch
customerstringThe Stripe Customer ID

When authentication fails, return HTTP 401 to trigger the SDK’s unauthenticated state.

Install and configure the SDK
iOS

The BillingSDK for iOS provides a native iOS experience for subscription management and feature access control.

Add the SDK to your project

During private preview, install the BillingSDK package:

  1. Open your project in Xcode
  2. Select File → Add Package Dependencies
  3. Type https://github.com/stripe-samples/billing-ios-sdk into the Search or Enter Package URL field
  4. Select BillingSDK and click Add Package

Configure the SDK

Initialize the SDK in a shared class to handle the BillingSDK setup and authentication:

BillingManager.swift
import Foundation import BillingSDK import SwiftUI class BillingManager { static let shared = BillingManager() public let billing: BillingSDK private init() { // Initialize with your publishable key and set how long entitlements can be stale before needing to refresh. let configuration = BillingSDK.Configuration( publishableKey: "pk_test_…", maximumStaleEntitlementsDuration: TimeInterval(60 * 5) ) self.billing = BillingSDK(configuration: configuration) // Set up authentication (see below for implementation) setupCustomerSessionProvider() // Add entitlement change listener (see below for implementation) setupEntitlementListener() } }

注

The SDK handles thread safety internally—you can safely call its methods from any thread.

Set up the Customer Session provider

Add the authentication method to your billing manager:

BillingManager+Authentication.swift
import Foundation import BillingSDK extension BillingManager { func setupCustomerSessionProvider() { billing.setCustomerSessionProvider { [weak self] in await self?.fetchCustomerSession() } } private func fetchCustomerSession() async -> UBCustomerSessionDetails? { // Configure request to your backend endpoint guard let url = URL(string: "https://your-backend.example.com/customer-session") else { print("Invalid URL") return nil } var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") // Send any additional headers needed to authenticate the user, e.g. an auth token. request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization") do { // Make the request let (data, response) = try await URLSession.shared.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { print("Invalid response format") return nil } switch httpResponse.statusCode { case 200: // Parse the successful response let decoder = JSONDecoder() decoder.dateDecodingStrategy = .secondsSince1970 return try decoder.decode(UBCustomerSessionDetails.self, from: data) case 401: // User not authenticated - SDK will enter unauthenticated state print("Authentication required") return nil default: print("Unexpected status code: \(httpResponse.statusCode)") return nil } } catch { print("Session provider error: \(error.localizedDescription)") return nil } } // Call this method when your user signs out func handleSignOut() async { await billing.reset() // Clear session data and caches } }

注

The BillingSDK automatically calls your session provider whenever it needs to authenticate with Stripe. When the provider returns nil, the SDK enters an unauthenticated state.

Display subscription buy buttons
iOS

Buy buttons provide a prebuilt UI element to let customers purchase subscriptions. Each button is linked to a specific product and price in your Stripe account.

Create buy buttons in the Dashboard

Follow this guide to create buy buttons in the Dashboard. If you plan to use the entitlement checking functionality, ensure that the products you’ve created have Entitlements attached to them.

Display a buy button

SubscriptionViewController.swift
import SwiftUI import BillingSDK struct SubscriptionView: View { @State private var buyButton: BuyButton? @State private var isLoading = true @State private var errorMessage: String? // Replace with your buy button ID from the Dashboard let buttonId = "buy_btn_1Abcdef123456" var body: some View { VStack { if isLoading { ProgressView("Loading subscription options...") } else if let buyButton = buyButton { // Display the prebuilt buy button UI // Alternatively the BuyButton class also provides all the data to render // a custom UI element. buyButton.view() .frame(height: 56) .padding(.horizontal) } else if let errorMessage = errorMessage { Text("Error: \(errorMessage)") .foregroundColor(.red) Button("Retry") { loadBuyButton() } } } .onAppear { loadBuyButton() } } func loadBuyButton() { isLoading = true errorMessage = nil Task { do { // Fetch the button from Stripe buyButton = try await BillingManager.shared.billing.getBuyButton(id: buttonId) isLoading = false } catch { errorMessage = "Failed to load buy button" isLoading = false } } } }

注

Buy buttons work even when users aren’t authenticated. The SDK creates a new Stripe Customer during purchase if needed.

Check and validate entitlements
iOS

Entitlements let you control access to premium features based on a customer’s active subscriptions.

Check a specific entitlement

Verify if a user has access to a specific feature:

PremiumFeaturesViewController.swift
func checkPremiumAccess() async { do { let hasPremiumAccess = try await BillingManager.shared.billing.hasEntitlement(lookupKey: "premium_tier") if hasPremiumAccess { // User has premium access - enable features unlockPremiumFeatures() } else { // User doesn't have access - show upsell showPremiumUpsell() } } catch { showErrorMessage("Couldn't verify entitlements status") } }

Get all active entitlements

Retrieve all entitlements the customer has access to:

func loadUserEntitlements() async { do { // Force refresh ensures we get the latest data from the server let entitlements = try await BillingManager.shared.billing.getActiveEntitlements(forceRefresh: true) // Map entitlements to app features let features = entitlements.map { $0.lookupKey } updateAvailableFeatures(features) } catch { handleError(error) } }

Listen for entitlement changes

Set up a listener to be notified when entitlements change:

BillingManager+Entitlements.swift
import Foundation import BillingSDK extension BillingManager { func setupEntitlementListener() { billing.onEntitlementsChanged { updatedEntitlements in // Update UI based on new entitlements DispatchQueue.main.async { self.updateAppFeatures(based: updatedEntitlements) self.refreshUI() } } } }

注

When no session is present, getActiveEntitlements() returns an empty array.

Present the customer portal
iOS

The customer portal lets subscribers manage their subscriptions, payment methods, and billing information.

Show the customer portal

Present the portal when users need to manage their subscription:

AccountViewController.swift
func manageSubscriptionTapped() { Task { do { let portal = try await BillingManager.shared.billing.getCustomerPortal() // Show portal within your app (recommended) portal.presentCustomerPortal(from: self) } catch { if let billingError = error as? BillingSDKError { switch billingError { case .unauthenticated: // User needs to log in first showLoginPrompt() default: // Handle other errors showErrorMessage("Could not load subscription management") } } else { showErrorMessage("Could not load subscription management") } } } }

警告

The customer portal requires an active authenticated session. If the user isn’t logged in, the SDK throws an .unauthenticated error.

External redirection (optional)

You can also open the portal in a browser:

let portal = try await BillingManager.shared.billing.getCustomerPortal() portal.redirectToCustomerPortal() // Opens in default browser

Session management
iOS

Session management

Reset the SDK when users sign out to clear session data and caches:

AuthManager.swift
func signOut() async { // Clear your app's auth state UserDefaults.standard.removeObject(forKey: "auth_token") // Reset the BillingSDK state await BillingManager.shared.billing.reset() // Navigate to login screen showLoginScreen() }

Test your integration

During development, use your sandbox API keys and sandbox Customer IDs to avoid creating real charges.

Testing scenarios

Test these common scenarios:

ScenarioSteps
New subscription purchasePresent a buy button to a new user
Subscription managementUse the customer portal to change plans
Entitlement verificationCheck a premium feature with hasEntitlement
Error handlingTest with invalid keys or expired sessions
Sign out flowVerify billing.reset() clears cached data

参照情報

  • Entitlements
  • Configure the customer portal
  • How subscriptions work
このページはお役に立ちましたか。
はいいいえ
  • お困りのことがございましたら 、サポートにお問い合わせください。
  • 早期アクセスプログラムにご参加ください。
  • 変更ログをご覧ください。
  • ご不明な点がございましたら、お問い合わせください。
  • LLM ですか?llms.txt を読んでください。
  • Powered by Markdoc