@@ -977,7 +977,6 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis
977
977
$ subscription ->save ();
978
978
}
979
979
980
- $ stripe_customer = new WC_Stripe_Customer ();
981
980
$ stripe_customer_id = $ subscription ->get_meta ( '_stripe_customer_id ' , true );
982
981
983
982
// If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data.
@@ -1011,82 +1010,83 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis
1011
1010
}
1012
1011
}
1013
1012
1014
- $ stripe_customer ->set_id ( $ stripe_customer_id );
1015
-
1016
- $ payment_method_to_display = __ ( 'N/A ' , 'woocommerce-gateway-stripe ' );
1017
-
1018
1013
try {
1019
- // Retrieve all possible payment methods for subscriptions.
1020
- foreach ( WC_Stripe_Customer::STRIPE_PAYMENT_METHODS as $ payment_method_type ) {
1021
- foreach ( $ stripe_customer ->get_payment_methods ( $ payment_method_type ) as $ source ) {
1022
- if ( $ source ->id !== $ stripe_source_id ) {
1023
- continue ;
1024
- }
1014
+ $ saved_payment_method = WC_Stripe_Subscriptions_Helper::get_subscription_payment_method_details ( $ stripe_customer_id , $ stripe_source_id );
1025
1015
1026
- // Legacy handling for Stripe Card objects. ref: https://docs.stripe.com/api/cards/object
1027
- if ( isset ( $ source ->object ) && WC_Stripe_Payment_Methods::CARD === $ source ->object ) {
1028
- /* translators: 1) card brand 2) last 4 digits */
1029
- $ payment_method_to_display = sprintf ( __ ( 'Via %1$s card ending in %2$s ' , 'woocommerce-gateway-stripe ' ), ( isset ( $ source ->brand ) ? wc_get_credit_card_type_label ( $ source ->brand ) : __ ( 'N/A ' , 'woocommerce-gateway-stripe ' ) ), $ source ->last4 );
1030
- break 2 ;
1031
- }
1032
-
1033
- switch ( $ source ->type ) {
1034
- case WC_Stripe_Payment_Methods::CARD :
1035
- /* translators: 1) card brand 2) last 4 digits */
1036
- $ payment_method_to_display = sprintf ( __ ( 'Via %1$s card ending in %2$s ' , 'woocommerce-gateway-stripe ' ), ( isset ( $ source ->card ->brand ) ? wc_get_credit_card_type_label ( $ source ->card ->brand ) : __ ( 'N/A ' , 'woocommerce-gateway-stripe ' ) ), $ source ->card ->last4 );
1037
- break 3 ;
1038
- case WC_Stripe_Payment_Methods::SEPA_DEBIT :
1039
- /* translators: 1) last 4 digits of SEPA Direct Debit */
1040
- $ payment_method_to_display = sprintf ( __ ( 'Via SEPA Direct Debit ending in %1$s ' , 'woocommerce-gateway-stripe ' ), $ source ->sepa_debit ->last4 );
1041
- break 3 ;
1042
- case WC_Stripe_Payment_Methods::CASHAPP_PAY :
1043
- /* translators: 1) Cash App Cashtag */
1044
- $ payment_method_to_display = sprintf ( __ ( 'Via Cash App Pay (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ source ->cashapp ->cashtag );
1045
- break 3 ;
1046
- case WC_Stripe_Payment_Methods::LINK :
1047
- /* translators: 1) email address associated with the Stripe Link payment method */
1048
- $ payment_method_to_display = sprintf ( __ ( 'Via Stripe Link (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ source ->link ->email );
1049
- break 3 ;
1050
- case WC_Stripe_Payment_Methods::ACH :
1051
- $ payment_method_to_display = sprintf (
1052
- /* translators: 1) account type (checking, savings), 2) last 4 digits of account. */
1053
- __ ( 'Via %1$s Account ending in %2$s ' , 'woocommerce-gateway-stripe ' ),
1054
- ucfirst ( $ source ->us_bank_account ->account_type ),
1055
- $ source ->us_bank_account ->last4
1056
- );
1057
- break 3 ;
1058
- case WC_Stripe_Payment_Methods::BECS_DEBIT :
1059
- $ payment_method_to_display = sprintf (
1060
- /* translators: last 4 digits of account. */
1061
- __ ( 'BECS Direct Debit ending in %s ' , 'woocommerce-gateway-stripe ' ),
1062
- $ source ->au_becs_debit ->last4
1063
- );
1064
- break 3 ;
1065
- case WC_Stripe_Payment_Methods::ACSS_DEBIT :
1066
- $ payment_method_to_display = sprintf (
1067
- /* translators: 1) bank name, 2) last 4 digits of account. */
1068
- __ ( 'Via %1$s ending in %2$s ' , 'woocommerce-gateway-stripe ' ),
1069
- $ source ->acss_debit ->bank_name ,
1070
- $ source ->acss_debit ->last4
1071
- );
1072
- break 3 ;
1073
- case WC_Stripe_Payment_Methods::BACS_DEBIT :
1074
- /* translators: 1) the Bacs Direct Debit payment method's last 4 numbers */
1075
- $ payment_method_to_display = sprintf ( __ ( 'Via Bacs Direct Debit ending in (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ source ->bacs_debit ->last4 );
1076
- break 3 ;
1077
- case WC_Stripe_Payment_Methods::AMAZON_PAY :
1078
- /* translators: 1) the Amazon Pay payment method's email */
1079
- $ payment_method_to_display = sprintf ( __ ( 'Via Amazon Pay (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ source ->billing_details ->email ?? '' );
1080
- break 3 ;
1081
- }
1082
- }
1016
+ if ( null !== $ saved_payment_method ) {
1017
+ return $ this ->get_payment_method_to_display_for_payment_method ( $ saved_payment_method );
1083
1018
}
1084
1019
} catch ( WC_Stripe_Exception $ e ) {
1085
1020
wc_add_notice ( $ e ->getLocalizedMessage (), 'error ' );
1086
1021
WC_Stripe_Logger::log ( 'Error: ' . $ e ->getMessage () );
1087
1022
}
1088
1023
1089
- return $ payment_method_to_display ;
1024
+ return __ ( 'N/A ' , 'woocommerce-gateway-stripe ' );
1025
+ }
1026
+
1027
+ /**
1028
+ * Helper function to get the descriptive text for a payment method or source.
1029
+ *
1030
+ * @param object $payment_method The payment method or source object.
1031
+ * @return string The descriptive text for the payment method or source.
1032
+ */
1033
+ protected function get_payment_method_to_display_for_payment_method ( object $ payment_method ): string {
1034
+ // Legacy handling for Stripe Card objects. ref: https://docs.stripe.com/api/cards/object
1035
+ if ( isset ( $ payment_method ->object ) && WC_Stripe_Payment_Methods::CARD === $ payment_method ->object ) {
1036
+ return sprintf (
1037
+ /* translators: 1) card brand 2) last 4 digits */
1038
+ __ ( 'Via %1$s card ending in %2$s ' , 'woocommerce-gateway-stripe ' ),
1039
+ ( isset ( $ payment_method ->brand ) ? wc_get_credit_card_type_label ( $ payment_method ->brand ) : __ ( 'N/A ' , 'woocommerce-gateway-stripe ' ) ),
1040
+ $ payment_method ->last4
1041
+ );
1042
+ }
1043
+
1044
+ switch ( $ payment_method ->type ) {
1045
+ case WC_Stripe_Payment_Methods::CARD :
1046
+ return sprintf (
1047
+ /* translators: 1) card brand 2) last 4 digits */
1048
+ __ ( 'Via %1$s card ending in %2$s ' , 'woocommerce-gateway-stripe ' ),
1049
+ ( isset ( $ payment_method ->card ->brand ) ? wc_get_credit_card_type_label ( $ payment_method ->card ->brand ) : __ ( 'N/A ' , 'woocommerce-gateway-stripe ' ) ),
1050
+ $ payment_method ->card ->last4
1051
+ );
1052
+ case WC_Stripe_Payment_Methods::SEPA_DEBIT :
1053
+ /* translators: 1) last 4 digits of SEPA Direct Debit */
1054
+ return sprintf ( __ ( 'Via SEPA Direct Debit ending in %1$s ' , 'woocommerce-gateway-stripe ' ), $ payment_method ->sepa_debit ->last4 );
1055
+ case WC_Stripe_Payment_Methods::CASHAPP_PAY :
1056
+ /* translators: 1) Cash App Cashtag */
1057
+ return sprintf ( __ ( 'Via Cash App Pay (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ payment_method ->cashapp ->cashtag );
1058
+ case WC_Stripe_Payment_Methods::LINK :
1059
+ /* translators: 1) email address associated with the Stripe Link payment method */
1060
+ return sprintf ( __ ( 'Via Stripe Link (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ payment_method ->link ->email );
1061
+ case WC_Stripe_Payment_Methods::ACH :
1062
+ return sprintf (
1063
+ /* translators: 1) account type (checking, savings), 2) last 4 digits of account. */
1064
+ __ ( 'Via %1$s Account ending in %2$s ' , 'woocommerce-gateway-stripe ' ),
1065
+ ucfirst ( $ payment_method ->us_bank_account ->account_type ),
1066
+ $ payment_method ->us_bank_account ->last4
1067
+ );
1068
+ case WC_Stripe_Payment_Methods::BECS_DEBIT :
1069
+ return sprintf (
1070
+ /* translators: last 4 digits of account. */
1071
+ __ ( 'BECS Direct Debit ending in %s ' , 'woocommerce-gateway-stripe ' ),
1072
+ $ payment_method ->au_becs_debit ->last4
1073
+ );
1074
+ case WC_Stripe_Payment_Methods::ACSS_DEBIT :
1075
+ return sprintf (
1076
+ /* translators: 1) bank name, 2) last 4 digits of account. */
1077
+ __ ( 'Via %1$s ending in %2$s ' , 'woocommerce-gateway-stripe ' ),
1078
+ $ payment_method ->acss_debit ->bank_name ,
1079
+ $ payment_method ->acss_debit ->last4
1080
+ );
1081
+ case WC_Stripe_Payment_Methods::BACS_DEBIT :
1082
+ /* translators: 1) the Bacs Direct Debit payment method's last 4 numbers */
1083
+ return sprintf ( __ ( 'Via Bacs Direct Debit ending in (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ payment_method ->bacs_debit ->last4 );
1084
+ case WC_Stripe_Payment_Methods::AMAZON_PAY :
1085
+ /* translators: 1) the Amazon Pay payment method's email */
1086
+ return sprintf ( __ ( 'Via Amazon Pay (%1$s) ' , 'woocommerce-gateway-stripe ' ), $ payment_method ->billing_details ->email ?? '' );
1087
+ }
1088
+
1089
+ return __ ( 'N/A ' , 'woocommerce-gateway-stripe ' );
1090
1090
}
1091
1091
1092
1092
/**
0 commit comments