Skip to content

Commit f5be887

Browse files
authored
Handling more metas with the Stripe order helper class (#4759)
* Handling more metas with the Stripe order helper class * Changelog and readme entries * Fix tests * Fix tests * Fix tests
1 parent 0e6b469 commit f5be887

10 files changed

+300
-53
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 10.2.0 - xxxx-xx-xx =
4+
* Dev - Expands the Stripe Order Helper class to handle mandate ID, Multibanco data, refund status, card brand, charge captured flag, status final flag, and the refund failure reason
45
* Dev - Remove the merchant email address from the System Status Report
56
* Dev - Replace the constant reference for the legacy SEPA payment method
67
* Update - Changes the list of payment methods shown in the Stripe account connection modal

includes/abstracts/abstract-wc-stripe-payment-gateway.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,12 @@ public function process_response( $response, $order ) {
582582
*/
583583
do_action( 'wc_gateway_stripe_process_payment_charge', $response, $order );
584584

585-
$order_id = $order->get_id();
586-
$captured = ( isset( $response->captured ) && $response->captured ) ? 'yes' : 'no';
585+
$order_id = $order->get_id();
586+
$order_helper = WC_Stripe_Order_Helper::get_instance();
587+
$captured = isset( $response->captured ) && $response->captured;
587588

588589
// Store charge data.
589-
$order->update_meta_data( '_stripe_charge_captured', $captured );
590+
$order_helper->set_stripe_charge_captured( $order, $captured );
590591

591592
if ( isset( $response->balance_transaction ) ) {
592593
$this->update_fees( $order, is_string( $response->balance_transaction ) ? $response->balance_transaction : $response->balance_transaction->id );
@@ -596,16 +597,16 @@ public function process_response( $response, $order ) {
596597
// The mandate ID is not available for the intent object, so we need to fetch the charge.
597598
// Mandate ID is necessary for renewal payments for certain payment methods and Indian cards.
598599
if ( isset( $response->payment_method_details->card->mandate ) ) {
599-
$order->update_meta_data( '_stripe_mandate_id', $response->payment_method_details->card->mandate );
600+
$order_helper->update_stripe_mandate_id( $order, $response->payment_method_details->card->mandate );
600601
} elseif ( isset( $response->payment_method_details->acss_debit->mandate ) ) {
601-
$order->update_meta_data( '_stripe_mandate_id', $response->payment_method_details->acss_debit->mandate );
602+
$order_helper->update_stripe_mandate_id( $order, $response->payment_method_details->acss_debit->mandate );
602603
}
603604

604605
if ( isset( $response->payment_method, $response->payment_method_details ) ) {
605606
WC_Stripe_Payment_Tokens::update_token_from_method_details( $order->get_customer_id(), $response->payment_method, $response->payment_method_details );
606607
}
607608

608-
if ( 'yes' === $captured ) {
609+
if ( $captured ) {
609610
/**
610611
* Charge can be captured but in a pending state. Payment methods
611612
* that are asynchronous may take couple days to clear. Webhook will
@@ -1179,8 +1180,9 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
11791180

11801181
$request = [];
11811182

1183+
$order_helper = WC_Stripe_Order_Helper::get_instance();
11821184
$order_currency = $order->get_currency();
1183-
$captured = $order->get_meta( '_stripe_charge_captured', true );
1185+
$captured = $order_helper->is_stripe_charge_captured( $order );
11841186
$charge_id = $order->get_transaction_id();
11851187

11861188
if ( ! $charge_id ) {
@@ -1192,7 +1194,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
11921194
}
11931195

11941196
// If order is only authorized, don't pass amount.
1195-
if ( 'yes' !== $captured ) {
1197+
if ( ! $captured ) {
11961198
unset( $request['amount'] );
11971199
}
11981200

@@ -1210,12 +1212,10 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
12101212
}
12111213

12121214
// Only treat zero-amount as a no-op for captured charges (real refunds), not for voiding pre-auths.
1213-
if ( 'yes' === $captured && '0.00' === sprintf( '%0.2f', $amount ?? 0 ) ) {
1215+
if ( $captured && '0.00' === sprintf( '%0.2f', $amount ?? 0 ) ) {
12141216
return true;
12151217
}
12161218

1217-
$order_helper = WC_Stripe_Order_Helper::get_instance();
1218-
12191219
$request['charge'] = $charge_id;
12201220
WC_Stripe_Logger::log( "Info: Beginning refund for order {$charge_id} for the amount of {$amount}" );
12211221
$response = new stdClass();
@@ -1248,7 +1248,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
12481248
}
12491249
}
12501250

1251-
if ( ! $intent_cancelled && 'yes' === $captured ) {
1251+
if ( ! $intent_cancelled && $captured ) {
12521252
$order_helper->lock_order_refund( $order );
12531253
$response = WC_Stripe_API::request( $request, 'refunds' );
12541254
}
@@ -1286,7 +1286,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
12861286
}
12871287

12881288
// If charge wasn't captured, skip creating a refund and cancel order.
1289-
if ( 'yes' !== $captured ) {
1289+
if ( ! $captured ) {
12901290
/* translators: amount (including currency symbol) */
12911291
$order->add_order_note( sprintf( __( 'Pre-Authorization for %s voided.', 'woocommerce-gateway-stripe' ), $formatted_amount ) );
12921292
$order->update_status( OrderStatus::CANCELLED );
@@ -1718,16 +1718,16 @@ public function save_intent_to_order( $order, $intent ) {
17181718
$charge = $this->get_latest_charge_from_intent( $intent );
17191719

17201720
if ( isset( $charge->payment_method_details->card->mandate ) ) {
1721-
$order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->card->mandate );
1721+
$order_helper->update_stripe_mandate_id( $order, $charge->payment_method_details->card->mandate );
17221722
} elseif ( isset( $charge->payment_method_details->acss_debit->mandate ) ) {
1723-
$order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->acss_debit->mandate );
1723+
$order_helper->update_stripe_mandate_id( $order, $charge->payment_method_details->acss_debit->mandate );
17241724
}
17251725
} elseif ( 'setup_intent' === $intent->object ) {
17261726
$order_helper->update_stripe_setup_intent_id( $order, $intent->id );
17271727

17281728
// Add mandate for free trial subscriptions.
17291729
if ( isset( $intent->mandate ) ) {
1730-
$order->update_meta_data( '_stripe_mandate_id', $intent->mandate );
1730+
$order_helper->update_stripe_mandate_id( $order, $intent->mandate );
17311731
}
17321732
}
17331733

@@ -1990,7 +1990,7 @@ public function create_and_confirm_intent_for_off_session( $order, $prepared_sou
19901990
}
19911991

19921992
// Add mandate if it exists.
1993-
$mandate = $order->get_meta( '_stripe_mandate_id', true );
1993+
$mandate = WC_Stripe_Order_Helper::get_instance()->get_stripe_mandate_id( $order );
19941994
if ( ! empty( $mandate ) ) {
19951995
$request['mandate'] = $mandate;
19961996
}

includes/class-wc-stripe-order-handler.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function show_warning_for_uncaptured_orders( $order_id ) {
6060
$gateway = WC_Stripe::get_instance()->get_main_stripe_gateway();
6161

6262
// Bail if the order is already captured or if manual capture is disabled.
63-
if ( 'yes' === $order->get_meta( '_stripe_charge_captured', true ) || $gateway->is_automatic_capture_enabled() ) {
63+
if ( WC_Stripe_Order_Helper::get_instance()->is_stripe_charge_captured( $order ) || $gateway->is_automatic_capture_enabled() ) {
6464
return;
6565
}
6666

@@ -291,15 +291,15 @@ private function maybe_process_legacy_redirect() {
291291
* @return stdClass|void Result of payment capture.
292292
*/
293293
public function capture_payment( $order_id ) {
294-
$result = new stdClass();
295-
$order = wc_get_order( $order_id );
294+
$result = new stdClass();
295+
$order = wc_get_order( $order_id );
296+
$order_helper = WC_Stripe_Order_Helper::get_instance();
296297

297298
if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) {
298299
$charge = $order->get_transaction_id();
299-
$captured = $order->get_meta( '_stripe_charge_captured', true );
300300
$is_stripe_captured = false;
301301

302-
if ( $charge && 'no' === $captured ) {
302+
if ( $charge && 'no' === $order_helper->get_stripe_charge_captured( $order ) ) { // Strictly checking for 'no' value.
303303
$order_total = $order->get_total();
304304

305305
if ( 0 < $order->get_total_refunded() ) {
@@ -369,7 +369,7 @@ public function capture_payment( $order_id ) {
369369
if ( $is_stripe_captured ) {
370370
/* translators: transaction id */
371371
$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) );
372-
$order->update_meta_data( '_stripe_charge_captured', 'yes' );
372+
$order_helper->set_stripe_charge_captured( $order, true );
373373

374374
// Store other data such as fees
375375
$order->set_transaction_id( $result->id );
@@ -403,9 +403,9 @@ public function cancel_payment( $order_id ) {
403403
$order = wc_get_order( $order_id );
404404

405405
if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) {
406-
$captured = $order->get_meta( '_stripe_charge_captured', true );
406+
$captured = WC_Stripe_Order_Helper::get_instance()->is_stripe_charge_captured( $order );
407407

408-
if ( 'no' === $captured ) {
408+
if ( ! $captured ) {
409409
// To cancel a pre-auth, we need to refund the charge.
410410
$this->process_refund( $order_id );
411411
}

0 commit comments

Comments
 (0)