import SwiftUI import PassKit struct ContentView: View { @StateObject private var handler = PaymentHandler() @State private var amount: String = "7.250" private var canPay: Bool { PKPaymentAuthorizationController.canMakePayments() && (Double(amount) ?? 0) > 0 } var body: some View { VStack(spacing: 24) { VStack(spacing: 4) { Text("TruePay").font(.largeTitle).bold() Text("Apple Pay demo").foregroundColor(.secondary) } HStack { Text("Amount") Spacer() TextField("0.000", text: $amount) .keyboardType(.decimalPad) .multilineTextAlignment(.trailing) .frame(width: 120) Text(Config.currencyCode).foregroundColor(.secondary) } .padding() .background(Color(.secondarySystemBackground)) .cornerRadius(12) if canPay { ApplePayButton { handler.startPayment(amount: Double(amount) ?? 0) } .frame(height: 48) .cornerRadius(10) } else { Text("Apple Pay is not available on this device.") .font(.footnote).foregroundColor(.secondary) } if let status = handler.status { Text(status) .font(.callout) .foregroundColor(handler.succeeded ? .green : .red) .multilineTextAlignment(.center) } Spacer() } .padding() } } /// Native PKPaymentButton wrapped for SwiftUI. struct ApplePayButton: UIViewRepresentable { let action: () -> Void func makeUIView(context: Context) -> PKPaymentButton { let button = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .black) button.addTarget(context.coordinator, action: #selector(Coordinator.tapped), for: .touchUpInside) return button } func updateUIView(_ uiView: PKPaymentButton, context: Context) {} func makeCoordinator() -> Coordinator { Coordinator(action: action) } final class Coordinator: NSObject { let action: () -> Void init(action: @escaping () -> Void) { self.action = action } @objc func tapped() { action() } } }