首页/移动开发/passkit-wallet
P

passkit-wallet

by @dpearson2699v1.0.0
0.0(0)

"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 and contact fields, or working with PKPaymentRequest, PKPaymentAu

PassKitApple WalletDigital PassesLoyalty ProgramsMobile PaymentsGitHub
安装方式
npx skills add dpearson2699/swift-ios-skills --skill passkit-wallet
compare_arrows

Before / After 效果对比

1
使用前
1在没有 PassKit 框架的情况下,实现应用内支付或将票据/会员卡添加到数字钱包需要开发者自行处理支付网关集成、卡片生成和安全存储等复杂流程。这不仅开发难度大,而且难以提供与系统原生体验一致的用户界面和安全性。
2
3```swift
4// 假设通过第三方支付SDK手动实现支付,缺乏Apple Pay的无缝体验
5// func processPaymentManually(amount: Decimal) {
6//     let thirdPartySDK = ThirdPartyPaymentSDK()
7//     thirdPartySDK.startPaymentFlow(amount: amount, completion: { result in
8//         // ... 处理支付结果
9//     })
10// }
11print("手动实现支付和卡包功能复杂且用户体验不佳。")
12```
使用后
1使用 PassKit 框架,开发者可以轻松集成 Apple Pay 支付和 Wallet 卡包功能。它提供了标准化的 API 来创建支付请求、处理支付授权、添加票据到 Wallet,并能自动处理安全性和用户界面,提供无缝的原生体验。
2
3```swift
4import PassKit
5
6// MARK: - Apple Pay 支付示例
7func setupApplePayButton(in view: UIView) {
8    if PKPaymentAuthorizationController.canMakePayments(using: .capability3DS) {
9        let applePayButton = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .black)
10        applePayButton.addTarget(self, action: #selector(startApplePay), for: .touchUpInside)
11        view.addSubview(applePayButton)
12        // ... 布局按钮
13    } else {
14        print("设备不支持 Apple Pay 或未配置卡片。")
15    }
16}
17
18@objc func startApplePay() {
19    let paymentRequest = PKPaymentRequest()
20    paymentRequest.merchantIdentifier = "merchant.com.example.myapp"
21    paymentRequest.supportedNetworks = [.visa, .masterCard, .amex]
22    paymentRequest.merchantCapabilities = .capability3DS
23    paymentRequest.countryCode = "US"
24    paymentRequest.currencyCode = "USD"
25    paymentRequest.paymentSummaryItems = [
26        PKPaymentSummaryItem(label: "商品总价", amount: NSDecimalNumber(string: "19.99")),
27        PKPaymentSummaryItem(label: "运费", amount: NSDecimalNumber(string: "5.00")),
28        PKPaymentSummaryItem(label: "总计", amount: NSDecimalNumber(string: "24.99"))
29    ]
30
31    let authorizationController = PKPaymentAuthorizationController(paymentRequest: paymentRequest)
32    authorizationController.delegate = self // 需要实现 PKPaymentAuthorizationControllerDelegate
33    authorizationController.present(completion: nil)
34}
35
36// MARK: - Wallet Pass 示例
37func addPassToWallet(passData: Data) {
38    let addPassesViewController = PKAddPassesViewController(pass: PKPass(data: passData, error: nil))
39    // present addPassesViewController
40    print("Pass 已准备好添加到 Wallet。")
41}
42```

description SKILL.md


name: passkit-wallet description: "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 and contact fields, or working with PKPaymentRequest, PKPaymentAuthorizationController, PKPaymentButton, PKPass, PKAddPassesViewController, PKPassLibrary, or Apple Pay checkout flows."

PassKit — Apple Pay & Wallet

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

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.

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.

import SwiftUI
import PassKit

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

UIKit

Use PKPaymentButton for UIKit-based interfaces.

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

Button types: .buy, .setUp, .inStore, .donate, .checkout, .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.

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: 9.99),
        PKPaymentSummaryItem(label: "Shipping", amount: 4.99),
        PKPaymentSummaryItem(label: "My Store", amount: 14.98) // Total (last item)
    ]

    return request
}

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

Requesting Shipping and Contact Info

request.requiredShippingContactFields = [.postalAddress, .emailAddress, .name]
request.requiredBillingContactFields = [.postalAddress]

request.shippingMethods = [
    PKShippingMethod(label: "Standard", amount: 4.99),
    PKShippingMethod(label: "Express", amount: 9.99),
]
request.shippingMethods?[0].identifier = "standard"
request.shippingMethods?[0].detail = "5-7 business days"
request.shippingMethods?[1].identifier = "express"
request.shippingMethods?[1].detail = "1-2 business days"

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).

@MainActor
func startPayment() {
    let request = createPaymentRequest()
    let controller = PKPaymentAuthorizationController(paymentRequest: request)
    controller.delegate = self
    controller.present()
}

Handling Payment Authorization

