Files
Wordpress-Snippets/Stripe/payments/index.php
2024-11-23 09:16:20 +00:00

44 lines
1.3 KiB
PHP

<?php
/* enqueue stripe js */
function lcp_enqueue_stripe_scripts() {
wp_enqueue_script('stripe-js', 'https://js.stripe.com/v3/', null, null, true);
}
add_action('wp_enqueue_scripts', 'lcp_enqueue_stripe_scripts');
// Include the Stripe PHP SDK
require_once plugin_dir_path( __FILE__ ) . 'stripe-php/init.php';
// Handle the payments
if (isset($_POST['stripeToken'])) {
$token = $_POST['stripeToken']; // The token received from Stripe.js
try {
// Create a charge with the provided token
$charge = \Stripe\Charge::create([
'amount' => 5000, // Amount in cents, e.g., $50.00
'currency' => 'usd',
'description' => 'Example charge',
'source' => $token, // The token generated on the client side
]);
// Check if the payment was successful
if ($charge->status == 'succeeded') {
echo 'Payment was successful!';
}
} catch (\Stripe\Exception\CardException $e) {
// Handle error (card declined, etc.)
echo 'Error: ' . $e->getMessage();
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
/* In the backend code of your plugin, initialize Stripe with your secret API key: */
/* Needs to be stored in DB securely */
\Stripe\Stripe::setApiKey('your_secret_key'); // Replace with your Stripe Secret Key