-
Notifications
You must be signed in to change notification settings - Fork 292
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
Convert BraintreeDemoCardTokenizationViewController
to Swift
#1095
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 0 additions & 6 deletions
6
...plication/Features/Credit Card Tokenization/BraintreeDemoCardTokenizationViewController.h
This file was deleted.
Oops, something went wrong.
134 changes: 0 additions & 134 deletions
134
...plication/Features/Credit Card Tokenization/BraintreeDemoCardTokenizationViewController.m
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
...ation/Features/Credit Card Tokenization/BraintreeDemoCardTokenizationViewController.swift
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,120 @@ | ||
import UIKit | ||
import BraintreeCard | ||
|
||
class BraintreeDemoCardTokenizationViewController: BraintreeDemoPaymentButtonBaseViewController { | ||
|
||
let cardFormView = BTCardFormView() | ||
let autofillButton = UIButton(type: .system) | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
createSubviews() | ||
layoutConstraints() | ||
} | ||
|
||
override func createPaymentButton() -> UIView! { | ||
let submitButton = UIButton(type: .system) | ||
submitButton.setTitle("Submit", for: .normal) | ||
submitButton.setTitleColor(.blue, for: .normal) | ||
submitButton.setTitleColor(.lightGray, for: .highlighted) | ||
submitButton.setTitleColor(.lightGray, for: .disabled) | ||
submitButton.addTarget(self, action: #selector(tappedSubmit), for: .touchUpInside) | ||
submitButton.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
return submitButton | ||
} | ||
|
||
@objc func tappedSubmit() { | ||
progressBlock("Tokenizing card details!") | ||
|
||
let cardClient = BTCardClient(apiClient: apiClient) | ||
let card = newCard() | ||
|
||
setFieldsEnabled(false) | ||
cardClient.tokenize(card) { nonce, error in | ||
self.setFieldsEnabled(true) | ||
|
||
guard let nonce else { | ||
self.progressBlock(error?.localizedDescription) | ||
return | ||
} | ||
|
||
self.completionBlock(nonce) | ||
} | ||
} | ||
|
||
@objc func tappedAutofill() { | ||
cardFormView.cardNumberTextField.text = "4111111111111111" | ||
cardFormView.cvvTextField.text = "123" | ||
cardFormView.expirationTextField.text = generateFutureDate() | ||
} | ||
|
||
private func newCard() -> BTCard { | ||
let card = BTCard() | ||
|
||
if let cardNumber = cardFormView.cardNumber { | ||
card.number = cardNumber | ||
} | ||
|
||
if let expirationYear = cardFormView.expirationYear { | ||
card.expirationYear = expirationYear | ||
} | ||
|
||
if let expirationMonth = cardFormView.expirationMonth { | ||
card.expirationMonth = expirationMonth | ||
} | ||
|
||
if let cvv = cardFormView.cvv { | ||
card.cvv = cvv | ||
} | ||
|
||
return card | ||
} | ||
|
||
private func setFieldsEnabled(_ isEnabled: Bool) { | ||
cardFormView.cardNumberTextField.isEnabled = isEnabled | ||
cardFormView.expirationTextField.isEnabled = isEnabled | ||
cardFormView.cvvTextField.isEnabled = isEnabled | ||
autofillButton.isEnabled = isEnabled | ||
} | ||
|
||
private func generateFutureDate() -> String { | ||
let monthString = "12" | ||
|
||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yy" | ||
|
||
let futureYear = Calendar.current.date(byAdding: .year, value: 3, to: Date())! | ||
let yearString = dateFormatter.string(from: futureYear) | ||
|
||
return "\(monthString)/\(yearString)" | ||
} | ||
|
||
private func createSubviews() { | ||
cardFormView.translatesAutoresizingMaskIntoConstraints = false | ||
cardFormView.hidePhoneNumberField = true | ||
cardFormView.hidePostalCodeField = true | ||
setFieldsEnabled(true) | ||
|
||
autofillButton.setTitle("Autofill", for: .normal) | ||
autofillButton.setTitleColor(.blue, for: .normal) | ||
autofillButton.addTarget(self, action: #selector(tappedAutofill), for: .touchUpInside) | ||
autofillButton.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
view.addSubview(cardFormView) | ||
view.addSubview(autofillButton) | ||
} | ||
|
||
private func layoutConstraints() { | ||
NSLayoutConstraint.activate([ | ||
cardFormView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | ||
cardFormView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), | ||
cardFormView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor), | ||
cardFormView.heightAnchor.constraint(equalToConstant: 200), | ||
|
||
autofillButton.topAnchor.constraint(equalTo: cardFormView.bottomAnchor, constant: 10), | ||
autofillButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10), | ||
autofillButton.heightAnchor.constraint(equalToConstant: 30) | ||
]) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit - private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: 7d02e20