Created
February 28, 2017 08:53
-
-
Save jetrubyshared/30d3fca811d71eaf7b8b9df53b2de91f 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
// PaymentViewController.swift | |
class PaymentViewController: UIViewController, STPPaymentCardTextFieldDelegate { | |
let paymentTextField = STPPaymentCardTextField() | |
// Next, let's instantiate the STPPaymentCardTextField, set the PaymentViewController as its STPPaymentCardTextFieldDelegate, and add it to our view. | |
override func viewDidLoad() { | |
super.viewDidLoad(); | |
paymentTextField.frame = CGRectMake(15, 15, CGRectGetWidth(self.view.frame) - 30, 44) | |
paymentTextField.delegate = self | |
view.addSubview(paymentTextField) | |
// This will add an STPPaymentCardTextField to the controller to accept card numbers, expiration dates, and CVCs. It'll format the input, and validate it on the fly. | |
} | |
// When the user enters text into this field, the paymentCardTextFieldDidChange: method will be called on our view controller. In this callback, we can enable a save button that allows users to submit their valid cards if the form is valid: | |
func paymentCardTextFieldDidChange(textField: STPPaymentCardTextField) { | |
// Toggle navigation, for example | |
saveButton.enabled = textField.isValid | |
} | |
// Once you've collected the card number, expiration, and CVC, package them up in an STPCardParams object and invoke the createTokenWithCard: method on the STPAPIClient class, instructing the library to send off the credit card data to Stripe and return a token. | |
@IBAction func save(sender: UIButton) { | |
if let card = paymentTextField.card { | |
STPAPIClient.sharedClient().createTokenWithCard(card) { (token, error) -> Void in | |
if let error = error { | |
handleError(error) | |
} | |
else if let token = token { | |
createBackendChargeWithToken(token) { status in | |
... | |
} | |
} | |
} | |
} | |
} | |
// Sending the token to your server | |
func createBackendChargeWithToken(token: STPToken, completion: STPPaymentAuthorizationStatus -> ()) { | |
let url = NSURL(string: "https://example.com/token")! | |
let request = NSMutableURLRequest(URL: url) | |
request.HTTPMethod = "POST" | |
let body = "stripeToken=(token.tokenId)" | |
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding) | |
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration() | |
let session = NSURLSession(configuration: configuration) | |
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in | |
if error != nil { | |
completion(STPPaymentAuthorizationStatus.Failure) | |
} | |
else { | |
completion(STPPaymentAuthorizationStatus.Success) | |
} | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment