PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.3
WooCommerce Square v5.4.3
5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Framework / PaymentGateway / Payment_Gateway_Helper.php
woocommerce-square / includes / Framework / PaymentGateway Last commit date
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_Helper.php
257 lines
1 <?php
2 /**
3 * WooCommerce Plugin 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 defined( 'ABSPATH' ) or exit;
24
25 /**
26 * Payment Gateway Helper Class
27 *
28 * @since 3.0.0
29 */
30 class Payment_Gateway_Helper {
31
32
33 /** @var string the Visa card type ID **/
34 const CARD_TYPE_VISA = 'visa';
35
36 /** @var string the MasterCard card type ID **/
37 const CARD_TYPE_MASTERCARD = 'mastercard';
38
39 /** @var string the American Express card type ID **/
40 const CARD_TYPE_AMEX = 'amex';
41
42 /** @var string the Diners Club card type ID **/
43 const CARD_TYPE_DINERSCLUB = 'dinersclub';
44
45 /** @var string the Discover card type ID **/
46 const CARD_TYPE_DISCOVER = 'discover';
47
48 /** @var string the JCB card type ID **/
49 const CARD_TYPE_JCB = 'jcb';
50
51 /** @var string the CarteBleue card type ID **/
52 const CARD_TYPE_CARTEBLEUE = 'cartebleue';
53
54 /** @var string the Maestro card type ID **/
55 const CARD_TYPE_MAESTRO = 'maestro';
56
57 /** @var string the Laser card type ID **/
58 const CARD_TYPE_LASER = 'laser';
59
60
61 /**
62 * Perform standard luhn check. Algorithm:
63 *
64 * 1. Double the value of every second digit beginning with the second-last right-hand digit.
65 * 2. Add the individual digits comprising the products obtained in step 1 to each of the other digits in the original number.
66 * 3. Subtract the total obtained in step 2 from the next higher number ending in 0.
67 * 4. This number should be the same as the last digit (the check digit). If the total obtained in step 2 is a number ending in zero (30, 40 etc.), the check digit is 0.
68 *
69 * @since 3.0.0
70 * @param string $account_number the credit card number to check
71 * @return bool true if $account_number passes the check, false otherwise
72 */
73 public static function luhn_check( $account_number ) {
74
75 for ( $sum = 0, $i = 0, $ix = strlen( $account_number ); $i < $ix - 1; $i++) {
76
77 $weight = substr( $account_number, $ix - ( $i + 2 ), 1 ) * ( 2 - ( $i % 2 ) );
78 $sum += $weight < 10 ? $weight : $weight - 9;
79
80 }
81
82 return substr( $account_number, $ix - 1 ) == ( ( 10 - $sum % 10 ) % 10 );
83 }
84
85
86 /**
87 * Normalize a card type to a standard type ID and account for variations.
88 *
89 * @since 3.0.0
90 * @param string $card_type the card type to normalize
91 * @return string
92 */
93 public static function normalize_card_type( $card_type ) {
94
95 $card_types = self::get_card_types();
96
97 $card_type = strtolower( $card_type );
98
99 // stop here if the provided card type is already normalized
100 if ( in_array( $card_type, array_keys( $card_types ), true ) ) {
101 return $card_type;
102 }
103
104 $variations = wp_list_pluck( $card_types, 'variations' );
105
106 // if the provided card type matches a known variation, return the normalized card type
107 foreach ( $variations as $valid_type => $vars ) {
108
109 if ( in_array( $card_type, $vars, true ) ) {
110 $card_type = $valid_type;
111 break;
112 }
113 }
114
115 // otherwise, let it through unaltered
116 return $card_type;
117 }
118
119
120 /**
121 * Determine the credit card type from a given account number (only first 4
122 * required)
123 *
124 * @since 3.0.0
125 * @param string $account_number the credit card account number
126 * @return string the credit card type
127 */
128 public static function card_type_from_account_number( $account_number ) {
129
130 // card type regex patterns from https://github.com/stripe/jquery.payment/blob/master/src/jquery.payment.coffee
131 $types = array(
132 self::CARD_TYPE_VISA => '/^4/',
133 self::CARD_TYPE_MASTERCARD => '/^(5[1-5]|2[2-7])/',
134 self::CARD_TYPE_AMEX => '/^3[47]/',
135 self::CARD_TYPE_DINERSCLUB => '/^(36|38|30[0-5])/',
136 self::CARD_TYPE_DISCOVER => '/^(6011|65|64[4-9]|622)/',
137 self::CARD_TYPE_JCB => '/^35/',
138 self::CARD_TYPE_MAESTRO => '/^(5018|5020|5038|6304|6759|676[1-3])/',
139 self::CARD_TYPE_LASER => '/^(6706|6771|6709)/',
140 );
141
142 foreach ( $types as $type => $pattern ) {
143
144 if ( 1 === preg_match( $pattern, $account_number ) ) {
145 return $type;
146 }
147 }
148
149 return null;
150 }
151
152
153 /**
154 * Translates a credit card type or bank account name to a full name,
155 * e.g. 'mastercard' => 'MasterCard' or 'savings' => 'eCheck'
156 *
157 * @since 3.0.0
158 * @param string $payment_type the credit card or bank type, ie 'mastercard', 'amex', 'checking'
159 * @return string the credit card or bank account name, ie 'MasterCard', 'American Express', 'Checking Account'
160 */
161 public static function payment_type_to_name( $payment_type ) {
162
163 $name = '';
164
165 // normalize for backwards compatibility with gateways that pass the card type directly from Payment_Gateway::get_card_types()
166 $type = self::normalize_card_type( $payment_type );
167
168 // known payment type names, excluding credit cards
169 $payment_types = array(
170 'paypal' => esc_html__( 'PayPal', 'woocommerce-square' ),
171 'checking' => esc_html__( 'Checking Account', 'woocommerce-square' ),
172 'savings' => esc_html__( 'Savings Account', 'woocommerce-square' ),
173 'card' => esc_html__( 'Credit / Debit Card', 'woocommerce-square' ),
174 'bank' => esc_html__( 'Bank Account', 'woocommerce-square' ),
175 );
176
177 // add the credit card names
178 $payment_types = array_merge( wp_list_pluck( self::get_card_types(), 'name' ), $payment_types );
179
180 if ( isset( $payment_types[ $type ] ) ) {
181 $name = $payment_types[ $type ];
182 } elseif ( '' === $type ) {
183 $name = esc_html_x( 'Account', 'payment method type', 'woocommerce-square' );
184 } else {
185 $name = ucwords( str_replace( '-', ' ', $type ) );
186 }
187
188 /**
189 * Payment Gateway Type to Name Filter.
190 *
191 * Allow actors to modify the name returned given a payment type.
192 *
193 * @since 3.0.0
194 * @param string $name nice payment type name, e.g. American Express
195 * @param string $type payment type, e.g. amex
196 */
197 return apply_filters( 'wc_payment_gateway_payment_type_to_name', $name, $type );
198 }
199
200
201 /**
202 * Gets the known card types and their variations.
203 *
204 * Returns the card types in the format:
205 *
206 * 'mastercard' {
207 * 'name' => 'MasterCard',
208 * 'variations' => array( 'mc' ),
209 * }
210 *
211 * @since 3.0.0
212 *
213 * @return array
214 */
215 public static function get_card_types() {
216
217 return array(
218 self::CARD_TYPE_VISA => array(
219 'name' => esc_html_x( 'Visa', 'credit card type', 'woocommerce-square' ),
220 'variations' => array(),
221 ),
222 self::CARD_TYPE_MASTERCARD => array(
223 'name' => esc_html_x( 'MasterCard', 'credit card type', 'woocommerce-square' ),
224 'variations' => array( 'mc' ),
225 ),
226 self::CARD_TYPE_AMEX => array(
227 'name' => esc_html_x( 'American Express', 'credit card type', 'woocommerce-square' ),
228 'variations' => array( 'americanexpress' ),
229 ),
230 self::CARD_TYPE_DINERSCLUB => array(
231 'name' => esc_html_x( 'Diners Club', 'credit card type', 'woocommerce-square' ),
232 'variations' => array( 'diners' ),
233 ),
234 self::CARD_TYPE_DISCOVER => array(
235 'name' => esc_html_x( 'Discover', 'credit card type', 'woocommerce-square' ),
236 'variations' => array( 'disc' ),
237 ),
238 self::CARD_TYPE_JCB => array(
239 'name' => esc_html_x( 'JCB', 'credit card type', 'woocommerce-square' ),
240 'variations' => array(),
241 ),
242 self::CARD_TYPE_CARTEBLEUE => array(
243 'name' => esc_html_x( 'CarteBleue', 'credit card type', 'woocommerce-square' ),
244 'variations' => array(),
245 ),
246 self::CARD_TYPE_MAESTRO => array(
247 'name' => esc_html_x( 'Maestro', 'credit card type', 'woocommerce-square' ),
248 'variations' => array(),
249 ),
250 self::CARD_TYPE_LASER => array(
251 'name' => esc_html_x( 'Laser', 'credit card type', 'woocommerce-square' ),
252 'variations' => array(),
253 ),
254 );
255 }
256 }
257