Skip to content
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

chore: Use secrets file to get stripe publishable key in debug app #1028

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ fastlane/report.xml
### Primer
Debug App/Pods
Debug App/Podfile.lock
Debug App/secrets.defaults.properties
8 changes: 4 additions & 4 deletions Debug App/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ PODS:
- PrimerIPay88MYSDK (0.1.7)
- PrimerKlarnaSDK (1.1.1)
- PrimerNolPaySDK (1.0.1)
- PrimerSDK (2.31.1):
- PrimerSDK/Core (= 2.31.1)
- PrimerSDK/Core (2.31.1)
- PrimerSDK (2.31.2):
- PrimerSDK/Core (= 2.31.2)
- PrimerSDK/Core (2.31.2)
- PrimerStripeSDK (1.0.0)

DEPENDENCIES:
Expand Down Expand Up @@ -37,7 +37,7 @@ SPEC CHECKSUMS:
PrimerIPay88MYSDK: 436ee0be7e2c97e4e81456ccddee20175e9e3c4d
PrimerKlarnaSDK: 564105170cc7b467bf95c31851813ea41c468f8b
PrimerNolPaySDK: 08b140ed39b378a0b33b4f8746544a402175c0cc
PrimerSDK: ecc916d128b65c3881422562680bfe6562bf8106
PrimerSDK: b7d56fb276e17b5225c934b15c6aa25975d2db65
PrimerStripeSDK: c37d4e7c1b5256d67d4890c4cc4b38ddc9427489

