woocommerce /
woocommerce-gateway-stripe
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 3 | exit; |
||
| 4 | } |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Provides static methods as helpers. |
||
| 8 | * |
||
| 9 | * @since 4.0.0 |
||
| 10 | */ |
||
| 11 | class WC_Stripe_Helper { |
||
| 12 | /** |
||
| 13 | * Get Stripe amount to pay |
||
| 14 | * |
||
| 15 | * @param float $total Amount due. |
||
| 16 | * @param string $currency Accepted currency. |
||
| 17 | * |
||
| 18 | * @return float|int |
||
| 19 | */ |
||
| 20 | public static function get_stripe_amount( $total, $currency = '' ) { |
||
| 21 | if ( ! $currency ) { |
||
| 22 | $currency = get_woocommerce_currency(); |
||
| 23 | } |
||
| 24 | |||
| 25 | if ( in_array( strtolower( $currency ), self::no_decimal_currencies() ) ) { |
||
| 26 | return absint( $total ); |
||
| 27 | } else { |
||
| 28 | return absint( wc_format_decimal( ( (float) $total * 100 ), wc_get_price_decimals() ) ); // In cents. |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Localize Stripe messages based on code |
||
| 34 | * |
||
| 35 | * @since 3.0.6 |
||
| 36 | * @version 3.0.6 |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public static function get_localized_messages() { |
||
| 40 | return apply_filters( 'wc_stripe_localized_messages', array( |
||
| 41 | 'invalid_number' => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ), |
||
| 42 | 'invalid_expiry_month' => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ), |
||
| 43 | 'invalid_expiry_year' => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ), |
||
| 44 | 'invalid_cvc' => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ), |
||
| 45 | 'incorrect_number' => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ), |
||
| 46 | 'expired_card' => __( 'The card has expired.', 'woocommerce-gateway-stripe' ), |
||
| 47 | 'incorrect_cvc' => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ), |
||
| 48 | 'incorrect_zip' => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ), |
||
| 49 | 'card_declined' => __( 'The card was declined.', 'woocommerce-gateway-stripe' ), |
||
| 50 | 'missing' => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ), |
||
| 51 | 'processing_error' => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ), |
||
| 52 | 'invalid_request_error' => __( 'Could not find payment information. Please try with another payment method.', 'woocommerce-gateway-stripe' ), |
||
| 53 | ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * List of currencies supported by Stripe that has no decimals. |
||
| 58 | * |
||
| 59 | * @return array $currencies |
||
| 60 | */ |
||
| 61 | public static function no_decimal_currencies() { |
||
| 62 | return array( |
||
| 63 | 'bif', // Burundian Franc |
||
| 64 | 'djf', // Djiboutian Franc |
||
| 65 | 'jpy', // Japanese Yen |
||
| 66 | 'krw', // South Korean Won |
||
| 67 | 'pyg', // Paraguayan Guaraní |
||
| 68 | 'vnd', // Vietnamese Đồng |
||
| 69 | 'xaf', // Central African Cfa Franc |
||
| 70 | 'xpf', // Cfp Franc |
||
| 71 | 'clp', // Chilean Peso |
||
| 72 | 'gnf', // Guinean Franc |
||
| 73 | 'kmf', // Comorian Franc |
||
| 74 | 'mga', // Malagasy Ariary |
||
| 75 | 'rwf', // Rwandan Franc |
||
| 76 | 'vuv', // Vanuatu Vatu |
||
| 77 | 'xof', // West African Cfa Franc |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Stripe uses smallest denomination in currencies such as cents. |
||
| 83 | * We need to format the returned currency from Stripe into human readable form. |
||
| 84 | * The amount is not used in any calculations so returning string is sufficient. |
||
| 85 | * |
||
| 86 | * @param object $balance_transaction |
||
| 87 | * @param string $type Type of number to format |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public static function format_balance_fee( $balance_transaction, $type = 'fee' ) { |
||
| 91 | if ( ! is_object( $balance_transaction ) ) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | if ( in_array( strtolower( $balance_transaction->currency ), self::no_decimal_currencies() ) ) { |
||
| 96 | if ( 'fee' === $type ) { |
||
| 97 | return $balance_transaction->fee; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $balance_transaction->net; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ( 'fee' === $type ) { |
||
| 104 | return number_format( $balance_transaction->fee / 100, 2, '.', '' ); |
||
| 105 | } |
||
| 106 | |||
| 107 | return number_format( $balance_transaction->net / 100, 2, '.', '' ); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Checks Stripe minimum order value authorized per currency |
||
| 112 | */ |
||
| 113 | public static function get_minimum_amount() { |
||
| 114 | // Check order amount |
||
| 115 | switch ( get_woocommerce_currency() ) { |
||
| 116 | case 'USD': |
||
| 117 | case 'CAD': |
||
| 118 | case 'EUR': |
||
| 119 | case 'CHF': |
||
| 120 | case 'AUD': |
||
| 121 | case 'SGD': |
||
| 122 | $minimum_amount = 50; |
||
| 123 | break; |
||
| 124 | case 'GBP': |
||
| 125 | $minimum_amount = 30; |
||
| 126 | break; |
||
| 127 | case 'DKK': |
||
| 128 | $minimum_amount = 250; |
||
| 129 | break; |
||
| 130 | case 'NOK': |
||
| 131 | case 'SEK': |
||
| 132 | $minimum_amount = 300; |
||
| 133 | break; |
||
| 134 | case 'JPY': |
||
| 135 | $minimum_amount = 5000; |
||
| 136 | break; |
||
| 137 | case 'MXN': |
||
| 138 | $minimum_amount = 1000; |
||
| 139 | break; |
||
| 140 | case 'HKD': |
||
| 141 | $minimum_amount = 400; |
||
| 142 | break; |
||
| 143 | default: |
||
| 144 | $minimum_amount = 50; |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | |||
| 148 | return $minimum_amount; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets all the saved setting options from a specific method. |
||
| 153 | * If specific setting is passed, only return that. |
||
| 154 | * |
||
| 155 | * @since 4.0.0 |
||
| 156 | * @version 4.0.0 |
||
| 157 | * @param string $method The payment method to get the settings from. |
||
| 158 | * @param string $setting The name of the setting to get. |
||
| 159 | */ |
||
| 160 | public static function get_settings( $method = null, $setting = null ) { |
||
| 161 | $all_settings = null === $method ? get_option( 'woocommerce_stripe_settings', array() ) : get_option( 'woocommerce_stripe_' . $method . '_settings', array() ); |
||
| 162 | |||
| 163 | if ( null === $setting ) { |
||
| 164 | return $all_settings; |
||
| 165 | } |
||
| 166 | |||
| 167 | return isset( $all_settings[ $setting ] ) ? $all_settings[ $setting ] : ''; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if WC version is pre 3.0. |
||
| 172 | * |
||
| 173 | * @since 4.0.0 |
||
| 174 | * @version 4.0.0 |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public static function is_pre_30() { |
||
| 178 | return version_compare( WC_VERSION, '3.0.0', '<' ); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the webhook URL for Stripe triggers. Used mainly for |
||
| 183 | * asyncronous redirect payment methods in which statuses are |
||
| 184 | * not immediately chargeable. |
||
| 185 | * |
||
| 186 | * @since 4.0.0 |
||
| 187 | * @version 4.0.0 |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public static function get_webhook_url() { |
||
| 191 | return add_query_arg( 'wc-api', 'wc_stripe', trailingslashit( get_home_url() ) ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the order by Stripe source ID. |
||
| 196 | * |
||
| 197 | * @since 4.0.0 |
||
| 198 | * @version 4.0.0 |
||
| 199 | * @param string $source_id |
||
| 200 | */ |
||
| 201 | View Code Duplication | public static function get_order_by_source_id( $source_id ) { |
|
|
0 ignored issues
–
show
|
|||
| 202 | global $wpdb; |
||
| 203 | |||
| 204 | $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s", $source_id ) ); |
||
| 205 | |||
| 206 | if ( ! empty( $order_id ) ) { |
||
| 207 | return wc_get_order( $order_id ); |
||
| 208 | } |
||
| 209 | |||
| 210 | return false; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the order by Stripe charge ID. |
||
| 215 | * |
||
| 216 | * @since 4.0.0 |
||
| 217 | * @version 4.0.0 |
||
| 218 | * @param string $charge_id |
||
| 219 | */ |
||
| 220 | View Code Duplication | public static function get_order_by_charge_id( $charge_id ) { |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 221 | global $wpdb; |
||
| 222 | |||
| 223 | $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s", $charge_id ) ); |
||
| 224 | |||
| 225 | if ( ! empty( $order_id ) ) { |
||
| 226 | return wc_get_order( $order_id ); |
||
| 227 | } |
||
| 228 | |||
| 229 | return false; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Sanitize statement descriptor text. |
||
| 234 | * |
||
| 235 | * Stripe requires max of 22 characters and no |
||
| 236 | * special characters with ><"'. |
||
| 237 | * |
||
| 238 | * @since 4.0.0 |
||
| 239 | * @param string $statement_descriptor |
||
| 240 | * @return string $statement_descriptor Sanitized statement descriptor |
||
| 241 | */ |
||
| 242 | public static function clean_statement_descriptor( $statement_descriptor = '' ) { |
||
| 243 | $disallowed_characters = array( '<', '>', '"', "'" ); |
||
| 244 | |||
| 245 | // Remove special characters. |
||
| 246 | $statement_descriptor = str_replace( $disallowed_characters, '', $statement_descriptor ); |
||
| 247 | |||
| 248 | $statement_descriptor = substr( trim( $statement_descriptor ), 0, 22 ); |
||
| 249 | |||
| 250 | return $statement_descriptor; |
||
| 251 | } |
||
| 252 | } |
||
| 253 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.