Swift 数据无法’;因为它是’;t格式正确
我正在尝试使用Stripe和Firebase在IOS应用程序中创建Swift 数据无法’;因为它是’;t格式正确,swift,firebase,stripe-payments,Swift,Firebase,Stripe Payments,我正在尝试使用Stripe和Firebase在IOS应用程序中创建StripeEphemeralKeys,但当我尝试打印数据时,总是出现此错误无法读取数据,因为它的格式不正确。我不知道为什么 功能: exports.createStripeEphemeralKeys = functions.https.onCall((data, context) => { const customerId = data.customerId; const stripe_version = data.str
StripeEphemeralKeys
,但当我尝试打印数据时,总是出现此错误<代码>无法读取数据,因为它的格式不正确。我不知道为什么
功能:
exports.createStripeEphemeralKeys = functions.https.onCall((data, context) => {
const customerId = data.customerId;
const stripe_version = data.stripe_version;
return stripe.ephemeralKeys
.create({
customer: customerId,
stripe_version: stripe_version
})
});
func choosePaymentButtonTapped() {
guard let uid = Auth.auth().currentUser?.uid else { return }
var paymentContext: STPPaymentContext?
let ref = Database.database().reference().child("stripe_customers").child(uid)
ref.observeSingleEvent(of: .value) { (snapshot) in
guard let dict = snapshot.value as? [String: Any] else { return }
guard let customerId = dict["customer_id"] as? String else { return }
let customerContext = STPCustomerContext(keyProvider: StripeProvider(customerId: customerId))
paymentContext = STPPaymentContext(customerContext: customerContext)
paymentContext!.delegate = self
paymentContext!.hostViewController = self
paymentContext!.presentPaymentOptionsViewController()
}
}
class StripeProvider: NSObject, STPCustomerEphemeralKeyProvider {
lazy var functions = Functions.functions()
let customerId: String
init(customerId: String){
self.customerId = customerId
}
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let data: [String: Any] = [
"customerId": customerId,
"stripe_version": apiVersion
]
functions
.httpsCallable("createStripeEphemeralKeys")
.call(data) { result, error in
if let error = error {
print(error.localizedDescription)
completion(nil, error)
} else if let data = result?.data as? [String: Any] {
completion(data, nil)
}
}
}
}
演示文稿:
exports.createStripeEphemeralKeys = functions.https.onCall((data, context) => {
const customerId = data.customerId;
const stripe_version = data.stripe_version;
return stripe.ephemeralKeys
.create({
customer: customerId,
stripe_version: stripe_version
})
});
func choosePaymentButtonTapped() {
guard let uid = Auth.auth().currentUser?.uid else { return }
var paymentContext: STPPaymentContext?
let ref = Database.database().reference().child("stripe_customers").child(uid)
ref.observeSingleEvent(of: .value) { (snapshot) in
guard let dict = snapshot.value as? [String: Any] else { return }
guard let customerId = dict["customer_id"] as? String else { return }
let customerContext = STPCustomerContext(keyProvider: StripeProvider(customerId: customerId))
paymentContext = STPPaymentContext(customerContext: customerContext)
paymentContext!.delegate = self
paymentContext!.hostViewController = self
paymentContext!.presentPaymentOptionsViewController()
}
}
class StripeProvider: NSObject, STPCustomerEphemeralKeyProvider {
lazy var functions = Functions.functions()
let customerId: String
init(customerId: String){
self.customerId = customerId
}
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let data: [String: Any] = [
"customerId": customerId,
"stripe_version": apiVersion
]
functions
.httpsCallable("createStripeEphemeralKeys")
.call(data) { result, error in
if let error = error {
print(error.localizedDescription)
completion(nil, error)
} else if let data = result?.data as? [String: Any] {
completion(data, nil)
}
}
}
}
条带API:
exports.createStripeEphemeralKeys = functions.https.onCall((data, context) => {
const customerId = data.customerId;
const stripe_version = data.stripe_version;
return stripe.ephemeralKeys
.create({
customer: customerId,
stripe_version: stripe_version
})
});
func choosePaymentButtonTapped() {
guard let uid = Auth.auth().currentUser?.uid else { return }
var paymentContext: STPPaymentContext?
let ref = Database.database().reference().child("stripe_customers").child(uid)
ref.observeSingleEvent(of: .value) { (snapshot) in
guard let dict = snapshot.value as? [String: Any] else { return }
guard let customerId = dict["customer_id"] as? String else { return }
let customerContext = STPCustomerContext(keyProvider: StripeProvider(customerId: customerId))
paymentContext = STPPaymentContext(customerContext: customerContext)
paymentContext!.delegate = self
paymentContext!.hostViewController = self
paymentContext!.presentPaymentOptionsViewController()
}
}
class StripeProvider: NSObject, STPCustomerEphemeralKeyProvider {
lazy var functions = Functions.functions()
let customerId: String
init(customerId: String){
self.customerId = customerId
}
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let data: [String: Any] = [
"customerId": customerId,
"stripe_version": apiVersion
]
functions
.httpsCallable("createStripeEphemeralKeys")
.call(data) { result, error in
if let error = error {
print(error.localizedDescription)
completion(nil, error)
} else if let data = result?.data as? [String: Any] {
completion(data, nil)
}
}
}
}
看起来
onCall
返回的数据结构与您期望的不同:
我想您可能想使用
onRequest
来代替:看起来onCall
返回的数据结构与您期望的不同:
我想您可能想使用onRequest来代替:我也有同样的问题。问题是我没有在本地运行
onCall
函数。也就是说,虽然我让模拟器在本地运行,但我没有告诉我的客户机使用模拟器。这可以通过调用useFunctionsEmulator(源:[您的主机])来完成。
。更多信息。我也有同样的问题。问题是我没有在本地运行onCall
函数。也就是说,虽然我让模拟器在本地运行,但我没有告诉我的客户机使用模拟器。这可以通过调用useFunctionsEmulator(源:[您的主机])来完成。
。更多信息。我发现错误未能加载内部
函数?完整的错误实际上是错误域=com.firebase.functions code=13“INTERNAL”UserInfo={NSLocalizedDescription=INTERNAL}
这是某种firebase错误,那么:为什么否决?firebase的回复清楚地指出了一个内部错误。您能帮我看一下这个,并告诉我您是否知道可能是什么导致了错误吗?我得到错误未能加载内部
函数?完整的错误实际上是错误域=com.firebase.functions code=13“INTERNAL”UserInfo={NSLocalizedDescription=INTERNAL}
这是某种firebase错误,那么:为什么投票失败?firebase的回复清楚地指出了一个内部错误。您能帮我看一下这个,并告诉我您是否知道可能是什么导致了错误吗?