Add merchant session for macOS

This commit is contained in:
Balazs Perlaki-Horvath 2024-11-14 08:51:11 +01:00 committed by Kelson
parent d92962c117
commit 44b0a74036
2 changed files with 22 additions and 1 deletions

View File

@ -24,6 +24,7 @@ struct Payment {
let completeSubject = PassthroughSubject<Bool, Never>() let completeSubject = PassthroughSubject<Bool, Never>()
static let merchantSessionURL = URL(string: "https://apple-pay-gateway.apple.com" )!
static let merchantId = "merchant.org.kiwix.apple" static let merchantId = "merchant.org.kiwix.apple"
static let paymentSubscriptionManagingURL = "https://www.kiwix.org" static let paymentSubscriptionManagingURL = "https://www.kiwix.org"
static let supportedNetworks: [PKPaymentNetwork] = [ static let supportedNetworks: [PKPaymentNetwork] = [
@ -141,6 +142,25 @@ struct Payment {
@available(macOS 13.0, *) @available(macOS 13.0, *)
func onMerchantSessionUpdate() async -> PKPaymentRequestMerchantSessionUpdate { func onMerchantSessionUpdate() async -> PKPaymentRequestMerchantSessionUpdate {
.init(status: .success, merchantSession: nil) var request = URLRequest(url: Self.merchantSessionURL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse,
(200..<300).contains(httpResponse.statusCode),
let dict = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
throw MerchantSessionError.invalidStatus
}
let session = PKPaymentMerchantSession(dictionary: dict)
return .init(status: .success, merchantSession: session)
} catch (let error) {
os_log("Merchant session not established: %@", type: .debug, error.localizedDescription)
return .init(status: .failure, merchantSession: nil)
}
} }
} }
private enum MerchantSessionError: Error {
case invalidStatus
}

View File

@ -41,6 +41,7 @@ struct StripeKiwix {
func clientSecretForPayment(selectedAmount: SelectedAmount) async -> Result<String, Error> { func clientSecretForPayment(selectedAmount: SelectedAmount) async -> Result<String, Error> {
do { do {
// TODO: for monthly this should create a setup-intent !
var request = URLRequest(url: endPoint.appending(path: "create-payment-intent")) var request = URLRequest(url: endPoint.appending(path: "create-payment-intent"))
request.httpMethod = "POST" request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("application/json", forHTTPHeaderField: "Content-Type")