rails 3活跃商家购买代码

时间:2011-05-19 15:26:27

标签: ruby-on-rails payment-gateway activemerchant

我是rails的新用户和活跃商家的新用户,只想知道以下代码是否足以使用活跃商家进行付款处理。

如您所见,我使用授权和捕获而不是购买方法。我主要担心的是代码中的“带来的数量”减法(当支付处理失败时它是相反的部分),我不太确定如何在竞争条件或支付网关错误的情况下处理它。

请注意,变量事务是模型/表的实例变量,我存储支付网关响应的信息。

def purchase(item)
  price = price_in_cents(item.value)
  if !item.can_purchase
    errors[:base] << "We are sorry, all items are sold out at the moment."
    return false
  else
    response = GATEWAY.authorize(price, credit_card, purchase_options)
    transactions.create!(:action => "authorize", :value => price, :params => response)
    #p response
    if response.success?
      item.brought_quantity = item.brought_quantity + 1
      if item.save!
        response = GATEWAY.capture(price, response.authorization)
        transactions.create!(:action => "capture", :value => price, :params => response)
        if !response.success?
          errors[:base] << "There were some problem processing your payment, please either try again or contact us at support@foo.com with this error id: 111"
          @rd = RunningDeal.find_by_id(@item.id)
          @rd.brought_quantity = @rd.brought_quantity - 1
          @rd.save!
          return false
        end
      else
        errors[:base] << "We are sorry, all items are sold out at the moment."
        return false
      end
    else
      # problem process their payment, put out error
      errors[:base] << "There were some problem processing your payment, please either try again or contact us at support@foo.com with this error id: 111"
      return false
    end
  end
  return true
end

修改 好的,做了一些重构,这里是更新的代码,欢迎任何意见和建议。我删除了!在transaction.create上,因为这不足以引发异常。

以下是基于反馈的更新代码。

#from running_deal.rb
def decrement_deal_quantity
  self.brought_quantity = self.brought_quantity + 1
  return self.save!
end


def purchase(running_deal)
  price = price_in_cents(running_deal.value)
  if !running_deal.can_purchase
    errors[:base] << "We are sorry, all items are sold out at the moment."
    return false
  else
    auth_resp = GATEWAY.authorize(price, credit_card, purchase_options)
    transactions.create(:action => "authorize", :value => price, :success => auth_resp.success?, :message => auth_resp.message, :authorization => auth_resp.authorization, :params => auth_resp)
    if auth_resp.success?
      begin
        running_deal.decrement_deal_quantity
        cap_resp = GATEWAY.capture(price, auth_resp.authorization)
        transactions.create(:action => "capture", :value => price, :success => cap_resp.success?, :message => cap_resp.message, :authorization => cap_resp.authorization, :params => cap_resp)
      rescue
        GATEWAY.void(auth_resp.authorization, purchase_options) if auth_resp.success?
        errors[:base] << "There were some problem processing your payment, please either try again or contact us at support@foo.com"
        return false
      end
    else
      # problem process their payment, put out error
      errors[:base] << "There were some problem processing your payment, please either try again or contact us at support@foo.com"
      return false
    end
  end
  return true

1 个答案:

答案 0 :(得分:3)

处理交易很棘手。

几点想法:

  • 如果授权成功,捕获将在99.9%的情况下成功。你真的不需要担心这个案子。
  • 如果在authorize()成功之后代码中的某些内容失败(比如写入数据库的异常),则要调用void()到网关,以删除授权。否则资金将被冻结7天。
  • 需要将此代码移动到模型方法中:

      @rd = RunningDeal.find_by_id(@item.id)
      @rd.brought_quantity = @rd.brought_quantity - 1
      @rd.save!
    
  • 你需要在方法的底部添加子句以捕获异常,因为你正在调用create!()而不是create()(如果保存则返回true)

    救援Exception =&gt; Ë      #错误处理    端

  • 目前还不清楚为什么如果item.save!失败您的错误消息表明该项目已售罄?这完全是模糊不清的。

总的来说,你想做这样的事情:

  • 检查是否有足够的库存
  • 执行AUTH
  • 启动db transaction
  • 保存/更新所有数据库对象
  • 提交交易
  • 执行CAPTURE
  • 捕获异常,如果AUTH成功 - 执行VOID

希望这有帮助。