Hi I am trying to get ephemeral key using Firebase cloud function, below is my Swift file & node’s file
.swift
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!);
}
func submitDataToURL(_ urlString : String, withMethod method : String, requestData data : [String : Any]) {
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))");
})
dataTask.resume();
}
catch {
print("Excetion in submitDataToURL")
}
}
nodejs
app.post('/createEphemeralKeys', (req, res) => {
const stripe_version = req.body.api_version;
if (!stripe_version) {
res.status(400).end();
return;
}
stripe.ephemeralKeys.create(
{customer: req.body.customerId},
{stripe_version: stripe_version}
).then((key) => {
res.status(200).send(key);
}).catch((err) => {
res.status(500).end();
});
});
When I perform REST API call then in response I am getting Google SignIn URL, Even I tried in POSTMAN, the response is coming plain HTML text. I am using STPCustomerContext
& STPPaymentContext
, here spinner continues to spin.
This is logs in xcode debugger.
(<NSHTTPURLResponse: 0x170237a20> { URL: https://accounts.google.com/ServiceLogin?service=ah&passive=true&continue=https%3A%2F%2Fappengine.google.com%2F_ah%2Fconflogin%3Fcontinue%3Dhttps%3A%2F%2Fus-central1-devdatabasefresh.cloudfunctions.net%2FcreateEphemeralKeys } { status code: 200, headers {
"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 01 Feb 2018 17:44:46 GMT";
Expires = "Mon, 01 Jan 1990 00:00:00 GMT";
Pragma = "no-cache";
Server = GSE;
"Set-Cookie" = "GAPS=1:DPWaQyfDlbfYxiI2mrsbqszeoBaWZg:2mBfv5V9UXY2rkxf;Path=/;Expires=Sat, 01-Feb-2020 17:44:46 GMT;Secure;HttpOnly;Priority=HIGH";
"Strict-Transport-Security" = "max-age=31536000; includeSubDomains";
"alt-svc" = "hq=\":443\"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=\":443\"; ma=2592000; v=\"41,39,38,37,35\"";
"content-security-policy" = "script-src 'nonce-Gb8VeHY/ULjN1Yy9ofK4/tWy+I0' 'unsafe-inline' 'strict-dynamic' https: http: 'unsafe-eval';object-src 'none';base-uri 'self';report-uri /cspreport";
"x-auto-login" = "realm=com.google&args=service%3Dah%26continue%3Dhttps%253A%252F%252Fappengine.google.com%252F_ah%252Fconflogin%253Fcontinue%253Dhttps%253A%252F%252Fus-central1-devdatabasefresh.cloudfunctions.net%252FcreateEphemeralKeys";
"x-content-type-options" = nosniff;
"x-frame-options" = DENY;
"x-xss-protection" = "1; mode=block";
} })
I followed this youtube video.
Getting Started with Stripe! (Swift 3 in Xcode : Part 1)
I tried to use Alamofire's
.responseJSON
but it is failing.
Please let me know where am I making mistake or any hint/documentation will be very helpful.