0

Here is the request I am sending in Swift:

func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
    let url = self.baseURL.appendingPathComponent("ephemeral_keys")
    let params: Parameters = [
        "api_version":apiVersion,
        "uid":Model.shared.uid
        ]
    Alamofire.request(url, method: .post, parameters: params)
        .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)
            }
    }
}

Here is the cloud function I'm trying to execute. "api_version" is passed as a parameter, but api_version is undefined in the firebase logs. How do I get the api_version? What am I doing wrong?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const stripe = require('stripe')("sk_test_...");

stripe.setApiVersion('2018-11-08');

admin.initializeApp(functions.config().firebase);

exports.ephemeral_keys = functions.https.onRequest((req, res) => {
  const stripe_version = req.api_version;
  if (!stripe_version) {
    console.log('error with stripe version')
    console.log(stripe_version)
    res.status(400).end();
    return;
  }
  const uid = req.params.uid;
  const user = admin.firestore().collection('users').doc(uid);
  console.log('Customer id ' + user.customerId);

  stripe.ephemeralKeys.create(
    {customer: user.customerId},
    {stripe_version: stripe_version}
  ).then((key) => {
    console.log('Ephemeral key ' + key);
    res.status(200).json(key);
  }).catch((err) => {
    res.status(500).end();
  });
});
p3scobar
  • 131
  • 2
  • 6

1 Answers1

0

I don't know much about Alamofire, but from reading a bit about it and searching google, what you can do is this:

Option 1

The best convention is shooting out your request using a URL similar to this:

http://server-host-name:<port>/someName/api_version/2.0/getDataApi

And in the node server side do this:

exports.ephemeral_keys = functions.https.onRequest((req, res) => {
  const stripe_version = req.query.api_version;
  // Do what ever you want with it...
  // ...
}

Option 2

Using api_version as a query string parameter, so in your case i think you are doing it wrong, try this, in client side code:

 let params: Parameters = [
        "api_version":apiVersion,
        "uid":Model.shared.uid
        ]
    Alamofire.request(url, method: .post,
        parameters: params,
        encoding: URLEncoding(destination: .queryString))

This is in order to tell Alamofire to encode the parameters as a query string parameters (more about this read this Stackoverflow post)

Option 3

Or put the api version in a custom HTTP header, like this:

let headers: HTTPHeaders = [
  "api_version": MY_API_KEY,
  "Accept": "application/json"
]

Alamofire.request(yourURL, headers: headers)
  .responseJSON { response in
  debugPrint(response)
}

And try to exctract it in the node server side like this:

const stripe_version = req.headers.api_version;

BTW my best choice would be option 1 (best complies with REST API conventions)

(Not tested) check and let us know if this worked for you!

Mercury
  • 7,430
  • 3
  • 42
  • 54