如何在付款完成之前阻止 MongoDB Realm 中的数据

问题描述 投票:0回答:3

我有这个功能可以在我的领域存储库中添加订单

suspend fun addOrder(products: List<Product>) {
           realm.write {
            val order = Order().apply {
                products.forEach {
                    this.products.add(it)
                }
            }
            copyToRealm(order)
        }
}

我知道付款是 approved 当在 isPaid 集合中插入带有消息“已批准”的新对象时:{“消息”:“已批准”}

在支付开始前阻止数据并在支付完成后解除阻止的最佳方法是什么?如果付款失败,则应恢复该过程。我猜它就像普通数据库中的事务一样。

注意:从 UI(Kotlin 按钮)调用 AddOrder(list) 函数。

mongodb kotlin transactions realm
3个回答
0
投票

您可以使用 Realm 的交易功能,如下所示,它返回一个布尔值,指示付款是否被批准,您可以使用此返回值向用户显示一条消息,指示他们的订单是否成功。

suspend fun addOrder(products: List<Product>): Boolean {
    var isPaymentApproved = false
    try {
        realm.executeTransaction { transactionRealm ->
            // Create the order and add the products to it
            val order = transactionRealm.createObject(Order::class.java)
            products.forEach { product ->
                order.products.add(product)
            }
            // Block the data before payment starts
            order.isPaid.add(IsPaid("Blocked"))
            // Process payment (this is where you would call your payment gateway API)
            val paymentResult = processPayment()
            // Check if payment was approved
            if (paymentResult == "Approved") {
                // If payment was approved, mark the order as paid and unblock the data
                order.isPaid.clear()
                order.isPaid.add(IsPaid("Approved"))
                isPaymentApproved = true
            } else {
                // If payment failed, delete the order and block the data
                order.isPaid.clear()
                order.isPaid.add(IsPaid("Blocked"))
                order.deleteFromRealm()
            }
        }
    } catch (e: Exception) {
        // Handle any exceptions that occurred during the transaction
        // (e.g. network errors, payment gateway errors, etc.)
        realm.cancelTransaction()
    }
    return isPaymentApproved
}


0
投票

在我看来,你可以“锁定”它们,通过临时删除它们,如果支付成功,你不改变它们,但如果支付失败,你再添加回来。这实际上是一个真正的事务所做的,如果失败或其他方式添加和删除。 但是现在您可以更好地控制删除和添加。 为它们创建单独的方法。

希望对您有所帮助!


0
投票

hey 您可以修改 addOrder 函数以在将订单添加到 Realm 数据库之前启动事务。在交易中,您可以使用提供的产品创建一个新的 Order 对象,并将其 isPaid 字段设置为 false。

一旦付款完成,您就可以开始一个新的交易,将订单对象的isPaid字段更新为true,并向isPaid集合添加一个新对象,并显示消息“Approved”。如果支付失败,您可以将交易回滚到初始状态,有效地撤销交易期间所做的任何更改。

suspend fun addOrder(products: List<Product>) {
realm.executeTransactionAsync({ realm ->
val order = realm.createObject(Order::class.java).apply {
this.isPaid = false
products.forEach {
this.products.add(it)
}
}
}, {
// Transaction was successful, payment is approved
realm.executeTransactionAsync({ realm ->
val order = realm.where(Order::class.java)
.equalTo("isPaid", false)
.findFirst()
order?.let {
it.isPaid = true
val isPaidMessage = realm.createObject(IsPaidMessage::class.java)
isPaidMessage.message = "Approved"
it.isPaidMessages.add(isPaidMessage)
}
})
}, { error ->
// Transaction failed, payment is not approved
error.printStackTrace()
})
}
© www.soinside.com 2019 - 2024. All rights reserved.