使用Stripe的Swift应用程序 - 类型'STPCustomer'没有成员'decodingObject'

时间:2017-06-16 15:02:32

标签: ios swift alamofire stripe-payments

我正在尝试使用他们在GitHub上的Swift iOS(简单)示例中提供的模型将Stripe集成到应用程序中(可用于:https://github.com/stripe/stripe-ios)。

我将Stripe和Alamofire添加到我通过Cocoapods构建的新项目中,一切都完美排除了MyAPIClient.swift文件中的一个函数。这个功能是:

  @objc func retrieveCustomer(_ completion: @escaping STPCustomerCompletionBlock) {
    let url = self.baseURL.appendingPathComponent("customer")
    Alamofire.request(url)
        .validate(statusCode: 200..<300)
        .responseJSON { response in
            switch response.result {
            case .success(let result):
                if let customer = STPCustomer.decodedObject(fromAPIResponse: result as? [String: AnyObject]) { //Error occurs here      
                    completion(customer, nil)
                } else {
                    completion(nil, NSError.customerDecodingError)
                }
            case .failure(let error):
                completion(nil, error)
            }
    }
}

在包含“if let customer”语句的行上,抛出错误,解释Type'STPCustomer'没有成员'decodingObject'。我不确定我是否错误地添加了框架或者做了其他不正确的事情,因为这个语句在Stripe的快速示例项目中完美运行。以下是相关文件的全部内容:

import Foundation
import Stripe
import Alamofire

class MyAPIClient: NSObject, STPBackendAPIAdapter {

    static let sharedClient = MyAPIClient()
    var baseURLString: String? = nil
    var baseURL: URL {
        if let urlString = self.baseURLString, let url = URL(string: urlString) {
            return url
        } else {
            fatalError()
        }
    }

    func completeCharge(_ result: STPPaymentResult, amount: Int, completion: @escaping STPErrorBlock) {
        let url = self.baseURL.appendingPathComponent("charge")
        Alamofire.request(url, method: .post, parameters: [
            "source": result.source.stripeID,
            "amount": amount
            ])
            .validate(statusCode: 200..<300)
            .responseString { response in
                switch response.result {
                case .success:
                    completion(nil)
                case .failure(let error):
                    completion(error)
                }
        }
    }

    @objc func retrieveCustomer(_ completion: @escaping STPCustomerCompletionBlock) {
        let url = self.baseURL.appendingPathComponent("customer")
        Alamofire.request(url)
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                switch response.result {
                case .success(let result):
                    if let customer = STPCustomer.decodedObject(fromAPIResponse: result as? [String: AnyObject]) {


                        completion(customer, nil)
                    } else {
                        completion(nil, NSError.customerDecodingError)
                    }
                case .failure(let error):
                    completion(nil, error)
                }
        }
    }

    @objc func selectDefaultCustomerSource(_ source: STPSourceProtocol, completion: @escaping STPErrorBlock) {
        let url = self.baseURL.appendingPathComponent("customer/default_source")
        Alamofire.request(url, method: .post, parameters: [
            "source": source.stripeID,
            ])
            .validate(statusCode: 200..<300)
            .responseString { response in
                switch response.result {
                case .success:
                    completion(nil)
                case .failure(let error):
                    completion(error)
                }
        }
    }

    @objc func attachSource(toCustomer source: STPSourceProtocol, completion: @escaping STPErrorBlock) {
        let url = self.baseURL.appendingPathComponent("customer/sources")
        Alamofire.request(url, method: .post, parameters: [
            "source": source.stripeID,
            ])
            .validate(statusCode: 200..<300)
            .responseString { response in
                switch response.result {
                case .success:
                    completion(nil)
                case .failure(let error):
                    completion(error)
                }
        }
    }
}

extension NSError {
    static var customerDecodingError: NSError {
        return NSError(domain: StripeDomain, code: 50, userInfo: [
            NSLocalizedDescriptionKey: "Failed to decode the Stripe customer. Have you modified the example backend?"
            ])
    }
}

任何帮助表示赞赏!谢谢!

1 个答案:

答案 0 :(得分:1)

好吧,我是个白痴。 Stripe Pushed更新10.1.0,但没有使其10.1.0示例项目的新分支成为默认分支。现在一切都很好。