简体   繁体   English

可以在没有支付信息的情况下使用 Stripe JS 创建令牌

[英]Possible to create Token with Stripe JS with no payment information

I have a client that wants to have a Stripe subscription that does not cost anything.我有一个客户想要免费订阅 Stripe。 Currently, their application is set up to take CC info because their older plan did require a monthly fee.目前,他们的应用程序设置为获取 CC 信息,因为他们的旧计划确实需要按月付费。

I want to know if it is possible to create a Stripe customer token via Stripe JS without taking any kind of CC info/bank account etc from the user?我想知道是否可以通过 Stripe JS 创建 Stripe 客户令牌,而无需从用户那里获取任何类型的 CC 信息/银行帐户等?

I tried removing the CC fields with jQuery, but I keep getting a Stripe Invalid Request error because it cannot find the payment information.我尝试使用 jQuery 删除 CC 字段,但我不断收到 Stripe Invalid Request 错误,因为它找不到付款信息。

So to summarize, when a user selects the 'free' plan from the dropdown, they should not see the CC fields and they should be able to progress through the sign up page without any Stripe validations while also creating their customer token.总而言之,当用户从下拉列表中选择“免费”计划时,他们不应该看到 CC 字段,并且他们应该能够在没有任何 Stripe 验证的情况下进入注册页面,同时还可以创建他们的客户令牌。

Is this possible?这可能吗?

Relevant files相关文件

Hosts Controller #create主机控制器#create

  def create
    @host = User.new(host_params)

    if @host.valid?
      customer = Stripe::Customer.create(
        stripe_params.merge(email: host_params[:email], coupon: coupon)
      )

      @host.update(
        stripe_customer_id: customer[:id],
        subscription_plan: stripe_params[:plan]
      )

      sign_in(:user, @host)

      redirect_to dashboard_path, notice: t('notices.hosts.created')
    else
      render :new
    end
  rescue Stripe::CardError, Stripe::InvalidRequestError => error
    render :new, alert: error.message
  end

Stripe JS code条纹JS代码

$(document).on 'ready page:load', ->
    $cardFields = $('#credit-card-fields')
    $parentForm = $cardFields.parents('form')
    $parentBtn  = $('.js-payment-btn', $parentForm)

    return unless $parentForm.length

    $parentBtn.on 'click', (event) ->
      event.preventDefault()

      pub_key = $('meta[name=stripe-publishable-key]').attr('content')

      $parentBtn.prop('disabled', true)

      Stripe.setPublishableKey(pub_key)

      Stripe.card.createToken $parentForm, (status, response) ->
        if response.error
          $parentForm.find('.payment-errors').text(response.error.message)
          $parentBtn.prop('disabled', false)
        else
          token = response.id
S>        $parentForm.append($('<input type="hidden" name="stripe[source]" />').val(token))
          $parentForm.get(0).submit()

Stripe JS Form条纹 JS 表单

<script type="text/javascript" src="https://js.stripe.com/v2/" data-turbolinks-track="true"></script>

<div class="clearfix" id="credit-card-fields">
  <span class="payment-errors"></span>

  <div class="form-group">
    <div class="input-group">
      <span class="input-group-addon icon icon-credit-card"></span>
      <input id="card-number" class="form-control input-cc" required placeholder="Card Number" type="text" size="16" maxlength=16 data-stripe="number" />
    </div>
  </div>

  <div class="form-group side-by-side">
    <div class="input-group">
      <span class="input-group-addon icon icon-calendar"></span>
      <input id="card-month" class="form-control input-exp" required placeholder="MM" type="text" size="2" maxlength="2" data-stripe="exp-month" />
      <input id="card-year" class="form-control input-exp" required placeholder="YY" type="text" size="2" maxlength="2" data-stripe="exp-year" />
    </div>
  </div>

  <div class="form-group side-by-side">
    <div class="input-group">
      <span class="input-group-addon icon icon-lock"></span>
      <input id="card-cvc" class="form-control input-cvv" required placeholder="CVC" type="text" size="4" maxlength="4" data-stripe="cvc" />
    </div>
  </div>
</div>

It would not make sense to create a card token without any card details so this is not possible.创建没有任何卡详细信息的卡令牌是没有意义的,因此这是不可能的。 As you mentioned, if you try to create a token without the required parameters such as the card number or the expiration date you get an invalid_request_error back.正如您所提到的,如果您尝试创建没有所需参数(例如卡号或到期日期)的令牌,则会返回invalid_request_error

If you don't want to require card details then you should just not create a token.如果您不想要求卡详细信息,那么您不应该创建令牌。 When someone attempts to subscribe to a free plan, you would bypass the need to create a token and simply collect the rest of the information you want such as the customer's email address or name.当有人尝试订阅免费计划时,您无需创建令牌,只需收集所需的其余信息,例如客户的电子邮件地址或姓名。 Then server-side, you'd call the Create Customer API and pass the plan id in the plan parameter to subscribe them.然后在服务器端,您将调用Create Customer API并在plan参数中传递计划 ID 以订阅它们。

If the plan is free then there's no need to pass a token and you can simply ignore the source parameter completely.如果计划是免费的,则无需传递令牌,您可以完全忽略source参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM