• Namespace
  • Class
  • Tree
  • Deprecated
  • Hook Reference

Namespaces

  • None
  • PaymentPlugins

Classes

  • PaymentPlugins\WC_Stripe_Admin_Meta_Box_Product_Data
  • PaymentPlugins\WC_Stripe_Admin_Notices
  • WC_Payment_Gateway_Stripe
  • WC_Payment_Gateway_Stripe_ACH
  • WC_Payment_Gateway_Stripe_Afterpay
  • WC_Payment_Gateway_Stripe_Alipay
  • WC_Payment_Gateway_Stripe_ApplePay
  • WC_Payment_Gateway_Stripe_Bancontact
  • WC_Payment_Gateway_Stripe_BECS
  • WC_Payment_Gateway_Stripe_Boleto
  • WC_Payment_Gateway_Stripe_CC
  • WC_Payment_Gateway_Stripe_EPS
  • WC_Payment_Gateway_Stripe_FPX
  • WC_Payment_Gateway_Stripe_Giropay
  • WC_Payment_Gateway_Stripe_GooglePay
  • WC_Payment_Gateway_Stripe_GrabPay
  • WC_Payment_Gateway_Stripe_Ideal
  • WC_Payment_Gateway_Stripe_Klarna
  • WC_Payment_Gateway_Stripe_Local_Payment
  • WC_Payment_Gateway_Stripe_Multibanco
  • WC_Payment_Gateway_Stripe_OXXO
  • WC_Payment_Gateway_Stripe_P24
  • WC_Payment_Gateway_Stripe_Payment_Request
  • WC_Payment_Gateway_Stripe_Sepa
  • WC_Payment_Gateway_Stripe_Sofort
  • WC_Payment_Gateway_Stripe_WeChat
  • WC_Payment_Token_Stripe
  • WC_Payment_Token_Stripe_ACH
  • WC_Payment_Token_Stripe_ApplePay
  • WC_Payment_Token_Stripe_CC
  • WC_Payment_Token_Stripe_GooglePay
  • WC_Payment_Token_Stripe_Local
  • WC_Payment_Token_Stripe_Sepa
  • WC_Stripe_Account_Settings
  • WC_Stripe_Admin_Assets
  • WC_Stripe_Admin_Menus
  • WC_Stripe_Admin_Order_Metaboxes
  • WC_Stripe_Admin_Settings
  • WC_Stripe_Admin_User_Edit
  • WC_Stripe_Advanced_Settings
  • WC_Stripe_API_Operation
  • WC_Stripe_API_Request_Filter
  • WC_Stripe_API_Settings
  • WC_Stripe_Constants
  • WC_Stripe_Controller_Cart
  • WC_Stripe_Controller_Checkout
  • WC_Stripe_Controller_Gateway_Settings
  • WC_Stripe_Controller_GooglePay
  • WC_Stripe_Controller_Order_Actions
  • WC_Stripe_Controller_Payment_Intent
  • WC_Stripe_Controller_Payment_Method
  • WC_Stripe_Controller_Plaid
  • WC_Stripe_Controller_Product_Data
  • WC_Stripe_Controller_Source
  • WC_Stripe_Controller_Webhook
  • WC_Stripe_Customer_Manager
  • WC_Stripe_Field_Manager
  • WC_Stripe_Frontend_Scripts
  • WC_Stripe_Gateway
  • WC_Stripe_Gateway_Conversion
  • WC_Stripe_Install
  • WC_Stripe_Manager
  • WC_Stripe_Payment
  • WC_Stripe_Payment_Balance
  • WC_Stripe_Payment_Charge
  • WC_Stripe_Payment_Charge_Local
  • WC_Stripe_Payment_Factory
  • WC_Stripe_Payment_Intent
  • WC_Stripe_Product_Gateway_Option
  • WC_Stripe_Product_Option
  • WC_Stripe_Redirect_Handler
  • WC_Stripe_Rest_API
  • WC_Stripe_Rest_Controller
  • WC_Stripe_Settings_API
  • WC_Stripe_Shortcode_Payment_Buttons
  • WC_Stripe_Shortcodes
  • WC_Stripe_Update
  • WC_Stripe_Utils

Traits

  • WC_Payment_Token_Payment_Method_Trait
  • WC_Payment_Token_Source_Trait
  • WC_Stripe_Controller_Cart_Trait
  • WC_Stripe_Controller_Frontend_Trait
  • WC_Stripe_Local_Payment_Charge_Trait
  • WC_Stripe_Local_Payment_Intent_Trait
  • WC_Stripe_Payment_Charge_Trait
  • WC_Stripe_Payment_Intent_Trait
  • WC_Stripe_Settings_Trait

Functions

  • stripe_wc
  • wc_stripe_add_allowed_html
  • wc_stripe_add_number_precision
  • wc_stripe_api_options
  • wc_stripe_available_payment_gateways
  • wc_stripe_charge_dispute_closed
  • wc_stripe_charge_dispute_created
  • wc_stripe_credit_card_labels
  • wc_stripe_delete_customer
  • wc_stripe_display_prices_including_tax
  • wc_stripe_filter_address_state
  • wc_stripe_filter_order_id
  • wc_stripe_get_account_id
  • wc_stripe_get_available_local_gateways
  • wc_stripe_get_checkout_fields
  • wc_stripe_get_currencies
  • wc_stripe_get_current_page
  • wc_stripe_get_custom_forms
  • wc_stripe_get_customer_id
  • wc_stripe_get_customer_payment_tokens
  • wc_stripe_get_display_items
  • wc_stripe_get_error_messages
  • wc_stripe_get_idempotency_key
  • wc_stripe_get_local_payment_params
  • wc_stripe_get_order_from_source_id
  • wc_stripe_get_order_from_transaction
  • wc_stripe_get_publishable_key
  • wc_stripe_get_secret_key
  • wc_stripe_get_shipping_options
  • wc_stripe_get_site_locale
  • wc_stripe_get_template
  • wc_stripe_get_template_html
  • wc_stripe_hidden_field
  • wc_stripe_log
  • wc_stripe_log_error
  • wc_stripe_log_info
  • wc_stripe_mode
  • wc_stripe_order_cancelled
  • wc_stripe_order_has_shipping_address
  • wc_stripe_order_mode
  • wc_stripe_order_status_completed
  • wc_stripe_output_checkout_error
  • wc_stripe_payment_complete_order_status
  • wc_stripe_payment_gateways
  • wc_stripe_payment_intent_field
  • wc_stripe_pre_orders_active
  • wc_stripe_process_charge_failed
  • wc_stripe_process_charge_succeeded
  • wc_stripe_process_create_refund
  • wc_stripe_process_payment_intent_succeeded
  • wc_stripe_process_shop_subscription_meta
  • wc_stripe_process_source_chargeable
  • wc_stripe_remove_number_precision
  • wc_stripe_remove_order_locks
  • wc_stripe_restore_cart
  • wc_stripe_restore_cart_after_product_checkout
  • wc_stripe_retry_source_chargeable
  • wc_stripe_review_closed
  • wc_stripe_review_opened
  • wc_stripe_save_customer
  • wc_stripe_set_checkout_error
  • wc_stripe_set_idempotency_key
  • wc_stripe_shipping_address_serviceable
  • wc_stripe_stash_cart
  • wc_stripe_stash_product_cart
  • wc_stripe_token_field
  • wc_stripe_update_customer_location
  • wc_stripe_update_option_3_3_13
  • wc_stripe_update_shipping_methods
  • wc_stripe_woocommerce_payment_token_deleted
  • wcs_stripe_active
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 
<?php

defined( 'ABSPATH' ) || exit();

/**
 * Processes the charge via webhooks for local payment methods like P24, EPS, etc.
 *
 * @param \Stripe\Source  $source
 * @param WP_REST_Request $request
 *
 * @since 3.0.0
 * @package Stripe/Functions
 */
function wc_stripe_process_source_chargeable( $source, $request ) {
    if ( isset( $source->metadata['order_id'] ) ) {
        $order = wc_get_order( wc_stripe_filter_order_id( $source->metadata['order_id'], $source ) );
    } else {
        // try finding order using source.
        $order = wc_stripe_get_order_from_source_id( $source->id );
    }
    if ( ! $order ) {
        /**
         * If the order ID metadata is empty, it's possible the source became chargeable before
         * the plugin had a chance to update the order ID. Schedule a cron job to execute in 60 seconds
         * so the plugin can update the order ID and the charge can be processed.
         */
        if ( empty( $source->metadata['order_id'] ) ) {
            if ( method_exists( WC(), 'queue' ) && ! doing_action( 'wc_stripe_retry_source_chargeable' ) ) {
                WC()->queue()->schedule_single( time() + MINUTE_IN_SECONDS, 'wc_stripe_retry_source_chargeable', array( $source->id ) );
            }
        } else {
            wc_stripe_log_error( sprintf( 'Could not create a charge for source %s. No order ID was found in your WordPress database.', $source->id ) );
        }

        return;
    }

    /**
     *
     * @var WC_Payment_Gateway_Stripe $payment_method
     */
    $payment_method = WC()->payment_gateways()->payment_gateways()[ $order->get_payment_method() ];

    // if the order has a transaction ID, then a charge has already been created.
    if ( $payment_method->has_order_lock( $order ) || ( $transaction_id = $order->get_transaction_id() ) ) {
        wc_stripe_log_info( sprintf( 'source.chargeable event received. Charge has already been created for order %s. Event exited.', $order->get_id() ) );

        return;
    }
    $payment_method->set_order_lock( $order );
    $payment_method->set_new_source_token( $source->id );
    $result = $payment_method->payment_object->process_payment( $order );

    if ( ! is_wp_error( $result ) && $result->complete_payment ) {
        $payment_method->payment_object->payment_complete( $order, $result->charge );
    }
}

