我最近将Stripe iOS SDK和API从2019-05-16版本更新为最新版本。以前,我的代码可以使用Charges API正常工作,但是现在使用PaymentIntents存在问题。
我使用Stripe Connect和自定义帐户,同时创建单独的费用和转帐。
问题是当我尝试进行初次付款时,我没有这样的付款意图:(空)错误。但是实际上正在创建付款意图。付款为“未完成”和“ PaymentIntent状态:requires_payment_method”。
另一方面,如果我手动将从 MyAPIClient.swift / createCustomerKey 方法传递的(clientSecret)替换为 CheckoutViewController.swift / paymentContext( didCreatePaymentResult)方法,并使用以前未完成的费用中的另一个clientSecret密钥,都可以正常工作,并且付款成功。
我可以确认将clientSecret密钥正确地从服务器(Firebase Cloud函数)传递到客户端,然后从MyAPIClient传递到CheckoutViewController-请参见下面的代码中的print()。我还仔细检查了前端和后端的API密钥。
这是我的后端代码:
const stripe = require('stripe')(functions.config().stripe.token);
exports.createPaymentIntentForStripeAccount = functions.https.onRequest((req, res) => {
const uid = req.body.uid;
const amount = req.body.amount;
const currency = req.body.currency;
const customer_id = req.body.customer_id;
const transfer_group = req.body.transfer_group;
return stripe.paymentIntents.create({
amount: amount,
currency: currency,
customer: customer_id,
transfer_group: transfer_group,
capture_method: "manual",
}).then(function(paymentIntent) {
const clientSecret = paymentIntent.client_secret;
res.status(200).json(clientSecret);
});
});
客户端:
// MyAPIClient
func createPaymentIntent(amount: Int, bookingID: String, completion: @escaping ((Result<String>) -> Void)) {
let url = self.baseURL.appendingPathComponent("createPaymentIntentForStripeAccount")
let transfer_group = bookingID.components(separatedBy: "/").last!
let params: [String: Any] = [
"uid": UserService.currentUserProfile!.uid,
"customer_id": UserService.currentUserProfile!.stripe_customer_id!,
"amount": amount,
"transfer_group": transfer_group,
"currency": "USD"
]
Alamofire.request(url, method: .post, parameters: params)
.validate(statusCode: 200..<300)
.responseString { responseString in
switch responseString.result {
case .success(let clientSecret):
print(clientSecret)
completion(.success(clientSecret))
case .failure(let error):
completion(.failure(error))
}
}
}
// CheckoutViewController
func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
MyAPIClient.sharedClient.createPaymentIntent(amount: self.paymentContext.paymentAmount, bookingID: self.bookingRef.url) { result in
switch result {
case .success(let clientSecret):
let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
paymentIntentParams.configure(with: paymentResult)
print(clientSecret)
STPPaymentHandler.shared().confirmPayment(withParams: paymentIntentParams, authenticationContext: paymentContext) { status, paymentIntent, error in
switch status {
case .succeeded:
completion(.success, nil)
case .failed:
completion(.error, error)
case .canceled:
completion(.userCancellation, nil)
@unknown default:
completion(.error, nil)
}
}
case .failure(let error):
print("Failed to create a Payment Intent: \(error)")
completion(.error, error)
break
}
}
}
任何建议或反馈都将受到欢迎和赞赏。
谢谢。
答案 0 :(得分:0)
上面的代码用双引号引起了clientSecret,结果得到一个看起来像“” pi_xxx_secret_xxx“”的键,当然这是行不通的。
我应该在服务器端解析JSON后转义多余的引号,或者在客户端将其删除。例如:
completion(.success(clientSecret.replacingOccurrences(of: "\"", with: ""))