查看 braintree 事务是否具有成功状态

See if a braintree transaction has a successful status

Braintree提供了一个pretty massive list of different statuses to a transaction. Without hardcoding a massive list of statuses (that might change in the future), is there a recommended way to know if a Braintree::Transaction是否成功?

看起来他们在结果对象上有一个名为 is_success 的方法,如果成功,它将 return 为 True,否则为 False。

当您进行交易时,您应该返回一个包含交易是否成功的结果对象。

这是一个人为的例子

<?php

require 'braintree_setup.php';

$nonceFromTheClient = $_POST["payment-method-nonce"];

$result = Braintree\Transaction::sale([
    'amount' => '10.00',
    'paymentMethodNonce' => $nonceFromTheClient,
    'options' => [
        'submitForSettlement' => true,
        'threeDSecure' => [
            'required' => true
        ]
    ]

]);

if ($result->success) {
   die('Payment was successful');
}

编辑:关于您的评论,

如果您想获取交易集合,则需要使用 Braintree\Transaction::search() 方法。

一旦完成,您就可以像处理其他任何事情一样循环处理结果。

foreach ($transactions as $transaction) {
    var_dump($transaction);
}

我从 Braintree 支持人员那里得到了以下回复:

A successful transaction will go through the following statuses: Authorized > Submitted for Settlement > Settling > Settled. If you're aiming to only show successful transactions my recommendation would be to only pull transactions with a status of Settled. Once a transaction has reached the Settled status it will never change. However, transactions with a status of Submitted for Settlement or Settling, while rare, could potentially be declined or return an error during the settlement process.

在我的代码中,我选择执行以下操作:

def is_braintree_status_successful?(braintree_transaction)
  %w(authorized submitted_for_settlement settling settled).include?(braintree_transaction.status)
end

我也问了一个与 Braintree 支持类似的问题,答案可能对其他人有用,即使我决定使用 Stripe(经过所有测试后我发现 stripe 好多了;)。

问题

嗨,

只是集成了 Braintree,但找不到我应该如何获得交易确认(意味着它已正确“收费”)。当我创建一个“transaction.sale”(包括 submitForSettlement)时,我总是得到(沙箱)一个指示状态“已提交以进行结算”的响应,并且直到 Braintree 全局处理它(晚上, ETC。)。我也找不到相关的 webhook 来获得付款确认(因为我目前正在使用 PayPal - IPN)。

When/how 我能否收到付款确认信息,以便“交付”购买的商品?通过检查您的文档,就好像您要到第二天才能得到确认……它究竟是如何工作的?

谢谢!

回答

嗨纳乔,

感谢联系!

Braintree 交易要经过几个步骤,其中第二个步骤是提交结算。这意味着钱已经开始从客户账户中移动,并且是交易提交后的即时状态;提交后不可能立即获得 'settled' 或 'complete' 状态。处于此状态的交易通常会在 24 小时内进入结算,然后结算,但这可能取决于您的处理银行。

从提交结算到结算时交易失败的情况非常少见,因为金额在提交结算之前已经被授权。授权交易意味着资金在客户账户中被搁置一段时间,具体时间取决于付款方式,直到结算为止。我们会立即从处理银行获得关于授权是否成功的响应,您将在 Braintree_Transaction 响应对象中看到。

因此,您通常可以依靠提交结算的状态来反映有效、成功的交易。

我们不为交易提供 webhooks - 相反,我们的 API 旨在通过响应对象提供即时反馈。因此,我们只为非 API 调用触发的异步事件(例如订阅计费或支付资金)提供 webhook。我们鼓励我们的客户利用我们的 API 响应,以及我们的报告系统和全面的搜索呼叫选项,以获得他们需要的任何额外功能。

如果您想提取交易的更新状态,例如确认付款已结算,您可以使用 Transaction.find() API 请求,传入交易 ID 并读入来自响应对象的状态。这可以很容易地 运行 作为一个脚本,它遍历前几天的交易 ID 并每天更新每个交易的状态。

我希望这能更清楚地解释交易过程并为您提供一些选择!

亲切的问候,布伦特里

万一有帮助!