Skip to content

Commit

Permalink
Add surcharge options to Setting screen
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisNikolic committed Dec 4, 2024
1 parent 233a650 commit ceca053
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 43 deletions.
10 changes: 5 additions & 5 deletions Debug App/Resources/Localized Views/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1235,13 +1235,13 @@
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="7" translatesAutoresizingMaskIntoConstraints="NO" id="vYu-uC-Lwg" userLabel="Surcharge Stack View">
<rect key="frame" x="0.0" y="51" width="374" height="58"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Apple Pay" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eF4-2g-Y8U">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Card Network surcharge" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eF4-2g-Y8U" userLabel="Card Network">
<rect key="frame" x="0.0" y="0.0" width="374" height="17"/>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="248" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="e.g. customer_1234" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="XRQ-T7-rG4">
<textField opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="248" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Amount " textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="XRQ-T7-rG4">
<rect key="frame" x="0.0" y="24" width="374" height="34"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
Expand Down Expand Up @@ -1327,7 +1327,6 @@
<outlet property="applePayShippingContactPhoneSwitch" destination="5AD-oh-3KE" id="2lJ-0f-1Zm"/>
<outlet property="applePayShippingControlStackView" destination="hmd-vB-Ft9" id="LCC-bs-UVf"/>
<outlet property="applePayShippingDetailsSwitch" destination="l2a-lu-nlK" id="RbG-0x-lQg"/>
<outlet property="applePaySurchargeTextField" destination="XRQ-T7-rG4" id="dnh-ml-Uz0"/>
<outlet property="applyThemingSwitch" destination="Xts-VY-QHZ" id="qQC-rP-kmS"/>
<outlet property="billingAddressCityTextField" destination="b68-sT-v40" id="537-Vp-BCo"/>
<outlet property="billingAddressCountryTextField" destination="Gxo-QT-E5X" id="8Yg-xu-Kez"/>
Expand Down Expand Up @@ -1380,6 +1379,7 @@
<outlet property="surchargeGroupStackView" destination="32O-OV-0ey" id="FDB-vq-q3D"/>
<outlet property="surchargeStackView" destination="vYu-uC-Lwg" id="7yI-tG-axF"/>
<outlet property="surchargeSwitch" destination="9eb-B5-B2t" id="bc9-Bn-FWV"/>
<outlet property="surchargeTextField" destination="XRQ-T7-rG4" id="dnh-ml-Uz0"/>
<outlet property="test3DSScenarioTextField" destination="io7-2z-MxC" id="Cy0-nh-2P5"/>
<outlet property="test3DSStackView" destination="fxV-mt-ulz" id="65c-eM-qZo"/>
<outlet property="testErrorDescriptionTextField" destination="dNP-tk-egr" id="0np-AM-Wdo"/>
Expand Down Expand Up @@ -1910,13 +1910,13 @@
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="placeholderTextColor">
<color red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.29803921570000003" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.23529411764705882" green="0.23529411764705882" blue="0.2627450980392157" alpha="0.29803921568627451" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemRedColor">
<color red="1" green="0.23137254900000001" blue="0.18823529410000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="1" green="0.23137254901960785" blue="0.18823529411764706" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
71 changes: 59 additions & 12 deletions Debug App/Sources/Model/CreateClientToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,21 +376,24 @@ struct ClientSessionRequestBody {
var extraMerchantData: [String: Any]?
var captureVaultedCardCvv: Bool?
var merchantName: String?
var networks: NetworkOptionGroup?

enum CodingKeys: CodingKey {
case surcharge, instalmentDuration, extraMerchantData, captureVaultedCardCvv, merchantName
case surcharge, instalmentDuration, extraMerchantData, captureVaultedCardCvv, merchantName, networks
}

init(surcharge: SurchargeOption?,
instalmentDuration: String?,
extraMerchantData: [String: Any]?,
captureVaultedCardCvv: Bool?,
merchantName: String?) {
merchantName: String?,
networks: NetworkOptionGroup?) {
self.surcharge = surcharge
self.instalmentDuration = instalmentDuration
self.extraMerchantData = extraMerchantData
self.captureVaultedCardCvv = captureVaultedCardCvv
self.merchantName = merchantName
self.networks = networks
}

func encode(to encoder: Encoder) throws {
Expand All @@ -409,27 +412,36 @@ struct ClientSessionRequestBody {
let jsonString = String(data: jsonData, encoding: .utf8)
try container.encode(jsonString, forKey: .extraMerchantData)
}


if let captureVaultedCardCvv = captureVaultedCardCvv {
try container.encode(captureVaultedCardCvv, forKey: .captureVaultedCardCvv)
}

if let merchantName = merchantName {
try container.encode(merchantName, forKey: .merchantName)
}

if let networks = networks {
try container.encode(networks, forKey: .networks)
}
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

surcharge = try container.decodeIfPresent(SurchargeOption.self, forKey: .surcharge)
instalmentDuration = try container.decodeIfPresent(String.self, forKey: .instalmentDuration)

let jsonString = try container.decodeIfPresent(String.self, forKey: .extraMerchantData)
if let jsonData = jsonString?.data(using: .utf8) {
if let jsonString = try container.decodeIfPresent(String.self, forKey: .extraMerchantData),
let jsonData = jsonString.data(using: .utf8) {
extraMerchantData = try JSONSerialization.jsonObject(with: jsonData) as? [String: Any]
} else {
extraMerchantData = nil
}

merchantName = try container.decodeIfPresent(String.self, forKey: .merchantName)

captureVaultedCardCvv = try container.decodeIfPresent(Bool.self, forKey: .captureVaultedCardCvv) ?? false
captureVaultedCardCvv = try container.decodeIfPresent(Bool.self, forKey: .captureVaultedCardCvv)
merchantName = try container.decodeIfPresent(String.self, forKey: .merchantName)
networks = try container.decodeIfPresent(NetworkOptionGroup.self, forKey: .networks)
}

var dictionaryValue: [String: Any]? {
Expand All @@ -447,15 +459,19 @@ struct ClientSessionRequestBody {
dic["extraMerchantData"] = extraMerchantData
}

if let captureVaultedCardCvv = captureVaultedCardCvv, captureVaultedCardCvv == true {
if let captureVaultedCardCvv = captureVaultedCardCvv {
dic["captureVaultedCardCvv"] = captureVaultedCardCvv
}

if let merchantName = merchantName {
dic["merchantName"] = merchantName
}

return dic.keys.count == 0 ? nil : dic
if let networks = networks {
dic["networks"] = networks.dictionaryValue
}

return dic.isEmpty ? nil : dic
}
}

Expand All @@ -472,8 +488,39 @@ struct ClientSessionRequestBody {
return dic.keys.count == 0 ? nil : dic
}
}
}

