从[object Promise]获取数据?

问题描述 投票:0回答:1

我如何从[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

android node.js square-connect
1个回答
0
投票

我已经找到了问题的解决方案,基本上我的“凭证访问令牌正在使用授权访问令牌”,这是第一个错误,另一个是由于itempotency密钥(根据API最多可包含45个字符)参考square connect api,另一种是我如何返回响应,即按照我使用的诺言,响应应采用JSON格式。这是源代码(Java很好,无需进行编辑),它只是在nodejs端。在GCF平台上的环境变量中引用了API密钥。这将有效地允许使用“无服务器方式”通过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 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);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.