kiwix-apple/Model/Payment.swift
Balazs Perlaki-Horvath c28de99fe4 Update dependencies
2024-11-30 20:03:58 +01:00

120 lines
4.5 KiB
Swift

// 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
import Combine
import StripeApplePay
struct Payment {
let completeSubject = PassthroughSubject<Bool, Never>()
static let merchantId = "merchant.org.kiwix.apple"
static let stripePublicKey = "pk_test_oglo2v3Wc7ibH2oQe5oUDkhi"
static let paymentSubscriptionManagingURL = "https://www.kiwix.org"
static let supportedNetworks: [PKPaymentNetwork] = [
.amex,
.discover,
.electron,
.mada,
.maestro,
.masterCard,
.visa,
]
static let capabilities: PKMerchantCapability = [.threeDSecure, .credit, .debit, .emv]
static let currencyCodes = ["USD", "EUR", "CHF"]
static let defaultCurrencyCode = "USD"
static let minimumAmount: Double = 5
static let oneTimes: [AmountOption] = [
.init(value: 10),
.init(value: 34, isAverage: true),
.init(value: 50)
]
static let monthlies: [AmountOption] = [
.init(value: 5),
.init(value: 8, isAverage: true),
.init(value: 10)
]
static func paymentButtonType() -> PayWithApplePayButtonLabel? {
if PKPaymentAuthorizationController.canMakePayments() {
return PayWithApplePayButtonLabel.donate
}
if PKPaymentAuthorizationController.canMakePayments(
usingNetworks: Payment.supportedNetworks,
capabilities: Payment.capabilities) {
return PayWithApplePayButtonLabel.setUp
}
return nil
}
func donationRequest(for selectedAmount: SelectedAmount) -> PKPaymentRequest {
let request = PKPaymentRequest()
request.merchantIdentifier = Self.merchantId
request.merchantCapabilities = Self.capabilities
request.countryCode = "CH"
request.currencyCode = selectedAmount.currency
request.supportedNetworks = Self.supportedNetworks
request.requiredBillingContactFields = [.postalAddress]
let recurring: PKRecurringPaymentRequest? = if selectedAmount.isMonthly {
PKRecurringPaymentRequest(paymentDescription: "payment.description.label".localized,
regularBilling: .init(label: "payment.monthly_support.label".localized,
amount: NSDecimalNumber(value: selectedAmount.value),
type: .final),
managementURL: URL(string: Self.paymentSubscriptionManagingURL)!)
} else {
nil
}
request.recurringPaymentRequest = recurring
request.paymentSummaryItems = [
PKPaymentSummaryItem(
label: "payment.summary.title".localized,
amount: NSDecimalNumber(value: selectedAmount.value),
type: .final
)
]
return request
}
func onPaymentAuthPhase(phase: PayWithApplePayButtonPaymentAuthorizationPhase) {
switch phase {
case .willAuthorize:
break
case .didAuthorize(let payment, let resultHandler):
// call our server to get payment / setup intent and return the client.secret
// async http call...
let stripe = STPApplePaySimple()
stripe.complete(payment: payment,
returnURLPath: nil, // TODO: update the return path for confirmations
usingClientSecretProvider: )
let result = PKPaymentAuthorizationResult(status: .success, errors: nil)
resultHandler(result)
case .didFinish:
completeSubject.send(true)
@unknown default:
break
}
}
@available(macOS 13.0, *)
func onMerchantSessionUpdate() async -> PKPaymentRequestMerchantSessionUpdate {
.init(status: .success, merchantSession: nil)
}
}