Implement PKPaymentAuthorizationControllerDelegate to process the payment token.

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()
    }
}

Handling Shipping Changes

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 a .pkpass file and present PKAddPassesViewController.

func addPassToWallet(data: Data) {
    guard let pass = try? PKPass(data: data) else { return }

    if let addController = PKAddPassesViewController(pass: pass) {
        addController.delegate = self
        present(addController, animated: true)
    }
}

SwiftUI Wallet Button

import PassKit
import SwiftUI

struct AddPassButton: View {
    let passData: Data

    var body: some View {
        Button("Add to Wallet") {
            addPass()
        }
    }

    func addPass() {
        guard let pass = try? PKPass(data: passData) else { return }
        let library = PKPassLibrary()
        library.addPasses([pass]) { status in
            switch status {
            case .shouldReviewPasses:
                // Present review UI
                break
            case .didAddPasses:
                // Passes added successfully
                break
            case .didCancelAddPasses:
                break
            @unknown default:
                break
            }
        }
    }
}

Checking Pass Library

Use PKPassLibrary to inspect and manage passes the user already has.

let library = PKPassLibrary()

// Check if a specific pass is already in Wallet
let hasPass = library.containsPass(pass)

// Retrieve passes your app can access
let passes = library.passes()

// Check if pass library is available
guard PKPassLibrary.isPassLibraryAvailable() else { return }

Common Mistakes

DON'T: Use StoreKit for physical goods

Apple Pay (PassKit) is for physical goods and services. StoreKit is for digital content, subscriptions, and in-app purchases. Using the wrong framework leads to App Review rejection.

// WRONG: Using StoreKit to sell a physical product
let product = try await Product.products(for: ["com.example.tshirt"])

// CORRECT: Use Apple Pay for physical goods
let request = PKPaymentRequest()
request.paymentSummaryItems = [
    PKPaymentSummaryItem(label: "T-Shirt", amount: 29.99),
    PKPaymentSummaryItem(label: "My Store", amount: 29.99)
]

DON'T: Hardcode merchant ID in multiple places

// WRONG: Merchant ID scattered across the codebase
let 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 configuration
enum PaymentConfig {
    static let merchantIdentifier = "merchant.com.example.app"
    static let countryCode = "US"
    static let currencyCode = "USD"
    static let supportedNetworks: [PKPaymentNetwork] = [.visa, .masterCard, .amex]
}

DON'T: Forget the total line item

The last item in paymentSummaryItems is the total row. If you omit it, the payment sheet shows no merchant name or total.

// WRONG: No total item
request.paymentSummaryItems = [
    PKPaymentSummaryItem(label: "Widget", amount: 9.99)
]

// CORRECT: Last item is the total with your merchant name
request.paymentSummaryItems = [
    PKPaymentSummaryItem(label: "Widget", amount: 9.99),
    PKPaymentSummaryItem(label: "My Store", amount: 9.99) // Total
]

DON'T: Skip the canMakePayments check

// WRONG: Show Apple Pay button without checking
PayWithApplePayButton(.buy) { startPayment() }

// CORRECT: Only show when available
if PKPaymentAuthorizationController.canMakePayments(
    usingNetworks: PaymentConfig.supportedNetworks
) {
    PayWithApplePayButton(.buy) { startPayment() }
} else {
    // Show alternative checkout or setup button
    Button("Set Up Apple Pay") { /* guide user */ }
}

DON'T: Dismiss the controller before completing authorization

// WRONG: Dismissing inside didAuthorizePayment
func paymentAuthorizationController(
    _ controller: PKPaymentAuthorizationController,
    didAuthorizePayment payment: PKPayment,
    handler completion: @escaping (PKPaymentAuthorizationResult) -> Void
) {
    controller.dismiss() // Too early -- causes blank sheet
    completion(.init(status: .success, errors: nil))
}

// CORRECT: Dismiss only in paymentAuthorizationControllerDidFinish
func paymentAuthorizationControllerDidFinish(
    _ controller: PKPaymentAuthorizationController
) {
    controller.dismiss()
}

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
  • Last item in paymentSummaryItems is the total with merchant display name
  • Payment token sent to server for processing (never decoded client-side)
  • paymentAuthorizationControllerDidFinish dismisses the controller
  • Shipping method changes recalculate totals via delegate callback
  • StoreKit used for digital goods; Apple Pay used for physical goods
  • Wallet passes loaded from signed .pkpass bundles
  • PKPassLibrary.isPassLibraryAvailable() checked before pass operations
  • Apple Pay button uses system-provided PKPaymentButton or PayWithApplePayButton
  • Error states handled in authorization result (network failures, declined cards)

References

forum用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价,来写第一条吧

统计数据

安装量0
评分0.0 / 5.0
版本1.0.0
更新日期2026年3月17日
对比案例1 组

用户评分

0.0(0)
5
0%
4
0%
3
0%
2
0%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code

时间线

创建2026年3月17日
最后更新2026年3月17日