0

我正在尝试像这样模拟 Stripe:

stub_request(:post, "https://api.stripe.com/v1/tokens").with(
 :body => {"card" => {"number" => "4242424242424242", "exp_month"=> "12", "exp_year" => "2018", "cvc" => "123"}}
).
to_return(:status => 200, :headers => {}, :body => {
  "id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
  "livemode" => false,
  "created" => 1404517660,
  "used" => false,
  "object" => "token",
  "type" => "card",
  "card" => {
    "id" => "card_14CgP22eZvKYlo2CcdUNvicW",
    "object" => "card",
    "last4" => "4242",
    "brand" => "Visa",
    "funding" => "credit",
    "exp_month" => 12,
    "exp_year" => 2018,
    "fingerprint" => "Xt5EWLLDS7FJjR1c",
    "country" => "US"
  }
})

no implicit conversion of Hash into String我打电话时遇到错误:

token = Stripe::Token.create(:card => {
  :number => params["number"],
  :exp_month => params["expiry_month"],
  :exp_year => params["expiry_year"],
  :cvc => params["cvc"]})

body这是因为 in中的哈希to_return,但我不知道如何避免该错误。

如果我添加.to_s到哈希中,我会得到:

来自 API 的无效响应对象:"{\"id\"=>\"tok_14CgP22eZvKYlo2CkgJ6ymLE\", \"livemode\"=>false, \"created\"=>1404517660, \"used\"=>false, \" object\"=>\"token\", \"type\"=>\"card\", \"card\"=>{\"id\"=>\"card_14CgP22eZvKYlo2CcdUNvicW\", \"object\" =>\"card\", \"last4\"=>\"4242\", \"brand\"=>\"Visa\", \"funding\"=>\"credit\", \"exp_month \"=>12, \"exp_year\"=>2018, \"fingerprint\"=>\"Xt5EWLLDS7FJjR1c\", \"country\"=>\"US\"}}"(HTTP 响应代码为 200)

如果我.to_json在末尾添加 a ,则Stripe::Token通过,stack error too deep但当我尝试以token任何方式使用结果时出现错误。

你有什么建议吗?

4

1 回答 1

0

可能是您的存根未正确返回。它可能有助于使您的存根不那么具体。例如:

body = {
  "id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
  "livemode" => false
  # snip...
}

stub_request(:post, "https://api.stripe.com/v1/tokens")
  .to_return(:status => 200, :body => body.to_json)

但是,很难确切地知道抛出了什么错误,所以我只能给出可能有帮助的模糊想法。token调用会给您带来什么错误?

于 2014-07-06T10:23:44.533 回答