从 [object Promise] 获取数据?

get data from [object Promise]?

如何从 [object Promise] 获取信息? 我正在使用 GCF(云函数)来处理方形付款。

截至目前,我收到了 { response="OK:[object Promise]" }

的回复

这是在云平台上处理云函数:

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
  };
});

这是在 Android 设备上处理返回的数据:

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();
        }
       });
   }

我正在查看的一些来源:

connect-api-example using nodejs on github

square-conect on npm

我找到了问题的解决方案,基本上我的“凭证访问令牌正在使用授权访问令牌”是第一个错误,另一个是由于 itempotency 密钥的最大限制为 45 个字符到 API 引用 square connect api,另一个是我如何返回响应,就我消费的承诺而言,响应应采用 JSON 格式。这是源代码,(java 很好,不需要编辑)它只在 nodejs 端。 API键在GCF平台的环境变量端被引用。这将有效地允许使用“无服务器方法”通过 android 应用程序处理方形支付。

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 amount 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);
    }
});