12

I have a problem with setting custom amount, I would like to set data-amount to whatever user will choose in input id="custom-donation-amount", how should I do that. my attempt doesn't work.

<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-image="images/button.png"
  data-key="pk_test_FTZOOu9FL0iYJXXXXXX"
  data-name="Fund"
  data-label= "DONATE"
  data-description="ysysys"
  data-amount = 
  >

    $('.donate-button').click(function(event){
    var self = $(this);
    var amount = 0;
     amount = $('#custom-donation-amount').val();
      if (amount === "") {
        amount = 50;
      }

    amount = self.attr('data-amount');
    amount = Math.abs(amount * 100);
  });

</script>
 <input type="number" id="custom-donation-amount" placeholder="50.00" min="0" step="10.00"/>
lipenco
  • 1,358
  • 5
  • 16
  • 30

4 Answers4

14

The particular method (simple, embedded form) being used won't work towards the sought end. You must instead use the more flexible custom integration, as seen in the docs. The only thing not included in the provided example is how to hook up the amount input, which is not at all difficult.

<input class="form-control"
       type="number"
       id="custom-donation-amount"
       placeholder="50.00"
       min="0"
       step="10.00"/>
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton ">Purchase</button>
<script>
  var handler = StripeCheckout.configure({
    key: 'pk_test_ppailxxxxxxxxxxxxxxxxxxx',
    image: '/square-image.png',
    token: function(token) {
      // Use the token to create the charge with a server-side script.
      // You can access the token ID with `token.id`
    }
  });

  document.getElementById('customButton').addEventListener('click', function(e) {
    // This line is the only real modification...
    var amount = $("#custom-donation-amount").val() * 100;
    handler.open({
      name: 'Demo Site',
      description: 'Some donation',
      // ... aside from this line where we use the value.
      amount: amount
    });
    e.preventDefault();
  });
</script>

Note that you'll actually have to fill out the token: function passed to the StripeCheckout.configure call. In particular, you'll need to either submit a form or an ajax request and handle that accordingly on your server. You will then use this information on the server, together with the secrete key, to send a payment creation request to Stripe. The Stripe documentation has details on what information will need to be sent to their API call, and thus, what information you'll need to pass along to the API call on your server. In particular, note that you'll probably have to do some additional grabbing of prices etc in your token function.

metasoarous
  • 2,854
  • 1
  • 23
  • 24
4

You'll need another script tag for that, you can't use javascript in a script tag that has a source.

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-image="images/button.png" data-key="pk_test_FTZOOu9FL0iYJXXXXXX" data-name="Fund" data-label="DONATE" data-description="ysysys" data-amount="0"></script>
<script type="text/javascript">
    $(function() {
        $('.donate-button').click(function(event) {
            var amount = $('#custom-donation-amount').val();        
            $('.stripe-button').attr('data-amount', amount)
        });
    });
</script>


<input type="number" id="custom-donation-amount" placeholder="50.00" min="0" step="10.00 " />
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • 1
    This does solve the problem of updating the data-amount attribute, but it doesn't actually _solve the problem_. The value ending up in the form popup after submitting doesn't update. Not sure why, but it doesn't. – metasoarous Sep 15 '14 at 07:27
  • Hi @metasoarous, did you find a solution to this in the end? I am in the same boat now. I can update the data-amount correctly but the Stripe button still shows the original value. – Gortron Oct 04 '14 at 22:37
  • See my answer below; it worked for me. Ask a question there if you have any issues. – metasoarous Oct 05 '14 at 17:24
0

This is possible to get a variable amount in the simple checkout form. I'm not sure what framework you are using, but I was able to achieve this in my Ruby (Padrino - not Rails) app using gon as follows. 'charge_amount_pence' is a helper method returning a string representing the charge to be made. Re your particular need, this could be set to a variable picked by a user via an Ajax request, for example.

<script type="text/javascript">
    window.gon = {};
    gon.charge = charge_amount_pence;
</script>

And in the checkout script:

"data-amount" = gon.charge

I actually use the same helper method in my controller to ensure the correct amount is actually charged.

MatzFan
  • 877
  • 8
  • 17
0

How about creating new stripe element? This worked for me,

<script type="text/javascript">
    $(function() {
        $('.donate-button').click(function(event) {
            var holder = document.getElementById('aNewDiv');
            var script = document.createElement('script');
            script.src = 'https://checkout.stripe.com/checkout.js';
            script.setAttribute('class', 'stripe-button');
            script.setAttribute('data-amount', $('#Amount').val()*100);
            script.setAttribute('data-key',"pk_test_W0ML1BmfFMa6z3MgD90s7WeN");
            script.setAttribute('data-name', "Fund");
            script.setAttribute('data-description', "ysysys");
            script.setAttribute('data-locale', "auto");
        }
    }
</script>
Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59