결제 처리를 위해 Stripe와 Alamofire를 사용하는 앱을 만들려고합니다. 한 지점에서 작동 한 다음 작동이 중지되었지만 업데이트 또는 오류로 인한 것인지 확실하지 않습니다. 또한 Heroku를 사용하여 백엔드 Nodejs 파일을 실행하고 있으며 서버 측에서 오류가 발생하지 않으며 Stripe에서 테스트 지불이 진행됩니다. Heroku가 올바른 파일 유형을 내 앱으로 다시 보내지 않는 것과 거의 같습니다.
이 오류가 계속 발생합니다.
===========Error===========
Error Code: 10
Error Messsage: Response could not be serialized, input data was nil or zero length.
Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
===========================
암호:
import Foundation
import Stripe
import Alamofire
class StripeClient {
static let sharedClient = StripeClient()
var baseURLString: String? = nil
var baseURL: URL{
if let urlString = self.baseURLString, let url = URL(string: urlString) {
print(url)
return url
} else {
fatalError()
}
}
func createAndConfirmPayment(_ token: STPToken, amount: Int, completion: @escaping (_ error: Error?) -> Void) {
let url = self.baseURL.appendingPathComponent("charge")
print(url)
let params: [String : Any] = ["stripeToken" : token.tokenId, "amount" : amount, "description" : Constats.defaultDescription, "currency" : Constats.defaultCurrency]
AF.request(url, method: .post, parameters: params)
.validate(statusCode: 200..<300)
.responseData(completionHandler: { (response) in
print(response)
switch response.result {
case .success( _):
print("Payment successful")
completion(nil)
case .failure(let error):
if (response.data?.count)! > 0 {print(error)}
print("\n\n===========Error===========")
print("Error Code: \(error._code)")
print("Error Messsage: \(error.localizedDescription)")
if let data = response.data, let str = String(data: data, encoding: String.Encoding.utf8){
print("Server Error: " + str)
}
debugPrint(error as Any)
print("===========================\n\n")
print("error processing the payment", error.localizedDescription)
completion(error)
}
})
}
}
Stripe 18.4, Alamofire 5.0, Xcode 11.3 및 Swift 5를 사용하고 있습니다. 감사합니다!
이 오류 Data
는 응답을 구문 분석 할 때 Alamofire가 예기치 않게 처리 할 필요 가 없음 을 의미합니다 . debugPrint(response)
응답에 대한 자세한 내용을보기 위해 실행할 수 있지만 일반적으로 서버가 적절한 204 또는 205 코드 (일반적으로 200)없이 빈 응답을 반환하여 응답이 비어 있어야 함을 표시 할 때 발생합니다. 이 경우 Alamofire 5.2 이상을 실행중인 경우 추가 빈 응답 코드를 응답 핸들러에 전달할 수 있습니다.
AF.request(...)
.validate()
.responseData(emptyResponseCodes: [200, 204, 205]) { response in
// Process response.
}
Alamofire 5.0 ~ 5.2 미만에서는 인스턴스를 직접 생성하여 응답 시리얼 라이저를 사용자 지정할 수 있습니다.
let serializer = DataResponseSerializer(emptyResponseCodes: Set([200, 204, 205]))
// Use it to process your responses.
AF.request(...)
.validate()
.response(responseSerializer: serializer) { response in
// Process response.
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다