-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from checkout/release/0.1.2-stub
Stub release for 0.2.0
- Loading branch information
Showing
31 changed files
with
8,158 additions
and
1,107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
Sources/CheckoutCardManagement/Models/CardDetails/Card+Management.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// | ||
// Card+Management.swift | ||
// | ||
// | ||
// Created by Alex Ioja-Yang on 19/01/2023. | ||
// | ||
|
||
import Foundation | ||
import CheckoutCardNetwork | ||
|
||
public extension Card { | ||
|
||
/// Possible Card State changes from the current state | ||
var possibleStateChanges: [CardState] { | ||
switch self.state { | ||
case .inactive, .suspended: | ||
return [.active, .revoked] | ||
case .active: | ||
return [.suspended, .revoked] | ||
case .revoked: | ||
return [] | ||
} | ||
} | ||
|
||
/// Add the card object to the Apple Wallet | ||
/// | ||
/// - Parameters: | ||
/// - cardhodlerID: Identifier for the cardholder owning the card | ||
/// - configuration: Specialised object used for Push Provisioning, received during Onboarding | ||
/// - provisioningToken: Push Provisioning token | ||
/// - completionHandler: Completion Handler returning the outcome of the provisioning operation | ||
func provision(cardholderID: String, | ||
configuration: ProvisioningConfiguration, | ||
provisioningToken: String, | ||
completionHandler: @escaping ((CheckoutCardManager.OperationResult) -> Void)) { | ||
let startTime = Date() | ||
manager?.cardService.addCardToAppleWallet(cardID: self.id, | ||
cardholderID: cardholderID, | ||
configuration: configuration, | ||
token: provisioningToken) { [weak self] result in | ||
switch result { | ||
case .success: | ||
let event = LogEvent.pushProvisioning(last4CardID: self?.partIdentifier ?? "", | ||
last4CardholderID: String(cardholderID.suffix(4))) | ||
self?.manager?.logger?.log(event, startedAt: startTime) | ||
completionHandler(.success) | ||
case .failure(let networkError): | ||
self?.manager?.logger?.log(.failure(source: "Push Provisioning", error: networkError), startedAt: startTime) | ||
completionHandler(.failure(.from(networkError))) | ||
} | ||
} | ||
} | ||
|
||
/// Request to activate the card | ||
func activate(completionHandler: @escaping ((CheckoutCardManager.OperationResult) -> Void)) { | ||
guard possibleStateChanges.contains(.active) else { | ||
completionHandler(.failure(.invalidStateRequested)) | ||
return | ||
} | ||
guard let sessionToken = manager?.sessionToken else { | ||
completionHandler(.failure(.unauthenticated)) | ||
return | ||
} | ||
let startTime = Date() | ||
|
||
manager?.cardService.activateCard(cardID: id, | ||
sessionToken: sessionToken) { [weak self] result in | ||
self?.handleCardOperationResult(result: result, | ||
newState: .active, | ||
startTime: startTime, | ||
operationSource: "Activate Card", | ||
completionHandler: completionHandler) | ||
} | ||
} | ||
|
||
/// Request to suspend the card, with option to provide a reason for change | ||
func suspend(reason: CardSuspendReason?, | ||
completionHandler: @escaping ((CheckoutCardManager.OperationResult) -> Void)) { | ||
guard possibleStateChanges.contains(.suspended) else { | ||
completionHandler(.failure(.invalidStateRequested)) | ||
return | ||
} | ||
guard let sessionToken = manager?.sessionToken else { | ||
completionHandler(.failure(.unauthenticated)) | ||
return | ||
} | ||
let startTime = Date() | ||
|
||
manager?.cardService.suspendCard(cardID: id, | ||
reason: reason, | ||
sessionToken: sessionToken) { [weak self] result in | ||
self?.handleCardOperationResult(result: result, | ||
newState: .suspended, | ||
reasonString: reason?.rawValue, | ||
startTime: startTime, | ||
operationSource: "Suspend Card", | ||
completionHandler: completionHandler) | ||
} | ||
} | ||
|
||
/// Request to revoke the card, with option to provide a reason for change | ||
func revoke(reason: CardRevokeReason?, | ||
completionHandler: @escaping ((CheckoutCardManager.OperationResult) -> Void)) { | ||
guard possibleStateChanges.contains(.revoked) else { | ||
completionHandler(.failure(.invalidStateRequested)) | ||
return | ||
} | ||
guard let sessionToken = manager?.sessionToken else { | ||
completionHandler(.failure(.unauthenticated)) | ||
return | ||
} | ||
let startTime = Date() | ||
|
||
manager?.cardService.revokeCard(cardID: id, | ||
reason: reason, | ||
sessionToken: sessionToken) { [weak self] result in | ||
self?.handleCardOperationResult(result: result, | ||
newState: .revoked, | ||
reasonString: reason?.rawValue, | ||
startTime: startTime, | ||
operationSource: "Revoke Card", | ||
completionHandler: completionHandler) | ||
} | ||
} | ||
|
||
private func handleCardOperationResult(result: OperationResult, | ||
newState: CardState, | ||
reasonString: String? = nil, | ||
startTime: Date, | ||
operationSource: String, | ||
completionHandler: @escaping ((CheckoutCardManager.OperationResult) -> Void)) { | ||
switch result { | ||
case .success: | ||
let event = LogEvent.stateManagement(idLast4: partIdentifier, | ||
originalState: state, | ||
requestedState: newState, | ||
reason: reasonString) | ||
manager?.logger?.log(event, startedAt: startTime) | ||
state = newState | ||
completionHandler(.success) | ||
case .failure(let networkError): | ||
manager?.logger?.log(.failure(source: operationSource, error: networkError), startedAt: startTime) | ||
completionHandler(.failure(.from(networkError))) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.