/**
 * When the charge has succeeded, the order should be completed.
 *
 * @param \Stripe\Charge  $charge
 * @param WP_REST_Request $request
 *
 * @since 3.0.5
 * @package Stripe/Functions
 */
function wc_stripe_process_charge_succeeded( $charge, $request ) {
    // charges that belong to a payment intent can be  skipped
    // because the payment_intent.succeeded event will be called.
    if ( $charge->payment_intent ) {
        return;
    }
    $order = wc_get_order( wc_stripe_filter_order_id( $charge->metadata['order_id'], $charge ) );
    if ( ! $order ) {
        wc_stripe_log_error( sprintf( 'Could not complete payment for charge %s. No order ID %s was found in your WordPress database.', $charge->id, $charge->metadata['order_id'] ) );

        return;
    }

    /**
     *
     * @var WC_Payment_Gateway_Stripe $payment_method
     */
    $payment_method = WC()->payment_gateways()->payment_gateways()[ $order->get_payment_method() ];
    /**
     * Make sure the payment method is asynchronous because synchronous payments are handled via the source.chargeable event which processes the payment.
     * This event is relevant for payment methods that receive a charge.succeeded event at some arbitrary amount of time
     * after the source is chargeable.
     */
    if ( $payment_method instanceof WC_Payment_Gateway_Stripe && ! $payment_method->synchronous ) {
        // If the order's charge status is not equal to charge status from Stripe, then complete_payment.
        if ( $order->get_meta( WC_Stripe_Constants::CHARGE_STATUS ) != $charge->status ) {
            // want to prevent plugin from processing capture_charge since charge has already been captured.
            remove_action( 'woocommerce_order_status_completed', 'wc_stripe_order_status_completed' );

            if ( stripe_wc()->advanced_settings->is_fee_enabled() ) {
                // retrieve the balance transaction
                $balance_transaction = WC_Stripe_Gateway::load()->mode( wc_stripe_order_mode( $order ) )->balanceTransactions->retrieve( $charge->balance_transaction );
                if ( ! is_wp_error( $balance_transaction ) ) {
                    $charge->balance_transaction = $balance_transaction;
                }
            }
            // call payment complete so shipping, emails, etc are triggered.
            $payment_method->payment_object->payment_complete( $order, $charge );
            $order->add_order_note( __( 'Charge.succeeded webhook received. Payment has been completed.', 'woo-stripe-payment' ) );
        }
    }
}

/**
 *
 * @param \Stripe\PaymentIntent $intent
 * @param WP_REST_Request       $request
 *
 * @since 3.1.0
 * @package Stripe/Functions
 */
function wc_stripe_process_payment_intent_succeeded( $intent, $request ) {
    $order = wc_get_order( wc_stripe_filter_order_id( $intent->metadata['order_id'], $intent ) );
    if ( ! $order ) {
        wc_stripe_log_error( sprintf( 'Could not complete payment for payment_intent %s. No order ID was found in your WordPress database.', $intent->id ) );

        return;
    }
    $payment_method = WC()->payment_gateways()->payment_gateways()[ $order->get_payment_method() ];

    if ( $payment_method instanceof WC_Payment_Gateway_Stripe_Local_Payment ) {
        /**
         * Delay the event by one second to allow the redirect handler to process
         * the payment.
         */
        sleep( 1 );

        if ( $payment_method->has_order_lock( $order ) || ( $transaction_id = $order->get_transaction_id() ) ) {
            wc_stripe_log_info( sprintf( 'payment_intent.succeeded event received. Intent has been completed for order %s. Event exited.', $order->get_id() ) );

            return;
        }

        $payment_method->set_order_lock( $order );
        $result = $payment_method->payment_object->process_payment( $order );

        if ( ! is_wp_error( $result ) && $result->complete_payment ) {
            $payment_method->payment_object->payment_complete( $order, $result->charge );
            $order->add_order_note( __( 'payment_intent.succeeded webhook received. Payment has been completed.', 'woo-stripe-payment' ) );
        }
    }
}

/**
 *
 * @param \Stripe\Charge  $charge
 * @param WP_REST_Request $request
 *
 * @since 3.1.1
 * @package Stripe/Functions
 */
function wc_stripe_process_charge_failed( $charge, $request ) {
    $order = wc_get_order( wc_stripe_filter_order_id( $charge->metadata['order_id'], $charge ) );

    if ( $order ) {
        $payment_methods = WC()->payment_gateways()->payment_gateways();
        if ( isset( $payment_methods[ $order->get_payment_method() ] ) ) {
            /**
             *
             * @var WC_Payment_Gateway_Stripe $payment_method
             */
            $payment_method = $payment_methods[ $order->get_payment_method() ];
            // only update order status if this is an asynchronous payment method,
            // and there is no completed date on the order. If there is a complete date it
            // means payment_complete was called on the order at some point
            if ( ! $payment_method->synchronous && ! $order->get_date_completed() ) {
                $order->update_status( apply_filters( 'wc_stripe_charge_failed_status', 'failed' ), $charge->failure_message );
            }
        }
    }
}

/**
 * Function that processes the charge.refund webhook. If the refund is created in the Stripe dashboard, a
 * refund will be created in the WC system to keep WC and Stripe in sync.
 *
 * @param \Stripe\Charge $charge
 *
 * @since 3.2.15
 */
function wc_stripe_process_create_refund( $charge ) {
    $mode  = $charge->livemode ? 'live' : 'test';
    $order = null;
    // get the order ID from the charge
    if ( isset( $charge->metadata['order_id'] ) ) {
        $order = wc_get_order( absint( $charge->metadata['order_id'] ) );
    } else {
        // charge didn't have order ID for whatever reason, so get order from charge ID
        $order = wc_stripe_get_order_from_transaction( $charge->id );
    }
    try {
        if ( ! $order ) {
            throw new Exception( sprintf( 'Could not match order with charge %s.', $charge->id ) );
        }
        // get the list of refunds and loop through them. Find the refund that doesn't have the required metadata attributes.
        foreach ( $charge->refunds as $refund ) {
            /**
             * @var \Stripe\Refund $refund
             */
            // refund was not created via WC
            if ( ! isset( $refund->metadata['order_id'], $refund->metadata['created_via'] ) ) {
                $args = array(
                    'amount'         => wc_stripe_remove_number_precision( $refund->amount, $order->get_currency() ),
                    'order_id'       => $order->get_id(),
                    'reason'         => $refund->reason,
                    'refund_payment' => false
                );
                // if the order has been fully refunded, items should be re-stocked
                if ( $order->get_total() == ( $args['amount'] + $order->get_total_refunded() ) ) {
                    $args['restock_items'] = true;
                    $line_items            = array();
                    foreach ( $order->get_items() as $item_id => $item ) {
                        $line_items[ $item_id ] = array(
                            'qty' => $item->get_quantity()
                        );
                    }
                    $args['line_items'] = $line_items;
                }
                // create the refund
                $result = wc_create_refund( $args );

                // Update the refund in Stripe with metadata
                if ( ! is_wp_error( $result ) ) {
                    $client = WC_Stripe_Gateway::load( $mode );
                    $order->add_order_note( sprintf( __( 'Order refunded in Stripe. Amount: %s', 'woo-stripe-payment' ), $result->get_formatted_refund_amount() ) );
                    $client->refunds->update( $refund->id, array(
                        'metadata' => array(
                            'order_id'    => $order->get_id(),
                            'created_via' => 'stripe_dashboard'
                        )
                    ) );
                    if ( stripe_wc()->advanced_settings->is_fee_enabled() ) {
                        // retrieve the charge but with expanded objects so fee and net can be calculated.
                        $charge = $client->charges->retrieve( $charge->id, array( 'expand' => array( 'balance_transaction', 'refunds.data.balance_transaction' ) ) );
                        if ( ! is_wp_error( $charge ) ) {
                            WC_Stripe_Utils::add_balance_transaction_to_order( $charge, $order, true );
                        }
                    }
                } else {
                    throw new Exception( $result->get_error_message() );
                }
            }
        }
    }
    catch ( Exception $e ) {
        wc_stripe_log_error( sprintf( 'Error processing refund webhook. Error: %s', $e->getMessage() ) );
    }
}

/**
 * @param $source_id
 *
 * @throws \Stripe\Exception\ApiErrorException
 */
function wc_stripe_retry_source_chargeable( $source_id ) {
    $source = WC_Stripe_Gateway::load()->sources->retrieve( $source_id );
    if ( ! is_wp_error( $source ) ) {
        wc_stripe_log_info( sprintf( 'Processing source.chargeable via scheduled action. Source ID %s', $source_id ) );
        wc_stripe_process_source_chargeable( $source, null );
    }
}

/**
 * @param Stripe\Dispute $dispute
 */
function wc_stripe_charge_dispute_created( $dispute ) {
    if ( stripe_wc()->advanced_settings->is_dispute_created_enabled() ) {
        $order = wc_stripe_get_order_from_transaction( $dispute->charge );
        if ( ! $order ) {
            wc_stripe_log_info( sprintf( 'No order found for charge %s. Dispute %s', $dispute->charge, $dispute->id ) );
        } else {
            $current_status = $order->get_status();
            $message        = sprintf( __( 'A dispute has been created for charge %1$s. Dispute status: %2$s.', 'woo-stripe-payment' ), $dispute->charge, strtoupper( $dispute->status ) );
            $order->update_status( apply_filters( 'wc_stripe_dispute_created_order_status', stripe_wc()->advanced_settings->get_option( 'dispute_created_status', 'on-hold' ), $dispute, $order ),
                $message );

            // update the dispute with metadata that can be used later
            WC_Stripe_Gateway::load( wc_stripe_order_mode( $order ) )->disputes->update( $dispute->id, array(
                'metadata' => array(
                    'order_id'          => $order->get_id(),
                    'prev_order_status' => $current_status
                )
            ) );
            // @todo send an email to the admin so they know a dispute was created
        }
    }
}

/**
 * @param Stripe\Dispute $dispute
 */
function wc_stripe_charge_dispute_closed( $dispute ) {
    if ( stripe_wc()->advanced_settings->is_dispute_closed_enabled() ) {
        if ( isset( $dispute->metadata['order_id'] ) ) {
            $order = wc_get_order( absint( $dispute->metadata['order_id'] ) );
        } else {
            $order = wc_stripe_get_order_from_transaction( $dispute->charge );
        }
        if ( ! $order ) {
            return wc_stripe_log_info( sprintf( 'No order found for charge %s. Dispute %s', $dispute->charge, $dispute->id ) );
        }
        $message = sprintf( __( 'Dispute %1$s has been closed. Result: %2$s.', 'woo-stripe-payment' ), $dispute->id, $dispute->status );
        switch ( $dispute->status ) {
            case 'won':
                //set the order's status back to what it was before the dispute
                if ( isset( $dispute->metadata['prev_order_status'] ) ) {
                    $status = $dispute->metadata['prev_order_status'];
                } else {
                    $status = $order->needs_processing() ? 'processing' : 'completed';
                }
                $order->update_status( $dispute->metadata['prev_order_status'], $message );
                break;
            case 'lost':
                $order->update_status( apply_filters( 'wc_stripe_dispute_closed_order_status', 'failed', $dispute, $order ), $message );
        }
    }
}

/**
 * @param Stripe\Review $review
 */
function wc_stripe_review_opened( $review ) {
    if ( stripe_wc()->advanced_settings->is_review_opened_enabled() ) {
        $order = wc_stripe_get_order_from_transaction( $review->charge );
        if ( $order ) {
            $status = $order->get_status();
            $order->update_meta_data( WC_Stripe_Constants::PREV_STATUS, $status );
            $message = sprintf( __( 'A review has been opened for charge %1$s. Reason: %2$s.', 'woo-stripe-payment' ), $review->charge, strtoupper( $review->reason ) );
            $order->update_status( apply_filters( 'wc_stripe_review_opened_order_status', 'on-hold', $review, $order ), $message );
        }
    }
}

/**
 * @param Stripe\Review $review
 */
function wc_stripe_review_closed( $review ) {
    if ( stripe_wc()->advanced_settings->is_review_closed_enabled() ) {
        $order = wc_stripe_get_order_from_transaction( $review->charge );
        if ( $order ) {
            $status = $order->get_meta( WC_Stripe_Constants::PREV_STATUS );
            if ( ! $status ) {
                $status = $order->needs_processing() ? 'processing' : 'completed';
            }
            $order->delete_meta_data( WC_Stripe_Constants::PREV_STATUS );
            $message = sprintf( __( 'A review has been closed for charge %1$s. Reason: %2$s.', 'woo-stripe-payment' ), $review->charge, strtoupper( $review->reason ) );
            $order->update_status( $status, $message );
        }
    }
}
Stripe API documentation generated by ApiGen