I need to allow users to enter custom amounts using Stripe. They enter what they want to pay in a input. Then I have a script that collects the amount below the default stripe checkout button. But how am I supposed to handle the charge server-side on charge.php
?
donation.php
//user enter custom amount
<input type="number" class='form-control'
id="custom-donation-amount"
placeholder="$50.00" min="0" step="10.00 " />
//default stripe checkout button
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_XXXXXXXXXXXXXXX"
data-amount="0"
data-name="ORGANIZATION"
data-description="Saving Penguins"
data-image="logo.png"
data-locale="auto"
data-zip-code="true"
data-billing-address="true"
data-label="DONATE"
data-currency="usd">
</script>
//script to collect variable amount
<script type="text/javascript">
$(function() {
$('.donate-button').click(function(event) {
var amount = $('#custom-donation-amount').val();
$('.stripe-button').attr('data-amount', amount)
});
});
</script>
charge.php
<?php require_once('./config.php');
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$customer = \Stripe\Customer::create(array(
'source' => $token,
'email' => $email)
);
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'usd'
));
header("Location: confirmation.php");
die();
?>
Stripe makes it easy to accept one-time or recurring online donations to support nonprofit communities and causes. This post provides an overview of how to set up a payment page with Stripe Payment Links to accept donations for a cause or service.
Set adjustable_quantity on your line_items when creating a Checkout Session to enable your customers to update the quantity of an item during checkout. You can customize the default settings for the minimum and maximum quantities allowed by setting adjustable_quantity. minimum and adjustable_quantity.
“Partnering with Stripe allows us to make giving even more seamless and ensure that donations from those who are able to support quickly get into the hands of those in need.” Now, when a GoFundMe is set up in the U.S or Canada, Stripe automates the money movement between donors and the beneficiary of that fundraiser.
Our fees are the lowest in the market, and we charge no setup fee. Donorbox also gives you the option of asking donors to cover their processing fees in their donation, which many will be willing to do. Stripe charges a 2.2% – 2.9% + $0.30 processing fee per donation.
Within the <form>
where you place Checkout, you'd also want to add an input to collect the Amount from the user. Note the name I've given my input a name of amount
<form method="POST" action="/charge.php">
<input type="number" class="form-control" name="amount" id="custom-donation-amount" placeholder="50.00" min="0" step="10.00" />
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_XXXXXXXXXXXXXXX"
data-name="ORGANIZATION"
data-description="Saving Penguins"
data-image="logo.png"
data-locale="auto"
data-billing-address="true"
data-label="DONATE"
data-currency="usd">
</script>
</form>
In your backend you could grab this amount
field when Checkout submits, much like you would the token created by Stripe
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$amount = $_POST['amount'];
// your code to create a charge
// ...
If you're looking to dynamically change the amount displayed within the Checkout window, you should use the Custom Checkout, rather than the basic Checkout block, or it may not update correctly.
See https://stripe.com/docs/checkout#integration-custom
Or, for a Fiddle: https://jsfiddle.net/ywain/g2ufa8xr/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With