POST EDITED
I have a CheckoutViewController
that I copied from the Stripe git repository and I implemented it into my app. At first I was trying to setup the STPPaymentContext
in a different vc but it was giving a bad access but now when I try to set it up in the Checkout vc, I get an assertion failure saying the JSON couldn't be parsed.
This is what I have in my MyAPIClient
class:
class MyAPIClient: NSObject, STPCustomerEphemeralKeyProvider {
let baseURL = "https://us-central1-attend-41f12.cloudfunctions.net/createEphemeralKeys"
let ticketVC: TicketFormViewController = TicketFormViewController()
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
ticketVC.getUsersStripeCustomerID { (customerid) in
if let id = customerid {
let url = URL(string:self.baseURL)!
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)!
urlComponents.queryItems = [URLQueryItem(name: "api_version", value: apiVersion), URLQueryItem(name: "customer_id", value: id)]
var request = URLRequest(url: urlComponents.url!)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request, completionHandler: { (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]) as [String : Any]??) else {
completion(nil, error)
return
}
completion(json, nil)
})
task.resume()
}
}
}
}
This is my Cloud Functions function that I created to try and get the ephemeral key:
exports.createEphemeralKeys = functions.https.onRequest(async (req,
res) => {
var api_version = req.body.api_version;
var customerId = req.body.customer_id;
if (!api_version) {
res.status(400).end();
return;
}
let key = await stripe.ephemeralKeys.create(
{ customer: customerId },
{ stripe_version: api_version }
);
res.json(key);
});
This is looking like it's causing the problem, I know it's not the URL because I made another API call this morning with a paymentIntent Cloud Function and it had a 200 Status Code. I copied the code from the Stripe docs and had faith that it would work but it let me down. Any suggestions.
UPDATE I also checked the Cloud Functions server logs, and the function is executing with a 400 Error Status Code, so it's definitely the Cloud Function that is the issue, isn't the json in the exports.createEphemeralKeys
function unmodified already, what else do I need to add for it to work?