即使 source_id 是一个字符串(使用 square connect 和云函数),也会获取 source_id 的字符串预期错误

Getting string expected error for source_id even though the source_id is a string(using square connect with cloud functions)

我创建了一个云函数来创建并完成对 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": "Expected a string value (line 1, character 14)","field": "source_id","category": "INVALID_REQUEST_ERROR"}]}\n'

我检查了source_id的值,它是一个字符串。但错误另有说明。

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

感谢您的宝贵时间!

我在 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。仅此而已...