i’m trying to charge customer when they has order status upaid.
and i trigger it by one admin action button from wp-admin side.
problem is when i try to charge i’m getting error StripeStripe’ not found
i check woocommerce stripe getway folder there is no library to call autoload in my code.
need help to fix my code. my code is given below.
function handle_charge_unpaid_order() {
if (
!isset($_GET['order_id']) ||
!wp_verify_nonce($_GET['_wpnonce'], 'charge_unpaid_order_' . $_GET['order_id'])
) {
wp_die('Invalid or missing nonce');
}
$order_id = intval($_GET['order_id']);
$order = wc_get_order($order_id);
if (!$order || $order->get_status() !== 'unpaid') {
wp_die('Invalid order or not unpaid');
}
if ($order->get_payment_method() !== 'stripe') {
wp_die('Order does not use Stripe');
}
$customer_id = $order->get_meta('_stripe_customer_id', true);
$card_sourceid = $order->get_meta('_stripe_source_id', true);
if (!$customer_id || !$card_sourceid) {
wp_die('No Stripe customer or saved payment method found.');
}
// Load Stripe keys
$options = get_option('woocommerce_stripe_settings');
$test_mode = isset($options['testmode']) && $options['testmode'] === 'yes';
$secret_key = $test_mode ? $options['test_secret_key'] : $options['secret_key'];
if (!$secret_key) {
wp_die('Stripe API key not configured.');
}
// ✅ Ensure Stripe SDK is loaded
if ( ! class_exists( 'StripeStripe' ) ) {
if ( defined( 'WC_STRIPE_PLUGIN_PATH' ) ) {
require_once WC_STRIPE_PLUGIN_PATH . '/includes/libraries/stripe-client/init.php';
} else {
wp_die('Stripe SDK not found. Please make sure the WooCommerce Stripe plugin is active.');
}
}
StripeStripe::setApiKey($secret_key);
try {
$payment_intent = StripePaymentIntent::create([
'amount' => intval(round($order->get_total() * 100)), // Convert to cents
'currency' => strtolower(get_woocommerce_currency()),
'customer' => $customer_id,
'payment_method' => $card_sourceid,
'off_session' => true,
'confirm' => true,
'metadata' => [
'order_id' => $order->get_id(),
'site' => get_bloginfo('name'),
],
]);
$order->payment_complete($payment_intent->id);
$order->add_order_note('Stripe payment successful. PaymentIntent ID: ' . $payment_intent->id);
wp_redirect(admin_url('admin.php?page=wc-orders&action=edit&id=' . $order_id));
exit;
} catch (StripeExceptionCardException $e) {
$order->update_status('failed');
$order->add_order_note('Stripe card error: ' . $e->getMessage());
wp_die('Stripe Card Error: ' . esc_html($e->getMessage()));
} catch (Exception $e) {
$order->update_status('failed');
$order->add_order_note('Stripe charge failed: ' . $e->getMessage());
wp_die('Stripe Error: ' . esc_html($e->getMessage()));
}
} ```