Skip to content

Instantly share code, notes, and snippets.

@Gujci
Created July 2, 2020 08:57
Show Gist options
  • Save Gujci/71eba08ec067e904f6e4b2a00f2af43d to your computer and use it in GitHub Desktop.
Save Gujci/71eba08ec067e904f6e4b2a00f2af43d to your computer and use it in GitHub Desktop.
SwiftUI wrapper around the STPPaymentCardTextField from Stripe
struct StripePaymentCardTextField: UIViewRepresentable {
@Binding var cardParams: STPPaymentMethodCardParams
@Binding var isValid: Bool
func makeUIView(context: Context) -> STPPaymentCardTextField {
let input = STPPaymentCardTextField()
input.borderWidth = 0
input.delegate = context.coordinator
return input
}
func makeCoordinator() -> StripePaymentCardTextField.Coordinator { Coordinator(self) }
func updateUIView(_ view: STPPaymentCardTextField, context: Context) { }
class Coordinator: NSObject, STPPaymentCardTextFieldDelegate {
var parent: StripePaymentCardTextField
init(_ textField: StripePaymentCardTextField) {
parent = textField
}
func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
parent.cardParams = textField.cardParams
parent.isValid = textField.isValid
}
}
}
// MARK: - Preview
struct StripePaymentCardTextField_Previews: PreviewProvider {
static var previews: some View {
Group {
VStack {
StripePaymentCardTextField(cardParams: .constant(STPPaymentMethodCardParams()), isValid: .constant(true))
}
VStack {
StripePaymentCardTextField(cardParams: .constant(STPPaymentMethodCardParams()), isValid: .constant(true))
}
.environment(\.colorScheme, .dark)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment