无法按照协议分析临时密钥响应



我正试图在我的IOS应用程序中创建一个临时密钥。我可以成功地创建一个条纹客户,保存在我的firebase控制台和条纹面板上。然而,当我尝试创建临时密钥时,在尝试查看结账控制器后,我在ios控制台中收到了错误。

'无法按照协议STPCustomerPhemeralKeyProvider解析临时密钥响应。确保您的后端正在将临时密钥的未修改JSON发送到您的应用程序。

在我看到的firebase函数日志上,

createEphemeralKey请求的内容类型不正确。

createEphemeralKey无效请求,无法处理。

在我的index.js文件中,我使用的代码是

exports.createEphemeralKey = functions.https.onCall(async(data, context) => {
var stripeVersion = data.api_version;
const customerId = data.customer_id;
return stripe.ephemeralKeys.create(
{customer: customerId},
{stripe_version: stripeVersion}
).then((key) => {
return key
}).catch((err) => {
console.log(err)
})
})

以下是我创建条纹客户的方法。

exports.createStripeCustomer = functions.auth.user().onCreate((user) => {
return stripe.customers.create({
email: user.email,
}).then((customer) => {
return admin.database().ref(`/stripe_customers/${user.uid}/customer_id`).set(customer.id);
});
});

然后myAPIClient看起来像。

enum APIError: Error {
case unknown
var localizedDescription: String {
switch self {
case .unknown:
return "Unknown error"
}
}
}
static let sharedClient = MyAPIClient()
var baseURLString: String? = "https://myProject.cloudfunctions.net/"
var baseURL: URL {
if let urlString = self.baseURLString, let url = URL(string: urlString) {
return url
} else {
fatalError()
}
}
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let url = self.baseURL.appendingPathComponent("ephemeral_keys")
Alamofire.request(url, method: .post, parameters: [
"api_version": apiVersion,
])
.validate(statusCode: 200..<300)
.responseJSON { responseJSON in
switch responseJSON.result {
case .success(let json):
completion(json as? [String: AnyObject], nil)
case .failure(let error):
completion(nil, error)
}
}
}

在我的checkOutVC中,我有

var stripePublishableKey = "pk_test_testProjectKey"
var backendBaseURL: String? = "https://myProject.cloudfunctions.net"
let customerContext = STPCustomerContext(keyProvider: MyAPIClient())
init(price: Int, settings: Settings) {

if let stripePublishableKey = UserDefaults.standard.string(forKey: "StripePublishableKey") {
self.stripePublishableKey = stripePublishableKey
}
if let backendBaseURL = UserDefaults.standard.string(forKey: "StripeBackendBaseURL") {
self.backendBaseURL = backendBaseURL
}
let stripePublishableKey = self.stripePublishableKey
let backendBaseURL = self.backendBaseURL

assert(stripePublishableKey.hasPrefix("pk_"), "You must set your Stripe publishable key at the top of acceptWorker.swift to run this app.")
assert(backendBaseURL != nil, "You must set your backend base url at the top of acceptWorker.swift to run this app.")

Stripe.setDefaultPublishableKey(self.stripePublishableKey)
let config = STPPaymentConfiguration.shared()
config.appleMerchantIdentifier = self.appleMerchantID
config.companyName = self.companyName
config.requiredBillingAddressFields = settings.requiredBillingAddressFields
config.requiredShippingAddressFields = settings.requiredShippingAddressFields
config.shippingType = settings.shippingType
config.additionalPaymentOptions = settings.additionalPaymentOptions
config.cardScanningEnabled = true
self.country = settings.country
self.paymentCurrency = settings.currency

self.theme = settings.theme
MyAPIClient.sharedClient.baseURLString = self.backendBaseURL

let paymentContext = STPPaymentContext(customerContext: customerContext, configuration: config, theme: settings.theme)
self.paymentContext = STPPaymentContext(customerContext: customerContext)
super.init(nibName: nil, bundle: nil)
self.paymentContext.delegate = self
self.paymentContext.hostViewController = self
self.paymentContext.paymentAmount = 5000 // This is in cents, i.e. $50 USD
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

我为代码太长而道歉,但我真的遇到了麻烦。为什么后端不为客户创建ephemeralKey?

有两件事向我袭来:

  • 您已经编写了一个可调用类型函数(使用onCall(,但是尝试用普通的HTTP请求调用它。这些功能需要使用Firebase的客户端库调用(https://firebase.google.com/docs/functions/callable#call_the_function)。这个堆栈溢出答案提供了一些关于这方面的好链接:Firebase Cloud函数删除用户
  • 您的firebase函数正在从中解析stripe_versioncustomer_iddata,但您的请求仅发送api_version。你的您发送stripe_versioncustomer_id的代码

相关内容

  • 没有找到相关文章