问题
How can i get the information from [object Promise]
?
I am using GCF (cloud function) to process square payments.
As of right now I am getting back a response of { response="OK:[object Promise]" }
This is processing the cloud function on the cloud platform:
const functions = require('firebase-functions');
const SquareConnect = require('square-connect');
const crypto = require('crypto');
exports.fxtest = functions.https.onCall((data, context) => {
const defaultClient = SquareConnect.ApiClient.instance;
defaultClient.basePath = "https://connect.squareupsandbox.com";
const oauth2 = defaultClient.authentications['oauth2'];
oauth2.accessToken = 'sandbox-token-ommitted';
const idempotency_key = crypto.randomBytes(23).toString('hex');
const payments_api = new SquareConnect.PaymentsApi();
const item_source = data.source_id;
const item_price = 1.00;
const item_currency = 'USD';
const request_body = {
"idempotency_key": idempotency_key,
"source_id": item_source,
"amount_money": {
"amount": item_price,
"currency": item_currency
}
};
var rsp;
try{
const response = payments_api.createPayment(request_body)
.then(
r=> { return r; })
.catch(
e => { return e; });
const json = JSON.stringify('OK:' + response);
rsp = json;
} catch(error){
return rsp = 'ERROR:' + error;
}
return{
response: rsp
};
});
This is the processing the returned data on an Android device:
private FirebaseFunctions mFunctions;
private Task<HttpsCallableResult> fxtest(String text, Context ctx, CardDetails crds){
Map<String, Object> data = new HashMap<>();
data.put("source_id",crds.getNonce());
return this.mFunctions.getHttpsCallable("fxtest").call(data)
.addOnCompleteListener((Activity) ctx, new OnCompleteListener<HttpsCallableResult>() {
@Override public void onComplete(@NonNull Task<HttpsCallableResult> task) {
Toast.makeText(ctx, "result: " + task.getResult().getData(),Toast.LENGTH_LONG).show();
}
});
}
Some sources i was looking at:
connect-api-example using nodejs on github
square-conect on npm
回答1:
I've found the solution to the question, basically my "credentials access token was using authorization access token" that was the first mistake, the other was due to the itempotency key which has a max limit of 45 characters according to the API reference square connect api, and the other was how i returned the response which is meant to be in JSON format as far as the promise i consumed was doing. Here is the source code, (the java is fine, no need to edit that) it was only on the nodejs side. The API keys are referenced in the environmental variables side on the GCF platform. This will effectively allow processing a square payment through android applications using "serverless approach".
const functions = require('firebase-functions');
const SquareConnect = require('square-connect');
const crypto = require('crypto');
exports.fxtest = functions.https.onCall(async (data, context) => {
/* testing url for sandbox */
//defaultClient.basePath = process.env.TESTING_SQUARE_CONNECT_URL;
const defaultClient = SquareConnect.ApiClient.instance;
defaultClient.basePath = process.env.PRODUCTION_SQUARE_CONNECT_URL;
const oauth2 = defaultClient.authentications["oauth2"];
oauth2.accessToken = process.env.PRODUCTION_APPLICATION_ACCESS_TOKEN;
const idempotency_key = crypto.randomBytes(16).toString("hex");
const payments_api = new SquareConnect.PaymentsApi() ;
/* value of ammount is in cents as of 11/29/2019
, 1 is equal to 1 cent, 100 is equal to 100 cents */
const request_body = {
"idempotency_key": idempotency_key,
"source_id": data.source_id,
"amount_money": {
"amount": 100,
"currency": "USD"
},
};
try{
response = await payments_api.createPayment(request_body)
.then(
r=> {
if(r.ok) { return Promise.resolve(r); }
return Promise.reject(Error("TRY ERROR_ON_RESPONSE: " + JSON.stringify(r)))
})
.catch(
e=> {
return Promise.reject(Error("TRY ERROR_ON_EXCEPTION: " + JSON.stringify(e)))
});
return "TRY OKAY: " + JSON.stringify(response);
} catch(error){
return "CATCH ERROR: " + JSON.stringify(error);
}
});
来源:https://stackoverflow.com/questions/59105378/get-data-from-object-promise