Skip to content

Instantly share code, notes, and snippets.

@zra95
Created April 18, 2016 19:57
Show Gist options
  • Save zra95/00fa98114e4a0c0abd169463575f2913 to your computer and use it in GitHub Desktop.
Save zra95/00fa98114e4a0c0abd169463575f2913 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// StripeTest
//
// Created by Guillaume Tellier on 15/04/2016.
// Copyright © 2016 Guillaume Tellier. All rights reserved.
//
import UIKit
import Stripe
class ViewController: UIViewController, STPPaymentCardTextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var headerView: UIView!
@IBOutlet weak var cardsTableView: UITableView!
@IBOutlet weak var cardTextField: STPPaymentCardTextField!
var stripeUtil = StripeUtil()
var cards = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//only one section
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//count on our cards array
return self.cards.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//get card cell with cardCell identifier don't forget it on your storyboard
let cell = tableView.dequeueReusableCellWithIdentifier("cardCell") as! CardCell
//get the last4 value on the card json, create the string and pass it to the label
if let last4 = self.cards[indexPath.row]["last4"] {
cell.cardNumberLabel.text = "**** **** **** \(last4!)"
}
//get the month/year expiration values on the card json, create the string and pass it to the label
if let expirationMonth = self.cards[indexPath.row]["exp_month"], let expirationYear = self.cards[indexPath.row]["exp_year"] {
cell.expirationLabel.text = "\(expirationMonth!)/\(expirationYear!)"
}
return cell
}
//this method must be linked on the touch up inside button in the storyboard, don't forget it
@IBAction func sendCard() {
//extract the card parameters given by the STPCardTextField
let params = cardTextField.cardParams
//check if the customerId exist
if let tokenId = stripeUtil.customerId {
//if yes, call the createCard method of our stripeUtil object, pass customer id
self.stripeUtil.createCard(tokenId, card: params, completion: { (success) in
//there is a new card !
self.stripeUtil.getCardsList({ (result) in
if let result = result {
self.cards = result
}
//store results on our cards, clear textfield and reload tableView
dispatch_async(dispatch_get_main_queue(), {
self.cardTextField.clear()
self.cardsTableView.reloadData()
})
})
})
}
else {
//if not, create the user with our createUser method
self.stripeUtil.createUser(params, completion: { (success) in
self.stripeUtil.getCardsList({ (result) in
if let result = result {
self.cards = result
}
//store results on our cards, clear textfield and reload tableView
dispatch_async(dispatch_get_main_queue(), {
self.cardTextField.clear()
self.cardsTableView.reloadData()
})
})
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment