// Saving payment info is important for a future form entry meta update.
$this->intent = $this->retrieve_payment_intent( $this->payment_intent_id, [ 'expand' => [ 'customer' ] ] );
if ( $this->intent->status !== 'succeeded' ) {
// This error is unlikely to happen because the same check is done on a frontend.
$this->error = esc_html__( 'Stripe payment was not confirmed. Please check your Stripe dashboard.', 'wpforms-lite' );
// Saving customer and subscription info is important for a future form meta update.
$this->customer = $this->intent->customer;
* @param array $args Subscription arguments.
* @throws ApiErrorException If the request fails.
public function process_subscription( $args ) {
if ( $this->payment_method_id ) {
$this->charge_subscription( $args );
} elseif ( $this->payment_intent_id ) {
$this->finalize_subscription();
* Request a subscription charge to be made by Stripe.
* @param array $args Subscription payment arguments.
protected function charge_subscription( $args ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
if ( empty( $this->payment_method_id ) ) {
$this->error = esc_html__( 'Stripe subscription stopped, missing PaymentMethod id.', 'wpforms-lite' );
'plan' => $this->get_plan_id( $args ),
'form_name' => $args['form_title'],
'form_id' => $args['form_id'],
'cycles' => $args['cycles'] ?? null,
'expand' => [ 'latest_invoice.payment_intent' ],
if ( isset( $args['application_fee_percent'] ) ) {
$sub_args['application_fee_percent'] = $args['application_fee_percent'];
if ( isset( $args['description'] ) ) {
$sub_args['description'] = $args['description'];
$this->set_customer( $args['email'], $args['customer_name'] ?? '', $args['customer_address'] ?? [], $args['customer_phone'] ?? '', $args['customer_metadata'] ?? [] );
$sub_args['customer'] = $this->get_customer( 'id' );
$sub_args['payment_behavior'] = 'default_incomplete';
$sub_args['off_session'] = true;
$sub_args['payment_settings'] = [
'save_default_payment_method' => 'on_subscription',
if ( Helpers::is_link_supported() ) {
$sub_args['payment_settings']['payment_method_types'] = [ 'card', 'link' ];
// Create the subscription.
$this->subscription = Subscription::create( $sub_args, Helpers::get_auth_opts() );
$this->intent = $this->subscription->latest_invoice->payment_intent;
if ( ! $this->intent || ! in_array( $this->intent->status, [ 'succeeded', 'requires_action', 'requires_confirmation', 'requires_payment_method' ], true ) ) {
$this->error = esc_html__( 'Stripe subscription stopped. invalid PaymentIntent status.', 'wpforms-lite' );
if ( $this->intent->status === 'succeeded' ) {
$this->set_bypass_captcha_3dsecure_token( $args );
if ( in_array( $this->intent->status , [ 'requires_confirmation', 'requires_payment_method' ], true ) ) {
$this->request_confirm_payment_ajax( $this->intent );
$this->request_3dsecure_ajax( $this->intent );
} catch ( Exception $e ) {
$this->handle_exception( $e );
* Finalize a subscription after 3D Secure authorization is finished successfully.
* @throws ApiErrorException If the request fails.
protected function finalize_subscription() {
// Saving payment info is important for a future form entry meta update.
$this->intent = $this->retrieve_payment_intent( $this->payment_intent_id, [ 'expand' => [ 'invoice.subscription', 'customer' ] ] );
if ( $this->intent->status !== 'succeeded' ) {
// This error is unlikely to happen because the same check is done on a frontend.
$this->error = esc_html__( 'Stripe subscription was not confirmed. Please check your Stripe dashboard.', 'wpforms-lite' );
// Saving customer and subscription info is important for a future form meta update.
$this->customer = $this->intent->customer;
$this->subscription = $this->intent->invoice->subscription;
* Get saved Stripe PaymentIntent object or its key.
* @param string $key Name of the key to retrieve.
public function get_payment( $key = '' ) {
return $this->get_var( 'intent', $key );
* Get details from a saved Charge object.
* @param string|array $keys Key or an array of keys to retrieve.
public function get_charge_details( $keys ) {
$charge = isset( $this->intent->charges->data[0] ) ? $this->intent->charges->data[0] : null;
if ( empty( $charge ) || empty( $keys ) ) {
if ( is_string( $keys ) ) {
foreach ( $keys as $key ) {
if ( isset( $charge->payment_method_details->card, $charge->payment_method_details->card->{$key} ) ) {
$result[ $key ] = sanitize_text_field( $charge->payment_method_details->card->{$key} );
if ( isset( $charge->payment_method_details->{$key} ) ) {
$result[ $key ] = sanitize_text_field( $charge->payment_method_details->{$key} );
if ( isset( $charge->billing_details->{$key} ) ) {
$result[ $key ] = sanitize_text_field( $charge->billing_details->{$key} );
* Request a frontend 3D Secure authorization from a user.
* @param PaymentIntent $intent PaymentIntent to authorize.
protected function request_3dsecure_ajax( $intent ) {
if ( ! isset( $intent->status, $intent->next_action->type ) ) {
if ( $intent->status !== 'requires_action' || $intent->next_action->type !== 'use_stripe_sdk' ) {
'action_required' => true,
'payment_intent_client_secret' => $intent->client_secret,
'payment_method_id' => $this->payment_method_id,
* Request a frontend payment confirmation from a user.
* @param PaymentIntent $intent PaymentIntent to authorize.
protected function request_confirm_payment_ajax( $intent ) {
'action_required' => true,
'payment_intent_client_secret' => $intent->client_secret,
'payment_method_id' => $this->payment_method_id,
* Set an encrypted token as a PaymentIntent metadata item.
* @since 1.9.6 Added $args parameter.
* @param array $args Additional arguments.
* @throws ApiErrorException In case payment intent save wasn't successful.
private function set_bypass_captcha_3dsecure_token( array $args = [] ) {
$form_data = wpforms()->obj( 'process' )->form_data;
// Set token only if captcha is enabled for the form.
if ( empty( $form_data['settings']['recaptcha'] ) ) {
$this->intent->metadata['captcha_3dsecure_token'] = Crypto::encrypt( $this->intent->id );
$this->intent->metadata['spam_reason'] = $args['metadata']['spam_reason'] ?? null;
$this->intent->update( $this->intent->id, $this->intent->serializeParameters(), Helpers::get_auth_opts() );
* Bypass CAPTCHA check on successful 3dSecure check.
* @param bool $is_bypassed True if CAPTCHA is bypassed.
* @param array $entry Form entry data.
* @param array $form_data Form data and settings.
* @throws ApiErrorException In case payment intent save wasn't successful.
public function bypass_captcha_on_3dsecure_submit( $is_bypassed, $entry, $form_data ) {
// Firstly, run checks that may prevent bypassing:
// 1) Sanity check to prevent possible tinkering with captcha on non-payment forms.
// 2) All Captcha providers are enabled by the same setting.
! Helpers::is_payments_enabled( $form_data ) ||
empty( $form_data['settings']['recaptcha'] ) ||
empty( $entry['payment_intent_id'] )
// This is executed before payment processing kicks in and fills `$this->intent`.
// PaymentIntent intent has to be retrieved from Stripe instead of getting it from `$this->intent`.
$intent = $this->retrieve_payment_intent( $entry['payment_intent_id'] );
if ( empty( $intent->status ) || $intent->status !== 'succeeded' ) {
$token = ! empty( $intent->metadata['captcha_3dsecure_token'] ) ? $intent->metadata['captcha_3dsecure_token'] : '';
if ( Crypto::decrypt( $token ) !== $intent->id ) {
// Cleanup the token to prevent its repeated usage and declutter the metadata.
$intent->metadata['captcha_3dsecure_token'] = null;
$intent->update( $intent->id, $intent->serializeParameters(), Helpers::get_auth_opts() );
if ( isset( $intent->metadata['spam_reason'] ) ) {
* Retrieve Mandate object from Stripe.
* @param string $id Mandate id.
* @param array $args Additional arguments.
* @throws ApiErrorException If the request fails.
public function retrieve_mandate( string $id, array $args = [] ) {
$defaults = [ 'id' => $id ];
if ( isset( $args['mode'] ) ) {
$auth_opts = [ 'api_key' => Helpers::get_stripe_key( 'secret', $args['mode'] ) ];
$args = wp_parse_args( $args, $defaults );
return Mandate::retrieve( $args, $auth_opts ?? Helpers::get_auth_opts() );
} catch ( Exception $e ) {
'Stripe: Unable to get Mandate.',
'type' => [ 'payment', 'error' ],
* Create Stripe Setup Intent.
* @param array $intent_data Intent data.
* @param array $args Additional arguments.
* @throws ApiErrorException If the request fails.
* @return SetupIntent|null
public function create_setup_intent( array $intent_data, array $args ) {
if ( isset( $args['mode'] ) ) {
$auth_opts = [ 'api_key' => Helpers::get_stripe_key( 'secret', $args['mode'] ) ];
return SetupIntent::create( $intent_data, $auth_opts ?? Helpers::get_auth_opts() );
} catch ( Exception $e ) {
'Stripe: Unable to create Setup Intent.',
'type' => [ 'payment', 'error' ],
* @param string $country Country code.
* @param array $args Additional arguments.
* @throws ApiErrorException If the request fails.
* @return CountrySpec|null
public function get_country_specs( string $country, array $args = [] ) {
if ( isset( $args['mode'] ) ) {
$auth_opts = [ 'api_key' => Helpers::get_stripe_key( 'secret', $args['mode'] ) ];
return CountrySpec::retrieve( $country, $auth_opts ?? Helpers::get_auth_opts() );
} catch ( Exception $e ) {
'Stripe: Unable to get Country specs.',
'type' => [ 'payment', 'error' ],