【发布时间】: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