struct NetworkOptionGroup: Codable {
var VISA: NetworkOption?
var MASTERCARD: NetworkOption?
var JCB: NetworkOption?

var dictionaryValue: [String: Any]? {
var dic: [String: Any] = [:]

if let VISA = VISA {
dic["VISA"] = VISA.dictionaryValue
}

if let MASTERCARD = MASTERCARD {
dic["MASTERCARD"] = MASTERCARD.dictionaryValue
}

if let JCB = JCB {
dic["JCB"] = JCB.dictionaryValue
}

return dic.isEmpty ? nil : dic
}
}

struct NetworkOption: Codable {
var surcharge: SurchargeOption

var dictionaryValue: [String: Any] {
return ["surcharge": surcharge.dictionaryValue ?? [:]]
}
}
}
}

extension Encodable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ struct MerchantMockDataManager {
instalmentDuration: "test",
extraMerchantData: extraMerchantData,
captureVaultedCardCvv: false,
merchantName: nil))
merchantName: nil,
networks: nil))

static var extraMerchantData: [String: Any] = [
"subscription": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class MerchantSessionAndSettingsViewController: UIViewController {

@IBOutlet weak var surchargeSwitch: UISwitch!
@IBOutlet weak var surchargeStackView: UIStackView!
@IBOutlet weak var applePaySurchargeTextField: UITextField!
@IBOutlet weak var surchargeTextField: UITextField!

@IBOutlet weak var primerSDKButton: UIButton!
@IBOutlet weak var primerHeadlessSDKButton: UIButton!
Expand Down Expand Up @@ -525,19 +525,35 @@ class MerchantSessionAndSettingsViewController: UIViewController {
instalmentDuration: nil,
extraMerchantData: nil,
captureVaultedCardCvv: enableCVVRecaptureFlowSwitch.isOn,
merchantName: nil)
merchantName: nil,
networks: nil)

clientSession.paymentMethod?.options?.PAYMENT_CARD = option
}

let applePayOptions = ClientSessionRequestBody.PaymentMethod.PaymentMethodOption(surcharge: nil,
instalmentDuration: nil,
extraMerchantData: nil,
captureVaultedCardCvv: nil,
merchantName: "Primer Merchant iOS")

let applePayOptions = ClientSessionRequestBody.PaymentMethod.PaymentMethodOption(surcharge: nil,
instalmentDuration: nil,
extraMerchantData: nil,
captureVaultedCardCvv: nil,
merchantName: "Primer Merchant iOS",
networks: nil)
clientSession.paymentMethod?.options?.APPLE_PAY = applePayOptions


