Last active
May 30, 2016 16:52
-
-
Save amratab/0e67af5e8938c3e212f605470da0931e to your computer and use it in GitHub Desktop.
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
import UIKit | |
import Stripe | |
class PaymentViewController: UIViewController, STPPaymentCardTextFieldDelegate { | |
var paymentTextField: STPPaymentCardTextField! | |
var saveButton: UIButton! | |
var saveAction: UIAlertAction! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
paymentTextField = STPPaymentCardTextField() | |
paymentTextField.delegate = self | |
saveButton = UIButton(type: UIButtonType.Custom) | |
saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in | |
self.saveCard() | |
}) | |
saveAction.enabled = false | |
// Do any additional setup after loading the view. | |
} | |
func saveCard() { | |
let card = paymentTextField.cardParams | |
print("In save card") | |
STPAPIClient.sharedClient().createTokenWithCard(card) { (token, error) -> Void in | |
if let error = error { | |
self.handleError(error) | |
} | |
else if let token = token { | |
self.createBackendChargeWithToken(token) { status in | |
print("Found me some token " + token.tokenId) | |
} | |
} | |
} | |
} | |
func paymentCardTextFieldDidChange(textField: STPPaymentCardTextField) { | |
// Toggle navigation, for example | |
print("save action fired!") | |
saveAction.enabled = textField.valid | |
} | |
func handleError( error: NSError ) { | |
print("got error" + error.description) | |
} | |
func createBackendChargeWithToken(token: STPToken, completion: (Bool) -> Void) { | |
print("Did some backgorund function for saving token " + token.tokenId) | |
completion(true) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func showAlertButtonTapped(sender: UIButton) { | |
// create the alert | |
let alertController = UIAlertController(title: "Add Card", message: "Add your card details here and hit save\n\n\n", preferredStyle: UIAlertControllerStyle.Alert) | |
// alertController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width-20, 400) | |
// let margin:CGFloat = 8.0 | |
// let rect = CGRectMake(margin, margin, alertController.view.bounds.size.width - margin * 4.0, 44) | |
// let customView = UIView(frame: rect) | |
paymentTextField.frame = CGRectMake(5, 75, alertController.view.bounds.size.width - 150, 44)// | |
alertController.view.addSubview(paymentTextField) | |
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {(alert: UIAlertAction!) in print("cancel")}) | |
alertController.addAction(saveAction) | |
alertController.addAction(cancelAction) | |
self.presentViewController(alertController, animated: true, completion:{}) } | |
/* | |
// MARK: - Navigation | |
// In a storyboard-based application, you will often want to do a little preparation before navigation | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
// Get the new view controller using segue.destinationViewController. | |
// Pass the selected object to the new view controller. | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment