Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
signalapp
GitHub Repository: signalapp/Signal-iOS
Path: blob/main/Signal/src/ViewControllers/Donations/DonationViewsUtil+WaitForSubscriptionJob.swift
1 views
//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//

import Foundation
public import SignalServiceKit

private extension Optional where Wrapped == DonationPaymentMethod {
    var timeoutDuration: TimeInterval {
        // Default to the longer duration if we don't know what payment method
        // we are.
        return self?.timeoutDuration ?? 30
    }
}

private extension DonationPaymentMethod {
    var timeoutDuration: TimeInterval {
        switch self {
        case .applePay, .creditOrDebitCard, .paypal:
            // We hope these payments will process quickly, so we'll wait a
            // decent amount of time before timing out in the hopes that we can
            // learn the status of the completed payment synchronously.
            return 30
        case .sepa, .ideal:
            // We expect SEPA payments (including those fronted by iDEAL)
            // will not process in a realistically-waitable time, so use
            // a shortened window before timing out for them.
            return 10
        }
    }
}

extension DonationViewsUtil {
    public static func waitForRedemption(paymentMethod: DonationPaymentMethod?, _ block: sending @escaping () async throws -> Void) async throws {
        do {
            try await withUncooperativeTimeout(seconds: paymentMethod.timeoutDuration, operation: block)
        } catch is UncooperativeTimeoutError {
            throw DonationJobError.timeout
        } catch {
            throw DonationJobError.assertion
        }
    }
}