Admin
1 month ago
Api
2 years ago
ApplePay
4 months ago
Handlers
11 months ago
Integrations
1 year ago
PaymentTokens
1 year ago
Payment_Gateway.php
4 months ago
Payment_Gateway_Direct.php
1 year ago
Payment_Gateway_Helper.php
3 years ago
Payment_Gateway_My_Payment_Methods.php
3 years ago
Payment_Gateway_Payment_Form.php
4 months ago
Payment_Gateway_Plugin.php
3 months ago
Payment_Gateway_Privacy.php
1 year ago
Payment_Gateway_My_Payment_Methods.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Payment Gateway Framework |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2019, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 15 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework\PaymentGateway; |
| 22 | |
| 23 | use WooCommerce\Square\Framework\PaymentGateway\PaymentTokens\Payment_Gateway_Payment_Token; |
| 24 | |
| 25 | defined( 'ABSPATH' ) || exit; |
| 26 | |
| 27 | /** |
| 28 | * My Payment Methods Class |
| 29 | * |
| 30 | * Renders the My Payment Methods table on the My Account page and handles |
| 31 | * any associated actions (deleting a payment method, etc) |
| 32 | * |
| 33 | * @since 3.0.0 |
| 34 | */ |
| 35 | class Payment_Gateway_My_Payment_Methods { |
| 36 | |
| 37 | |
| 38 | /** @var Payment_Gateway_Plugin */ |
| 39 | protected $plugin; |
| 40 | |
| 41 | /** @var Payment_Gateway_Payment_Token[] array of token objects */ |
| 42 | protected $tokens; |
| 43 | |
| 44 | /** @var Payment_Gateway_Payment_Token[] array of token objects */ |
| 45 | protected $credit_card_tokens; |
| 46 | |
| 47 | /** @var Payment_Gateway_Payment_Token[] array of token objects */ |
| 48 | protected $echeck_tokens; |
| 49 | |
| 50 | /** @var bool true if there are tokens */ |
| 51 | protected $has_tokens; |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Setup Class |
| 56 | * |
| 57 | * Note: this constructor executes during the `wp` action |
| 58 | * |
| 59 | * @param Payment_Gateway_Plugin $plugin gateway plugin |
| 60 | * @since 3.0.0 |
| 61 | */ |
| 62 | public function __construct( $plugin ) { |
| 63 | |
| 64 | $this->plugin = $plugin; |
| 65 | |
| 66 | add_filter( 'woocommerce_account_payment_methods_columns', array( $this, 'filter_payment_method_columns' ) ); |
| 67 | add_action( 'woocommerce_account_payment_methods_column_method', array( $this, 'render_payment_method_method_data' ) ); |
| 68 | add_action( 'woocommerce_account_payment_methods_column_details', array( $this, 'render_payment_method_details_data' ) ); |
| 69 | } |
| 70 | |
| 71 | /** Payment Method actions ************************************************/ |
| 72 | |
| 73 | /** |
| 74 | * Adds the details column next to method. |
| 75 | * |
| 76 | * @param array $columns Array of columns for payment methods. |
| 77 | * @return array |
| 78 | */ |
| 79 | public function filter_payment_method_columns( $columns ) { |
| 80 | $filtered_columns = array(); |
| 81 | |
| 82 | foreach ( $columns as $slug => $title ) { |
| 83 | if ( 'method' === $slug ) { |
| 84 | $filtered_columns[ $slug ] = $title; |
| 85 | $filtered_columns['details'] = ' '; |
| 86 | } else { |
| 87 | $filtered_columns[ $slug ] = $title; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $filtered_columns; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Renders the card brand. |
| 96 | * |
| 97 | * @param array $method Holds data about the payment method. |
| 98 | */ |
| 99 | public function render_payment_method_method_data( $method ) { |
| 100 | echo esc_html( $method['method']['brand'] ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Renders card details. |
| 105 | * |
| 106 | * @param array $method Holds data about the payment method. |
| 107 | */ |
| 108 | public function render_payment_method_details_data( $method ) { |
| 109 | $card_brand = $method['method']['brand']; |
| 110 | $card_image_url = wc_square()->get_gateway()->get_payment_method_image_url( $card_brand ); |
| 111 | ?> |
| 112 | |
| 113 | <?php if ( ! empty( $card_image_url ) ) : ?> |
| 114 | <img |
| 115 | src="<?php echo esc_url( $card_image_url ); ?>" |
| 116 | width="40" |
| 117 | height="25" |
| 118 | alt="<?php echo esc_attr( $card_brand ); ?>" |
| 119 | title="<?php echo esc_attr( $card_brand ); ?>" |
| 120 | > |
| 121 | <?php endif; ?> |
| 122 | <span>• • •<?php echo esc_html( $method['method']['last4'] ); ?></span> |
| 123 | <?php |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Redirect back to the Payment Methods (WC 2.6+) or My Account page |
| 128 | * |
| 129 | * @since 3.0.0 |
| 130 | */ |
| 131 | protected function redirect_to_my_account() { |
| 132 | |
| 133 | wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) ); |
| 134 | exit; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Return the gateway plugin, primarily a convenience method to other actors |
| 140 | * using filters |
| 141 | * |
| 142 | * @since 3.0.0 |
| 143 | * |
| 144 | * @return Payment_Gateway_Plugin |
| 145 | */ |
| 146 | public function get_plugin() { |
| 147 | |
| 148 | return $this->plugin; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Returns true if at least one of the plugin's gateways supports the |
| 154 | * add new payment method feature |
| 155 | * |
| 156 | * @since 3.0.0 |
| 157 | * @return bool |
| 158 | */ |
| 159 | protected function supports_add_payment_method() { |
| 160 | |
| 161 | foreach ( $this->get_plugin()->get_gateways() as $gateway ) { |
| 162 | |
| 163 | if ( $gateway->is_direct_gateway() && $gateway->supports_add_payment_method() ) { |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Determines if we're viewing the My Account -> Payment Methods page. |
| 174 | * |
| 175 | * @since 3.0.0 |
| 176 | * |
| 177 | * @return bool |
| 178 | */ |
| 179 | protected function is_payment_methods_page() { |
| 180 | global $wp; |
| 181 | |
| 182 | return is_user_logged_in() && is_account_page() && isset( $wp->query_vars['payment-methods'] ); |
| 183 | } |
| 184 | } |
| 185 |