             success = true
         }
         
-        var payload = type(of: self).commonPayload()
-        payload["event"] = paymentSheetPaymentEventValue(
-            isCustom: isCustom, paymentMethod: paymentMethod, success: success)
-        if isSimulatorOrTest {
-            payload["is_development"] = true
-        }
-        logPayload(payload)
+        logPaymentSheetEvent(event: paymentSheetPaymentEventValue(
+                                isCustom: isCustom,
+                                paymentMethod: paymentMethod,
+                                success: success))
     }
 
     func logPaymentSheetShow(
         isCustom: Bool,
         paymentMethod: AnalyticsPaymentMethodType
     ) {
-        var payload = type(of: self).commonPayload()
-        payload["event"] = paymentSheetShowEventValue(
-            isCustom: isCustom, paymentMethod: paymentMethod)
-        if isSimulatorOrTest {
-            payload["is_development"] = true
-        }
-        logPayload(payload)
+        logPaymentSheetEvent(event: paymentSheetShowEventValue(
+                                isCustom: isCustom,
+                                paymentMethod: paymentMethod))
     }
     
     func logPaymentSheetPaymentOptionSelect(
         isCustom: Bool,
         paymentMethod: AnalyticsPaymentMethodType
     ) {
-        var payload = type(of: self).commonPayload()
-        payload["event"] = paymentSheetPaymentOptionSelectEventValue(
-            isCustom: isCustom, paymentMethod: paymentMethod)
-        if isSimulatorOrTest {
-            payload["is_development"] = true
-        }
-        logPayload(payload)
+        logPaymentSheetEvent(event: paymentSheetPaymentOptionSelectEventValue(
+                                isCustom: isCustom,
+                                paymentMethod: paymentMethod))
     }
 
-    func paymentSheetInitEventValue(isCustom: Bool, configuration: PaymentSheet.Configuration)
+
+    // MARK: - String builders
+    enum AnalyticsPaymentMethodType : String {
+        case newPM = "newpm"
+        case savedPM = "savedpm"
+        case applePay = "applepay"
+    }
+    
+    private static let mc = "mc"
+    
+    private func customOrComplete(_ isCustom: Bool) -> String {
+        isCustom ? "custom" : "complete"
+    }
+    
+    private func paymentSheetInitEventValue(isCustom: Bool, configuration: PaymentSheet.Configuration)
         -> String
     {
         return [
-            "mc",
-            isCustom ? "custom" : "complete",
+            Self.mc,
+            customOrComplete(isCustom),
             "init",
             configuration.customer != nil ? "customer" : nil,
             configuration.applePay != nil ? "applepay" : nil,

import Foundation

extension STPAnalyticsClient {
    func logPaymentSheetInitialized(
        isCustom: Bool = false, configuration: PaymentSheet.Configuration
    ) {
        var payload = type(of: self).commonPayload()
        payload["event"] = paymentSheetInitEventValue(
            isCustom: isCustom, configuration: configuration)
        if isSimulatorOrTest {
            payload["is_development"] = true
        }
        logPayload(payload)
    }
    
    enum AnalyticsPaymentMethodType : String {
        case newPM = "newpm"
        case savedPM = "savedpm"
        case applePay = "applepay"
    }
    
    func logPaymentSheetPayment(
        isCustom: Bool,
        paymentMethod: AnalyticsPaymentMethodType,

        ].compactMap({ $0 }).joined(separator: "_")
    }
    
    private func paymentSheetShowEventValue(
        isCustom: Bool,
        paymentMethod: AnalyticsPaymentMethodType
    ) -> String
    {
        return [
            Self.mc,
            customOrComplete(isCustom),
            "sheet",
            paymentMethod.rawValue,
            "show",
        ].compactMap({ $0 }).joined(separator: "_")
    }
    
    private func paymentSheetPaymentEventValue(
        isCustom: Bool,
        paymentMethod: AnalyticsPaymentMethodType,
        success: Bool
    ) -> String
    {
        return [
            Self.mc,
            customOrComplete(isCustom),
            "payment",
            paymentMethod.rawValue,
            success ? "success" : "failure"
        ].compactMap({ $0 }).joined(separator: "_")
    }
    
    private func paymentSheetPaymentOptionSelectEventValue(
        isCustom: Bool,
        paymentMethod: AnalyticsPaymentMethodType
    ) -> String
    {
        return [
            Self.mc,
            customOrComplete(isCustom),
            "paymentoption",
            paymentMethod.rawValue,
            "select"
        ].compactMap({ $0 }).joined(separator: "_")
    }

    // MARK: - Internal
    private func logPaymentSheetEvent(event: String) {
        var payload = type(of: self).commonPayload()
        if isSimulatorOrTest {
            payload["is_development"] = true
        }
        payload["event"] = event
        logPayload(payload)
    }
    
    private var isSimulatorOrTest: Bool {
        #if targetEnvironment(simulator)
            return true
        #else
            return NSClassFromString("XCTest") != nil
        #endif
    }
    
}

extension PaymentSheetViewController.Mode {
