iOS-Swift,Stripe createEphemeralKeys返回Google SignIn页面

您好我正在尝试使用Firebase云function获取临时密钥,下面是我的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 SignIn URL,即使我在POSTMAN中尝试过,响应也是纯HTML文本。 我正在使用STPCustomerContextSTPPaymentContext ,这里spinner继续旋转。

这是登录xcode调试器。

  ( { 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"; } }) 

我关注了这个YouTubevideo。

Stripe入门! (Xcode中的Swift 3:第1部分)

我试图使用Alamofire's .responseJSON但它失败了。

请让我知道我在哪里犯错,或者任何提示/文档将非常有用。

似乎我对nodejs代码的方法不正确,因为我使用了express,后来用下面的代码改变了代码实现。

 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文件看起来像这样。

 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") } } }