数据格式不正确,因此无法读取

时间:2020-04-14 05:30:41

标签: swift firebase stripe-payments

我正在尝试使用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)
            }
    }
}
}

2 个答案:

答案 0 :(得分:1)

我有同样的问题。问题是我没有在本地运行onCall函数。也就是说,尽管我的仿真器在本地运行,但我没有告诉客户端使用仿真器。可以通过调用useFunctionsEmulator(origin: [YOUR HOST])来完成。更多信息here

答案 1 :(得分:-1)

看起来onCall返回的数据结构与您期望的不同:refer this post

我认为您可能想改用onRequesthttps://firebase.google.com/docs/functions/callable-reference