-
Notifications
You must be signed in to change notification settings - Fork 42
/
class-sv-wc-payment-gateway-helper.php
executable file
·284 lines (231 loc) · 8.96 KB
/
class-sv-wc-payment-gateway-helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* WooCommerce Plugin Framework
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@skyverge.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade the plugin to newer
* versions in the future. If you wish to customize the plugin for your
* needs please refer to http://www.skyverge.com
*
* @package SkyVerge/WooCommerce/Plugin/Classes
* @author SkyVerge
* @copyright Copyright (c) 2013-2024, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_1;
defined( 'ABSPATH' ) or exit;
if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\v5_15_1\\SV_WC_Payment_Gateway_Helper' ) ) :
/**
* SkyVerge Payment Gateway Helper Class
*
* The purpose of this class is to centralize common utility functions that
* are commonly used in SkyVerge payment gateway plugins
*
* @since 3.0.0
*/
#[\AllowDynamicProperties]
class SV_WC_Payment_Gateway_Helper {
/** @var string the Visa card type ID **/
const CARD_TYPE_VISA = 'visa';
/** @var string the MasterCard card type ID **/
const CARD_TYPE_MASTERCARD = 'mastercard';
/** @var string the American Express card type ID **/
const CARD_TYPE_AMEX = 'amex';
/** @var string the Diners Club card type ID **/
const CARD_TYPE_DINERSCLUB = 'dinersclub';
/** @var string the Discover card type ID **/
const CARD_TYPE_DISCOVER = 'discover';
/** @var string the JCB card type ID **/
const CARD_TYPE_JCB = 'jcb';
/** @var string the CarteBleue card type ID **/
const CARD_TYPE_CARTEBLEUE = 'cartebleue';
/** @var string the Maestro card type ID **/
const CARD_TYPE_MAESTRO = 'maestro';
/** @var string the Laser card type ID **/
const CARD_TYPE_LASER = 'laser';
/**
* Perform standard luhn check. Algorithm:
*
* 1. Double the value of every second digit beginning with the second-last right-hand digit.
* 2. Add the individual digits comprising the products obtained in step 1 to each of the other digits in the original number.
* 3. Subtract the total obtained in step 2 from the next higher number ending in 0.
* 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.
*
* @since 3.0.0
* @param string $account_number the credit card number to check
* @return bool true if $account_number passes the check, false otherwise
*/
public static function luhn_check( $account_number ) {
for ( $sum = 0, $i = 0, $ix = strlen( $account_number ); $i < $ix - 1; $i++) {
$weight = substr( $account_number, $ix - ( $i + 2 ), 1 ) * ( 2 - ( $i % 2 ) );
$sum += $weight < 10 ? $weight : $weight - 9;
}
return substr( $account_number, $ix - 1 ) == ( ( 10 - $sum % 10 ) % 10 );
}
/**
* Normalize a card type to a standard type ID and account for variations.
*
* @since 4.5.0
* @param string $card_type the card type to normalize
* @return string
*/
public static function normalize_card_type( $card_type ) {
$card_types = self::get_card_types();
$card_type = strtolower( $card_type );
// stop here if the provided card type is already normalized
if ( in_array( $card_type, array_keys( $card_types ) ) ) {
return $card_type;
}
$variations = wp_list_pluck( $card_types, 'variations' );
// if the provided card type matches a known variation, return the normalized card type
foreach ( $variations as $valid_type => $vars ) {
if ( in_array( $card_type, $vars ) ) {
$card_type = $valid_type;
break;
}
}
// otherwise, let it through unaltered
return $card_type;
}
/**
* Determines the credit card type from a given account number (only first 4 required).
*
* @since 4.0.0
*
* @param string $account_number the credit card account number
* @return string the credit card type
*/
public static function card_type_from_account_number( $account_number ) {
// card type regex patterns from https://github.com/stripe/jquery.payment/blob/master/src/jquery.payment.coffee
$types = array(
self::CARD_TYPE_VISA => '/^4/',
self::CARD_TYPE_MASTERCARD => '/^(5[1-5]|2[2-7])/',
self::CARD_TYPE_AMEX => '/^3[47]/',
self::CARD_TYPE_DINERSCLUB => '/^(36|38|30[0-5])/',
self::CARD_TYPE_DISCOVER => '/^(6011|65|64[4-9]|622)/',
self::CARD_TYPE_JCB => '/^35/',
self::CARD_TYPE_MAESTRO => '/^(5018|5020|5038|6304|6759|676[1-3])/',
self::CARD_TYPE_LASER => '/^(6706|6771|6709)/',
);
foreach ( $types as $type => $pattern ) {
if ( 1 === preg_match( $pattern, $account_number ) ) {
return $type;
}
}
return null;
}
/**
* Translates a credit card type or bank account name to a full name,
* e.g. 'mastercard' => 'MasterCard' or 'savings' => 'eCheck'
*
* @since 4.0.0
* @param string $payment_type the credit card or bank type, ie 'mastercard', 'amex', 'checking'
* @return string the credit card or bank account name, ie 'MasterCard', 'American Express', 'Checking Account'
*/
public static function payment_type_to_name( $payment_type ) {
// normalize for backwards compatibility with gateways that pass the card type directly from \SV_WC_Payment_Gateway::get_card_types()
$type = self::normalize_card_type( $payment_type );
// known payment type names, excluding credit cards
$payment_types = array(
'paypal' => esc_html__( 'PayPal', 'woocommerce-plugin-framework' ),
'checking' => esc_html__( 'Checking Account', 'woocommerce-plugin-framework' ),
'savings' => esc_html__( 'Savings Account', 'woocommerce-plugin-framework' ),
'card' => esc_html__( 'Credit / Debit Card', 'woocommerce-plugin-framework' ),
'bank' => esc_html__( 'Bank Account', 'woocommerce-plugin-framework' ),
);
// add the credit card names
$payment_types = array_merge( wp_list_pluck( self::get_card_types(), 'name' ), $payment_types );
if ( isset( $payment_types[ $type ] ) ) {
$name = $payment_types[ $type ];
} elseif ( '' === $type ) {
$name = esc_html_x( 'Account', 'payment method type', 'woocommerce-plugin-framework' );
} else {
$name = ucwords( str_replace( '-', ' ', $type ) );
}
/**
* Payment Gateway Type to Name Filter.
*
* Allow actors to modify the name returned given a payment type.
*
* @since 4.0.0
* @param string $name nice payment type name, e.g. American Express
* @param string $type payment type, e.g. amex
*/
return apply_filters( 'wc_payment_gateway_payment_type_to_name', $name, $type );
}
/**
* Gets the known card types and their variations.
*
* Returns the card types in the format:
*
* 'mastercard' {
* 'name' => 'MasterCard',
* 'variations' => array( 'mc' ),
* }
*
* @since 4.5.0
*
* @return array
*/
public static function get_card_types() {
return array(
self::CARD_TYPE_VISA => array(
'name' => esc_html_x( 'Visa', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array(),
),
self::CARD_TYPE_MASTERCARD => array(
'name' => esc_html_x( 'MasterCard', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array( 'mc', 'master-card' ),
),
self::CARD_TYPE_AMEX => array(
'name' => esc_html_x( 'American Express', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array( 'americanexpress' ),
),
self::CARD_TYPE_DINERSCLUB => array(
'name' => esc_html_x( 'Diners Club', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array( 'diners' ),
),
self::CARD_TYPE_DISCOVER => array(
'name' => esc_html_x( 'Discover', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array( 'disc' ),
),
self::CARD_TYPE_JCB => array(
'name' => esc_html_x( 'JCB', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array(),
),
self::CARD_TYPE_CARTEBLEUE => array(
'name' => esc_html_x( 'CarteBleue', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array(),
),
self::CARD_TYPE_MAESTRO => array(
'name' => esc_html_x( 'Maestro', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array(),
),
self::CARD_TYPE_LASER => array(
'name' => esc_html_x( 'Laser', 'credit card type', 'woocommerce-plugin-framework' ),
'variations' => array(),
),
);
}
/**
* Formats the given expiration year to include the last two digits only.
*
* @since 5.9.0
*
* @param string $exp_year a credit card expiration year
* @return string
*/
public static function format_exp_year( $exp_year ) {
return substr( $exp_year, -2 );
}
}
endif;