Skip to content

Commit

Permalink
feat(POM-365): support nAPM failed state (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-vysotskyi-cko authored Apr 25, 2024
1 parent ca53829 commit 0ea8c68
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public enum PONativeAlternativePaymentMethodState: String, Decodable {

/// Invoice is captured.
case captured = "CAPTURED"

/// Payment did fail.
case failed = "FAILED"
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,8 @@ import UIKit
state = .submitting(snapshot: startedState)
invoicesService.initiatePayment(request: request) { [weak self] result in
switch result {
case let .success(response) where response.nativeApm.state == .pendingCapture:
self?.send(event: .didSubmitParameters(additionalParametersExpected: false))
self?.setAwaitingCaptureStateUnchecked(
gateway: startedState.gateway, parameterValues: response.nativeApm.parameterValues
)
case let .success(response) where response.nativeApm.state == .captured:
self?.setCapturedStateUnchecked(
gateway: startedState.gateway, parameterValues: response.nativeApm.parameterValues
)
case let .success(response):
self?.defaultValues(for: response.nativeApm.parameterDefinitions) { values in
self?.restoreStartedStateAfterSubmission(nativeApm: response.nativeApm, defaultValues: values)
}
self?.completeSubmissionUnchecked(with: response, startedState: startedState)
case let .failure(failure):
self?.restoreStartedStateAfterSubmissionFailureIfPossible(failure, replaceErrorMessages: true)
}
Expand Down Expand Up @@ -187,6 +176,9 @@ import UIKit
case .captured:
setCapturedStateUnchecked(gateway: details.gateway, parameterValues: details.parameterValues)
return
case .failed:
setFailureStateUnchecked(failure: POFailure(code: .generic(.mobile)))
return
}
if details.parameters.isEmpty {
logger.debug("Will set started state with empty inputs, this may be unexpected")
Expand All @@ -204,6 +196,31 @@ import UIKit
logger.info("Did start payment, waiting for parameters")
}

// MARK: - Submission

private func completeSubmissionUnchecked(
with response: PONativeAlternativePaymentMethodResponse, startedState: State.Started
) {
switch response.nativeApm.state {
case .customerInput:
defaultValues(for: response.nativeApm.parameterDefinitions) { [weak self] values in
self?.restoreStartedStateAfterSubmission(nativeApm: response.nativeApm, defaultValues: values)
}
case .pendingCapture:
send(event: .didSubmitParameters(additionalParametersExpected: false))
setAwaitingCaptureStateUnchecked(
gateway: startedState.gateway, parameterValues: response.nativeApm.parameterValues
)
case .captured:
setCapturedStateUnchecked(
gateway: startedState.gateway, parameterValues: response.nativeApm.parameterValues
)
case .failed:
let failure = POFailure(code: .generic(.mobile))
setFailureStateUnchecked(failure: failure)
}
}

// MARK: - Awaiting Capture State

private func setAwaitingCaptureStateUnchecked(
Expand Down

0 comments on commit 0ea8c68

Please sign in to comment.