【问题标题】:Getting string expected error for source_id even though the source_id is a string(using square connect with cloud functions)即使 source_id 是字符串,也会为 source_id 获取字符串预期错误(使用带有云函数的方形连接)
【发布时间】:2020-09-11 23:14:43
【问题描述】:

我已经做了一个云函数来创建并完成一个支付请求到 square connect API。

这是函数:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

var SquareConnect = require("square-connect");
var defaultClient = SquareConnect.ApiClient.instance;

exports.sqProcessPayments = functions
  .region("europe-west1")
  .firestore.document("users/{userId}")
  .onUpdate(async (change, context) => {
    var accessToken = "access--token";
    let previousData = change.before.data();
    let updatedData = change.after.data();
    let previousNonce = previousData["payment_nonce"];
    let updatedNonce = updatedData["payment_nonce"];
    if (previousNonce !== updatedNonce) {
      var oauth2 = defaultClient.authentications["oauth2"];
      oauth2.accessToken = accessToken;
      var idempotencyKey = new Date();
      var apiInstance = new SquareConnect.PaymentsApi();
      var body = new SquareConnect.CreatePaymentRequest({
        source_id: updatedNonce.toString(),
        idempotency_key: idempotencyKey.toString(),
        amount_money: {
        amount: 10,
        currency: "USD",
        },
        location_id: "location-id",
        autocomplete: true,
      });
      apiInstance
        .createPayment(body)
        .then(
          function (data) {
            console.log(
              "API called successfully. Returned data: " + data.payment.id
            ); 
            return data;
          },
          function (error) {
            console.error(error);
          }
        )
        .catch((error) => console.log(error));
      }
    });

但是我在运行这个函数时遇到了这个错误

'{"errors": [{"code": "EXPECTED_STRING","detail": "需要一个字符串值(第 1 行,第 14 个字符)","field": "source_id","category": " INVALID_REQUEST_ERROR"}]}\n'

我检查了source_id 的值,它是一个字符串。但错误说明并非如此。

请帮我解决这个问题。任何帮助都会很棒。

感谢您的宝贵时间!

【问题讨论】:

    标签: node.js google-cloud-functions square-connect


    【解决方案1】:

    我在CreatePaymentRequest 代码中找到了这个。这个问题与body对象的创建有关。

    我认为正确的语法是:

    var body = SquareConnect.CreatePaymentRequest.constructFromObject({
            source_id: updatedNonce.toString(),
            idempotency_key: idempotencyKey.toString(),
            amount_money: {
                    amount: 10,
                    currency: "USD",
            },
            location_id: "location-id",
            autocomplete: true,
          });
    

    或者你可以使用:

    var body = new SquareConnect.CreatePaymentRequest(
        updatedNonce.toString(),
        idempotencyKey.toString(),
        amount_money: {
                        amount: 10,
                        currency: "USD",
                }
     )
    

    但在这种情况下,您只能添加source_ididempotency_keyamount_money。仅此而已...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      相关资源
      最近更新 更多