How to accept donations through Stripe using PHP
Here is a basic example of how to accept donations through Stripe using PHP:
Create a Stripe account and get your API keys:
- Go to the Stripe website and sign up for an account.
- Navigate to the Developers section and get your API keys.
Create a form to collect payment information:
- Create an HTML form with input fields for the donor's name, email, and credit card information.
Validate the payment information on the server:
- Use PHP to validate the payment information sent from the form.
Create a Stripe charge:
- Use the Stripe PHP library to create a charge using the payment information and the Stripe API keys.
Handle the response from Stripe:
- Check the response from Stripe to determine if the charge was successful.
- If the charge was successful, you can send a thank-you email to the donor or store the transaction information in a database.
Here is an example of the code to create a Stripe charge in PHP:
phprequire_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_test_your_api_key_here");
try {
$charge = \Stripe\Charge::create([
"amount" => 1000,
"currency" => "usd",
"source" => $_POST['stripeToken'],
"description" => "Example charge"
]);
echo 'Charge successful!';
} catch (\Stripe\Error\Card $e) {
echo 'Charge failed.';
}
This is just a basic example to get you started. You will likely need to modify this code to fit your specific needs.
Comments
Post a Comment