-
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.
[MBL-1393] Moves Stripe Intent Logic Into It's Own Service (#2050)
* Create StripeIntentService that creates a new payment intent * update existing .createPaymentIntent call sites * Add setup intent func to StripeIntentServiceType * update existing createStripeSetupIntent call sites * convert service to class so that it can be tested more easily * tests for StripeSetupIntentService * clean up tests * inject stripe intent service only where needed instead of putting it in our already large AppEnvironment * remove tests * update MockStripeIntentService to use apiService * This way it's a better representation of the actual service * inject MockStripeIntentService into PledgePaymentMethodsViewModelTests and update tests * inject MockStripeIntentService into PaymentMethodSettingsViewModelTests and update tests * inject MockStripeIntentService into PostCampaignCheckoutViewModelTests and update tests * make assertion comment clearer * cleanup from initial testing pattern
- Loading branch information
1 parent
8e37fd7
commit 52f6afe
Showing
12 changed files
with
222 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Foundation | ||
import KsApi | ||
import ReactiveSwift | ||
|
||
public protocol StripeIntentServiceType { | ||
func createPaymentIntent(for projectId: String, pledgeTotal: Double) | ||
-> SignalProducer<PaymentIntentEnvelope, ErrorEnvelope> | ||
func createSetupIntent( | ||
for projectId: String?, | ||
context: GraphAPI.StripeIntentContextTypes | ||
) -> SignalProducer<ClientSecretEnvelope, ErrorEnvelope> | ||
} | ||
|
||
/// This is the module that creates either a Stripe payment intent or a setup intent. | ||
public class StripeIntentService: StripeIntentServiceType { | ||
public init() {} | ||
|
||
/** | ||
Returns a signal producer that emits a `PaymentIntentEnvelope` or `ErrorEnvelope` value representing whether or not a payment intent was created and returned successfully. | ||
The returned producer emits once and completes. | ||
- parameter for: The types to register that we will request permissions for. | ||
- parameters: | ||
- projectId: The GraphID of a project | ||
- pledgeTotal: The final pledge total of the current pledge | ||
*/ | ||
|
||
public func createPaymentIntent( | ||
for projectId: String, | ||
pledgeTotal: Double | ||
) -> SignalProducer<PaymentIntentEnvelope, ErrorEnvelope> { | ||
AppEnvironment.current.apiService | ||
.createPaymentIntentInput(input: CreatePaymentIntentInput( | ||
projectId: projectId, | ||
amountDollars: String(format: "%.2f", pledgeTotal), | ||
digitalMarketingAttributed: nil | ||
)) | ||
} | ||
|
||
/** | ||
Returns a signal producer that contains a `ClientSecretEnvelope` or `ErrorEnvelope` representing whether or not a setup intent was created and returned successfully. | ||
The returned producer emits once and completes. | ||
- parameter for: The types to register that we will request permissions for. | ||
- parameters: | ||
- projectId: The optional GraphID of a project | ||
- context: The context for which this intent is being created | ||
*/ | ||
|
||
public func createSetupIntent( | ||
for projectId: String?, | ||
context: GraphAPI.StripeIntentContextTypes | ||
) -> SignalProducer<ClientSecretEnvelope, ErrorEnvelope> { | ||
AppEnvironment.current.apiService | ||
.createStripeSetupIntent( | ||
input: CreateSetupIntentInput(projectId: projectId, context: context) | ||
) | ||
} | ||
} |
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,46 @@ | ||
@testable import KsApi | ||
@testable import Library | ||
import ReactiveSwift | ||
|
||
public class MockStripeIntentService: StripeIntentServiceType { | ||
public private(set) var setupIntentRequests: Int = 0 | ||
public private(set) var paymentIntentRequests: Int = 0 | ||
|
||
public init() {} | ||
|
||
public func createPaymentIntent( | ||
for projectId: String, | ||
pledgeTotal: Double | ||
) -> SignalProducer<PaymentIntentEnvelope, ErrorEnvelope> { | ||
assert( | ||
AppEnvironment.current.apiService as? MockService != nil, | ||
"AppEnvironment.current.apiService should be a MockService when used in test." | ||
) | ||
|
||
self.paymentIntentRequests += 1 | ||
|
||
return AppEnvironment.current.apiService | ||
.createPaymentIntentInput(input: CreatePaymentIntentInput( | ||
projectId: projectId, | ||
amountDollars: String(format: "%.2f", pledgeTotal), | ||
digitalMarketingAttributed: nil | ||
)) | ||
} | ||
|
||
public func createSetupIntent( | ||
for projectId: String?, | ||
context: GraphAPI.StripeIntentContextTypes | ||
) -> SignalProducer<ClientSecretEnvelope, ErrorEnvelope> { | ||
assert( | ||
AppEnvironment.current.apiService as? MockService != nil, | ||
"AppEnvironment.current.apiService should be a MockService when used in test." | ||
) | ||
|
||
self.setupIntentRequests += 1 | ||
|
||
return AppEnvironment.current.apiService | ||
.createStripeSetupIntent( | ||
input: CreateSetupIntentInput(projectId: projectId, context: context) | ||
) | ||
} | ||
} |
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
Oops, something went wrong.