Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm implementing Stripe into a django website and everything is working except for one part. In my cart, users can update the items which changes the total. Everything is working correctly except for setting the data-amount on the Stripe Checkout js script.

When the page loads, everything works great, however if the customer changes their cart, the data-amount does not update. I have another box which shows the total, and that amount updates fine.

<!-- here is the script tag in HTML-->
<script
id="stripe-script"
src="https://checkout.stripe.com/checkout.js" 
class="stripe-button"
data-image="{% static 'img/marketplace.png' %}"
data-key="{{ STRIPE_PUBLIC_KEY }}"
data-name="Serendipity Artisan Blends"
data-description="Purchase Items"
data-amount="{{ cart_stripe_total }}">
</script>

And then my javascript that attempts to update is this:

function updateTotal(amount) {
    /* update the total in the cart in both the table cell and
        in the stripe button data-amount */
    var totalStr = shoppingTotalCell.text().replace('$', ''),
        originalTotal = parseFloat(totalStr),
        newTotal = originalTotal + amount,
        newTotalStripe = newTotal * 100,
        newTotalStr = newTotal.toFixed(2),
        script = $('#stripe-script');

    shoppingTotalCell.text('$' + newTotalStr);

    console.log(script.data("amount"));
    // this returns the correct original amount

    script.data("amount", newTotalStripe);

    console.log(script.data("amount"));
    /* this returns the updated amount, however the HTML data-amount 
        attribute does not update. */
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

Turns out that to have a dynamic data-amount for the stripe payment, you have to use Custom Checkout instead of Simple Checkout. This code did the trick.

      <button class="btn btn-primary btn-lg" id="stripe-button">
        Checkout <span class="glyphicon glyphicon-shopping-cart"></span>
      </button>

      <script>
        $('#stripe-button').click(function(){
          var token = function(res){
            var $id = $('<input type=hidden name=stripeToken />').val(res.id);
            var $email = $('<input type=hidden name=stripeEmail />').val(res.email);
            $('form').append($id).append($email).submit();
          };

          var amount = $("#stripeAmount").val();
          StripeCheckout.open({
            key:         '{{ STRIPE_PUBLIC_KEY }}',
            amount:      amount,
            name:        'Serendipity Artisan Blends',
            image:       '{% static "img/marketplace.png" %}',
            description: 'Purchase Products',
            panelLabel:  'Checkout',
            token:       token
          });

          return false;
        });
      </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...