if let text = surchargeTextField.text, let amount = Int(text), surchargeSwitch.isOn {
let surcharge = ClientSessionRequestBody.PaymentMethod.SurchargeOption(amount: amount)
var networkOptionGroup = ClientSessionRequestBody.PaymentMethod.NetworkOptionGroup()
networkOptionGroup.VISA = ClientSessionRequestBody.PaymentMethod.NetworkOption(surcharge: surcharge)
networkOptionGroup.JCB = ClientSessionRequestBody.PaymentMethod.NetworkOption(surcharge: surcharge)
networkOptionGroup.MASTERCARD = ClientSessionRequestBody.PaymentMethod.NetworkOption(surcharge: surcharge)
let paymentCardOptions = ClientSessionRequestBody.PaymentMethod.PaymentMethodOption(surcharge: nil,
instalmentDuration: nil,
extraMerchantData: nil,
captureVaultedCardCvv: nil,
merchantName: "Primer Merchant iOS",
networks: networkOptionGroup)
clientSession.paymentMethod?.options?.PAYMENT_CARD = paymentCardOptions
}

if let metadata = metadataTextField.text, !metadata.isEmpty {
clientSession.metadata = MetadataParser().parse(metadata)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class CardFormPaymentMethodTokenizationViewModel: PaymentMethodTokenizationViewM
clientSessionActionsModule.selectPaymentMethodIfNeeded(self.config.type, cardNetwork: cardNetwork.network.rawValue)
}
.done {
self.configurePayButton(cardNetwork: cardNetwork.network)
self.configureAmountLabels(cardNetwork: cardNetwork.network)
}
.catch { _ in }
}
Expand Down Expand Up @@ -697,13 +697,16 @@ class CardFormPaymentMethodTokenizationViewModel: PaymentMethodTokenizationViewM
}
}

func configurePayButton(cardNetwork: CardNetwork?) {
var amount: Int = AppState.current.amount ?? 0
func configureAmountLabels(cardNetwork: CardNetwork?) {

if let surcharge = alternativelySelectedCardNetwork?.surcharge ?? cardNetwork?.surcharge {
amount += surcharge
if let surcharge = alternativelySelectedCardNetwork?.surcharge ?? cardNetwork?.surcharge,
PrimerAPIConfigurationModule.apiConfiguration?.clientSession?.order?.merchantAmount == nil {
configureSurchargeLabel(surchargetAmount: surcharge)
} else {
hideSurchargeLabel()
}

var amount: Int = AppState.current.amount ?? 0

Check warning on line 709 in Sources/PrimerSDK/Classes/PCI/Tokenization View Models/FormsTokenizationViewModel/CardFormPaymentMethodTokenizationViewModel.swift

View workflow job for this annotation

GitHub Actions / Unit Tests - SDK

variable 'amount' was never mutated; consider changing to 'let' constant
configurePayButton(amount: amount)
}

Expand All @@ -720,6 +723,18 @@ class CardFormPaymentMethodTokenizationViewModel: PaymentMethodTokenizationViewM
}
}

func configureSurchargeLabel(surchargetAmount: Int) {
DispatchQueue.main.async {
print(surchargetAmount)
}
}

func hideSurchargeLabel() {
DispatchQueue.main.async {
print("hideSurchargeLabel")
}
}

override func submitButtonTapped() {
self.uiModule.submitButton?.startAnimating()
let viewEvent = Analytics.Event.ui(
Expand Down Expand Up @@ -985,7 +1000,7 @@ extension CardFormPaymentMethodTokenizationViewModel: PrimerTextFieldViewDelegat
clientSessionActionsModule.selectPaymentMethodIfNeeded(self.config.type, cardNetwork: network)
}
.done {
self.updateButtonUI()
self.configureAmountLabels(cardNetwork: cardNetwork)
}
.catch { _ in }

Expand All @@ -997,22 +1012,13 @@ extension CardFormPaymentMethodTokenizationViewModel: PrimerTextFieldViewDelegat
clientSessionActionsModule.unselectPaymentMethodIfNeeded()
}
.done {
self.updateButtonUI()
self.configureAmountLabels(cardNetwork: cardNetwork)
}
.catch { _ in }
}
}
}

extension CardFormPaymentMethodTokenizationViewModel {

private func updateButtonUI() {
if let amount = AppState.current.amount, self.uiModule.isSubmitButtonAnimating == false {
self.configurePayButton(amount: amount)
}
}
}

extension CardFormPaymentMethodTokenizationViewModel: SFSafariViewControllerDelegate {

func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
Expand Down

0 comments on commit ceca053

Please sign in to comment.