【问题标题】:<FIRESTORE> I can't get the combination of transaction and event processing to work<FIRESTORE> 我无法组合使用事务和事件处理
【发布时间】:2021-09-29 02:02:46
【问题描述】:

非常感谢您的帮助。

我目前正在实现一个功能,当使用 Firebase 的 Firestore 和 PayPal 确认付款时,将数据注册到数据库。

当支付或注册数据库失败时,两者之一仍在处理中。

如果数据库注册失败,支付也会被取消。 如果支付失败,请取消对数据库的注册。

如果支付失败,在数据库中取消注册。 我认为在这种情况下可以使用 Firestore 事务,但是 但是,我还不知道如何构建它们。

我已经包含了下面每个的代码。 按钮,支付成功时执行onApprove中的流程,支付失败时执行onApprove中的流程。 按钮,支付成功时执行onApprove中的流程,支付失败时执行onError中的流程。

我正在尝试使用 Firestore 事务来做我想做的事情。 事务过程中的按钮,我认为它不会起作用,因为我不想在 .Buttons 方法本身中捕获错误。 Buttons 方法,它不起作用,因为您不想在处理此 .Buttons 方法本身时捕获错误。还有比如,如果你在支付完成后写一个交易流程(在onApprove中),它不会起作用,因为 我认为您无法取消付款,因为付款已经完成。

我不知道如何实现上述目标。 如果你知道怎么做,请告诉我。

Firestore 交易文档 https://firebase.google.com/docs/firestore/manage-data/transactions?hl=ja#web-v8

// Create a reference to the SF doc.
var sfDocRef = db.collection("cities").doc("SF");

// Uncomment to initialize the doc.
// sfDocRef.set({ population: 0 });

return db.runTransaction((transaction) => {
    // This code may get re-run multiple times if there are conflicts.
    return transaction.get(sfDocRef).then((sfDoc) => {
        if (!sfDoc.exists) {
            throw "Document does not exist!";
        }

        // Add one person to the city population.
        // Note: this could be done without a transaction
        // by updating the population using FieldValue.increment()
        Note: this could be done without a transaction // by updating the population using FieldValue.increment(). var newPopulation = sfDoc.data().population + 1;
        transaction.update(sfDocRef, { population: newPopulation });
    });
}).then(() => {
    console.log("Transaction successfully committed!");
}).catch((error) => {
    console.log("Transaction failed: ", error);
});
```

PayPal's code
```
  <script
    function initPayPalButton() {
      paypal.Buttons({
        style: {
          shape: 'rect',
          color: 'gold',
          layout: 'vertical',
          label: 'paypal',
          
        },

        createOrder: function(data, actions) {
          return actions.order.create({
            purchase_units: [{"amount":{"currency_code": "JPY", "value":1}}]]
          });
        },

        onApprove: function(data, actions) {
          return actions.order.capture().then(function(details) {
            alert('Transaction completed by ' + details.payer.name.given_name + '!') ;
          });
        },

        onError: function(err) {
          console.log(err);
        }
      }).render('#paypal-button-container');
    }
    initPayPalButton();
  </script>.
```

【问题讨论】:

    标签: javascript firebase google-cloud-firestore paypal


    【解决方案1】:

    如果我没记错的话,如果付款得到用户的批准,您希望使用交易在 Firestore 中添加一个文档。 onApprove 方法“从交易中获取资金并向买家显示一条消息,让他们知道交易成功。该方法在买家在 paypal.com 上批准交易后调用”

    如果你在付款完成后写一个交易流程(在onApprove中),它是行不通的,因为我认为你不能取消付款,因为已经付款了。

    这是什么意思? onApprove函数只有在用户批准支付然后Stripe SDK调用一些Orders API时才会触发。

    onApprove: function(data, actions) {
      // This function captures the funds from the transaction.
      return actions.order.capture().then(async function(details) {
        // alert('Transaction completed by ' + details.payer.name.given_name + '!') 
        // This is where you run Firestore's transaction
        await addTxnToDatabase(details);
      }); 
    },
    

    您真的需要 Firestore 交易吗?您是在更新某些内容还是只是在 Firestore 中添加新文档?如果您只是添加一个新文档,那么我猜事务没有用,因为 “读取操作必须在事务中的写入操作之前进行”。您只需编写一个新文档即可。

    async function addTxnToDatabase() {
      return firebase.firestore().collection("payments").add({..}) // any data returned by Paypal
    }
    

    如果你想增加一些东西,那么你可以使用增量方法:

    docRef.update({ field: firebase.firestore.FieldValue.increment(50) })
    

    此外,查看Paypal Webhooks 可能会很有用,它可以为您触发包含某些支付相关事件数据的云函数。

    【讨论】:

    • >如果我没记错的话,如果付款得到用户批准,您想使用交易在 Firestore 中添加文档。 -> 完全正确。我现在担心的是,如果我只是在付款后在 onAprroved 方法中插入 firestore 的 update 或 add 方法,如果该操作(到 firestore)失败,则只会付款,不会更新数据库。
    • @k_ele 是正确的。我会尝试添加一个 recursive function 以防万一失败。但我还是更喜欢使用云功能,所以如果出现故障,我可以通过电子邮件或其他方式通知自己。 Webhooks 仍然是最佳选择。我不确定 Paypal,但 Stripe 每小时重试一次,以防处理 webhook 失败,并在问题仍然存在时通知您。
    • 感谢您的回答!递归或使用 webhook 可能是更好的方法!我会朝那个方向研究,非常感谢!
    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多