Skip to content

Commit

Permalink
[NTV-1896] Present Payment Sheet With Feature Flag Enabled. (#1731)
Browse files Browse the repository at this point in the history
* added boilerplate for showing sheet without create_setup_intent

* updates to gql schema that support the create_setup_intent with no project id

* payment sheet presented on payment method settings.

* added tests

* pr corrections
  • Loading branch information
msadoon committed Sep 19, 2022
1 parent 28d69e7 commit ea32c18
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 407 deletions.
@@ -1,11 +1,13 @@
import KsApi
import Library
import Prelude
import Stripe
import UIKit

internal final class PaymentMethodsViewController: UIViewController, MessageBannerViewControllerPresenting {
private let dataSource = PaymentMethodsDataSource()
private let viewModel: PaymentMethodsViewModelType = PaymentMethodsViewModel()
private var paymentSheetFlowController: PaymentSheet.FlowController?

@IBOutlet private var tableView: UITableView!

Expand Down Expand Up @@ -93,7 +95,15 @@ internal final class PaymentMethodsViewController: UIViewController, MessageBann
self?.goToAddCardScreen(with: intent)
}

self.viewModel.outputs.errorLoadingPaymentMethods
self.viewModel.outputs.goToPaymentSheet
.observeForUI()
.observeValues { [weak self] data in
guard let strongSelf = self else { return }

strongSelf.goToPaymentSheet(data: data)
}

self.viewModel.outputs.errorLoadingPaymentMethodsOrSetupIntent
.observeForUI()
.observeValues { [weak self] message in
self?.messageBannerViewController?.showBanner(with: .error, message: message)
Expand Down Expand Up @@ -143,6 +153,69 @@ internal final class PaymentMethodsViewController: UIViewController, MessageBann

// MARK: - Private Helpers

private func goToPaymentSheet(data: PaymentSheetSetupData) {
PaymentSheet.FlowController
.create(
setupIntentClientSecret: data.clientSecret,
configuration: data.configuration
) { [weak self] result in
guard let strongSelf = self else { return }

switch result {
case let .failure(error):
/** TODO: https://kickstarter.atlassian.net/browse/PAY-1954
* strongSelf.viewModel.inputs.shouldCancelPaymentSheetAppearance(state: true)
*/

strongSelf.messageBannerViewController?
.showBanner(with: .error, message: error.localizedDescription)
case let .success(paymentSheetFlowController):
strongSelf.paymentSheetFlowController = paymentSheetFlowController
strongSelf.paymentSheetFlowController?.presentPaymentOptions(from: strongSelf) { [weak self] in
guard let strongSelf = self else { return }

/** TODO: https://kickstarter.atlassian.net/browse/PAY-1900
* strongSelf.confirmPaymentResult(with: data.clientSecret)
*/
}
}
}
}

private func confirmPaymentResult(with _: String) {
guard self.paymentSheetFlowController?.paymentOption != nil else {
/** TODO: https://kickstarter.atlassian.net/browse/PAY-1954
* strongSelf.viewModel.inputs.shouldCancelPaymentSheetAppearance(state: true)
*/

return
}

self.paymentSheetFlowController?.confirm(from: self) { [weak self] paymentResult in

guard let strongSelf = self else { return }

/** TODO: https://kickstarter.atlassian.net/browse/PAY-1954
* strongSelf.viewModel.inputs.shouldCancelPaymentSheetAppearance(state: true)
*/

guard let existingPaymentOption = strongSelf.paymentSheetFlowController?.paymentOption else { return }

switch paymentResult {
case .completed:
/** TODO: https://kickstarter.atlassian.net/browse/PAY-1898
* strongSelf.viewModel.inputs.paymentSheetDidAdd(newCard: existingPaymentOption, setupIntent: clientSecret)
*/
fatalError()
case .canceled:
strongSelf.messageBannerViewController?
.showBanner(with: .error, message: Strings.general_error_something_wrong())
case let .failed(error):
strongSelf.messageBannerViewController?.showBanner(with: .error, message: error.localizedDescription)
}
}
}

private func configureHeaderFooterViews() {
if let header = SettingsTableViewHeader.fromNib(nib: Nib.SettingsTableViewHeader) {
header.configure(with: Strings.Any_payment_methods_you_saved_to_Kickstarter())
Expand Down
28 changes: 19 additions & 9 deletions KsApi/GraphAPI.swift
Expand Up @@ -254,23 +254,24 @@ public enum GraphAPI {
/// - stripeToken
/// - stripeCardId
/// - reusable
/// - intentClientSecret
/// - clientMutationId: A unique identifier for the client performing the mutation.
public init(paymentType: PaymentTypes, stripeToken: String, stripeCardId: Swift.Optional<String?> = nil, reusable: Swift.Optional<Bool?> = nil, clientMutationId: Swift.Optional<String?> = nil) {
graphQLMap = ["paymentType": paymentType, "stripeToken": stripeToken, "stripeCardId": stripeCardId, "reusable": reusable, "clientMutationId": clientMutationId]
public init(paymentType: Swift.Optional<PaymentTypes?> = nil, stripeToken: Swift.Optional<String?> = nil, stripeCardId: Swift.Optional<String?> = nil, reusable: Swift.Optional<Bool?> = nil, intentClientSecret: Swift.Optional<String?> = nil, clientMutationId: Swift.Optional<String?> = nil) {
graphQLMap = ["paymentType": paymentType, "stripeToken": stripeToken, "stripeCardId": stripeCardId, "reusable": reusable, "intentClientSecret": intentClientSecret, "clientMutationId": clientMutationId]
}

public var paymentType: PaymentTypes {
public var paymentType: Swift.Optional<PaymentTypes?> {
get {
return graphQLMap["paymentType"] as! PaymentTypes
return graphQLMap["paymentType"] as? Swift.Optional<PaymentTypes?> ?? Swift.Optional<PaymentTypes?>.none
}
set {
graphQLMap.updateValue(newValue, forKey: "paymentType")
}
}

public var stripeToken: String {
public var stripeToken: Swift.Optional<String?> {
get {
return graphQLMap["stripeToken"] as! String
return graphQLMap["stripeToken"] as? Swift.Optional<String?> ?? Swift.Optional<String?>.none
}
set {
graphQLMap.updateValue(newValue, forKey: "stripeToken")
Expand All @@ -295,6 +296,15 @@ public enum GraphAPI {
}
}

public var intentClientSecret: Swift.Optional<String?> {
get {
return graphQLMap["intentClientSecret"] as? Swift.Optional<String?> ?? Swift.Optional<String?>.none
}
set {
graphQLMap.updateValue(newValue, forKey: "intentClientSecret")
}
}

/// A unique identifier for the client performing the mutation.
public var clientMutationId: Swift.Optional<String?> {
get {
Expand Down Expand Up @@ -349,13 +359,13 @@ public enum GraphAPI {
/// - Parameters:
/// - projectId
/// - clientMutationId: A unique identifier for the client performing the mutation.
public init(projectId: GraphQLID, clientMutationId: Swift.Optional<String?> = nil) {
public init(projectId: Swift.Optional<GraphQLID?> = nil, clientMutationId: Swift.Optional<String?> = nil) {
graphQLMap = ["projectId": projectId, "clientMutationId": clientMutationId]
}

public var projectId: GraphQLID {
public var projectId: Swift.Optional<GraphQLID?> {
get {
return graphQLMap["projectId"] as! GraphQLID
return graphQLMap["projectId"] as? Swift.Optional<GraphQLID?> ?? Swift.Optional<GraphQLID?>.none
}
set {
graphQLMap.updateValue(newValue, forKey: "projectId")
Expand Down

0 comments on commit ea32c18

Please sign in to comment.