如何解决即使source_id是字符串,也要获取source_id的字符串预期错误使用平方连接与云函数
我已经使用了云功能来创建并完成付款请求以平方连接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));
}
});
'{“错误”:[{“代码”:“ EXPECTED_STRING”,“详细信息”:“预期的字符串值(第1行,字符14)”,“字段”:“ source_id”,“类别”:“ 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,}
)
,但是在这种情况下,您只能添加source_id
,idempotency_key
和amount_money
。没什么...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。