I have a code that supposed to retrieve client secret and pass it to js but it doens’t work as I get error 500. I have created a function called retrieve_client_secret() that’s supposed to retrieve it but seems it’s not the right way.
Maybe I have to use AJAX to fetch the client secret before processing to payment procedure?
<?php
/**
* Plugin Name: Stripe-PHP-integration
* Description: PHP API Integration For WordPress
* Author: Null
* Version: 1.0
**/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
require_once 'stripe/init.php';
use StripeStripe;
add_action('init', function() {
Stripe::setApiKey('sk_test_');
Stripe::setClientId('pk_test_');
});
header('Content-Type: application/json');
add_action('plugins_loaded', 'init_custom_gateway_class');
function init_custom_gateway_class(){
class WC_Gateway_Custom extends WC_Payment_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
$this->id = 'Stripe-Payment';
$this->icon = apply_filters('woocommerce_custom_gateway_icon', '');
$this->has_fields = true;
$this->method_title = __( 'Stripe PHP API Integration For WooCommerce', $this->domain );
$this->method_description = __( 'Allows payments using Stripe PHP API', $this->domain );
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions', $this->description );
$this->order_status = $this->get_option( 'order_status', 'pending' );
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
// Customer Emails
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
add_action('woocommerce_payment_fields', array($this, 'payment_fields'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
add_action('wp_footer', array($this, 'include_stripe_js'));
add_action('init', array($this, 'retrieve_client_secret'));
}
public function enqueue_styles() {
wp_enqueue_style('stripe-custom-styles', 'https://getvagu.com/wp-content/plugins/woocommerce-gateway-stripe/assets/css/stripe-styles.css?ver=7.7.0');
}
public function include_stripe_js() {
?>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test_');
let elements;
const options = {
mode: 'payment',
amount: 1099,
currency: 'usd',
appearance: {/*...*/},
clientSecret: '<?php echo retrieve_client_secret() ?>'
};
initialize();
document.querySelector("#stripe_form_checkout").addEventListener("submit", handleSubmit);
function initialize() {
elements = stripe.elements(options);
const paymentElement = elements.create('payment', {
fields: {
billingDetails: {
address: {
country: 'never'
}
}
}
});
paymentElement.mount('#stripe_payment');
}
async function handleSubmit(e) {
e.preventDefault();
const { setupIntent, error } = await stripe.confirmSetup({
elements,
confirmParams: {
return_url: "https://getvagu.com/checkout",
},
redirect: 'if_required'
});
if(error) {
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
} else {
showMessage("An unexpected error occurred.");
}
} else {
console.log(setupIntent)
}
}
</script>
<?php
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', $this->domain ),
'type' => 'checkbox',
'label' => __( 'Enable Custom Payment', $this->domain ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', $this->domain ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', $this->domain ),
'default' => __( 'Stripe PHP API', $this->domain ),
'desc_tip' => true,
),
'order_status' => array(
'title' => __( 'Order Status', $this->domain ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'description' => __( 'Choose whether status you wish after checkout.', $this->domain ),
'default' => 'wc-completed',
'desc_tip' => true,
'options' => wc_get_order_statuses()
),
'description' => array(
'title' => __( 'Description', $this->domain ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', $this->domain ),
'default' => __('Payment Information', $this->domain),
'desc_tip' => true,
),
'instructions' => array(
'title' => __( 'Instructions', $this->domain ),
'type' => 'textarea',
'description' => __( 'Instructions that will be added to the thank you page and emails.', $this->domain ),
'default' => '',
'desc_tip' => true,
),
);
}
/**
* Output for the order received page.
*/
public function thankyou_page() {
if ( $this->instructions )
echo wpautop( wptexturize( $this->instructions ) );
}
/**
* Add content to the WC emails.
*
* @access public
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $this->instructions && ! $sent_to_admin && $order->payment_method && $order->has_status( 'on-hold' ) ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
}
/**
* Renders the Stripe elements form.
*
*/
public function payment_fields(){
?>
<div id="stripe_payment_form"></div>
<?php
}
public function retrieve_client_secret(){
$payment_intent = StripePaymentIntent::create([
'amount' => $order->get_total() * 100,
'currency' => strtolower(get_woocommerce_currency()),
]);
$clientSecret = $payment_intent->clientSecret;
return $clientSecret;
}
/**
* Process the payment and return the result.
*
* @param int $order_id
* @return array
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
// Set order status
$order->update_status('pending', __('Waiting for payment confirmation', $this->domain));
// or call the Payment complete
// $order->payment_complete();
// Remove cart
WC()->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order),
);
}
}
}
add_filter( 'woocommerce_payment_gateways', 'Stripe_PHP_API_Woocommerce_Integration' );
function Stripe_PHP_API_Woocommerce_Integration( $methods ) {
$methods[] = 'WC_Gateway_Custom';
return $methods;
}
?>