我在user.rb
中有以下方法:
def create_card(params)
begin
token = Stripe::Token.create(:card => {
:number => params["number"],
:exp_month => params["expiry_month"],
:exp_year => params["expiry_year"],
:cvc => params["cvc"]})
rescue Stripe::CardError => card
end
if token.present?
begin
card_object = Stripe::Customer.create(:card => token.id, :description => "#{token.card.type} #{token.card.last4}")
rescue Stripe::CardError => card
end
end
if card_object.present?
card = self.cards.create! :card_id => card_object.id, :brand => token.card.type, :display_number => token.card.last4, :expiry_month => token.card.exp_month, :expiry_year => token.card.exp_year
end
return card
end
即使我提供了错误的卡片信息,它也会生成令牌(和卡片),所以我用Pry进入了方法。这就是我所看到的:
> params
=> {"number"=>"4242424242424242", "cvc"=>"123", "expiry_month"=>"12", "expiry_year"=>""}
> token = Stripe::Token.create(:card => {
:number => params["number"],
:exp_month => params["expiry_month"],
:exp_year => params["expiry_year"],
:cvc => params["cvc"]})
=> #<Stripe::Token:0x3ffec3d0e9d4 id=test_tok_1> JSON: {
"id": "test_tok_1",
"livemode": false,
"used": false,
"object": "token",
"type": "card",
"card": {"id":"test_cc_2","object":"card","last4":"4242","type":"Visa","brand":"Visa","funding":"credit","exp_month":"12","exp_year":"","fingerprint":"eXWMGVNbMZcworZC","customer":"test_cus_default","country":"US","name":"Johnny App","address_line1":null,"address_line2":null,"address_city":null,"address_state":null,"address_zip":null,"address_country":null,"cvc_check":null,"address_line1_check":null,"address_zip_check":null,"number":"4242424242424242","cvc":"123"}
}
为什么要生成令牌?
当我检查此方法返回的卡片对象时,我得到:
> card
+----+------------+----------------+--------------+-------------+-------+-------------------------+-------------------------+----------+---------+
| id | card_id | display_number | expiry_month | expiry_year | brand | created_at | updated_at | order_id | user_id |
+----+------------+----------------+--------------+-------------+-------+-------------------------+-------------------------+----------+---------+
| 2 | test_cus_3 | 4242 | 12 | | Visa | 2016-03-03 23:46:41 UTC | 2016-03-03 23:46:41 UTC | | 1 |
+----+------------+----------------+--------------+-------------+-------+-------------------------+-------------------------+----------+---------+
> card.errors
=> #<ActiveModel::Errors:0x007ff13f1cb7d0
@base=
#<Card:0x007ff13f1e9d70
id: 2,
card_id: "test_cus_3",
display_number: "4242",
expiry_month: 12,
expiry_year: nil,
brand: "Visa",
created_at: Thu, 03 Mar 2016 23:46:41 UTC +00:00,
updated_at: Thu, 03 Mar 2016 23:46:41 UTC +00:00,
order_id: nil,
user_id: 1>,
@messages={}>
答案 0 :(得分:0)
答案 1 :(得分:-1)
您似乎正在提供一个成功的卡号参数。所有测试编号均为documented here。
无论您是否指定有效的到期年份,此特定数字将始终成功。
如果您尝试提出expired_card
CardError
,请尝试使用4000000000000069