1

我正在将 Stripe 集成到我的 Rails 应用程序中,当我尝试运行付款表单时,我的 Stripe 日志中出现以下错误:

{ "error": { "type": "invalid_request_error", "message": "您必须提供有效卡" }

使用此请求正文:

{“计划”:“1”,“描述”:“jjets718@yahoo.com”}

我意识到,在请求正文中,当我在付款表单中提交信用卡信息时,我应该拥有 Stripe 给我的令牌。当我将付款保存在控制器中时,我使用我在模型中定义的以下方法:

def save_with_payment
 if valid?
   customer = Stripe::Customer.create(description: email, plan: 1, card: stripeToken)
   self.stripe_customer_token = customer.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."
  false
   end

如果有人可以帮助我弄清楚为什么在我创建客户时没有将 Stripe 令牌提交给 Stripe,以及如何修复潜在的错误,那就太好了。我的 Stripe javascript 生成一个名为“stripeToken”的 Stripe 令牌;所以问题一定在于控制器如何获取令牌并创建客户。

<head>
    <script type="text/javascript" src="https://js.stripe.com/v1/"></script>
    <!-- jQuery is used only for this example; it isn't required to use Stripe -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
        // this identifies your website in the createToken call below
        Stripe.setPublishableKey('pk_gV4NDoU8ymr5md49FJeNb5E1lIAda');

        function stripeResponseHandler(status, response) {
            if (response.error) {
                // re-enable the submit button
                $('.submit-button').removeAttr("disabled");
                // show the errors on the form
                $(".payment-errors").html(response.error.message);
            } else {
                var form$ = $("#payment-form");
                // token contains id, last4, and card type
                var token = response['id'];
                // insert the token into the form so it gets submitted to the server
                form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
                // and submit
                form$.get(0).submit();
            }
        }

        $(document).ready(function() {
            $("#payment-form").submit(function(event) {
                // disable the submit button to prevent repeated clicks
                $('.submit-button').attr("disabled", "disabled");
                // createToken returns immediately - the supplied callback submits the form if there are no errors
                Stripe.createToken({
                    number: $('.card-number').val(),
                    cvc: $('.card-cvc').val(),
                    exp_month: $('.card-expiry-month').val(),
                    exp_year: $('.card-expiry-year').val()
                }, stripeResponseHandler);
                return false; // submit from callback
            });
        });

        if (window.location.protocol === 'file:') {
            alert("stripe.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact support@stripe.com if you need assistance.");
        }
    </script>
</head>
4

0 回答 0