I have created an app and am using the Firebase backend for both Android and IOS. The problem, I am having is:
I have a firebase-function that collects the ‘uid’ and ‘email’ from payoutRequest() and then sends the information to Paypal so that the driver can be paid from PayPal.
this Android code works:
public static final MediaType MEDIA_TYPE = MediaType.parse(“application/json”); ProgressDialog progress;
private void payoutRequest() {
progress = new ProgressDialog(this);
progress.setTitle("Processing your payout ...");
progress.setMessage("Please Wait .....");
progress.setCancelable(false);
progress.show();
// HTTP Request ....
final OkHttpClient client = new OkHttpClient();
// in json - we need variables for the hardcoded uid and Email
JSONObject postData = new JSONObject();
try {
postData.put("uid", FirebaseAuth.getInstance().getCurrentUser().getUid());
postData.put("email", mPayoutEmail.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
// Request body ...
RequestBody body = RequestBody.create(MEDIA_TYPE, postData.toString());
// Build Request ...
final Request request = new Request.Builder()
.url("https://us-central1-ryyde-sj.cloudfunctions.net/payout")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("Authorization", "Your Token")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// something went wrong right off the bat
progress.dismiss();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// response successful ....
// refers to response.status('200') or ('500')
int responseCode = response.code();
if (response.isSuccessful()) {
switch(responseCode) {
case 200:
Snackbar.make(findViewById(R.id.layout),
"Payout Successful!", Snackbar.LENGTH_LONG)
.show();
break;
case 500:
Snackbar.make(findViewById(R.id.layout),
"Error: no payout available", Snackbar
.LENGTH_LONG).show();
break;
default:
Snackbar.make(findViewById(R.id.layout),
"Error: couldn't complete the transaction",
Snackbar.LENGTH_LONG).show();
break;
}
} else {
Snackbar.make(findViewById(R.id.layout),
"Error: couldn't complete the transaction",
Snackbar.LENGTH_LONG).show();
}
progress.dismiss();
}
});
}
this SWIFT code isn’t working like it should be like the Android code:
In an attempt to create an equivalent of the Android code, I did this:
let params: Parameters = [“uid”: FIRAuth.auth()?.currentUser!.uid as Any, “email”: txtPayoutEmail.text!]
let url = "https://us-central1-ryyde-sj.cloudfunctions.net/payout"
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"cache-control": "no-cache",
"Authorization": "Your Token"
]
let postData = try! JSONSerialization.data(withJSONObject: params, options: [])
print("postData: \(postData)")
let request = NSMutableURLRequest(url: NSURL(string: url)! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("status code: \(httpStatus.statusCode)")
print("response: \(String(describing: response))")
}
if (error != nil) {
print("No data found: \(error!)")
} else {
_ = response as? HTTPURLResponse
// print(httpResponse!)
let parsedObject = try! JSONSerialization.jsonObject(with: postData, options: .allowFragments)
print(parsedObject)
}
})
dataTask.resume()
What it does and doesn’t do:
When I run the above SWIFT code, it collects the ‘uid’ and ‘email’ in the firebase-functions logs but it doesn’t send it to Paypal using the same index.js file I am using with Android.
I know it is not the index file because it works for android, so it has to do with the request I am sending in SWIFT.
For a reference of the post I put up on stackoverflow, please see this post:
how to get Swift equivalent from Android code
Please help!