2015-01-18 20 views
6

Sto implementando Stripe in un sito Web django e tutto funziona tranne una parte. Nel mio carrello, gli utenti possono aggiornare gli articoli che modificano il totale. Tutto funziona correttamente, tranne che per impostare l'importo dei dati nello script js Stripe Checkout.Aggiornamento dati stripe-importo

Quando la pagina viene caricata, tutto funziona perfettamente, tuttavia se il cliente cambia il carrello, l'importo dei dati non viene aggiornato. Ho un'altra scatola che mostra il totale, e quella quantità si aggiorna bene.

<!-- 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> 

E poi il mio javascript che tenta di aggiornare è questo:

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. */ 
    } 

risposta

19

scopre che per avere un data-valore dinamico per il pagamento striscia, è necessario utilizzare Custom Checkout invece di semplice Checkout. Questo codice ha fatto il trucco.

 <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> 
+0

Grazie a Dio per voi! – sgrutman978

Problemi correlati