Last active
August 31, 2018 20:36
-
-
Save zra95/af1a69410a0c8772d55e905965a34c3d 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 Foundation | |
import Stripe | |
class StripeUtil { | |
var stripeTool = StripeTools() | |
var customerId: String? | |
let defaultSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) | |
var dataTask: NSURLSessionDataTask? | |
//createUser | |
func createUser(card: STPCardParams, completion: (success: Bool) -> Void) { | |
//Stripe iOS SDK will gave us a token to make APIs call possible | |
stripeTool.generateToken(card) { (token) in | |
if(token != nil) { | |
//request to create the user | |
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.stripe.com/v1/customers")!) | |
//params array where you can put your user informations | |
var params = [String:String]() | |
params["email"] = "[email protected]" | |
//transform this array into a string | |
var str = "" | |
params.forEach({ (key, value) in | |
str = "\(str)\(key)=\(value)&" | |
}) | |
//basic auth | |
request.setValue(self.stripeTool.getBasicAuth(), forHTTPHeaderField: "Authorization") | |
//POST method, refer to Stripe documentation | |
request.HTTPMethod = "POST" | |
request.HTTPBody = str.dataUsingEncoding(NSUTF8StringEncoding) | |
//create request block | |
self.dataTask = self.defaultSession.dataTaskWithRequest(request) { (data, response, error) in | |
//get returned error | |
if let error = error { | |
print(error) | |
completion(success: false) | |
} | |
else if let httpResponse = response as? NSHTTPURLResponse { | |
//you can also check returned response | |
if(httpResponse.statusCode == 200) { | |
if let data = data { | |
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) | |
//serialize the returned datas an get the customerId | |
if let id = json["id"] as? String { | |
self.customerId = id | |
self.createCard(id, card: card) { (success) in | |
completion(success: true) | |
} | |
} | |
} | |
} | |
else { | |
completion(success: false) | |
} | |
} | |
} | |
//launch request | |
self.dataTask?.resume() | |
} | |
} | |
} | |
//create card for given user | |
func createCard(stripeId: String, card: STPCardParams, completion: (success: Bool) -> Void) { | |
stripeTool.generateToken(card) { (token) in | |
if(token != nil) { | |
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.stripe.com/v1/customers/\(stripeId)/sources")!) | |
//token needed | |
var params = [String:String]() | |
params["source"] = token!.tokenId | |
var str = "" | |
params.forEach({ (key, value) in | |
str = "\(str)\(key)=\(value)&" | |
}) | |
//basic auth | |
request.setValue(self.stripeTool.getBasicAuth(), forHTTPHeaderField: "Authorization") | |
request.HTTPMethod = "POST" | |
request.HTTPBody = str.dataUsingEncoding(NSUTF8StringEncoding) | |
self.dataTask = self.defaultSession.dataTaskWithRequest(request) { (data, response, error) in | |
if let error = error { | |
print(error) | |
completion(success: false) | |
} | |
else if let data = data { | |
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) | |
// print(json) | |
completion(success: true) | |
} | |
} | |
self.dataTask?.resume() | |
} | |
} | |
} | |
//get user card list | |
func getCardsList(completion: (result: [AnyObject]?) -> Void) { | |
//request to create the user | |
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.stripe.com/v1/customers/\(self.customerId!)/sources?object=card")!) | |
//basic auth | |
request.setValue(self.stripeTool.getBasicAuth(), forHTTPHeaderField: "Authorization") | |
//POST method, refer to Stripe documentation | |
request.HTTPMethod = "GET" | |
//create request block | |
self.dataTask = self.defaultSession.dataTaskWithRequest(request) { (data, response, error) in | |
//get returned error | |
if let error = error { | |
print(error) | |
completion(result: nil) | |
} | |
else if let httpResponse = response as? NSHTTPURLResponse { | |
//you can also check returned response | |
if(httpResponse.statusCode == 200) { | |
if let data = data { | |
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) | |
let cardsArray = json["data"] as? [AnyObject] | |
completion(result: cardsArray) | |
} | |
} | |
else { | |
completion(result: nil) | |
} | |
} | |
} | |
//launch request | |
self.dataTask?.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment