Nuevo shortcode Stripe

This PHP code defines a WordPress shortcode to display payment information after a Stripe Checkout session, including error handling and session retrieval.
 avatar
unknown
php
10 months ago
1.5 kB
20
Indexable
<?php

// [stripe_thankyou] – muestra info de la sesión de Checkout
add_shortcode( 'stripe_thankyou', function () {
    $config = [
        'live' => [
            'secret_key'     => '',
            'webhook_secret' => '',
        ],
        'test' => [
            'secret_key'     => '',
            'webhook_secret' => '',
        ],
    ];

    if ( empty( $_GET['session_id'] ) ) {
        return '<p>No se recibió la sesión de pago.</p>';
    }

    $mode = isset( $_GET['testmode'] ) && $_GET['testmode'] == 'true' ? 'test' : 'live';

    try {
        \Stripe\Stripe::setApiKey( $config[$mode]['secret_key'] );

        $session = \Stripe\Checkout\Session::retrieve(
            sanitize_text_field( $_GET['session_id'] )
        );
    } catch ( Exception $e ) {
        return '<p>Error al recuperar la sesión: ' .
               esc_html( $e->getMessage() ) . '</p>';
    }

    ob_start(); ?>
        <h2>¡Gracias, <?php echo esc_html( $session->customer_details->name ?: 'cliente' ); ?>!</h2>
        <p>Tu pago se ha completado correctamente.</p>
        <ul>
            <li><strong>Importe total:</strong>
                <?php echo number_format( $session->amount_total / 100, 2 ); ?>
                <?php echo strtoupper( $session->currency ); ?>
            </li>
            <li><strong>Nº de pago:</strong> <?php echo esc_html( $session->payment_intent ); ?></li>
        </ul>
    <?php
    return ob_get_clean();
} );

?>
Editor is loading...
Leave a Comment