无法读取数据,因为数据的Stripe格式不正确



我正试图使用Stripe和Firebase在我的IOS应用程序中创建StripeEphemeralKeys,但当我尝试打印数据时,我一直收到这个错误。The data couldn’t be read because it isn’t in the correct format.我不明白为什么!

功能:

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

条纹API:

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函数。也就是说,虽然我让模拟器在本地运行,但我没有告诉我的客户使用模拟器。这是通过调用useFunctionsEmulator(origin: [YOUR HOST])来完成的。点击此处了解更多信息。

看起来onCall返回的数据结构与您预期的不同:https://firebase.google.com/docs/functions/callable-reference

我认为您可能需要使用onRequest:https://firebase.google.com/docs/functions/http-events#using_express_request_and_response_objects

最新更新