0

我在rails中有一个控制器操作,它应该检查交易的当前braintree状态是“submitted_for_settlement”还是“authorized”,然后使交易无效,但如果状态为“settled”,则退还交易。不知何故,控制器正在取消所有交易并且没有像我想要的那样工作。这是我的代码示例:

@transaction = Braintree::Transaction.find(@id)
if @transaction.status == "authorized" || "submitted_for_settlement" 
  @result = Braintree::Transaction.void(@id)       
elsif @transaction.status == "settled"
   @result = Braintree::Transaction.refund(@id)            
end  


if @result.success?  
@order.update(status: "voided")
 redirect_to orders_path, notice: "transaction successfully voided  "
elsif @result.transaction

 redirect_to orders_path, alert: "transaction could not be cancelled code: #{@result.transaction.processor_response_code} text: #{@result.transaction.processor_response_text}"

else
      errors = @result.errors.map { |error| "Error: #{error.code}: #{error.message}" }
      flash[:error] = errors
      redirect_to orders


end

我在已结算的交易中收到一条错误消息,上面写着“交易只有在获得授权或提交_for_settlement 的情况下才能作废”,但所有已授权并提交结算的交易都正确触发。

更麻烦的是,即使我收到错误并且braintree已结算的交易保持不变,控制器也会取消数据库中的状态。

为什么我会收到此错误?

4

1 回答 1

1

换行

if @transaction.status == "authorized" || "submitted_for_settlement"

@transaction.status == "authorized" || @transaction.status == "submitted_for_settlement"

因为==比 具有更高的优先级||@transaction.status == "authorized" || "submitted_for_settlement"相当于(@transaction.status == "authorized") || "submitted_for_settlement"。因此,如果状态为“已授权”,则此表达式返回true,否则返回字符串"submitted_for_settlement",这在 Ruby 中也被认为是真值。

我建议定义一些方法Braintree::Transaction

class Braintree::Transaction
  %w(authorized submitted_for_settlement settled).each do |status|
    define_method("#{status}?") do
      self.status == status
    end
  end
end

然后在你的控制器中

if @transaction.authorized? || @transaction.submitted_for_settlement?
  # ...
elsif @transaction.settled?
  # ...
end
于 2016-06-19T23:17:46.363 回答