iOS-Swift,Stripe createEphemeralKeys 返回 Google 登录页面 [英] iOS-Swift, Stripe createEphemeralKeys Returning Google SignIn Page
问题描述
您好,我正在尝试使用 Firebase 云功能获取临时密钥,下面是我的 Swift 文件 &节点文件
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();
});
});
当我执行 REST API 调用时,作为响应,我得到了 Google 登录 URL,即使我在 POSTMAN 中尝试过,响应也是纯 HTML 文本.我正在使用 STPCustomerContext
&STPPaymentContext
,这里 spinner 继续旋转.
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.
这是 xcode 调试器中的日志.
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";
} })
我关注了这个 youtube 视频.
I followed this youtube video.
Stripe 入门!(Xcode 中的 Swift 3:第 1 部分)
我尝试使用 Alamofire 的
.responseJSON
但失败了.
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.
推荐答案
看来我的nodejs代码做法不对,因为我用的是express,后来改了代码实现如下.
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);
});
});
并且输出日志如下
{
id: 'ephkey_1BramAFjruqsvjkVQGdZLiV5',
object: 'ephemeral_key',
associated_objects: [ { type: 'customer', id: 'cus_CEPMtLbshv7EaP' } ],
created: 1517701830,
expires: 1517705430,
livemode: false,
secret: 'ek_test_YWNjdF8xQmxUb0FGanJ1cXN2amtWLHVPcUdMN3d4UEhncW1sQkNJYmlOdzhwUGdjVUxOd1Y'
}
swift 文件如下所示.
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")
}
}
}
这篇关于iOS-Swift,Stripe createEphemeralKeys 返回 Google 登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!