简体   繁体   English

如何使用 Stripe 的 JavaScript API 验证优惠券?

[英]How to validate coupon using Stripe's JavaScript API?

I am using Stripe's API (v2) - https://stripe.com/docs/stripe.js我正在使用 Stripe 的 API (v2) - https://stripe.com/docs/stripe.js

This is being implemented using Laravel's cashier, a package that ships with Laravel, with docs .这是使用 Laravel 的收银员实现的,收银员是 Laravel 附带的一个包,带有docs

I have a very basic form at the moment, collecting the user's card info, which is then passed over to Stripe who will validate it and return a token.目前我有一个非常基本的表单,收集用户的卡信息,然后将其传递给 Stripe,后者将对其进行验证并返回一个令牌。 This token is placed in my form and is what I store and use in my system (again, all handled cleanly by Laravel).这个令牌放在我的表单中,是我在我的系统中存储和使用的(同样,所有这些都由 Laravel 干净地处理)。

I need to use coupons as part of this checkout process.我需要使用优惠券作为此结帐流程的一部分。 I assumed it would be a simple case of adding the coupon field and Stripe would do the rest, but unfortunately that isn't the case - there is no validation taking place of the coupon when it is entered and the form is submitted.我认为这是一个添加优惠券字段的简单案例,Stripe 会完成其余的工作,但不幸的是,事实并非如此 - 输入优惠券并提交表单时,不会对优惠券进行验证。

Processing the coupon after submit is fine, as Laravel handles that.提交后处理优惠券很好,因为 Laravel 会处理它。

My question: How can I get Stripe to validate an entered coupon using their JavaScript API?我的问题:如何让 Stripe 使用他们的 JavaScript API 验证输入的优惠券?

Below is my form and the accompanying JS:以下是我的表格和随附的 JS:

Form:形式:

<form method="POST" action="/subscribe/individual" accept-charset="UTF-8" class="form-horizontal" role="form" id="subscription-form">

<input name="_token" type="hidden" value="ep1tcaWMRGrPOLSkBCBJQo1USynWW6aTjDh9xN3W">

<div class="payment-errors"></div>
<div id="signupalert" style="display:none" class="alert alert-danger">
    <p>Error:</p>
    <span></span>
</div>

<div class="form-group">
    <label for="ccn" class="col-md-3 control-label">Credit card number</label>
    <div class="col-md-9">
        <input class="form-control" data-stripe="number" name="ccn" type="text" value="" id="ccn">
    </div>
</div>

<div class="form-group">
    <label for="expiration" class="col-md-3 control-label">Expiration date</label>
    <div class="col-md-6">
        <select class="form-control" data-stripe="exp-month" name="month"><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select>
    </div>
    <div class="col-md-3">
        <select class="form-control" data-stripe="exp-year" name="year"><option value="2014" selected="selected">2014</option><option value="2015">2015</option><option value="2016">2016</option><option value="2017">2017</option><option value="2018">2018</option><option value="2019">2019</option><option value="2020">2020</option><option value="2021">2021</option><option value="2022">2022</option><option value="2023">2023</option><option value="2024">2024</option><option value="2025">2025</option><option value="2026">2026</option><option value="2027">2027</option><option value="2028">2028</option><option value="2029">2029</option></select>
    </div>
</div>

<div class="form-group">
    <label for="cvc" class="col-md-3 control-label">CVC number</label>
    <div class="col-md-3">
        <input class="form-control" data-stripe="cvc" name="cvc" type="text" value="" id="cvc">
    </div>
</div>

<div class="form-group">
    <label for="coupon" class="col-md-3 control-label">Coupon</label>
    <div class="col-md-3">
        <input class="form-control" data-stripe="coupon" name="coupon" type="text" value="" id="coupon">
    </div>
</div>

<div class="form-group">
    <!-- Button -->                                        
    <div class="col-md-offset-3 col-md-9">
        <button type="submit" id="btn-signup" class="btn btn-info">Sign Up</button>
    </div>
</div>

JavaScript: JavaScript:

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script>
    Stripe.setPublishableKey('*** removed ***');
    jQuery(function($) {
        $('#subscription-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;
        });
    });

    var stripeResponseHandler = function(status, response) {
        var $form = $('#subscription-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 {
            // 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" />').val(token));
            // and submit
            $form.get(0).submit();
        }
    };
</script>

I'm pretty sure you can't.我很确定你不能。

I'm validating the coupon via ajax, and make a server side call to Stripe.我正在通过 ajax 验证优惠券,并向 Stripe 发出服务器端调用。 You can then apply the coupon to any purchase transactions on the server side when you accept the POST.然后,您可以在接受 POST 后将优惠券应用于服务器端的任何购买交易。

The solution I found is to我找到的解决方案是

  • create a coupon on the Stripe dashboard在 Stripe 仪表板上创建优惠券
  • use the same value for the coupon id and code对优惠券idcode使用相同的值
  • Then by calling the coupons retrieve method you get a boolean attribute " valid " in the coupon response returned.然后通过调用优惠券retrieve方法,您将在返回的优惠券响应中获得一个布尔属性“ valid ”。 Here's a response example from Stripe Docs:这是来自 Stripe Docs 的响应示例:
{
  "id": "25_5OFF",
  "object": "coupon",
  "amount_off": null,
  "created": 1617028691,
  "currency": "usd",
  "duration": "repeating",
  "duration_in_months": 3,
  "livemode": false,
  "max_redemptions": null,
  "metadata": {},
  "name": "25.5% off",
  "percent_off": 25.5,
  "redeem_by": null,
  "times_redeemed": 0,
  "valid": true
}

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

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