Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andy Harkness
2,192 PointsVariable Amounts with Stripe and PHP
I am am developing a website for a charity. I have Stripe checkout working at the moment, but I would like to allow people to enter the amount they would like to donate rather than a fixed amount. Stripe mention variable amounts in their documentation on https://support.stripe.com/questions/how-can-i-use-a-variable-amount-with-stripe-checkout but once I start digging around their documentation I get totally lost!
Andy Harkness
2,192 PointsHi Andrew,
I've been using Hampton's Tutorial which uses a mixture of PHP and JS.
3 Answers
Andrew Shook
31,709 PointsIf you've been following along with Hampton Paulk, then what you will need to do is adds an input field to the form and then use JS/jQ to read the value of the input field and add it StripesCheckout object. Try this. Add this input field to the page with the button on it.
<input type="number" id="custom-donation" placeholder="10.00" min="1.00" step="10.00 " />
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="XX_XX_XXXXXXX_XXX" data-name="Fund" data-label="DONATE" data-amount="100"></script>
then add this to your site JS file ( assuming you are using jQuery).
$('#custom-donation').onchange(function(){
// Stripe accepts payment amounts in cents so we have to convert dollars to cents by multiplying by 100
var amount = parseInt( $(this).val()*100);
$(".stripe-button").attr( "data-amount", amount );
}
This should give your users the ability to donate in $1 increments and prevents you from having to validate a text field.
Andy Harkness
2,192 PointsThanks Andrew,
That's definitely helpful! When it comes to the charge.php file I presume I'll need to reflect the amount the user inputted, any idea how I would go about this?
Andrew Shook
31,709 PointsAt the top of your charge.php file put this code:
echo var_dump($_POST);
Then look through the post variable for something called amount (that's a guess I don't know what it will be called). Once you find the amount in the post variable add it to the amount prop. in the Stripe_Charge::create() method like so:
$charge = Stripe_Charge::create(
array(
'amount' => $_POST['whatever_the_amount_index_is_named'],
'currency' => 'usd',
'customer' => $customer->id
)
);
Andrew Shook
31,709 PointsAndy Harkness, how is your stripe integration coming along?
Andy Harkness
2,192 PointsHi Andrew,
Not ideally unfortunately. I am using Wordpress and another plugin seems to be preventing Stripe from working properly (either the above code or any other Stripe plugins). At the minute they cannot live without this other plugin so Stripe is not a runner. On the up side I'm working on another project where all of the above should work a treat. I'm planning to use Stripe for a number of projects, so getting to grips with the code will be invaluable.
Thanks for all your help!!
Andrew Shook
31,709 PointsWhat plugin is causing the problem, and what problem is it causing?
Andy Harkness
2,192 PointsIt's called CiviCRM, which has caused me major headaches in the past. I think they are aware of the issue, so I'll just wait for them to come up with a solution and use Paypal for now!
Andrew Shook
31,709 PointsGood luck with that wait.
Andrew Shook
31,709 PointsAndrew Shook
31,709 Pointsare you using the php implementation or the JS?