Home / Admin / Retrieve Card Details from Stripe Checkout One-Time Payments
Duplicate Snippet

Embed Snippet on Your Site

Retrieve Card Details from Stripe Checkout One-Time Payments

This snippet ensures that the last four digits and card brand are available in payment confirmations for one-time Stripe Checkout payments. Since Stripe doesn't save the payment method to the customer in these cases (unlike subscriptions), the code retrieves the PaymentIntent directly via the Stripe API to access the card details.

Code Preview
php
<?php
add_filter(
    'simpay_payment_confirmation_template_tag_card-last4',
    function( $value, $payment_confirmation_data ) {
        if (
            isset( $payment_confirmation_data['checkout_session'] )
            && ! isset( $payment_confirmation_data['subscription'] )
        ) {
            $paymentintent = \SimplePay\Core\API\PaymentIntents\retrieve(
                array(
                    'id' => $payment_confirmation_data['checkout_session']->payment_intent,
                    'expand' => array(
                        'latest_charge',
                    ),
                ),
                $payment_confirmation_data['form']->get_api_request_args()
            );
            $card = $paymentintent->latest_charge->payment_method_details->card;
            return $card->last4;
        }
        return $value;
    },
    99,
    2
);
add_filter(
    'simpay_payment_confirmation_template_tag_card-brand',
    function( $value, $payment_confirmation_data ) {
        if (
            isset( $payment_confirmation_data['checkout_session'] )
            && ! isset( $payment_confirmation_data['subscription'] )
        ) {
            $paymentintent = \SimplePay\Core\API\PaymentIntents\retrieve(
                array(
                    'id' => $payment_confirmation_data['checkout_session']->payment_intent,
                    'expand' => array(
                        'latest_charge',
                    ),
                ),
                $payment_confirmation_data['form']->get_api_request_args()
            );
            $card = $paymentintent->latest_charge->payment_method_details->card;
            return ucfirst( $card->brand );
        }
        return $value;
    },
    99,
    2
);

Comments

Add a Comment