【问题标题】:Create Customer w/ simple Stripe Checkout使用简单的 Stripe Checkout 创建客户
【发布时间】:2014-10-04 22:14:06
【问题描述】:

我的目标是验证用户卡信息并将该信息存储在客户对象中。

我经营一个付费送货服务,并且正在构建一个黑客来防止虚假订单(如果人们下错订单或没有出现,我们可以通过条形仪表板向他们收费)。

完整的条带集成是长期目标,但我需要尽快完成。我已阅读(重新阅读)文档,但遇到了问题。

简单的条纹结账效果很好,但我不知道如何从那里创建客户。

脚本:

    <form action="/charge" method="POST">
      <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="TEST KEY"
        data-image="/square-image.png"
        data-name="Food Bazooka"
        data-description="Customer Verification. No charges :)"
        data-panel-label="Confirm and Verify"
        data-label="Confirm">
      </script>


    </form>

非常感谢任何反馈或想法。

谢谢!

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    您正在使用嵌入式表单,而您需要使用自定义表单和一些服务器端代码。

    您首先需要从您的表单中创建一个代表客户卡的一次性使用令牌 (假设您的表格包含信用卡号、到期日等...)

    取自文档:

    表单标记:

    <form action="/customer" method="POST" id="payment-form">
      <span class="payment-errors"></span>
    
      <div class="form-row">
        <label>
          <span>Card Number</span>
          <input type="text" size="20" data-stripe="number"/>
        </label>
      </div>
    
      <div class="form-row">
        <label>
          <span>CVC</span>
          <input type="text" size="4" data-stripe="cvc"/>
        </label>
      </div>
    
      <div class="form-row">
        <label>
          <span>Expiration (MM/YYYY)</span>
          <input type="text" size="2" data-stripe="exp-month"/>
        </label>
        <span> / </span>
        <input type="text" size="4" data-stripe="exp-year"/>
      </div>
    
      <button type="submit">Submit Payment</button>
    </form>
    

    Javascript:

    jQuery(function($) {
      $('#payment-form').submit(function(event) {
        var $form = $(this);
    
        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);
    
        Stripe.card.createToken($form, stripeResponseHandler);
    
        // Prevent the form from submitting with the default action
        return false;
      });
    });
    
    function stripeResponseHandler(status, response) {
      var $form = $('#payment-form');
    
      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // response contains id and card, which contains additional card details
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));
        // and submit
        $form.get(0).submit();
      }
    };
    

    这实质上是采用您的表单,并在提交之前添加一个名为 stripeToken 的隐藏字段

    注意表单操作是 /customer

    我看到您正在使用标签中的 Ruby On Rails - 所以您需要使用控制器处理客户 POST

    这是您需要做的:
    https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later

    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here https://dashboard.stripe.com/account
    Stripe.api_key = "sk_test_X9S2nHIxFy399uoNvakwJYSn"
    
    # Get the credit card details submitted by the form
    # notice stripeToken - this is the hidden field 
    token = params[:stripeToken]
    
    # Create a Customer
    customer = Stripe::Customer.create(
      :card => token,
      :description => "payinguser@example.com"
    )
    
    # Charge the Customer instead of the card
    # ** I have commented this block out, as you say you do not want to charge the customer
    # Stripe::Charge.create(
        # :amount => 1000, # incents 
        # :currency => "gbp",
        # :customer => customer.id
    # )
    
    # Save the customer ID in your database so you can use it later
    save_stripe_customer_id(user, customer.id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2023-01-18
      • 2015-12-26
      • 2014-11-12
      • 2017-12-23
      • 2014-03-30
      • 2019-03-28
      相关资源
      最近更新 更多