Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
33636d7
feat: add Paykit subscription payments
ben-kaufman Aug 26, 2026
9ad0634
chore: name subscription changelog fragment
ben-kaufman Aug 26, 2026
563c68c
fix: polish subscription payments
ben-kaufman Aug 27, 2026
ff5aac4
fix: preserve subscription details flow
ben-kaufman Sep 1, 2026
6837029
fix: reconcile subscription stack
ben-kaufman Sep 1, 2026
1b8d3bf
fix: support hardware subscription payments
ben-kaufman Sep 1, 2026
49436fc
fix: avoid duplicate payment handlers
ben-kaufman Sep 1, 2026
1d4b816
fix: track hardware subscription payments
ben-kaufman Sep 2, 2026
2a5254d
fix: clear failed hardware proofs
ben-kaufman Sep 2, 2026
7a70b30
test: synchronize onchain proof reconciliation
ben-kaufman Sep 2, 2026
a79bf4d
fix: show normalized monthly cost
ben-kaufman Sep 3, 2026
432b7e5
refactor: remove unused acceptance accessor
ben-kaufman Sep 3, 2026
7c01303
fix: harden Paykit subscription recovery
ben-kaufman Sep 4, 2026
9136de4
fix: harden subscription edge cases
ben-kaufman Sep 4, 2026
8338720
fix: harden initial subscription payments
ben-kaufman Sep 4, 2026
62ee2f0
fix: preserve uncertain lightning payments
ben-kaufman Sep 4, 2026
39fc5cc
fix: distinguish pending paykit request ids
ben-kaufman Sep 6, 2026
92376ab
fix: distinguish pre-broadcast subscription failures
ben-kaufman Sep 7, 2026
559e300
fix: confirm subscription funding changes
ben-kaufman Sep 7, 2026
dc243ca
fix: show both units in payment request details
ben-kaufman Sep 8, 2026
420004d
fix: preserve Paykit child accessibility identifiers
ben-kaufman Sep 9, 2026
df49bd0
Merge branch 'master' into codex/paykit-subscriptions
ben-kaufman Sep 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 113 additions & 7 deletions Bitkit/AppScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ struct AppScene: View {
}

var body: some View {
appEventContent
}

private var configuredContent: some View {
mainContent
.sheet(
item: $sheets.forgotPinSheetItem,
Expand All @@ -141,6 +145,7 @@ struct AppScene: View {
.task(priority: .userInitiated, setupTask)
.task(id: scenePhase) { await pollIncomingPaykitPaymentRequests() }
.task(id: initialPaykitSyncGeneration) { await pollIncomingPaykitPaymentRequestsDuringInitialSync() }
.task { await handlePendingPaykitSubscriptionNotification() }
.onChange(of: currency.hasStaleData) { _, newValue in handleCurrencyStaleData(newValue) }
.onChange(of: wallet.walletExists) { _, newValue in handleWalletExistsChange(newValue) }
.onChange(of: wallet.nodeLifecycleState) { _, newValue in handleNodeLifecycleChange(newValue) }
Expand Down Expand Up @@ -202,13 +207,21 @@ struct AppScene: View {
.environment(hwWalletManager)
.environment(calculatorInputManager)
.environment(paykitPaymentRequestManager)
}

private var appEventContent: some View {
configuredContent
.onChange(of: pubkyProfile.authState, initial: true) { _, authState in
if authState == .authenticated, let pk = pubkyProfile.publicKey {
paykitPaymentRequestManager.activate(identity: pk)
Task {
try? await contactsManager.loadContacts(for: pk)
await refreshPrivateOnlyPaykitReceiverMarker()
await refreshIncomingPaykitPaymentRequests()
await refreshIncomingPaykitPaymentRequests(presentItems: false)
await handlePendingPaykitSubscriptionNotification()
if PaykitSubscriptionNotificationTargetStore.load() == nil {
await presentNextIncomingPaykitItem()
}
if !PaykitFeatureFlags.isUIEnabled, wallet.walletExists == true {
await retryPendingPaykitEndpointRemoval()
}
Expand Down Expand Up @@ -238,6 +251,15 @@ struct AppScene: View {
.onReceive(PrivatePaykitService.initialLinkBurstStartedPublisher) {
initialPaykitSyncGeneration += 1
}
.onReceive(PaykitPaymentProofService.proofStateChangedPublisher) {
Task { await refreshIncomingPaykitPaymentRequests() }
}
.onReceive(PaykitPaymentProofService.onchainPaymentResolutionPublisher) { resolution in
Task { await associateResolvedPaykitOnchainPayment(resolution) }
}
.onReceive(NotificationCenter.default.publisher(for: .paykitSubscriptionPaymentDue)) { _ in
Task { await handlePendingPaykitSubscriptionNotification() }
}
.onChange(of: sheets.activeSheetConfiguration?.id) { _, activeSheetId in
guard activeSheetId == nil, !sheets.isReplacingSheet else { return }
Task {
Expand All @@ -246,7 +268,7 @@ struct AppScene: View {
sheets.activeSheetConfiguration == nil,
!sheets.isReplacingSheet
else { return }
await presentNextIncomingPaykitPaymentRequest()
await presentNextIncomingPaykitItem()
}
}
.onChange(of: paykitPaymentRequestManager.requestedPresentationId) { _, requestId in
Expand All @@ -259,11 +281,15 @@ struct AppScene: View {
.onChange(of: paykitPaymentRequestManager.pendingRequests) { _, requests in
guard let request = app.contactPaymentContext?.incomingPaymentRequest,
!requests.contains(where: { $0.id == request.id }),
!paykitPaymentRequestManager.isApprovedForPayment(request),
sheets.activeSheetConfiguration?.id == .send
!paykitPaymentRequestManager.isApprovedForPayment(request)
else { return }

let activeSheetId = sheets.activeSheetConfiguration?.id
guard activeSheetId == .send ||
(activeSheetId == .subscription && app.contactPaymentContext?.isInitialSubscriptionPayment == true)
else { return }

sheets.hideSheetIfActive(.send, reason: "Incoming payment request is no longer available")
sheets.hideSheet(reason: "Incoming payment request is no longer available")
}
.onChange(of: navigation.currentRoute) { oldRoute, newRoute in
guard shouldDiscardPendingImport(currentRoute: oldRoute, destination: newRoute) else {
Expand Down Expand Up @@ -764,7 +790,7 @@ struct AppScene: View {
}

@discardableResult
private func refreshIncomingPaykitPaymentRequests() async -> Bool {
private func refreshIncomingPaykitPaymentRequests(presentItems: Bool = true) async -> Bool {
guard PaykitFeatureFlags.isUIEnabled,
wallet.walletExists == true,
pubkyProfile.authState == .authenticated
Expand All @@ -777,10 +803,40 @@ struct AppScene: View {
let previousRequests = paykitPaymentRequestManager.pendingRequests
await paykitPaymentRequestManager.refreshEligibleTargets(savedPublicKeys: contactsManager.contacts.map(\.publicKey))
await paykitPaymentRequestManager.refresh()
await presentNextIncomingPaykitPaymentRequest()
if presentItems {
await presentNextIncomingPaykitItem()
}
return paykitPaymentRequestManager.pendingRequests != previousRequests
}

private func associateResolvedPaykitOnchainPayment(_ resolution: PaykitOnchainPaymentResolution) async {
if let identity = pubkyProfile.publicKey,
PubkyPublicKeyFormat.matches(resolution.identity, identity)
{
do {
_ = try await tryNTimes(
toTry: {
try? await activity.syncLdkNodePayments()
return try await activity.findActivity(byPaymentId: resolution.transactionId)
},
times: 12,
interval: 2
)
try await activity.setContact(
resolution.requestId.counterparty,
forPaymentId: resolution.transactionId,
syncLdkPayments: false
)
} catch {
Logger.warn(
"Failed to associate resolved Paykit payment \(resolution.transactionId) with its contact: \(error)",
context: "AppScene"
)
}
}
await PaykitPaymentProofService.shared.consumeOnchainPaymentResolution(resolution)
}

private func pollIncomingPaykitPaymentRequests() async {
guard scenePhase == .active else { return }

Expand Down Expand Up @@ -947,6 +1003,56 @@ struct AppScene: View {
await presentNextIncomingPaykitPaymentRequest()
}

private func handlePendingPaykitSubscriptionNotification() async {
guard let target = PaykitSubscriptionNotificationTargetStore.load() else { return }
guard let identity = pubkyProfile.publicKey else { return }
guard target.matches(identity: identity) else {
PaykitSubscriptionNotificationTargetStore.clear()
return
}
guard sheets.activeSheetConfiguration == nil,
!sheets.isReplacingSheet,
app.contactPaymentContext == nil
else { return }
await refreshIncomingPaykitPaymentRequests(presentItems: false)
guard let request = paykitPaymentRequestManager.pendingRequests.first(where: target.matches) else {
if paykitPaymentRequestManager.historyRequests.contains(where: target.matches) {
PaykitSubscriptionNotificationTargetStore.clear()
} else if paykitPaymentRequestManager.hasDismissedSubscriptionPayment(matching: target) {
PaykitSubscriptionNotificationTargetStore.clear()
} else if !paykitPaymentRequestManager.subscriptions.contains(where: {
$0.paymentRequestId == target.paymentRequestId &&
PubkyPublicKeyFormat.matches($0.counterparty, target.counterparty) &&
$0.counterpartyReceiverPath == target.counterpartyReceiverPath &&
$0.isActive(at: Date())
}) {
PaykitSubscriptionNotificationTargetStore.clear()
}
return
}

if paykitPaymentRequestManager.requestedPresentationId != request.id {
guard paykitPaymentRequestManager.requestPresentation(request) else { return }
}
await presentNextIncomingPaykitPaymentRequest()
}

private func presentNextIncomingPaykitItem() async {
guard sheets.activeSheetConfiguration == nil, !sheets.isReplacingSheet else { return }
if PaykitSubscriptionNotificationTargetStore.load() != nil {
await handlePendingPaykitSubscriptionNotification()
guard PaykitSubscriptionNotificationTargetStore.load() == nil,
sheets.activeSheetConfiguration == nil,
!sheets.isReplacingSheet
else { return }
}
if let subscription = paykitPaymentRequestManager.subscriptionProposalForPresentation() {
sheets.showSheet(.subscription, data: SubscriptionSheetItem(route: .review(subscription)))
return
}
await presentNextIncomingPaykitPaymentRequest()
}

private func retryPendingPaykitEndpointRemoval() async {
if PublicPaykitService.isCleanupPending {
do {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "subscription-clock.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 72 additions & 1 deletion Bitkit/BitkitApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,70 @@ import SwiftUI
/// Communication bridge between delegates and SwiftUI views
extension Notification.Name {
static let quickActionSelected = Notification.Name("quickActionSelected")
static let paykitSubscriptionPaymentDue = Notification.Name("paykitSubscriptionPaymentDue")
}

struct PaykitSubscriptionNotificationTarget: Codable, Equatable {
let payerIdentity: String
let paymentRequestId: String
let counterparty: String
let counterpartyReceiverPath: String
let billingPeriodStartsAt: String

init?(userInfo: [AnyHashable: Any]) {
guard let payerIdentity = userInfo["payer_identity"] as? String,
let paymentRequestId = userInfo["payment_request_id"] as? String,
let counterparty = userInfo["counterparty"] as? String,
let counterpartyReceiverPath = userInfo["counterparty_receiver_path"] as? String,
let billingPeriodStartsAt = userInfo["billing_period_starts_at"] as? String
else { return nil }

self.payerIdentity = payerIdentity
self.paymentRequestId = paymentRequestId
self.counterparty = counterparty
self.counterpartyReceiverPath = counterpartyReceiverPath
self.billingPeriodStartsAt = billingPeriodStartsAt
}

func matches(_ request: PaykitPaymentRequest) -> Bool {
paymentRequestId == request.paymentRequestId &&
PubkyPublicKeyFormat.matches(counterparty, request.counterparty) &&
counterpartyReceiverPath == request.counterpartyReceiverPath &&
request.billingPeriod.map {
PaykitSubscriptionTimestamp.string(from: $0.startsAt) == billingPeriodStartsAt
} == true
}

func matches(_ requestId: PaykitPaymentRequest.ID) -> Bool {
paymentRequestId == requestId.paymentRequestId &&
PubkyPublicKeyFormat.matches(counterparty, requestId.counterparty) &&
counterpartyReceiverPath == requestId.counterpartyReceiverPath &&
requestId.billingPeriodStartsAt.map {
PaykitSubscriptionTimestamp.string(from: $0) == billingPeriodStartsAt
} == true
}

func matches(identity: String) -> Bool {
PubkyPublicKeyFormat.matches(payerIdentity, identity)
}
}

enum PaykitSubscriptionNotificationTargetStore {
private static let key = "paykitSubscriptionNotificationTarget"

static func save(_ target: PaykitSubscriptionNotificationTarget) {
guard let data = try? JSONEncoder().encode(target) else { return }
UserDefaults.standard.set(data, forKey: key)
}

static func load() -> PaykitSubscriptionNotificationTarget? {
guard let data = UserDefaults.standard.data(forKey: key) else { return nil }
return try? JSONDecoder().decode(PaykitSubscriptionNotificationTarget.self, from: data)
}

static func clear() {
UserDefaults.standard.removeObject(forKey: key)
}
}

class AppDelegate: NSObject, UIApplicationDelegate {
Expand Down Expand Up @@ -81,7 +145,14 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
) {
let userInfo = response.notification.request.content.userInfo

PushNotificationManager.shared.handleNotification(userInfo)
if userInfo["bitkit_action"] as? String == "paykit_subscription_due" {
if let target = PaykitSubscriptionNotificationTarget(userInfo: userInfo) {
PaykitSubscriptionNotificationTargetStore.save(target)
}
NotificationCenter.default.post(name: .paykitSubscriptionPaymentDue, object: nil, userInfo: userInfo)
} else {
PushNotificationManager.shared.handleNotification(userInfo)
}

// TODO: if user tapped on an incoming tx we should open it on that tx view
completionHandler()
Expand Down
15 changes: 5 additions & 10 deletions Bitkit/Components/Button/SecondaryButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct SecondaryButtonView: View {
}
}
.frame(maxWidth: (size == .large || shouldExpand) ? .infinity : nil)
.frame(height: buttonHeight)
.frame(height: size.height)
.padding(.horizontal, 16)
.background(isPressed ? Color.white10 : Color.clear)
.background(BlurView())
Expand All @@ -36,18 +36,13 @@ struct SecondaryButtonView: View {
}

private var textColor: Color {
isDisabled ? .white32 : .white80
guard !isDisabled else { return .white32 }
return size == .small ? .white64 : .white80
}

private var borderColor: Color {
isDisabled ? .clear : .gray4
}

private var buttonHeight: CGFloat {
switch size {
case .small: 37
case .large: 56
}
guard !isDisabled else { return .clear }
return size == .small ? .white16 : .gray4
}

private var strokeWidth: CGFloat {
Expand Down
Loading
Loading