zach wilcox
zach wilcox

Reputation: 75

JSON could not be serialized because of error

I am trying to use Stripe payment to allow users to send payments. I can successfully create a stripe customer when there is a user created in my app, however I can not find the ephemeralKey and I think that is why I am seeing,

Error, JSON could not be serialized because of error: The data couldn't be read because it isn't in the correct format.

When I create the stripe customer, the code I use is

exports.createStripeCustomer = functions.auth.user().onCreate((user) => {
  return stripe.customers.create({
    email: user.email,
  }).then((customer) => {
    return admin.database().ref(`/stripe_customers/${user.uid}/customer_id`).set(customer.id);
  });
});

and how I am trying to create the ephemeralKey

exports.createEphemeralKey = functions.https.onRequest((req, res) => {
  const stripe_version = req.body.api_version;
  const customerId = req.body.customerId
  if (!stripe_version) {
    console.log('I did not see any api version')
    res.status(400).end()
    return;
  }

  stripe.ephemeralKeys.create(
    {customer: customerId},
    {stripe_version: stripe_version}
  ).then((key) => {
     console.log("Ephemeral key: " + key)
     res.status(200).json(key)
  }).catch((err) => {
    console.log('stripe version is ' + stripe_version + " and customer id is " + customerId + " for key: " + stripe_key + " and err is " + err.message )
    res.status(500).json(err)
  });
});

and myApiCLient is as

   enum APIError: Error {
     case unknown

     var localizedDescription: String {
         switch self {
         case .unknown:
             return "Unknown error"
         }
     }
 }

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

func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
  let url = self.baseURL.appendingPathComponent("ephemeral_keys")
    
    let defaults = UserDefaults.standard
    let customerid = defaults.string(forKey: "customer_id")
    AF.request(url, method: .post, parameters: [
        "api_version": apiVersion, "customer_id": customerid!
        ])
        .validate(statusCode: 200..<300)
        .responseJSON { responseJSON in
            switch responseJSON.result {
            case .success(let json):
                completion(json as? [String: AnyObject], nil)
            case .failure(let error):
                completion(nil, error)
            }
    }
}

I am trying to figure out the issue I am having but it is not very clear because in my firebase functions logs all I see is

image1

This isn't very clear and I have tried to print out statements to the console but they are not showing in my functions log. Is there anything that sticks out that needs to be corrected??

I have finally received an error in my stripe console that says,

  {
  "error": {
    "message": "Must provide exactly one of these parameters: [:customer, :issuing_card].",
    "type": "invalid_request_error"
  }
} 

Upvotes: 0

Views: 677

Answers (1)

floatingLomas
floatingLomas

Reputation: 8737

You don't appear to be sending the customer_id from your client side code, so it's likely undefined in your API call to create the Ephemeral Key - but you should be able to confirm that in your Stripe Dashboard logs.

Upvotes: 1

Related Questions