AgenticCheckoutUtils.php
4 months ago
ArrayUtils.php
2 years ago
CartController.php
1 month ago
CartTokenUtils.php
1 year ago
CheckoutTrait.php
1 month ago
DraftOrderTrait.php
1 year ago
JsonWebToken.php
11 months ago
LocalPickupUtils.php
5 months ago
NoticeHandler.php
1 year ago
OrderAuthorizationTrait.php
6 months ago
OrderController.php
1 month ago
Pagination.php
2 years ago
PaymentUtils.php
1 year ago
ProductItemTrait.php
1 month ago
ProductLinksTrait.php
3 months ago
ProductQuery.php
2 months ago
ProductQueryFilters.php
11 months ago
QuantityLimits.php
11 months ago
RateLimits.php
1 year ago
SanitizationUtils.php
2 years ago
ValidationUtils.php
2 years ago
PaymentUtils.php
118 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 5 | |
| 6 | /** |
| 7 | * PaymentUtils |
| 8 | * |
| 9 | * Utility class for payment methods. |
| 10 | */ |
| 11 | class PaymentUtils { |
| 12 | |
| 13 | /** |
| 14 | * Callback for woocommerce_payment_methods_list_item filter to add token id |
| 15 | * to the generated list. |
| 16 | * |
| 17 | * @param array $list_item The current list item for the saved payment method. |
| 18 | * @param \WC_Token $token The token for the current list item. |
| 19 | * |
| 20 | * @return array The list item with the token id added. |
| 21 | */ |
| 22 | public static function include_token_id_with_payment_methods( $list_item, $token ) { |
| 23 | $list_item['tokenId'] = $token->get_id(); |
| 24 | $brand = ! empty( $list_item['method']['brand'] ) ? |
| 25 | strtolower( $list_item['method']['brand'] ) : |
| 26 | ''; |
| 27 | if ( ! empty( $brand ) && esc_html__( 'Credit card', 'woocommerce' ) !== $brand ) { |
| 28 | $list_item['method']['brand'] = wc_get_credit_card_type_label( $brand ); |
| 29 | } |
| 30 | return $list_item; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get enabled payment gateways. |
| 35 | * |
| 36 | * @return array |
| 37 | */ |
| 38 | public static function get_enabled_payment_gateways() { |
| 39 | $payment_gateways = WC()->payment_gateways->payment_gateways(); |
| 40 | return array_filter( |
| 41 | $payment_gateways, |
| 42 | function ( $payment_gateway ) { |
| 43 | return 'yes' === $payment_gateway->enabled; |
| 44 | } |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns enabled saved payment methods for a customer and the default method if there are multiple. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public static function get_saved_payment_methods() { |
| 54 | if ( ! is_user_logged_in() ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | add_filter( 'woocommerce_payment_methods_list_item', [ self::class, 'include_token_id_with_payment_methods' ], 10, 2 ); |
| 59 | |
| 60 | $enabled_payment_gateways = self::get_enabled_payment_gateways(); |
| 61 | $saved_payment_methods = wc_get_customer_saved_methods_list( get_current_user_id() ); |
| 62 | $payment_methods = [ |
| 63 | 'enabled' => [], |
| 64 | 'default' => null, |
| 65 | ]; |
| 66 | |
| 67 | // Filter out payment methods that are not enabled. |
| 68 | foreach ( $saved_payment_methods as $payment_method_group => $saved_payment_methods ) { |
| 69 | $payment_methods['enabled'][ $payment_method_group ] = array_values( |
| 70 | array_filter( |
| 71 | $saved_payment_methods, |
| 72 | function ( $saved_payment_method ) use ( $enabled_payment_gateways, &$payment_methods ) { |
| 73 | if ( true === $saved_payment_method['is_default'] && null === $payment_methods['default'] ) { |
| 74 | $payment_methods['default'] = $saved_payment_method; |
| 75 | } |
| 76 | return in_array( $saved_payment_method['method']['gateway'], array_keys( $enabled_payment_gateways ), true ); |
| 77 | } |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | remove_filter( 'woocommerce_payment_methods_list_item', [ self::class, 'include_token_id_with_payment_methods' ], 10, 2 ); |
| 83 | |
| 84 | return $payment_methods; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the default payment method for a customer. |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public static function get_default_payment_method() { |
| 93 | $saved_payment_methods = self::get_saved_payment_methods(); |
| 94 | // A saved payment method exists, set as default. |
| 95 | if ( $saved_payment_methods && ! empty( $saved_payment_methods['default'] ) ) { |
| 96 | return $saved_payment_methods['default']['method']['gateway'] ?? ''; |
| 97 | } |
| 98 | |
| 99 | $chosen_payment_method = WC()->session->get( 'chosen_payment_method' ); |
| 100 | |
| 101 | // If payment method is already stored in session, use it. |
| 102 | if ( $chosen_payment_method ) { |
| 103 | return $chosen_payment_method; |
| 104 | } |
| 105 | |
| 106 | // If no saved payment method exists, use the first enabled payment method. |
| 107 | $enabled_payment_gateways = self::get_enabled_payment_gateways(); |
| 108 | |
| 109 | if ( empty( $enabled_payment_gateways ) ) { |
| 110 | return ''; |
| 111 | } |
| 112 | |
| 113 | $first_key = array_key_first( $enabled_payment_gateways ); |
| 114 | $first_payment_method = $enabled_payment_gateways[ $first_key ]; |
| 115 | return $first_payment_method->id ?? ''; |
| 116 | } |
| 117 | } |
| 118 |