问题
Here is Stripe's example code for retrieving a customer (https://github.com/stripe/stripe-ios/blob/master/Example/Stripe%20iOS%20Example%20(Simple)/MyAPIClient.swift):
@objc func retrieveCustomer(_ completion: @escaping STPCustomerCompletionBlock) {
guard let key = Stripe.defaultPublishableKey() , !key.contains("#") else {
let error = NSError(domain: StripeDomain, code: 50, userInfo: [
NSLocalizedDescriptionKey: "Please set stripePublishableKey to your account's test publishable key in CheckoutViewController.swift"
])
completion(nil, error)
return
}
guard let baseURLString = baseURLString, let baseURL = URL(string: baseURLString) else {
// This code is just for demo purposes - in this case, if the example app isn't properly configured, we'll return a fake customer just so the app works.
let customer = STPCustomer(stripeID: "cus_test", defaultSource: self.defaultSource, sources: self.sources)
completion(customer, nil)
return
}
let path = "/customer"
let url = baseURL.appendingPathComponent(path)
let request = URLRequest.request(url, method: .GET, params: [:])
let task = self.session.dataTask(with: request) { (data, urlResponse, error) in
DispatchQueue.main.async {
let deserializer = STPCustomerDeserializer(data: data, urlResponse: urlResponse, error: error)
if let error = deserializer.error {
completion(nil, error)
return
} else if let customer = deserializer.customer {
completion(customer, nil)
}
}
}
task.resume()
}
Stripe has a customer deserializer that specifies that "STPCustomerDeserializer
expects the JSON response to be in the exact same format as the Stripe API." The Stripe API is here in Nodejs:
// Retrieve Stripe Customer
app.get('/customer', function(request, response) {
// Load the Stripe Customer ID for your logged in user
var customer = 'cus_abc...';
stripe.customers.retrieve(customerId, function(err, customer) {
if (err) {
response.status(402).send('Error retrieving customer.');
} else {
response.json(customer);
}
});
The response I get is an error: The data could not be read because it isn't in the correct format.
I think it wants me to return JSON but I tried that several different ways to no avail. Such as:
func retrieveCustomer(_ completion: @escaping STPCustomerCompletionBlock) {
guard let key = Stripe.defaultPublishableKey() , !key.contains("#") else {
let error = NSError(domain: StripeDomain, code: 50, userInfo: [
NSLocalizedDescriptionKey: "Set PubKey"])
completion(nil, error)
return
}
guard let baseURLString = baseURLString, let baseURL = URL(string: baseURLString) else {
let customer = STPCustomer(stripeID: "", defaultSource: self.defaultSource, sources: self.sources)
completion(customer, nil)
return
}
let path = "/customer"
let url = baseURL.appendingPathComponent(path)
let request = URLRequest.request(url, method: .GET, params: [:])
let task = self.session.dataTask(with: request) { (data, urlResponse, error) in
DispatchQueue.main.async {
let deserializer = STPCustomerDeserializer(data: data, urlResponse: urlResponse, error: error)
if let error = deserializer.error {
completion(nil, error)
return
} else if let customer = deserializer.customer {
do {
let parser = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as Any
print(parser)
} catch {
print(error)
}
completion(customer, nil)
}
}
}
task.resume()
}
What am I missing!?
来源:https://stackoverflow.com/questions/41494186/the-data-could-not-be-read-because-it-isnt-in-the-correct-format-json-swift-3