-
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
753 donate with apple pay button for iOS to support Kiwix #1022
Merged
kelson42
merged 54 commits into
main
from
753-implement-apple-pay-and-use-the-donate-with-apple-pay-button-for-support-kiwix
Nov 30, 2024
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
05ee7c9
Add merchantID for ApplePay
BPerlakiH 3fef622
Import PassKit
BPerlakiH ce2a80c
Initial payment setup
BPerlakiH 695b7e4
Add Donate with Apple Pay button to iOS
BPerlakiH 9fcee6b
Add payment files
BPerlakiH d3b1bf6
macOS donation window
BPerlakiH 244c896
Integrating PaymentForm
BPerlakiH 9c5c66f
Fixup for iOS
BPerlakiH d76de07
Handle window close on transaction complete
BPerlakiH 64bc860
Close popup on success/fail
BPerlakiH 1ce64a1
Fixlint, handle sheet to be closed on iOS
BPerlakiH 1e26839
Localize strings
BPerlakiH 3a3c605
Clean up
BPerlakiH cd0df1a
Remove unused value
BPerlakiH 6113433
Update merchant id
BPerlakiH 027a294
Remove in app payments merchant id
BPerlakiH 5462667
Add Stripe
BPerlakiH f53347c
Add empty merchant id
BPerlakiH 9fd6b84
Remove stripe dependency
BPerlakiH 9509d7c
Revert "Remove stripe dependency"
BPerlakiH 2a1f2a6
Add merchant id
BPerlakiH cd9af5c
Add StipeApplePay to iOS
BPerlakiH ce530ae
Add merchant id, again
BPerlakiH c28de99
Update dependencies
BPerlakiH 9e9dadc
Remove dependency
BPerlakiH 5b91de8
Add dependency
BPerlakiH 24486ac
Externalise public key, test with local server
BPerlakiH d92962c
Add merchant id, log payment phase. Working on iPad sym with local se…
BPerlakiH 44b0a74
Add merchant session for macOS
BPerlakiH c4e5087
Change required postal address to email address
BPerlakiH 60a1d10
Fixlint
BPerlakiH 3a61563
Use api.donation.kiwix.org
rgaudin ddb5d0a
Fixlint
BPerlakiH 86e1518
Show Thank You on donation success
BPerlakiH f0fa920
Show thank you on donation success
BPerlakiH 889af89
Remove support button for macOS
BPerlakiH cfcb6fc
Disable monthly options from payments
BPerlakiH af9a95b
Add payment log
BPerlakiH 7b910dc
Fixlint
BPerlakiH b8f5f9d
Fix closures
BPerlakiH 7018978
Try build with full bundleID
BPerlakiH 90ee8c7
Revert "Try build with full bundleID"
BPerlakiH efa35e6
Remove in app payments merchant id
BPerlakiH 281d618
Add merchant id to Kiwix target only
BPerlakiH 9f761d4
Revert CI/CD to develop
BPerlakiH 52727f5
Fix donation button click area, header padding
BPerlakiH 6ae88f6
Add docs to Payment
BPerlakiH d23e30e
Payment docs up
BPerlakiH f18edc6
Revert CI / CD to main
BPerlakiH 92b6f73
Revert
BPerlakiH a0a585a
Add all payment methods
BPerlakiH 9335169
Add Error pop up for donations
BPerlakiH ea40c54
Add macOS payment flow, but still with disabled UI
BPerlakiH 9692781
Fixlint
BPerlakiH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// 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 | ||
import os | ||
|
||
/// Payment processing based on: | ||
/// Apple-Pay button: | ||
/// https://developer.apple.com/documentation/passkit_apple_pay_and_wallet/apple_pay#2872687 | ||
/// as described in: What’s new in Wallet and Apple Pay from WWDC 2022 | ||
/// (https://developer.apple.com/videos/play/wwdc2022/10041/) | ||
/// | ||
/// Combined with Stripe's lightweight Apple Pay framework | ||
/// https://github.com/stripe/stripe-ios/blob/master/StripeApplePay/README.md | ||
/// based on the App Clip example project: | ||
/// https://github.com/stripe/stripe-ios/tree/master/Example/AppClipExample | ||
/// | ||
/// Whereas the Stripe SDK is based on the older | ||
/// PKPaymentAuthorizationController (before PayWithApplePayButton was available) | ||
/// https://developer.apple.com/documentation/passkit_apple_pay_and_wallet/apple_pay#2870963 | ||
/// | ||
/// The Stripe SDK has been brought up to date (with 2022 WWDC changes) | ||
/// and modified to be compatible with macOS as well, see SPM dependencies | ||
/// https://github.com/CodeLikeW/stripe-apple-pay | ||
/// https://github.com/CodeLikeW/stripe-core | ||
struct Payment { | ||
|
||
enum FinalResult { | ||
case thankYou | ||
case error | ||
} | ||
|
||
/// Decides if the Thank You / Error pop up should be shown | ||
/// - Returns: `FinalResult` only once | ||
@MainActor | ||
static func showResult() -> FinalResult? { | ||
// make sure `true` is "read only once" | ||
let value = Self.finalResult | ||
Self.finalResult = nil | ||
return value | ||
} | ||
@MainActor | ||
static private var finalResult: Payment.FinalResult? | ||
|
||
let completeSubject = PassthroughSubject<Void, Never>() | ||
|
||
static let kiwixPaymentServer = URL(string: "https://api.donation.kiwix.org/v1/stripe")! | ||
static let merchantSessionURL = URL(string: "https://apple-pay-gateway.apple.com" )! | ||
static let merchantId = "merchant.org.kiwix.apple" | ||
static let paymentSubscriptionManagingURL = "https://www.kiwix.org" | ||
static let supportedNetworks: [PKPaymentNetwork] = [ | ||
BPerlakiH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.amex, | ||
.bancomat, | ||
.bancontact, | ||
.cartesBancaires, | ||
.chinaUnionPay, | ||
.dankort, | ||
.discover, | ||
.eftpos, | ||
.electron, | ||
.elo, | ||
.girocard, | ||
.interac, | ||
.idCredit, | ||
.JCB, | ||
.mada, | ||
.maestro, | ||
.masterCard, | ||
.mir, | ||
.privateLabel, | ||
.quicPay, | ||
.suica, | ||
.visa, | ||
.vPay | ||
] | ||
static let capabilities: PKMerchantCapability = [.threeDSecure, .credit, .debit, .emv] | ||
|
||
/// NOTE: consider that these currencies support double precision, eg: 5.25 USD. | ||
/// Revisit `SelectedAmount`, and `SelectedPaymentAmount` | ||
/// before adding a zero-decimal currency such as: ¥100 | ||
static let currencyCodes = ["USD", "EUR", "CHF"] | ||
static let defaultCurrencyCode = "USD" | ||
private static let minimumAmount: Double = 5 | ||
/// The Sripe `amount` value supports up to eight digits | ||
/// (e.g., a value of 99999999 for a USD charge of $999,999.99). | ||
/// see: https://docs.stripe.com/api/payment_intents/object#payment_intent_object-amount | ||
static let maximumAmount: Int = 99999999 | ||
static func isInValidRange(amount: Double?) -> Bool { | ||
guard let amount else { return false } | ||
return minimumAmount <= amount && amount <= Double(maximumAmount)*100.0 | ||
} | ||
|
||
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) | ||
] | ||
|
||
/// Checks Apple Pay capabilities, and returns the button label accrodingly | ||
/// Setup button if no cards added yet, | ||
/// nil if Apple Pay is not supported | ||
/// or donation button, if all is OK | ||
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.merchantCapabilities = .threeDSecure | ||
request.requiredBillingContactFields = [.emailAddress] | ||
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(selectedAmount: SelectedAmount, | ||
phase: PayWithApplePayButtonPaymentAuthorizationPhase) { | ||
switch phase { | ||
case .willAuthorize: | ||
os_log("onPaymentAuthPhase: .willAuthorize") | ||
case .didAuthorize(let payment, let resultHandler): | ||
os_log("onPaymentAuthPhase: .didAuthorize") | ||
// call our server to get payment / setup intent and return the client.secret | ||
Task { @MainActor [resultHandler] in | ||
let paymentServer = StripeKiwix(endPoint: Self.kiwixPaymentServer, | ||
payment: payment) | ||
do { | ||
let publicKey = try await paymentServer.publishableKey() | ||
StripeAPI.defaultPublishableKey = publicKey | ||
} catch let serverError { | ||
Self.finalResult = .error | ||
resultHandler(.init(status: .failure, errors: [serverError])) | ||
return | ||
} | ||
// we should update the return path for confirmations | ||
// see: https://github.com/kiwix/kiwix-apple/issues/1032 | ||
let stripe = StripeApplePaySimple() | ||
let result = await stripe.complete(payment: payment, | ||
returnURLPath: nil, | ||
usingClientSecretProvider: { | ||
await paymentServer.clientSecretForPayment(selectedAmount: selectedAmount) | ||
}) | ||
// calling any UI refreshing state / subject from here | ||
// will block the UI in the payment state forever | ||
// therefore it's defered via static finalResult | ||
switch result.status { | ||
case .success: | ||
Self.finalResult = .thankYou | ||
case .failure: | ||
Self.finalResult = .error | ||
default: | ||
Self.finalResult = nil | ||
} | ||
resultHandler(result) | ||
os_log("onPaymentAuthPhase: .didAuthorize: \(result.status == .success)") | ||
} | ||
case .didFinish: | ||
os_log("onPaymentAuthPhase: .didFinish") | ||
completeSubject.send(()) | ||
@unknown default: | ||
os_log("onPaymentAuthPhase: @unknown default") | ||
} | ||
|
||
} | ||
|
||
@available(macOS 13.0, *) | ||
func onMerchantSessionUpdate() async -> PKPaymentRequestMerchantSessionUpdate { | ||
guard let session = await StripeKiwix.stripeSession(endPoint: Self.kiwixPaymentServer) else { | ||
await MainActor.run { | ||
Self.finalResult = .error | ||
} | ||
return .init(status: .failure, merchantSession: nil) | ||
} | ||
return .init(status: .success, merchantSession: session) | ||
} | ||
} | ||
|
||
private enum MerchantSessionError: Error { | ||
case invalidStatus | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we disable the support button for macOS