Passkit

作者 dpearson26998d90fd121a26无许可证1.1K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库2个月前更新

Integrate Apple Pay payments and Wallet passes using PassKit. Use when adding Apple Pay buttons, creating payment requests, handling payment authorization, adding passes to Wallet, configuring merchant capabilities, managing shipping/contact fields, or working with PKPaymentRequest, PKPaymentAuthorizationController, PKPaymentButton, AddPassToWalletButton, PKPass, PKAddPassesViewController, PKPassLibrary, Wallet pass distribution, or Apple Pay checkout flows for physical goods, real-world services, donations, and eligible recurring payments.

AI 生成的概览

指导在 iOS 应用中使用 PassKit 集成 Apple Pay 支付与 Wallet 凭证。

功能
提供 Swift 指导,涵盖添加 Apple Pay 按钮、构建 PKPaymentRequest、展示支付表单以及处理支付授权。还涉及将已签名的 .pkpass 文件添加到 Wallet、检查凭证库以及商户配置。参考文件包含定期支付和凭证更新等扩展模式。
适用场景
适用于在 iOS 应用中为实物商品、服务、捐赠或符合条件的定期付款实现 Apple Pay 时。也适用于添加 Wallet 凭证或按清单审查 PassKit 结账代码时。
运行要求
需要具备 Apple Pay 能力的 Apple 平台项目、商户 ID 和支付处理证书,以及用于处理令牌的支付服务商。仅为说明文档,无脚本;附带一个参考 Markdown 文件。

PassKit

Accept Apple Pay payments for physical goods, real-world services, donations, and eligible recurring payments, and add passes to the user's Wallet. Covers payment buttons, payment requests, authorization, Wallet passes, and merchant configuration. Targets Swift 6.3 / iOS 26+.

For advanced Apple Pay flows, one PKPaymentRequest can set only one optional advanced request type: recurring, automatic reload, deferred, Apple Pay Later availability, or multi-token contexts. Use separate payment requests when a checkout needs more than one of those modes.

Contents

Setup

Project Configuration

  1. Enable the Apple Pay capability in Xcode
  2. Create a Merchant ID in the Apple Developer portal (format: merchant.com.example.app)
  3. Generate and install a Payment Processing Certificate for your merchant ID
  4. Add the merchant ID to your entitlements

Availability Check

Always verify the device can make payments before showing Apple Pay UI. If you check for an active card with canMakePayments(usingNetworks:capabilities:), Apple's HIG expects Apple Pay to be a primary, prominent payment option wherever you use that check.

