Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MBL-1373] ValidateCheckout Before Completing Checkout With ApplePay #2047

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ enum PostCampaignCheckoutApplePayError: Error {
extension PostCampaignCheckoutViewController: STPApplePayContextDelegate {
func applePayContext(
_: StripeApplePay.STPApplePayContext,
didCreatePaymentMethod _: StripePayments.STPPaymentMethod,
didCreatePaymentMethod paymentMethod: StripePayments.STPPaymentMethod,
paymentInformation payment: PKPayment,
completion: @escaping StripeApplePay.STPIntentClientSecretCompletionBlock
) {
Expand All @@ -440,6 +440,7 @@ extension PostCampaignCheckoutViewController: STPApplePayContextDelegate {
let transactionId = payment.token.transactionIdentifier

let params = ApplePayParams(
paymentMethodId: paymentMethod.stripeId,
scottkicks marked this conversation as resolved.
Show resolved Hide resolved
paymentInstrumentName: paymentDisplayName,
paymentNetwork: paymentNetworkName,
transactionIdentifier: transactionId,
Expand Down
3 changes: 3 additions & 0 deletions KsApi/mutations/inputs/ApplePayParams.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import Foundation

public struct ApplePayParams: Encodable, Equatable {
public let paymentMethodId: String?
scottkicks marked this conversation as resolved.
Show resolved Hide resolved
let paymentInstrumentName: String
let paymentNetwork: String
let transactionIdentifier: String
let token: String

public init(
paymentMethodId: String? = nil,
paymentInstrumentName: String,
paymentNetwork: String,
transactionIdentifier: String,
token: String
) {
self.paymentMethodId = paymentMethodId
self.paymentInstrumentName = paymentInstrumentName
self.paymentNetwork = paymentNetwork
self.transactionIdentifier = transactionIdentifier
Expand Down
1 change: 1 addition & 0 deletions Library/ViewModels/PledgeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ public class PledgeViewModel: PledgeViewModelType, PledgeViewModelInputs, Pledge
)
.map { paymentData, token in
(
nil,
scottkicks marked this conversation as resolved.
Show resolved Hide resolved
paymentData.displayName,
paymentData.network,
paymentData.transactionIdentifier,
Expand Down
41 changes: 31 additions & 10 deletions Library/ViewModels/PostCampaignCheckoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,6 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
self.validateCheckoutSuccess = Signal
.merge(validateCheckoutNewCardSuccess, validateCheckoutExistingCardSuccess)

let validateCheckoutError = Signal
.merge(validateCheckoutExistingCard.errors(), validateCheckoutNewCard.errors())

self.showErrorBannerWithMessage = validateCheckoutError
.map { _ in Strings.Something_went_wrong_please_try_again() }

// MARK: ApplePay

/*
Expand All @@ -270,6 +264,7 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
3) Present the payment authorization form
4) Payment authorization form calls applePayContextDidCreatePayment with ApplePay params
5) Payment authorization form calls paymentAuthorizationDidFinish
6) Validate checkout using the checkoutId, payment source id, and payment intent
*/

let createPaymentIntentForApplePay: Signal<Signal<PaymentIntentEnvelope, ErrorEnvelope>.Event, Never> =
Expand Down Expand Up @@ -324,6 +319,33 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
}
.skipNil()

let validateCheckoutWithApplePay = Signal.combineLatest(
newPaymentIntentForApplePay,
checkoutId,
self.applePayParamsSignal.signal
)
.takeWhen(self.applePayContextDidCompleteSignal)
.switchMap { (clientSecret: String, checkoutId: String, applePayParams: ApplePayParams) in
AppEnvironment.current.apiService
.validateCheckout(
checkoutId: checkoutId,
paymentSourceId: applePayParams.paymentMethodId ?? "",
paymentIntentClientSecret: clientSecret
)
.ksr_delay(AppEnvironment.current.apiDelayInterval, on: AppEnvironment.current.scheduler)
.materialize()
}

let validateCheckoutError = Signal
.merge(
validateCheckoutExistingCard.errors(),
validateCheckoutNewCard.errors(),
validateCheckoutWithApplePay.errors()
)

self.showErrorBannerWithMessage = validateCheckoutError
.map { _ in Strings.Something_went_wrong_please_try_again() }

// MARK: CompleteOnSessionCheckout

let completeCheckoutWithCreditCardInput: Signal<GraphAPI.CompleteOnSessionCheckoutInput, Never> = Signal
Expand All @@ -345,13 +367,12 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
}

let completeCheckoutWithApplePayInput: Signal<GraphAPI.CompleteOnSessionCheckoutInput, Never> = Signal
.combineLatest(newPaymentIntentForApplePay, checkoutId, self.applePayParamsSignal.mapConst(true))
.takeWhen(self.applePayContextDidCompleteSignal)
.combineLatest(newPaymentIntentForApplePay, checkoutId)
.takeWhen(validateCheckoutWithApplePay.values())
.map {
(
clientSecret: String,
checkoutId: String,
_: Bool
checkoutId: String
) -> GraphAPI.CompleteOnSessionCheckoutInput in
GraphAPI
.CompleteOnSessionCheckoutInput(
Expand Down