-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ApplePayTokenUseCase and tests (#2258)
* Create ApplePayTokenUseCase * Tweak input/output protocols for ApplePayTokenUseCase * Make changes to make ApplePayTokenUseCase easier to integrate
- Loading branch information
1 parent
dbf6b11
commit 1a79974
Showing
3 changed files
with
326 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import Foundation | ||
import KsApi | ||
import PassKit | ||
import ReactiveSwift | ||
|
||
public protocol ApplePayTokenUseCaseType { | ||
var uiInputs: ApplePayTokenUseCaseUIInputs { get } | ||
var uiOutputs: ApplePayTokenUseCaseUIOutputs { get } | ||
var dataOutputs: ApplePayTokenUseCaseDataOutputs { get } | ||
} | ||
|
||
public protocol ApplePayTokenUseCaseUIInputs { | ||
func applePayButtonTapped() | ||
func paymentAuthorizationDidAuthorizePayment(paymentData: ( | ||
displayName: String?, | ||
network: String?, | ||
transactionIdentifier: String | ||
)) | ||
func paymentAuthorizationViewControllerDidFinish() | ||
func stripeTokenCreated(token: String?, error: Error?) -> PKPaymentAuthorizationStatus | ||
} | ||
|
||
public protocol ApplePayTokenUseCaseUIOutputs { | ||
var goToApplePayPaymentAuthorization: Signal<PaymentAuthorizationData, Never> { get } | ||
} | ||
|
||
public protocol ApplePayTokenUseCaseDataOutputs { | ||
var applePayParams: Signal<ApplePayParams?, Never> { get } | ||
var applePayAuthorizationStatus: Signal<PKPaymentAuthorizationStatus, Never> { get } | ||
var paymentAuthorizationDidFinish: Signal<Void, Never> { get } | ||
} | ||
|
||
/** | ||
A use case for ApplePay transactions in the regular (not post!) pledge flow. | ||
|
||
To complete an ApplePay payment, the use case should be used in this order: | ||
- `uiInputs.applePayButtonTapped()` - The ApplePay button has been tapped | ||
- `uiOutputs.goToApplePayPaymentAuthorization` - The view controller should display a `PKPaymentAuthorizationViewController` with the sent `PKPaymentRequest` | ||
- `uiInputs.paymentAuthorizationDidAuthorizePayment(paymentData:)` - The `PKPaymentAuthorizationViewController` successfully authorized a payment | ||
- `uiInputs.stripeTokenCreated(token:error:)` - Stripe successfully turned the `PKPayment` into a Stripe token. Returns a status, which is also sent by the `applePayAuthorizationStatus` signal. | ||
- `uiInputs.paymentAuthorizationViewControllerDidFinish()` - The `PKPaymentAuthorizationViewController` was dismissed | ||
- `dataOutputs.applePayParams` - Sends parameters which can be used in `CreateBacking` or `UpdateBacking`. Sends an initial `nil`value, by default. | ||
|
||
Other inputs and outputs: | ||
|
||
Data Inputs: | ||
- `initialData` - An `initialData` event is required for any other signals to send. | ||
|
||
Data Outputs: | ||
- `applePayAuthorizationStatus` - Sends an event indicating whether the ApplePay flow succeeded or failed. | ||
- `paymentAuthorizationDidFinish` - Sends an event with the ApplePay spreadsheet closes. | ||
*/ | ||
|
||
public final class ApplePayTokenUseCase: ApplePayTokenUseCaseType, ApplePayTokenUseCaseUIInputs, | ||
ApplePayTokenUseCaseUIOutputs, ApplePayTokenUseCaseDataOutputs { | ||
init(initialData: Signal<PaymentAuthorizationData, Never>) { | ||
self.goToApplePayPaymentAuthorization = initialData | ||
.takeWhen(self.applePayButtonTappedSignal) | ||
|
||
let pkPaymentData = self.pkPaymentSignal | ||
.map { pkPayment -> PKPaymentData? in | ||
guard let displayName = pkPayment.displayName, let network = pkPayment.network else { | ||
return nil | ||
} | ||
|
||
return (displayName, network, pkPayment.transactionIdentifier) | ||
} | ||
|
||
let applePayStatusSuccess = Signal.combineLatest( | ||
self.stripeTokenSignal.skipNil(), | ||
self.stripeErrorSignal.filter { $0 == nil }, | ||
pkPaymentData.skipNil() | ||
) | ||
.mapConst(PKPaymentAuthorizationStatus.success) | ||
|
||
let applePayStatusFailure = Signal.merge( | ||
self.stripeErrorSignal.skipNil().ignoreValues(), | ||
self.stripeTokenSignal.filter { $0 == nil }.ignoreValues(), | ||
pkPaymentData.filter { $0 == nil }.ignoreValues() | ||
) | ||
.mapConst(PKPaymentAuthorizationStatus.failure) | ||
|
||
self.createApplePayBackingStatusProperty <~ Signal.merge( | ||
applePayStatusSuccess, | ||
applePayStatusFailure | ||
) | ||
|
||
let applePayParams = Signal.combineLatest( | ||
pkPaymentData.skipNil(), | ||
self.stripeTokenSignal.skipNil() | ||
) | ||
.map { paymentData, token in | ||
( | ||
paymentData.displayName, | ||
paymentData.network, | ||
paymentData.transactionIdentifier, | ||
token | ||
) | ||
} | ||
.map(ApplePayParams.init) | ||
.takeWhen(self.paymentAuthorizationDidFinishSignal) | ||
|
||
self.applePayParams = Signal.merge( | ||
initialData.mapConst(nil), | ||
applePayParams.wrapInOptional() | ||
) | ||
|
||
self.applePayAuthorizationStatus = self.createApplePayBackingStatusProperty.signal | ||
} | ||
|
||
// MARK: - Inputs | ||
|
||
private let (applePayButtonTappedSignal, applePayButtonTappedObserver) = Signal<Void, Never>.pipe() | ||
public func applePayButtonTapped() { | ||
self.applePayButtonTappedObserver.send(value: ()) | ||
} | ||
|
||
private let (pkPaymentSignal, pkPaymentObserver) = Signal<( | ||
displayName: String?, | ||
network: String?, | ||
transactionIdentifier: String | ||
), Never>.pipe() | ||
public func paymentAuthorizationDidAuthorizePayment(paymentData: ( | ||
displayName: String?, | ||
network: String?, | ||
transactionIdentifier: String | ||
)) { | ||
self.pkPaymentObserver.send(value: paymentData) | ||
} | ||
|
||
private let (paymentAuthorizationDidFinishSignal, paymentAuthorizationDidFinishObserver) | ||
= Signal<Void, Never>.pipe() | ||
public func paymentAuthorizationViewControllerDidFinish() { | ||
self.paymentAuthorizationDidFinishObserver.send(value: ()) | ||
} | ||
|
||
public var paymentAuthorizationDidFinish: Signal<Void, Never> { | ||
return self.paymentAuthorizationDidFinishSignal | ||
} | ||
|
||
private let (stripeTokenSignal, stripeTokenObserver) = Signal<String?, Never>.pipe() | ||
private let (stripeErrorSignal, stripeErrorObserver) = Signal<Error?, Never>.pipe() | ||
|
||
private let createApplePayBackingStatusProperty = MutableProperty<PKPaymentAuthorizationStatus>(.failure) | ||
public func stripeTokenCreated(token: String?, error: Error?) -> PKPaymentAuthorizationStatus { | ||
self.stripeTokenObserver.send(value: token) | ||
self.stripeErrorObserver.send(value: error) | ||
|
||
return self.createApplePayBackingStatusProperty.value | ||
} | ||
|
||
// MARK: - Outputs | ||
|
||
public let goToApplePayPaymentAuthorization: Signal<PaymentAuthorizationData, Never> | ||
public let applePayParams: Signal<ApplePayParams?, Never> | ||
public let applePayAuthorizationStatus: Signal<PKPaymentAuthorizationStatus, Never> | ||
|
||
// MARK: - Interface | ||
|
||
public var uiInputs: ApplePayTokenUseCaseUIInputs { return self } | ||
public var uiOutputs: ApplePayTokenUseCaseUIOutputs { return self } | ||
public var dataOutputs: ApplePayTokenUseCaseDataOutputs { return self } | ||
} |
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,155 @@ | ||
@testable import KsApi | ||
@testable import Library | ||
import PassKit | ||
import ReactiveExtensions_TestHelpers | ||
import ReactiveSwift | ||
import XCTest | ||
|
||
final class ApplePayTokenUseCaseTests: TestCase { | ||
let (initialDataSignal, initialDataObserver) = Signal<PaymentAuthorizationData, Never>.pipe() | ||
let (allRewardsTotalSignal, allRewardsTotalObserver) = Signal<Double, Never>.pipe() | ||
let (additionalPledgeAmountSignal, additionalPledgeAmountObserver) = Signal<Double, Never>.pipe() | ||
let (allRewardsShippingTotalSignal, allRewardsShippingTotalObserver) = Signal<Double, Never>.pipe() | ||
|
||
var useCase: ApplePayTokenUseCase! | ||
|
||
let goToApplePayPaymentAuthorization = TestObserver<PaymentAuthorizationData, Never>() | ||
let applePayParams = TestObserver<ApplePayParams?, Never>() | ||
let applePayAuthorizationStatus = TestObserver<PKPaymentAuthorizationStatus, Never>() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
self.useCase = ApplePayTokenUseCase( | ||
initialData: self.initialDataSignal | ||
) | ||
|
||
self.useCase.uiOutputs.goToApplePayPaymentAuthorization | ||
.observe(self.goToApplePayPaymentAuthorization.observer) | ||
self.useCase.dataOutputs.applePayAuthorizationStatus.observe(self.applePayAuthorizationStatus.observer) | ||
self.useCase.dataOutputs.applePayParams.observe(self.applePayParams.observer) | ||
} | ||
|
||
func testUseCase_GoesToPaymentAuthorization_WhenApplePayButtonIsTapped() { | ||
let data = PaymentAuthorizationData( | ||
project: Project.template, | ||
reward: Reward.template, | ||
allRewardsTotal: 92.0, | ||
additionalPledgeAmount: 15.0, | ||
allRewardsShippingTotal: 33.0, | ||
merchantIdentifier: "foo.bar.baz" | ||
) | ||
|
||
self.initialDataObserver.send(value: data) | ||
|
||
self.goToApplePayPaymentAuthorization.assertDidNotEmitValue() | ||
|
||
self.useCase.uiInputs.applePayButtonTapped() | ||
|
||
self.goToApplePayPaymentAuthorization.assertDidEmitValue() | ||
} | ||
|
||
func testUseCase_ApplePayParams_DefaultToNil() { | ||
let data = PaymentAuthorizationData( | ||
project: Project.template, | ||
reward: Reward.template, | ||
allRewardsTotal: 92.0, | ||
additionalPledgeAmount: 15.0, | ||
allRewardsShippingTotal: 33.0, | ||
merchantIdentifier: "foo.bar.baz" | ||
) | ||
|
||
self.initialDataObserver.send(value: data) | ||
self.applePayParams.assertLastValue(nil) | ||
} | ||
|
||
func testUseCase_CompletingApplePayFlow_SendsApplePayParamsAndStatus() { | ||
let data = PaymentAuthorizationData( | ||
project: Project.template, | ||
reward: Reward.template, | ||
allRewardsTotal: 92.0, | ||
additionalPledgeAmount: 15.0, | ||
allRewardsShippingTotal: 33.0, | ||
merchantIdentifier: "foo.bar.baz" | ||
) | ||
|
||
self.initialDataObserver.send(value: data) | ||
|
||
self.useCase.uiInputs.applePayButtonTapped() | ||
self.goToApplePayPaymentAuthorization.assertDidEmitValue() | ||
|
||
self.useCase.uiInputs.paymentAuthorizationDidAuthorizePayment(paymentData: ( | ||
"Display Name", | ||
"Network", | ||
"Transaction Identifier" | ||
)) | ||
self.applePayParams.assertLastValue(nil, "Params shouldn't emit until transaction is finished") | ||
|
||
let status = self.useCase.uiInputs.stripeTokenCreated(token: "some_stripe_token", error: nil) | ||
XCTAssertEqual(status, PKPaymentAuthorizationStatus.success) | ||
|
||
self.applePayParams.assertLastValue(nil, "Params shouldn't emit until transaction is finished") | ||
|
||
self.useCase.uiInputs.paymentAuthorizationViewControllerDidFinish() | ||
|
||
self.applePayAuthorizationStatus.assertLastValue(PKPaymentAuthorizationStatus.success) | ||
self.applePayParams.assertDidEmitValue() | ||
|
||
XCTAssertNotNil(self.applePayParams.lastValue as Any) | ||
|
||
let params = self.applePayParams.lastValue!! | ||
XCTAssertEqual(params.token, "some_stripe_token") | ||
XCTAssertEqual(params.paymentInstrumentName, "Display Name") | ||
XCTAssertEqual(params.paymentNetwork, "Network") | ||
XCTAssertEqual(params.transactionIdentifier, "Transaction Identifier") | ||
} | ||
|
||
func testUseCase_StripeError_SendsFailedStatus() { | ||
let data = PaymentAuthorizationData( | ||
project: Project.template, | ||
reward: Reward.template, | ||
allRewardsTotal: 92.0, | ||
additionalPledgeAmount: 15.0, | ||
allRewardsShippingTotal: 33.0, | ||
merchantIdentifier: "foo.bar.baz" | ||
) | ||
|
||
self.initialDataObserver.send(value: data) | ||
|
||
self.useCase.uiInputs.applePayButtonTapped() | ||
self.useCase.uiInputs.paymentAuthorizationDidAuthorizePayment(paymentData: ( | ||
"Display Name", | ||
"Network", | ||
"Transaction Identifier" | ||
)) | ||
|
||
let status = self.useCase.uiInputs.stripeTokenCreated(token: nil, error: TestError()) | ||
XCTAssertEqual(status, PKPaymentAuthorizationStatus.failure) | ||
|
||
self.useCase.uiInputs.paymentAuthorizationViewControllerDidFinish() | ||
|
||
self.applePayAuthorizationStatus.assertLastValue(PKPaymentAuthorizationStatus.failure) | ||
self.applePayParams.assertLastValue(nil, "Params shouldn't emit when Stripe fails") | ||
} | ||
|
||
func testUseCase_ApplePayIsCanceled_DoesNotSendParams() { | ||
let data = PaymentAuthorizationData( | ||
project: Project.template, | ||
reward: Reward.template, | ||
allRewardsTotal: 92.0, | ||
additionalPledgeAmount: 15.0, | ||
allRewardsShippingTotal: 33.0, | ||
merchantIdentifier: "foo.bar.baz" | ||
) | ||
|
||
self.initialDataObserver.send(value: data) | ||
|
||
self.useCase.uiInputs.applePayButtonTapped() | ||
self.useCase.uiInputs.paymentAuthorizationViewControllerDidFinish() | ||
|
||
self.applePayAuthorizationStatus.assertDidNotEmitValue() | ||
self.applePayParams.assertLastValue(nil, "Params shouldn't emit when ApplePay is canceled") | ||
} | ||
} | ||
|
||
private class TestError: Error {} |