swift
import PassKit
func canMakePayments() -> Bool {    // Check device supports Apple Pay at all    guard PKPaymentAuthorizationController.canMakePayments() else {        return false    }    // Check user has cards for the networks you support    return PKPaymentAuthorizationController.canMakePayments(        usingNetworks: [.visa, .masterCard, .amex, .discover],        capabilities: .threeDSecure    )}

Displaying the Apple Pay Button

SwiftUI

Use the built-in PayWithApplePayButton view in SwiftUI. Use Apple-provided button APIs for any control labeled Apple Pay; custom buttons must not include the Apple Pay logo or "Apple Pay" text.

swift
import SwiftUIimport PassKit
struct CheckoutView: View {    var body: some View {        PayWithApplePayButton(.buy) {            startPayment()        }        .payWithApplePayButtonStyle(.black)        .frame(height: 48)        .padding()    }}

UIKit

Use PKPaymentButton for UIKit-based interfaces.

swift
let button = PKPaymentButton(    paymentButtonType: .buy,    paymentButtonStyle: .black)button.cornerRadius = 12button.addTarget(self, action: #selector(startPayment), for: .touchUpInside)

Button types: .plain, .buy, .setUp, .inStore, .donate, .checkout, .continue, .book, .subscribe, .reload, .addMoney, .topUp, .order, .rent, .support, .contribute, .tip

Creating a Payment Request

Build a PKPaymentRequest with your merchant details and the items being purchased. PassKit amount APIs take NSDecimalNumber, not Double.

swift
func createPaymentRequest() -> PKPaymentRequest {    let request = PKPaymentRequest()    request.merchantIdentifier = "merchant.com.example.app"    request.countryCode = "US"    request.currencyCode = "USD"    request.supportedNetworks = [.visa, .masterCard, .amex, .discover]    request.merchantCapabilities = .threeDSecure
    request.paymentSummaryItems = [        PKPaymentSummaryItem(            label: "Widget",            amount: NSDecimalNumber(string: "9.99")        ),        PKPaymentSummaryItem(            label: "Shipping",            amount: NSDecimalNumber(string: "4.99")        ),        PKPaymentSummaryItem(            label: "My Store",            amount: NSDecimalNumber(string: "14.98")        ) // Total    ]
    return request}

The last item in paymentSummaryItems is treated as the total and its label appears in the Pay line on the payment sheet.

Requesting Shipping and Contact Info

Request only the contact fields needed to price, fulfill, or legally process the order. Collect required product choices, optional notes, per-item shipping destinations, and pickup locations before the Apple Pay button when the payment sheet cannot collect them accurately.

swift
request.requiredShippingContactFields = [.postalAddress, .emailAddress, .name]request.requiredBillingContactFields = [.postalAddress]
let standard = PKShippingMethod(    label: "Standard",    amount: NSDecimalNumber(string: "4.99"))standard.identifier = "standard"standard.detail = "5-7 business days"
let express = PKShippingMethod(    label: "Express",    amount: NSDecimalNumber(string: "9.99"))express.identifier = "express"express.detail = "1-2 business days"
request.shippingMethods = [standard, express]
request.shippingType = .shipping // .delivery, .storePickup, .servicePickup

Supported Networks

NetworkConstant
Visa.visa
Mastercard.masterCard
American Express.amex
Discover.discover
China UnionPay.chinaUnionPay
JCB.JCB
Maestro.maestro
Electron.electron
Interac.interac

Query available networks at runtime with PKPaymentRequest.availableNetworks().

Presenting the Payment Sheet

Use PKPaymentAuthorizationController (works in both SwiftUI and UIKit, no view controller needed). The controller's delegate is weak, so retain the controller for the life of the sheet.

swift
final class CheckoutCoordinator: NSObject {    private var paymentController: PKPaymentAuthorizationController?
    @MainActor    func startPayment() {        let controller = PKPaymentAuthorizationController(            paymentRequest: createPaymentRequest()        )        paymentController = controller        controller.delegate = self        controller.present { [weak self] presented in            if !presented {                self?.paymentController = nil            }        }    }}

Handling Payment Authorization

Implement PKPaymentAuthorizationControllerDelegate to process the payment token.

swift
extension CheckoutCoordinator: PKPaymentAuthorizationControllerDelegate {    func paymentAuthorizationController(        _ controller: PKPaymentAuthorizationController,        didAuthorizePayment payment: PKPayment,        handler completion: @escaping (PKPaymentAuthorizationResult) -> Void    ) {        // Send payment.token.paymentData to your payment processor        Task {            do {                try await paymentService.process(payment.token)                completion(PKPaymentAuthorizationResult(status: .success, errors: nil))            } catch {                completion(PKPaymentAuthorizationResult(status: .failure, errors: [error]))            }        }    }
    func paymentAuthorizationControllerDidFinish(        _ controller: PKPaymentAuthorizationController    ) {        controller.dismiss { [weak self] in            self?.paymentController = nil        }    }}

Handling Shipping Changes

swift
func paymentAuthorizationController(    _ controller: PKPaymentAuthorizationController,    didSelectShippingMethod shippingMethod: PKShippingMethod,    handler completion: @escaping (PKPaymentRequestShippingMethodUpdate) -> Void) {    let updatedItems = recalculateItems(with: shippingMethod)    let update = PKPaymentRequestShippingMethodUpdate(paymentSummaryItems: updatedItems)    completion(update)}

Wallet Passes

Adding a Pass to Wallet

Load signed .pkpass data, verify the device can add passes, then present PKAddPassesViewController when you want the user to review the pass before adding it. PKPass(data:) expects signed pass data and can throw invalid-data or invalid-signature errors. Name invalid-data and invalid-signature failures explicitly instead of hiding them behind a bare try? in review guidance.

swift
func addPassToWallet(data: Data) {    guard PKAddPassesViewController.canAddPasses() else {        return    }
    do {        let pass = try PKPass(data: data)        guard let addController = PKAddPassesViewController(pass: pass) else {            return        }        addController.delegate = self        present(addController, animated: true)    } catch {        // Signed pass data is invalid or the signature cannot be validated.        showRecoverablePassError(error)    }}

SwiftUI Wallet Button

Use AddPassToWalletButton as the SwiftUI equivalent to PKAddPassButton.

swift
import PassKitimport SwiftUI
struct AddPassButton: View {    let passData: Data    @State private var addedToWallet = false
    var body: some View {        if PKAddPassesViewController.canAddPasses(),           let pass = try? PKPass(data: passData) {            AddPassToWalletButton([pass]) { added in                addedToWallet = added            }            .addPassToWalletButtonStyle(.blackOutline)            .frame(width: 250, height: 50)        }    }}

Checking Pass Library

Use PKPassLibrary to inspect and manage passes the user already has. Check PKPassLibrary.isPassLibraryAvailable() before pass-library operations, but use PKAddPassesViewController.canAddPasses() to decide whether the device can add passes. passes() only returns passes your app can access through its entitlements. When replacing an existing pass, check the Boolean result from replacePass(with:) and handle failure. For signed pass bundle construction, update web services, and replacePass(with:), read references/wallet-passes.md [blocked].

swift
let library = PKPassLibrary()
// Check if a specific pass is already in Walletlet hasPass = library.containsPass(pass)
// Retrieve passes your app can accesslet passes = library.passes()
// Check if pass library is availableguard PKPassLibrary.isPassLibraryAvailable() else { return }

Common Mistakes

DON'T: Use StoreKit for physical goods

Apple Pay (PassKit) is for physical goods, real-world services, donations, and eligible recurring payments. StoreKit is for virtual goods, app features, and digital-content subscriptions. Using the wrong framework leads to App Review rejection.

DON'T: Hardcode merchant ID in multiple places

swift
// WRONG: Merchant ID scattered across the codebaselet request1 = PKPaymentRequest()request1.merchantIdentifier = "merchant.com.example.app"// ...elsewhere:let request2 = PKPaymentRequest()request2.merchantIdentifier = "merchant.com.example.app" // easy to get out of sync
// CORRECT: Centralize configurationenum PaymentConfig {    static let merchantIdentifier = "merchant.com.example.app"    static let countryCode = "US"    static let currencyCode = "USD"    static let supportedNetworks: [PKPaymentNetwork] = [.visa, .masterCard, .amex]}

Review Checklist

  • Apple Pay capability enabled and merchant ID configured in Developer portal
  • Payment Processing Certificate generated and installed
  • canMakePayments(usingNetworks:) checked before showing Apple Pay button
  • Apple Pay is prominent wherever active-card availability is checked
  • Product choices, optional details, and complex shipping choices collected before payment sheet
  • Last item in paymentSummaryItems is the total with merchant display name
  • Payment summary and token-context amounts use NSDecimalNumber
  • Payment token sent to server for processing (never decoded client-side)
  • PKPaymentAuthorizationController retained while presented and cleared after finish
  • paymentAuthorizationControllerDidFinish dismisses the controller
  • Shipping method changes recalculate totals via delegate callback
  • StoreKit used for virtual goods/digital content; Apple Pay used for physical goods, services, donations, and eligible recurring payments
  • Wallet passes loaded from signed .pkpass bundles
  • PKPass(data:) invalid-data and invalid-signature failures surfaced
  • PKPassLibrary.isPassLibraryAvailable() used for pass operations, not add-pass capability
  • PKAddPassesViewController.canAddPasses() checked before add-pass UI
  • PKPassLibrary.replacePass(with:) Boolean result checked when replacing a pass
  • Apple Pay button uses system-provided PKPaymentButton or PayWithApplePayButton
  • Add-to-Wallet UI uses system-provided PKAddPassButton, AddPassToWalletButton, or PKAddPassesViewController
  • Error states handled in authorization result (network failures, declined cards)

References

来源与署名

来源:dpearson2699/swift-ios-skills位于skills/passkit提交8d90fd1

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指导实现、审查和改进 iOS、iPadOS、watchOS 与 CarPlay 上的 WidgetKit 小组件和控制项。

Software Development1.1K2个月前更新

Weatherkit

dpearson2699

指导 iOS 开发者使用 WeatherService 获取 WeatherKit 预报、警报与署名信息。

Software Development1.1K2个月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分类1.1K2个月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分类1.1K2个月前更新

Tabletopkit

dpearson2699

指导使用 TabletopKit 在 visionOS 上构建多人空间桌游,涵盖棋具、座位、动作与 RealityKit 渲染。

Software Development1.1K2个月前更新

Swiftui Webkit

dpearson2699

指导在 iOS 26 及更高版本的 SwiftUI 应用中使用 WebKit for SwiftUI 嵌入和控制网页内容。

Software Development1.1K2个月前更新