PODFILE CHECKSUM: fa17ead44d40b0b09abc2f30a5cc3d8aefe389e1
Expand Down
8 changes: 8 additions & 0 deletions Debug App/Primer.io Debug App SPM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
040F19982CC69BE0005C9707 /* secrets.defaults.properties in Resources */ = {isa = PBXBuildFile; fileRef = 040F19972CC69BE0005C9707 /* secrets.defaults.properties */; };
040F199A2CC7A186005C9707 /* SecretsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 040F19992CC7A186005C9707 /* SecretsManager.swift */; };
041CD2FF2BFB7736009E6EF8 /* MerchantHeadlessCheckoutNolPayViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 041CD2FE2BFB7736009E6EF8 /* MerchantHeadlessCheckoutNolPayViewController.swift */; };
043C7FA22C49749E0018929E /* TestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043C7FA12C49749E0018929E /* TestHelper.swift */; };
0442C4072B7CE1E900EAD8EE /* TapGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0442C4062B7CE1E900EAD8EE /* TapGestureRecognizer.swift */; };
Expand Down Expand Up @@ -78,6 +80,8 @@
/* Begin PBXFileReference section */
00E3C8FE62D22147335F2455 /* MerchantResultViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MerchantResultViewController.swift; sourceTree = "<group>"; };
01C09DEAB07F42004B26A278 /* pt-PT */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-PT"; path = "pt-PT.lproj/LaunchScreen.strings"; sourceTree = "<group>"; };
040F19972CC69BE0005C9707 /* secrets.defaults.properties */ = {isa = PBXFileReference; lastKnownFileType = text; path = secrets.defaults.properties; sourceTree = SOURCE_ROOT; };
040F19992CC7A186005C9707 /* SecretsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretsManager.swift; sourceTree = "<group>"; };
041CD2FE2BFB7736009E6EF8 /* MerchantHeadlessCheckoutNolPayViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MerchantHeadlessCheckoutNolPayViewController.swift; sourceTree = "<group>"; };
043C7FA12C49749E0018929E /* TestHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestHelper.swift; sourceTree = "<group>"; };
0442C4062B7CE1E900EAD8EE /* TapGestureRecognizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TapGestureRecognizer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -189,6 +193,7 @@
0442C4052B7CE1E900EAD8EE /* Utilities */ = {
isa = PBXGroup;
children = (
040F19992CC7A186005C9707 /* SecretsManager.swift */,
043C7FA12C49749E0018929E /* TestHelper.swift */,
F08F63E12BA0BEAD006EF9A9 /* AppetizeConfigProvider.swift */,
F08F63E22BA0BEAD006EF9A9 /* MetadataParser.swift */,
Expand All @@ -211,6 +216,7 @@
094077DEFDC2739B10CF4183 /* Resources */ = {
isa = PBXGroup;
children = (
040F19972CC69BE0005C9707 /* secrets.defaults.properties */,
2A9F290E21D650154DEB2F19 /* Localized Views */,
B18D7E7738BF86467B0F1465 /* Images.xcassets */,
);
Expand Down Expand Up @@ -473,6 +479,7 @@
buildActionMask = 2147483647;
files = (
F00C66132ACC67FA00187028 /* Debug App Tests-Info.plist in Resources */,
040F19982CC69BE0005C9707 /* secrets.defaults.properties in Resources */,
F00C66142ACC67FA00187028 /* Images.xcassets in Resources */,
F00C66152ACC67FA00187028 /* LaunchScreen.xib in Resources */,
F00C66162ACC67FA00187028 /* Main.storyboard in Resources */,
Expand All @@ -495,6 +502,7 @@
044F805A2C6B4F9800E9F878 /* MerchantHeadlessCheckoutStripeAchViewController.swift in Sources */,
F00C65FA2ACC67FA00187028 /* Range+Extensions.swift in Sources */,
8723ADE72B8E0FAB00A5FE23 /* MerchantHeadlessCheckoutKlarnaViewController+Klarna.swift in Sources */,
040F199A2CC7A186005C9707 /* SecretsManager.swift in Sources */,
F00C65FB2ACC67FA00187028 /* String+Extensions.swift in Sources */,
049A055C2B4BF044002CEEBA /* MerchantHeadlessVaultManagerViewController.swift in Sources */,
F00C65FC2ACC67FA00187028 /* UIStackViewExtensions.swift in Sources */,
Expand Down
24 changes: 24 additions & 0 deletions Debug App/Primer.io Debug App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
040F19962CC67AA7005C9707 /* secrets.defaults.properties in Resources */ = {isa = PBXBuildFile; fileRef = 040F19942CC67AA7005C9707 /* secrets.defaults.properties */; };
044DE11B2CBFF93F00AAE28A /* secrets.defaults.properties in Resources */ = {isa = PBXBuildFile; fileRef = 044DE11A2CBFF93F00AAE28A /* secrets.defaults.properties */; };
044DE11D2CBFF98100AAE28A /* SecretsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 044DE11C2CBFF97D00AAE28A /* SecretsManager.swift */; };
044DE11F2CC0023200AAE28A /* SecretsManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 044DE11E2CC0023200AAE28A /* SecretsManagerTests.swift */; };
0453CB3B2C0A08AD00C8C750 /* DebugAppTestPlan.xctestplan in Resources */ = {isa = PBXBuildFile; fileRef = 0453CB3A2C0A08AD00C8C750 /* DebugAppTestPlan.xctestplan */; };
0478EAFE2C247BD600A4F5E2 /* TestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0478EAFD2C247BD600A4F5E2 /* TestHelper.swift */; };
0489E2BB2B68F71300FA0682 /* TapGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0489E2BA2B68F71300FA0682 /* TapGestureRecognizer.swift */; };
Expand Down Expand Up @@ -100,6 +104,10 @@
/* Begin PBXFileReference section */
00E3C8FE62D22147335F2455 /* MerchantResultViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MerchantResultViewController.swift; sourceTree = "<group>"; };
01C09DEAB07F42004B26A278 /* pt-PT */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-PT"; path = "pt-PT.lproj/LaunchScreen.strings"; sourceTree = "<group>"; };
040F19942CC67AA7005C9707 /* secrets.defaults.properties */ = {isa = PBXFileReference; lastKnownFileType = text; path = secrets.defaults.properties; sourceTree = "<group>"; };
044DE11A2CBFF93F00AAE28A /* secrets.defaults.properties */ = {isa = PBXFileReference; lastKnownFileType = text; path = secrets.defaults.properties; sourceTree = SOURCE_ROOT; };
044DE11C2CBFF97D00AAE28A /* SecretsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretsManager.swift; sourceTree = "<group>"; };
044DE11E2CC0023200AAE28A /* SecretsManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretsManagerTests.swift; sourceTree = "<group>"; };
0453CB3A2C0A08AD00C8C750 /* DebugAppTestPlan.xctestplan */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = DebugAppTestPlan.xctestplan; sourceTree = "<group>"; };
0478EAFD2C247BD600A4F5E2 /* TestHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelper.swift; sourceTree = "<group>"; };
0489E2BA2B68F71300FA0682 /* TapGestureRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TapGestureRecognizer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -227,9 +235,18 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
040F19952CC67AA7005C9707 /* Resources */ = {
isa = PBXGroup;
children = (
040F19942CC67AA7005C9707 /* secrets.defaults.properties */,
);
path = Resources;
sourceTree = "<group>";
};
0489E2B92B68F70400FA0682 /* Utilities */ = {
isa = PBXGroup;
children = (
044DE11C2CBFF97D00AAE28A /* SecretsManager.swift */,
0489E2BA2B68F71300FA0682 /* TapGestureRecognizer.swift */,
F08F63D92B9B5BC5006EF9A9 /* AppetizeConfigProvider.swift */,
F08F63DB2B9F27B0006EF9A9 /* MetadataParser.swift */,
Expand All @@ -249,18 +266,21 @@
04F323602BD408C600F5927C /* Tests */ = {
isa = PBXGroup;
children = (
040F19952CC67AA7005C9707 /* Resources */,
0453CB3A2C0A08AD00C8C750 /* DebugAppTestPlan.xctestplan */,
04C1906F2BEA5B75009A225A /* UnitTestsTestPlan.xctestplan */,
04F3235E2BD408C600F5927C /* Debug App Tests-Info.plist */,
04F3235D2BD408C600F5927C /* AppetizeConfigTests.swift */,
04F3235F2BD408C600F5927C /* MetadataParserTests.swift */,
044DE11E2CC0023200AAE28A /* SecretsManagerTests.swift */,
);
path = Tests;
sourceTree = "<group>";
};
094077DEFDC2739B10CF4183 /* Resources */ = {
isa = PBXGroup;
children = (
044DE11A2CBFF93F00AAE28A /* secrets.defaults.properties */,
2A9F290E21D650154DEB2F19 /* Localized Views */,
B18D7E7738BF86467B0F1465 /* Images.xcassets */,
);
Expand Down Expand Up @@ -559,6 +579,7 @@
buildActionMask = 2147483647;
files = (
A19EF5632B20E22E00A72F60 /* .swiftlint.yml in Resources */,
044DE11B2CBFF93F00AAE28A /* secrets.defaults.properties in Resources */,
EA7FAA4F8476BD3711D628CB /* Images.xcassets in Resources */,
F0C2147F6FA26527BE55549A /* LaunchScreen.xib in Resources */,
949864026D1CDE6F5C62C66E /* Main.storyboard in Resources */,
Expand All @@ -569,6 +590,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
040F19962CC67AA7005C9707 /* secrets.defaults.properties in Resources */,
0453CB3B2C0A08AD00C8C750 /* DebugAppTestPlan.xctestplan in Resources */,
04F323622BD408C600F5927C /* Debug App Tests-Info.plist in Resources */,
);
Expand Down Expand Up @@ -643,6 +665,7 @@
buildActionMask = 2147483647;
files = (
F03699592AC2E63700E4179D /* (null) in Sources */,
044DE11F2CC0023200AAE28A /* SecretsManagerTests.swift in Sources */,
04F323612BD408C600F5927C /* AppetizeConfigTests.swift in Sources */,
04F323632BD408C600F5927C /* MetadataParserTests.swift in Sources */,
);
Expand All @@ -663,6 +686,7 @@
DE53DA2D0AD108306C92E198 /* UIViewController+API.swift in Sources */,
85EDC3F175D699BFF6BD48EF /* ViewController+Primer.swift in Sources */,
60F6C3AFA11A7EDB0687856A /* ViewController+PrimerUIHelpers.swift in Sources */,
044DE11D2CBFF98100AAE28A /* SecretsManager.swift in Sources */,
5E24CD5B3084FE3E8A3E85EC /* CheckoutTheme.swift in Sources */,
04F323642BD409FE00F5927C /* BanksListModel.swift in Sources */,
87FC1AC92BE518BC00C9F474 /* MerchantHeadlessStripeAchFieldsView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "SECRETS_PATH=&quot;${PROJECT_DIR}/secrets.defaults.properties&quot;&#10;if [[ -f &quot;${SECRETS_PATH}&quot; ]]; then&#10; exit 0;&#10;fi&#10;cat &gt; &quot;${SECRETS_PATH}&quot; &lt;&lt;- EOM&#10;# Put secrets that are used by our SDK or third-party SDKs here&#10;#STRIPE_PUBLISHABLE_KEY=pk_test_*********&#10;EOM&#10;"
shellToInvoke = "/bin/bash">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F00C65F52ACC67FA00187028"
BuildableName = "Debug App SPM.app"
BlueprintName = "Debug App SPM"
ReferencedContainer = "container:Primer.io Debug App SPM.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
version = "1.3">
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "SECRETS_PATH=&quot;${PROJECT_DIR}/secrets.defaults.properties&quot;&#10;if [[ -f &quot;${SECRETS_PATH}&quot; ]]; then&#10; exit 0;&#10;fi&#10;cat &gt; &quot;${SECRETS_PATH}&quot; &lt;&lt;- EOM&#10;# Put secrets that are used by our SDK or third-party SDKs here&#10;#STRIPE_PUBLISHABLE_KEY=pk_test_*********&#10;EOM&#10;"
shellToInvoke = "/bin/bash">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BEB14ABCDF34E3D24759EAAB"
BuildableName = "Debug App.app"
BlueprintName = "Debug App"
ReferencedContainer = "container:Primer.io Debug App.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand Down
61 changes: 61 additions & 0 deletions Debug App/Sources/Utilities/SecretsManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// SecretsManager.swift
// Primer.io Debug App
//
// Created by Jack Newcombe on 16/10/2024.
// Copyright © 2024 Primer API Ltd. All rights reserved.
//

import Foundation
import PrimerSDK

class SecretsManager {

enum Keys: String {
case stripePublishableKey = "STRIPE_PUBLISHABLE_KEY"
}

private let logger = PrimerLogging.shared.logger

static let shared = SecretsManager()

let bundle: Bundle

init(bundle: Bundle = .main) {
self.bundle = bundle
}

lazy var properties: [Keys: String] = {
guard let fileUrl = bundle.url(forResource: "secrets.defaults", withExtension: "properties") else {
logger.warn(message: "Secrets file was not found in bundle. Check that `secrets.defaults.properties` is present in the root folder of the app.")
return [:]
}
guard let fileContents = FileManager.default.contents(atPath: fileUrl.path) else {
logger.warn(message: "Failed to load secrets file at path: \(fileUrl.path). Check that it is present and well formed.")
return [:]
}
guard let fileAsString = String(data: fileContents, encoding: .utf8) else {
logger.warn(message: "Failed to load secrets file as string. Check the file is a well formed and valid text file")
return [:]
}
var mapping: [Keys: String] = [:]
fileAsString.components(separatedBy: "\n").forEach { item in
// Allow skipping of comments lines
guard !item.hasPrefix("#") else{
return
}
let components = item.components(separatedBy: "=")
if components.count == 2, let key = Keys(rawValue: components[0]) {
mapping[key] = components[1]
} else {
logger.warn(message: "Tried to load poorly formed secret: \(item)")
}
}
return mapping
}()

func value(forKey key: Keys) -> String? {
properties.keys.contains(key) ? properties[key] : nil
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct MerchantMockDataManager {
}

static let customerIdStorageKey = "io.primer.debug.customer-id"
static let stripePublishableKey = "pk_test_51O8zfQKUK6bXIdC2xPvSS6UvriE9kwpvttwB7H9PAzhNAZoGzLgiDOyd4WooozeWHxoRrKo6b2VjZYxNCMX3W7bk00rMDivjau"

static var customerId: String {
if let customerId = UserDefaults.standard.string(forKey: customerIdStorageKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ class MerchantSessionAndSettingsViewController: UIViewController {
// additionalShippingContactFields: [.name, .emailAddress, .phoneNumber],
// requireShippingMethod: true) : nil

let stripePublishableKey = SecretsManager.shared.value(forKey: .stripePublishableKey)

let settings = PrimerSettings(
paymentHandling: selectedPaymentHandling,
paymentMethodOptions: PrimerPaymentMethodOptions(
Expand All @@ -645,7 +647,7 @@ class MerchantSessionAndSettingsViewController: UIViewController {
isCaptureBillingAddressEnabled: applePayCaptureBillingAddress,
showApplePayForUnsupportedDevice: false,
checkProvidedNetworks: applePayCheckProvidedNetworks),
stripeOptions: PrimerStripeOptions(publishableKey: MerchantMockDataManager.stripePublishableKey, mandateData: mandateData)),
stripeOptions: stripePublishableKey == nil ? nil : PrimerStripeOptions(publishableKey: stripePublishableKey!, mandateData: mandateData)),
uiOptions: uiOptions,
debugOptions: PrimerDebugOptions(is3DSSanityCheckEnabled: false)
)
Expand Down Expand Up @@ -673,6 +675,8 @@ class MerchantSessionAndSettingsViewController: UIViewController {
// additionalShippingContactFields: [.name, .emailAddress, .phoneNumber],
// requireShippingMethod: true) : nil

let stripePublishableKey = SecretsManager.shared.value(forKey: .stripePublishableKey)

let settings = PrimerSettings(
paymentHandling: selectedPaymentHandling,
paymentMethodOptions: PrimerPaymentMethodOptions(
Expand All @@ -683,7 +687,7 @@ class MerchantSessionAndSettingsViewController: UIViewController {
isCaptureBillingAddressEnabled: applePayCaptureBillingAddress,
showApplePayForUnsupportedDevice: false,
checkProvidedNetworks: applePayCheckProvidedNetworks),
stripeOptions: PrimerStripeOptions(publishableKey: MerchantMockDataManager.stripePublishableKey)),
stripeOptions: stripePublishableKey == nil ? nil : PrimerStripeOptions(publishableKey: stripePublishableKey!)),
uiOptions: nil,
debugOptions: PrimerDebugOptions(is3DSSanityCheckEnabled: false)
)
Expand Down
4 changes: 4 additions & 0 deletions Debug App/Tests/Resources/secrets.defaults.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Opening comment
STRIPE_PUBLISHABLE_KEY=pk_test_123
UNKNOWN=unknown
#COMMENTED=commented
22 changes: 22 additions & 0 deletions Debug App/Tests/SecretsManagerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// SecretsManaagerTests.swift
// Debug App Tests
//
// Created by Jack Newcombe on 16/10/2024.
// Copyright © 2024 Primer API Ltd. All rights reserved.
//

import XCTest
@testable import Debug_App

final class SecretsManagerTests: XCTestCase {

func testLoadFileAndGetSecret() throws {
let manager = SecretsManager(bundle: Bundle(for: type(of: self)))
XCTAssertEqual(manager.properties.count, 1)

let stripePublishableKey = manager.value(forKey: .stripePublishableKey)
XCTAssertEqual(stripePublishableKey, "pk_test_123")
}

}
Loading