Hi I am trying to get ephemeral key using Firebase cloud function, below is my Swift file & node’s file
.swift
class VIARestClient
It seems that my approach for nodejs code is not correct, as I was using express, later changed the code implementation with below code.
exports.createEphemeralKeys = functions.https.onRequest((req, res) => {
var api_version = req.body.api_version;
var customerId = req.body.customerId;
if (!api_version) {
res.status(400).end();
return;
}
stripe.ephemeralKeys.create(
{ customer: customerId },
{ stripe_version: api_version },
function(err, key) {
return res.send(key);
});
});
And the output log is as follows
{
id: 'ephkey_1BramAFjruqsvjkVQGdZLiV5',
object: 'ephemeral_key',
associated_objects: [ { type: 'customer', id: 'cus_CEPMtLbshv7EaP' } ],
created: 1517701830,
expires: 1517705430,
livemode: false,
secret: 'ek_test_YWNjdF8xQmxUb0FGanJ1cXN2amtWLHVPcUdMN3d4UEhncW1sQkNJYmlOdzhwUGdjVUxOd1Y'
}
the swift file looks like this.
class VIARestClient: NSObject, STPEphemeralKeyProvider {
static let restClient = VIARestClient();
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let URLString = API_ENDPOINT + CREATE_EMPHEREMAL_KEY as String;
let custID = "cus_CEPMtLbshv7EaP";
var requestData : [String : String]? = [String : String]()
requestData?.updateValue(apiVersion, forKey: "api_version");
requestData?.updateValue(custID, forKey: "customerId");
submitDataToURL(URLString, withMethod: "POST", requestData: requestData!) { (jsonResponse, err) in
if err != nil {
completion(nil, err)
}
else {
completion(jsonResponse, nil)
}
}
}
func submitDataToURL(_ urlString : String, withMethod method : String, requestData data : [String : Any], completion : @escaping (_ jsonResponse : [String : Any], _ err: Error?) -> Void) {
do {
guard let url = URL(string: urlString) else {return};
let defaultSession = URLSession(configuration: .default)
var urlRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60)
urlRequest.httpMethod = method;
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") // the request is JSON
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept") // the expected response is also JSON
let httpBodyData : Data?
try httpBodyData = JSONSerialization.data(withJSONObject: data, options: []);
urlRequest.httpBody = httpBodyData;
let dataTask = defaultSession.dataTask(with: urlRequest, completionHandler: { (responseData, urlResponse, error) in
print("responseData \(String(describing: responseData))");
print("urlResponse \(String(describing: urlResponse))");
if error == nil {
do {
let response = try JSONSerialization.jsonObject(with: responseData!, options: []) as! [String : Any];
print(response);
completion(response, nil);
}
catch {
print("Exception")
let response : [String : Any] = [String : Any]()
completion(response, error);
}
}
else {
let response : [String : Any] = [String : Any]()
completion(response, error);
}
});
dataTask.resume();
}
catch {
print("Excetion in submitDataToURL")
}
}
}