Initial payment setup

This commit is contained in:
Balazs Perlaki-Horvath 2024-09-21 19:45:29 +02:00 committed by Kelson
parent 3fef622b03
commit ce2a80cafa
2 changed files with 84 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import UserNotifications
import Combine import Combine
import Defaults import Defaults
import CoreKiwix import CoreKiwix
import PassKit
#if os(macOS) #if os(macOS)
final class AppDelegate: NSObject, NSApplicationDelegate { final class AppDelegate: NSObject, NSApplicationDelegate {
@ -109,6 +110,7 @@ struct RootView: View {
private let tabCloses = NotificationCenter.default.publisher(for: NSWindow.willCloseNotification) private let tabCloses = NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)
/// Close other tabs then the ones received /// Close other tabs then the ones received
private let keepOnlyTabs = NotificationCenter.default.publisher(for: .keepOnlyTabs) private let keepOnlyTabs = NotificationCenter.default.publisher(for: .keepOnlyTabs)
private let payment = Payment()
var body: some View { var body: some View {
NavigationSplitView { NavigationSplitView {
@ -127,6 +129,21 @@ struct RootView: View {
} }
} }
} }
.safeAreaInset(edge: .bottom) {
if PKPaymentAuthorizationController.canMakePayments(
usingNetworks: Payment.supportedNetworks,
capabilities: Payment.capabilities
) {
PayWithApplePayButton(
.donate,
request: payment.donationRequest(),
onPaymentAuthorizationChange: payment.onPaymentAuthPhase(phase:),
onMerchantSessionRequested: payment.onMerchantSessionUpdate
)
.frame(width: 200, height: 30, alignment: .center)
.padding()
}
}
.frame(minWidth: 150) .frame(minWidth: 150)
} detail: { } detail: {
switch navigation.currentItem { switch navigation.currentItem {

67
Model/Payment.swift Normal file
View File

@ -0,0 +1,67 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
import Foundation
import PassKit
import SwiftUI
struct Payment {
static let merchantId = "merchant.org.kiwix"
static let supportedNetworks: [PKPaymentNetwork] = [.masterCard, .visa, .discover, .amex, .chinaUnionPay, .electron, .girocard]
static let capabilities: PKMerchantCapability = [.threeDSecure, .credit, .debit, .emv]
func donationRequest() -> PKPaymentRequest {
let request = PKPaymentRequest()
request.merchantIdentifier = Self.merchantId
request.merchantCapabilities = Self.capabilities
request.countryCode = "CH"
request.currencyCode = "USD"
request.supportedNetworks = Self.supportedNetworks
request.paymentSummaryItems = [
PKPaymentSummaryItem(label: "Kiwix", amount: 15, type: .final)
]
return request
}
func onPaymentAuthPhase(phase: PayWithApplePayButtonPaymentAuthorizationPhase) {
debugPrint("onPaymentAuthPhase: \(phase)")
switch phase {
case .willAuthorize:
break
case .didAuthorize(let payment, let resultHandler):
// server.process(with: payment) { serverResult in
// guard case .success = serverResult else {
// // handle error
// resultHandler(PKPaymentAuthorizationResult(status: .failure, errors: Error()))
// return
// }
// // handle success
// let result = PKPaymentAuthorizationResult(status: .success, errors: nil)
// resultHandler(result)
// }
break
case .didFinish:
break
@unknown default:
break
}
}
@available(macOS 13.0, *)
func onMerchantSessionUpdate() async -> PKPaymentRequestMerchantSessionUpdate {
.init(status: .success, merchantSession: nil)
}
}