Skip to content

Commit

Permalink
Add alert prior to removing bank account (v2) (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
wooj-stripe authored Apr 13, 2022
1 parent 626f591 commit 643b8ab
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 31 deletions.
12 changes: 11 additions & 1 deletion Stripe/AddPaymentMethodViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ class AddPaymentMethodViewController: UIViewController {
private lazy var usBankAccountFormElement: PaymentMethodElement = {
// We are keeping usBankAccountInfo in memory to preserve state
// if the user switches payment method types
return makeElement(for: selectedPaymentMethodType)
let paymentMethodElement = makeElement(for: selectedPaymentMethodType)
if let usBankAccountPaymentMethodElement = paymentMethodElement as? USBankAccountPaymentMethodElement {
usBankAccountPaymentMethodElement.presentingViewControllerDelegate = self
}
return paymentMethodElement
}()
private lazy var paymentMethodFormElement: PaymentMethodElement = {
if selectedPaymentMethodType == .USBankAccount {
Expand Down Expand Up @@ -321,3 +325,9 @@ extension AddPaymentMethodViewController: ElementDelegate {
animateHeightChange()
}
}

extension AddPaymentMethodViewController: PresentingViewControllerDelegate {
func presentViewController(viewController: UIViewController, completion: (() -> Void)?) {
self.present(viewController, animated: true, completion: completion)
}
}
2 changes: 1 addition & 1 deletion Stripe/CircularButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class CircularButton: UIControl {
dark: UIColor(red: 43.0 / 255.0, green: 43.0 / 255.0, blue: 47.0 / 255.0, alpha: 1))
imageView.image = Image.icon_x.makeImage(template: true)
imageView.tintColor = dangerColor
accessibilityLabel = STPLocalizedString("Remove", "Text for remove button")
accessibilityLabel = String.Localized.remove
accessibilityIdentifier = "CircularButton.Remove"
}
imageView.translatesAutoresizingMaskIntoConstraints = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private extension PayWithLinkViewController.WalletViewController {
))

alertController.addAction(UIAlertAction(
title: "Remove", // TODO(ramont): Localize
title: String.Localized.remove,
style: .destructive,
handler: { _ in
self.paymentPicker.showLoader(at: index)
Expand Down
11 changes: 0 additions & 11 deletions Stripe/Resources/Localizations/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,9 @@
/* Payment Method type brand name. */
"Przelewy24" = "Przelewy24";

/* Accessibility label for a button that removes a saved payment method
Button title for confirmation alert to remove a saved payment method
Text for remove button */
"Remove" = "Remove";

/* Content for alert popup prompting to confirm removing a saved card. Remove {card brand} ending in {last 4} e.g. 'Remove VISA ending in 4242' */
"Remove %1$@ ending in %2$@" = "Remove %1$@ ending in %2$@";

/* Title for confirmation alert to remove a saved bank account payment method */
"Remove bank account" = "Remove bank account";

/* Content for alert popup prompting to confirm removing a saved bank account. e.g. 'Remove bank account ending in 4242' */
"Remove bank account ending in %@" = "Remove bank account ending in %@";

/* Title for confirmation alert to remove a card */
"Remove Card" = "Remove Card";

Expand Down
4 changes: 1 addition & 3 deletions Stripe/SavedPaymentMethodCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ extension SavedPaymentMethodCollectionView {
dangerColor: appearance.colors.danger)
button.backgroundColor = appearance.colors.danger
button.isAccessibilityElement = true
button.accessibilityLabel = STPLocalizedString(
"Remove",
"Accessibility label for a button that removes a saved payment method")
button.accessibilityLabel = String.Localized.remove
button.accessibilityIdentifier = "Remove"
return button
}()
Expand Down
14 changes: 3 additions & 11 deletions Stripe/SavedPaymentOptionsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ extension SavedPaymentOptionsViewController: PaymentOptionCellDelegate {
}
let viewModel = viewModels[indexPath.row]
let alert = UIAlertAction(
title: STPLocalizedString(
"Remove", "Button title for confirmation alert to remove a saved payment method"
), style: .destructive
title: String.Localized.remove, style: .destructive
) { (_) in
self.viewModels.remove(at: indexPath.row)
// the deletion needs to be in a performBatchUpdates so we make sure it is completed
Expand Down Expand Up @@ -377,15 +375,9 @@ extension STPPaymentMethod {
)
case .SEPADebit:
let last4 = sepaDebit?.last4 ?? ""
let formattedMessage = STPLocalizedString(
"Remove bank account ending in %@",
"Content for alert popup prompting to confirm removing a saved bank account. e.g. 'Remove bank account ending in 4242'"
)
let formattedMessage = String.Localized.removeBankAccountEndingIn
return (
title: STPLocalizedString(
"Remove bank account",
"Title for confirmation alert to remove a saved bank account payment method"
),
title: String.Localized.removeBankAccount,
message: String(format: formattedMessage, last4)
)
default:
Expand Down
29 changes: 26 additions & 3 deletions Stripe/USBankAccountPaymentMethodElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit
@_spi(STP) import StripeCore

final class USBankAccountPaymentMethodElement : Element {
var presentingViewControllerDelegate: PresentingViewControllerDelegate? = nil

var delegate: ElementDelegate? = nil

var view: UIView {
Expand Down Expand Up @@ -111,9 +113,30 @@ final class USBankAccountPaymentMethodElement : Element {

extension USBankAccountPaymentMethodElement: BankAccountInfoViewDelegate {
func didTapXIcon() {
self.bankInfoSectionElement.view.isHidden = true
self.linkedBank = nil
self.delegate?.didUpdate(element: self)
let completionClosure = {
self.bankInfoSectionElement.view.isHidden = true
self.linkedBank = nil
self.delegate?.didUpdate(element: self)
}

guard let last4BankAccount = self.linkedBank?.last4,
let presentingDelegate = presentingViewControllerDelegate else {
completionClosure()
return
}

let didTapAlert = UIAlertAction(title: String.Localized.remove, style: .destructive) { (_) in
completionClosure()
}
let didTapCancel = UIAlertAction(title: String.Localized.cancel,
style: .cancel,
handler: nil)
let alertController = UIAlertController(title: String.Localized.removeBankAccount,
message: String(format: String.Localized.removeBankAccountEndingIn, last4BankAccount),
preferredStyle: .alert)
alertController.addAction(didTapCancel)
alertController.addAction(didTapAlert)
presentingDelegate.presentViewController(viewController: alertController, completion: nil)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ Short string for postal code (text used in non-US countries) */
Label of an address field */
"Province" = "Province";

/* Button title for confirmation alert to remove a saved payment method */
"Remove" = "Remove";

/* Title for confirmation alert to remove a saved bank account payment method */
"Remove bank account" = "Remove bank account";

/* Content for alert popup prompting to confirm removing a saved bank account. e.g. 'Remove bank account ending in 4242' */
"Remove bank account ending in %@" = "Remove bank account ending in %@";

/* Caption for State field on address form (only countries that use state , like United States)
Label of an address field */
"State" = "State";
Expand Down
10 changes: 10 additions & 0 deletions StripeUICore/StripeUICore/Source/Elements/Element.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public extension Element {
func continueToNextField(element: Element)
}

/**
An Element uses this delegate to present a view controller
*/
@_spi(STP) public protocol PresentingViewControllerDelegate: ElementDelegate {
/**
Elements will call this function to delegate presentation of a view controller
*/
func presentViewController(viewController: UIViewController, completion: (() -> Void)?)
}

extension Element {
/// A poorly named convenience method that returns all Elements underneath this Element, including this Element.
func getAllSubElements() -> [Element] {
Expand Down
16 changes: 16 additions & 0 deletions StripeUICore/StripeUICore/Source/Helpers/String+Localized.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ import Foundation
"Error string displayed to user when they have entered an incomplete BSB number.")
}

static var removeBankAccountEndingIn: String {
STPLocalizedString(
"Remove bank account ending in %@",
"Content for alert popup prompting to confirm removing a saved bank account. e.g. 'Remove bank account ending in 4242'")
}

static var removeBankAccount: String {
STPLocalizedString(
"Remove bank account",
"Title for confirmation alert to remove a saved bank account payment method")
}

// MARK: - Control strings
static var cancel: String {
STPLocalizedString("Cancel", "Button title to cancel action in an alert")
Expand All @@ -229,4 +241,8 @@ import Foundation
static var `continue`: String {
STPLocalizedString("Continue", "Text for continue button")
}

static var remove: String {
STPLocalizedString("Remove", "Button title for confirmation alert to remove a saved payment method")
}
}

0 comments on commit 643b8ab

Please sign in to comment.