尽管检索到了clientSecret,Apple Pay支付仍无法通过



目标是成功地进行Apple Pay支付,并通过此API集成完成。我确实处于成功的边缘,但我还有一个错误无法解决。

func startApplePayCheckout() {
let backendUrlForIntent = "https://us-central1-xxxxx-41f12.cloudfunctions.net/createPaymentIntent"
guard let user = Auth.auth().currentUser else { return }
getUsersStripeCustomerID { (customerid) in
if let id = customerid {
// Create a PaymentIntent as soon as the view loads
let costAsAnInt = self.actualCostOfEvent.text?.replacingOccurrences(of: "$", with: "").replacingOccurrences(of: ".", with: "")
let costForStripe = Int(costAsAnInt!)
let url = URL(string: backendUrlForIntent)!
let json: [String: Any] = [
"amount": costForStripe! + (self.gothereFee * Int(self.stepperValue.value)),
"currency": "CAD",
"customer": id,
"setup_future_usage": "on_session",
]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
let task = URLSession.shared.dataTask(with: request, completionHandler: { [weak self] (data, response, error) in
guard let response = response as? HTTPURLResponse,
response.statusCode == 200,
let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
let clientSecret = json["clientSecret"] as? String else {
let message = error?.localizedDescription ?? "Failed to decode response from server."
self?.displayCancelledAlert(title: "Error Loading Page", message: message)
return
}
guard let paymentIntentID = json["paymentIntentID"] as? String else { return }

self?.db.document("student_users/(user.uid)/events_bought/(self?.nameToUseInAPICall)").setData(["stripePaymentIntentID": paymentIntentID], merge: true, completion: { (error) in
if let error = error {
print("There was an error setting the paymentIntentID in the document: (error)")
} else {
print("PaymentIntentID successfully stored!")
}
})
print("Created PaymentIntent")
self?.paymentIntentClientSecret = clientSecret
})
task.resume()
}
}

当Apple Pay表单出现时,我称之为正确。print语句工作正常,检索到clientSecret。现在,当我真正尝试付款时,我仍然会得到一个";付款未完成";,这就是我在didCreatePaymentMethod函数中得到的:

func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
guard let paymentIntentClient = paymentIntentClientSecret else {
print("There is an issue with the clientSecret")
return
}

let error = NSError(domain: backendUrl, code: 400, userInfo: [NSLocalizedDescriptionKey: "The payment cannot go through for some reason!"])

let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClient)
paymentIntentParams.paymentMethodId = paymentMethod.stripeId

let paymentHandler = STPPaymentHandler.shared()
paymentHandler.confirmPayment(paymentIntentParams, with: self) { (status, intent, error) in
switch status {
case .canceled:
print("Payment Canceled")
break
case .failed:
print("Payment Failed")
break
case .succeeded:
print("Payment Succeeded")
break
default:
break
}
}

completion(paymentIntentClient, error)
}

EDIT因此,实现了此功能后,当我查看日志时,支付实际上会收费,但对于实际的Apple Pay本身,它从未在Apple Pay表单中显示成功消息。以下是我在didCompleteWithStatus方法中的内容:

func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {

guard status == .success else {
print("Payment couldn't go through")
return
}
}

所以发生的奇怪的事情是,第一个方法最终是成功的,收费显示在API调用日志中,但didCompleteWith...方法总是以错误状态和"错误"结束;付款未完成";我一辈子都不明白为什么。

我认为问题在于您在以下行中创建了一个错误:

let error = NSError(domain: backendUrl, code: 400, userInfo: [NSLocalizedDescriptionKey: "The payment cannot go through for some reason!"])

然后将该错误输入到completion处理程序中:

completion(paymentIntentClient, error)

这导致了";付款未完成";Apple Pay表单中显示错误。

completion处理程序的想法是向其提供付款意向的客户端机密如果您不能向其提供客户端机密,则会出现错误,而不是两者都提供。文档对此进行了解释:

使用PaymentIntent的客户端机密或创建PaymentIntnt时发生的错误进行调用。

如果调用不带errorcompletion,则应该已设置好。

相关内容

  • 没有找到相关文章