Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 3活动商户购买代码_Ruby On Rails_Payment Gateway_Activemerchant - Fatal编程技术网

Ruby on rails rails 3活动商户购买代码

Ruby on rails rails 3活动商户购买代码,ruby-on-rails,payment-gateway,activemerchant,Ruby On Rails,Payment Gateway,Activemerchant,我对rails和active merchant都是新手,只想知道下面的代码对于使用active merchant进行支付处理是否足够好 如您所见,我使用的是authorize和capture,而不是购买方法。我主要关心的是代码中的“数量”减法(以及它的计数器部分,当支付处理失败时),我不太确定在出现竞争条件或支付网关出错的情况下如何处理它 请注意,变量transactions是一个模型/表的实例变量,我在其中存储支付网关响应的信息 def purchase(item) price = pri

我对rails和active merchant都是新手,只想知道下面的代码对于使用active merchant进行支付处理是否足够好

如您所见,我使用的是authorize和capture,而不是购买方法。我主要关心的是代码中的“数量”减法(以及它的计数器部分,当支付处理失败时),我不太确定在出现竞争条件或支付网关出错的情况下如何处理它

请注意,变量transactions是一个模型/表的实例变量,我在其中存储支付网关响应的信息

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
def采购(项目)
价格=价格(单位:美分)(项目价值)
如果!item.can\u可以购买吗
错误[:base]“authorize”,:value=>price,:params=>response)
#p反应
如果你成功了?
item.Bried_数量=item.Bried_数量+1
如果是item.save!
response=GATEWAY.capture(价格、响应、授权)
交易。创建!(:action=>capture,:value=>price,:params=>response)
如果!成功?
错误[:base]auth_resp.message,:authorization=>auth_resp.authorization,:params=>auth_resp)
如果认证成功?
开始
运行交易。减少交易数量
cap_resp=GATEWAY.capture(价格、认证和授权)
创建(:action=>“capture”,:value=>price,:success=>cap_resp.success?,:message=>cap_resp.message,:authorization=>cap_resp.authorization,:params=>cap_resp)
营救
如果验证成功,则GATEWAY.void(验证响应授权、购买选项)?

错误[:base]处理事务很棘手

两个想法:

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

      @rd = RunningDeal.find_by_id(@item.id)
      @rd.brought_quantity = @rd.brought_quantity - 1
      @rd.save!
    
  • 您需要在方法的底部添加子句来捕获异常,因为您正在调用create!()not create()(保存时返回true)

    救援异常=>e #错误处理 结束

  • 不清楚为什么要使用item.save!失败您的错误消息表明商品已售完?这是完全不清楚的

总的来说,您希望执行以下操作:

  • 检查是否有足够的库存
  • 执行身份验证
  • 启动数据库事务
  • 保存/更新所有数据库对象
  • 提交事务
  • 执行捕获
  • 捕获异常,如果身份验证成功,则执行VOID

希望这能有所帮助。

处理事务很棘手

两个想法:

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

      @rd = RunningDeal.find_by_id(@item.id)
      @rd.brought_quantity = @rd.brought_quantity - 1
      @rd.save!
    
  • 您需要在方法的底部添加子句来捕获异常,因为您正在调用create!()not create()(保存时返回true)

    救援异常=>e #错误处理 结束

  • 不清楚为什么要使用item.save!失败您的错误消息表明商品已售完?这是完全不清楚的

总的来说,您希望执行以下操作:

  • 检查是否有足够的库存
  • 执行身份验证
  • 启动数据库事务
  • 保存/更新所有数据库对象
  • 提交事务
  • 执行捕获
  • 捕获异常,如果身份验证成功,则执行VOID

希望这有帮助。

非常感谢。我已经根据你的建议更新了我的代码,你介意再看一下吗?非常感谢。我已经根据你的建议更新了代码,你介意再看一下吗?