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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
<?php
defined( 'ABSPATH' ) || exit();
class WC_Payment_Gateway_Stripe_CC extends WC_Payment_Gateway_Stripe {
use WC_Stripe_Payment_Intent_Trait;
protected $payment_method_type = 'card';
public function __construct() {
$this->id = 'stripe_cc';
$this->tab_title = __( 'Credit Cards', 'woo-stripe-payment' );
$this->template_name = 'credit-card.php';
$this->token_type = 'Stripe_CC';
$this->method_title = __( 'Stripe Credit Cards', 'woo-stripe-payment' );
$this->method_description = __( 'Credit card gateway that integrates with your Stripe account.', 'woo-stripe-payment' );
parent::__construct();
}
public function get_icon() {
$cards = $this->get_option( 'cards', array() );
$icons = array();
foreach ( $cards as $card ) {
$icons[ $card ] = stripe_wc()->assets_url( "img/cards/{$card}.svg" );
}
return wc_stripe_get_template_html(
'card-icons.php',
apply_filters( 'wc_stripe_cc_icon_template_args', array(
'cards' => $cards,
'icons' => $icons,
'assets_url' => stripe_wc()->assets_url()
), $this )
);
}
public function enqueue_checkout_scripts( $scripts ) {
$scripts->enqueue_script(
'credit-card',
$scripts->assets_url( 'js/frontend/credit-card.js' ),
array(
$scripts->prefix . 'external',
$scripts->prefix . 'wc-stripe',
)
);
$scripts->localize_script( 'credit-card', $this->get_localized_params() );
}
public function get_localized_params() {
$data = parent::get_localized_params();
return array_merge(
$data,
array(
'cardOptions' => $this->get_card_form_options(),
'customFieldOptions' => $this->get_card_custom_field_options(),
'custom_form' => $this->is_custom_form_active(),
'html' => array( 'card_brand' => sprintf( '<img id="wc-stripe-card" src="%s" />', $this->get_custom_form()['cardBrand'] ) ),
'cards' => array(
'visa' => stripe_wc()->assets_url( 'img/cards/visa.svg' ),
'amex' => stripe_wc()->assets_url( 'img/cards/amex.svg' ),
'mastercard' => stripe_wc()->assets_url( 'img/cards/mastercard.svg' ),
'discover' => stripe_wc()->assets_url( 'img/cards/discover.svg' ),
'diners' => stripe_wc()->assets_url( 'img/cards/diners.svg' ),
'jcb' => stripe_wc()->assets_url( 'img/cards/jcb.svg' ),
'unionpay' => stripe_wc()->assets_url( 'img/cards/china_union_pay.svg' ),
'unknown' => $this->get_custom_form()['cardBrand'],
),
'postal_regex' => $this->get_postal_code_regex(),
)
);
}
public function get_card_form_options() {
$options = array(
'style' => $this->get_form_style()
);
return apply_filters( 'wc_stripe_cc_form_options', $options, $this );
}
public function get_card_custom_field_options() {
$style = $this->get_form_style();
$options = array();
foreach ( [ 'cardNumber', 'cardExpiry', 'cardCvc' ] as $key ) {
$options[ $key ] = array( 'style' => $style );
}
return apply_filters( 'wc_stripe_get_card_custom_field_options', $options, $this );
}
public function get_form_style() {
if ( $this->is_custom_form_active() ) {
$style = $this->get_custom_form()['elementStyles'];
} else {
$style = array(
'base' => array(
'color' => '#32325d',
'fontFamily' => '"Helvetica Neue", Helvetica, sans-serif',
'fontSmoothing' => 'antialiased',
'fontSize' => '18px',
'::placeholder' => array( 'color' => '#aab7c4' ),
':focus' => array(),
),
'invalid' => array(
'color' => '#fa755a',
'iconColor' => '#fa755a',
),
);
}
return apply_filters( 'wc_stripe_cc_element_style', $style, $this );
}
public function get_custom_form() {
return wc_stripe_get_custom_forms()[ $this->get_option( 'custom_form' ) ];
}
public function get_element_options( $options = array() ) {
if ( $this->is_custom_form_active() ) {
return parent::get_element_options( $this->get_custom_form()['elementOptions'] );
}
return parent::get_element_options();
}
public function is_custom_form_active() {
return $this->get_option( 'form_type' ) === 'custom';
}
public function get_custom_form_template() {
$form = $this->get_option( 'custom_form' );
return wc_stripe_get_custom_forms()[ $form ]['template'];
}
public function postal_enabled() {
if ( is_checkout() ) {
return $this->is_active( 'postal_enabled' );
}
if ( is_add_payment_method_page() ) {
return true;
}
}
public function cvv_enabled() {
return $this->is_active( 'cvv_enabled' );
}
public function get_postal_code_regex() {
return array(
'AT' => '^([0-9]{4})$',
'BR' => '^([0-9]{5})([-])?([0-9]{3})$',
'CH' => '^([0-9]{4})$',
'DE' => '^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$',
'ES' => '^([0-9]{5})$',
'FR' => '^([0-9]{5})$',
'IT' => '^([0-9]{5})$/i',
'IE' => '([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}',
'JP' => '^([0-9]{3})([-])([0-9]{4})$',
'PT' => '^([0-9]{4})([-])([0-9]{3})$',
'US' => '^([0-9]{5})(-[0-9]{4})?$',
'CA' => '^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$',
'PL' => '^([0-9]{2})([-])([0-9]{3})',
'CZ' => '^([0-9]{3})(\s?)([0-9]{2})$',
'SK' => '^([0-9]{3})(\s?)([0-9]{2})$',
'NL' => '^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$',
);
}
public function add_stripe_order_args( &$args, $order ) {
if ( $this->is_active( 'force_3d_secure' ) && is_checkout() && ! doing_action( 'woocommerce_scheduled_subscription_payment_' . $this->id ) ) {
$args['payment_method_options']['card']['request_three_d_secure'] = 'any';
}
}
public function has_enqueued_scripts( $scripts ) {
return wp_script_is( $scripts->get_handle( 'credit-card' ) );
}
public function show_save_source() {
$page = wc_stripe_get_current_page();
if ( 'checkout' === $page ) {
if ( wcs_stripe_active() ) {
if ( WC_Subscriptions_Cart::cart_contains_subscription() ) {
return false;
}
if ( wcs_cart_contains_renewal() ) {
return false;
}
}
if ( wc_stripe_pre_orders_active() && WC_Pre_Orders_Cart::cart_contains_pre_order() ) {
return ! WC_Pre_Orders_Product::product_is_charged_upon_release( WC_Pre_Orders_Cart::get_pre_order_product() );
}
return apply_filters( 'wc_stripe_cc_show_save_source', $this->is_active( 'save_card_enabled' ) );
} elseif ( in_array( $page, array( 'add_payment_method', 'change_payment_method' ) ) ) {
return false;
} elseif ( 'order_pay' === $page ) {
return is_user_logged_in() && $this->is_active( 'save_card_enabled' );
}
}
public function process_zero_total_order( $order ) {
if ( defined( WC_Stripe_Constants::PROCESSING_PAYMENT ) ) {
$result = $this->save_payment_method( $this->get_new_source_token(), $order );
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
return $this->get_order_error();
}
$order->delete_meta_data( WC_Stripe_Constants::SETUP_INTENT_ID );
} else {
$setup_intent = $this->get_payment_intent_id();
$save_payment_method = $this->should_save_payment_method( $order );
if ( $setup_intent && $save_payment_method ) {
$result = $this->save_payment_method( $this->get_new_source_token(), $order );
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
return $this->get_order_error();
}
} elseif ( ! $setup_intent && $save_payment_method ) {
$result = $this->does_order_require_action( $order, $this->get_new_source_token() );
if ( is_wp_error( $result ) ) {
wc_add_notice( sprintf( __( 'Error processing payment. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ), 'error' );
return $this->get_order_error();
} elseif ( $result ) {
return $result;
}
} else {
$this->payment_method_token = $this->get_saved_source_id();
}
}
return $this->payment_object->process_zero_total_order( $order, $this );
}
private function does_order_require_action( $order, $payment_method ) {
$customer_id = wc_stripe_get_customer_id( $order->get_customer_id() );
if ( ( $intent_id = $order->get_meta( WC_Stripe_Constants::SETUP_INTENT_ID ) ) ) {
$intent = $this->gateway->setupIntents->update( $intent_id, array( 'payment_method' => $payment_method ) );
} else {
$params = array(
'confirm' => true,
'customer' => $customer_id,
'payment_method' => $payment_method,
'usage' => 'off_session',
'metadata' => array(
'gateway_id' => $this->id,
'order_id' => $order->get_id()
)
);
$this->add_stripe_order_args( $params, $order );
$intent = $this->payment_object->get_gateway()->setupIntents->create( apply_filters( 'wc_stripe_setup_intent_params', $params, $order, $this ) );
}
if ( is_wp_error( $intent ) ) {
return $intent;
}
$order->update_meta_data( WC_Stripe_Constants::SETUP_INTENT_ID, $intent->id );
$order->save();
if ( in_array( $intent->status, array(
'requires_action',
'requires_payment_method',
'requires_source_action',
'requires_source',
'requires_confirmation'
), true ) ) {
return array(
'result' => 'success',
'redirect' => $this->get_payment_intent_checkout_url( $intent, $order, 'setup_intent' ),
);
} elseif ( $intent->status === 'succeeded' ) {
$payment_method->payment_method_token = $intent->payment_method;
$order->delete_meta_data( WC_Stripe_Constants::SETUP_INTENT_ID );
return false;
}
}
}