【问题标题】:get data from [object Promise]?从 [object Promise] 获取数据?
【发布时间】:2021-08-25 03:59:42
【问题描述】:

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

    我找到了问题的解决方案,基本上我的“凭据访问令牌正在使用授权访问令牌”这是第一个错误,另一个是由于 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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2019-03-14
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      相关资源
      最近更新 更多