我的应用程序使用stripe.js
java库来生成与ryan bates did here相同的stripe_card_token
。
我试图向我的控制器添加单元测试,但在生成令牌时遇到了麻烦。我找不到生成令牌的函数,因为它似乎只在javascript中可用。
如何在测试中生成stripe_token?
下面是控制器处理条带的部分:
def save_with_payment!
if valid?
customer = Stripe::Customer.create(
description: "#{user.email} - #{user.id} - #{billing_name}",
email: billing_email,
plan: plan,
card: stripe_card_token)
self.stripe_customer_token = customer.id
plan = validate_actual_plan customer.subscriptions.data[0].plan.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card.
Your card wasn't charged. #{e.message}"
return false
end
下面是控制器:
def create
@subscription = Subscription.new(params[:subscription])
@subscription.user_id = current_user.id
@subscription.expiration_date = 1.month.from_now
@subscription.stripe_card_token = params[:subscription][:stripe_card_token]
respond_to do |format|
if @subscription.save_with_payment!
if current_user.upgrade_plan :premium
format.html { redirect_to user_trades_path(current_user), notice: 'Subscription was successfully created. Compliments you are now subscribed to the premium plan' }
format.json { render json: user_trades_path(current_user), status: :created, location: @subscription }
else
format.html { redirect_to home_pricing_path, notice: 'Error while upgrading your account, please contact us' }
format.json { render json: home_pricing_path, status: :created, location: @subscription }
end
else
format.html { render action: "new" }
format.json { render json: @subscription.errors, status: :unprocessable_entity }
end
end
end
这是生成令牌的Coffeescript:
processCard: ->
card =
number: $(page.cardNumber).val()
cvc: $(page.cardCode).val()
expMonth: $(page.cardMonth).val()
expYear: $(page.cardYear).val()
Stripe.card.createToken(card, @handleStripeResponse)
下面是测试结果:
test "should create subscription" do
begin
StripeMock.start
card = {
number: "4242424242424242",
cvc: "123",
expMonth: 1.month.from_now.month,
expYear: 1.year.from_now.year,
}
token = Stripe.card.createToken(card)
assert_difference('Subscription.count') do
post :create, subscription: {
billing_email: @subscription.billing_email,
billing_city: @subscription.billing_city,
billing_country: @subscription.billing_country,
billing_name: @subscription.billing_name,
billing_street2: @subscription.billing_street2,
billing_street: @subscription.billing_street,
billing_zip: @subscription.billing_zip,
stripe_card_token: token,
plan: @subscription.plan }
end
assert_redirected_to subscription_path(assigns(:subscription))
ensure
StripeMock.stop
end
end
发布于 2014-08-12 22:47:47
我认为创建令牌确实是可以使用条纹API的,如下所述:
https://stripe.com/docs/api#create_card_token
我还发现了我要订阅的计划的一个问题,因为我使用的是StripeMock,根本没有连接到真正的/测试条带。这意味着我的mock不知道我试图订阅的计划。
我还必须生成一个小函数来生成计划。
下面是它的代码:
def create_plan
plan = {
:amount => 1800,
:interval => 'month',
:name => 'premium',
:currency => 'gbp',
:id => 'premium'
}
response = Stripe::Plan.create plan
end
https://stackoverflow.com/questions/25265576
复制相似问题