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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Braintree_Payment_Gateway' ) ) {
return;
}
class WC_Braintree_CC_Payment_Gateway extends WC_Braintree_Payment_Gateway {
public $_3ds_nonce_key;
protected $other_plugin_ids = array( 'braintree_credit_card' );
public static $threeds_vaulted_nonce_name = '';
public function __construct() {
$this->id = 'braintree_cc';
$this->deprecated_id = 'braintree_payment_gateway';
$this->template = 'credit-card.php';
$this->token_type = 'CC';
$this->method_title = __( 'Braintree CC Gateway', 'woo-payment-gateway' );
$this->tab_title = __( 'Credit Cards', 'woo-payment-gateway' );
$this->method_description = __( 'Credit card gateway that integrates with your Braintree account', 'woo-payment-gateway' );
$this->_3ds_nonce_key = $this->id . '_3ds_nonce_key';
parent::__construct();
}
public function add_hooks() {
add_action( 'wc_braintree_localize_' . $this->id . '_settings', array( $this, 'get_admin_localized_params' ) );
add_filter( 'wc_braintree_order_transaction_args', array( $this, 'add_transaction_attributes' ), 10, 3 );
add_filter( 'woocommerce_payment_methods_list_item', array( $this, 'payment_methods_list_item' ), 10, 2 );
add_action( 'woocommerce_after_checkout_validation', array( $this, 'initialize_3ds' ) );
add_filter( 'wcs_braintree_add_payment_method_args', array( $this, 'wcs_add_payment_method_args' ), 10, 2 );
parent::add_hooks();
}
public function get_icon() {
$methods = $this->get_option( 'payment_methods' );
return wc_braintree_get_template_html(
'card-icons.php',
array(
'payment_methods' => empty( $methods ) ? array() : $methods,
'type' => $this->get_option( 'icon_style' ),
)
);
}
public function enqueue_checkout_scripts( $scripts ) {
if ( $this->get_option( 'form_type' ) === 'custom_form' ) {
$scripts->enqueue_script(
'hosted-fields',
$scripts->assets_url( 'js/frontend/credit-cards.js' ),
array(
'jquery',
$scripts->get_handle( 'client-manager' ),
$scripts->get_handle( 'hosted-fields-v3' ),
$scripts->get_handle( 'data-collector-v3' ),
$scripts->get_handle( '3ds-v3' ),
)
);
$form_name = $this->get_option( 'custom_form_design' );
$form = wc_braintree_get_custom_form( $form_name );
$scripts->enqueue_script( $form_name, $form['javascript'], array( $scripts->get_handle( 'hosted-fields' ) ) );
$scripts->enqueue_style( $form_name, $form['css'] );
$scripts->localize_script( 'hosted-fields', $this->localize_form_params() );
} else {
$scripts->enqueue_script(
'dropin-v3',
$scripts->assets_url( 'js/frontend/dropin-v3.js' ),
array(
'jquery',
$scripts->get_handle( 'client-manager' ),
$scripts->get_handle( 'dropin-v3-ext' ),
$scripts->get_handle( 'data-collector-v3' ),
$scripts->get_handle( '3ds-v3' ),
)
);
$scripts->localize_script( 'dropin-v3', $this->localize_form_params() );
}
}
public function localize_form_params() {
$path = braintree()->assets_path() . 'img/';
$icon_type = $this->get_option( 'icon_style' );
$data = array_merge(
$this->get_localized_standard_params(),
array(
'_3ds' => array( 'verify_vault' => $this->is_active( '3ds_enable_payment_token' ) ),
'custom_fields' => $this->get_custom_form_fields(),
'dynamic_card_display' => array( 'enabled' => $this->is_active( 'dynamic_card_display' ) ),
'form_styles' => $this->get_hosted_form_styles(),
'loader' => array( 'enabled' => $this->is_active( 'loader_enabled' ) ),
'icon_style' => $icon_type,
'html' => array(
'cards' => array(
'visa' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/visa.svg" ),
'master-card' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/master_card.svg" ),
'american-express' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/amex.svg" ),
'discover' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/discover.svg" ),
'diners-club' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/diners_club_international.svg" ),
'jcb' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/jcb.svg" ),
'maestro' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/maestro.svg" ),
'unionpay' => sprintf( '<img src="%s"/>', $path . "payment-methods/{$icon_type}/china_union_pay.svg" ),
),
),
'config_selector' => $this->config_key,
'_3ds_vaulted_nonce_selector' => $this->_3ds_nonce_key,
'urls' => array(
'_3ds' => array(
'vaulted_nonce' => braintree()->rest_api->_3ds->rest_url() . 'vaulted_nonce',
'client_token' => braintree()->rest_api->_3ds->rest_url() . 'client_token',
),
),
)
);
return $data;
}
public function add_transaction_attributes( $args, $order, $gateway_id ) {
if ( $gateway_id === $this->id ) {
if ( ! $this->use_saved_method() && ! $this->use_3ds_vaulted_nonce() ) {
$args['creditCard'] = array(
'cardholderName' => sprintf( '%1$s %2$s', $order->get_billing_first_name(), $order->get_billing_last_name() )
);
}
if ( ! $this->use_saved_method() ) {
if ( $this->get_option( 'form_type' ) === 'custom_form' ) {
if ( $this->is_active( 'postal_field_enabled' ) ) {
unset( $args['billing']['postalCode'] );
}
} else {
if ( in_array( 'postal_code', $this->get_config_data()['challenges'] ) ) {
if ( $this->is_active( 'dropin_postal_enabled' ) ) {
unset( $args['billing']['postalCode'] );
}
}
}
}
}
return $args;
}
public function use_saved_method() {
if ( $this->_3ds_active() && $this->use_3ds_vaulted_nonce() ) {
return false;
}
return parent::use_saved_method();
}
public function use_3ds_vaulted_nonce() {
return ! empty( $_POST[ $this->_3ds_nonce_key ] ) && wc_clean( $_POST[ $this->_3ds_nonce_key ] ) === 'true';
}
public function payment_methods_list_item( $item, $payment_token ) {
if ( 'Braintree_CC' !== $payment_token->get_type() ) {
return $item;
}
$card_type = $payment_token->get_card_type();
$item['method']['last4'] = $payment_token->get_last4();
$item['method']['brand'] = ( ! empty( $card_type ) ? $card_type : esc_html__( 'Credit card', 'woocommerce' ) );
$item['expires'] = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), - 2 );
$item['method_type'] = $payment_token->get_method_type();
$item['wc_braintree_method'] = true;
return $item;
}
public function initialize_3ds( $data ) {
if ( isset( $data['payment_method'] ) && $data['payment_method'] === $this->id && $this->_3ds_active() && $this->_3ds_validation_active() ) {
require_once braintree()->plugin_path() . 'includes/class-wc-braintree-3ds-validation.php';
try {
$id = ! empty( $_POST[ $this->nonce_key ] ) ? wc_clean( $_POST[ $this->nonce_key ] ) : null;
if ( $id ) {
$nonce = $this->gateway->paymentMethodNonce()->find( $id );
new WC_Braintree_3ds_Validation( $nonce, $this );
}
} catch ( \Braintree\Exception $e ) {
wc_add_notice( sprintf( __( 'There was an error processing your payment. Reason: %1$s', 'woo-payment-gateway' ),
wc_braintree_errors_from_object( $e ) ), 'error' );
}
}
}
public function _3ds_enabled() {
if ( is_add_payment_method_page() ) {
return false;
}
return $this->is_active( '3ds_enabled' );
}
public function _3ds_active() {
return $this->_3ds_enabled() && wc_braintree_evaluate_condition( $this->get_option( '3ds_conditions' ) );
}
public function _3ds_validation_active() {
return $this->get_option( '3ds_liability_not_shifted' ) !== 'no_action' || $this->get_option( '3ds_card_ineligible' ) !== 'no_action';
}
public function _3ds_authorize_order( $args, $order ) {
$args['options']['submitForSettlement'] = false;
$args['options']['threeDSecure'] = array( 'required' => false );
return $args;
}
public function _3ds_reject_order() {
wc_add_notice( __( 'Your payment method could not be processed at this time. Reason: 3D Secure not accepted or validation failed.',
'woo-payment-gateway' ), 'error' );
}
public function output_display_items( $page = '', $data = array() ) {
parent::output_display_items( $page, array(
'_3ds' => array(
'enabled' => $this->_3ds_enabled(),
'active' => $this->_3ds_active()
)
) );
}
public function wcs_add_payment_method_args( $args, $gateway_id ) {
if ( $gateway_id === $this->id ) {
if ( $this->is_custom_form_active() ) {
if ( ! $this->is_active( 'postal_field_enabled' ) && ! empty( $_POST['billing_postcode'] ) ) {
$args['billingAddress']['postalCode'] = wc_clean( $_POST['billing_postcode'] );
}
} else {
if ( ! $this->is_active( 'dropin_postal_enabled' ) && ! empty( $_POST['billing_postcode'] ) ) {
$args['billingAddress']['postalCode'] = wc_clean( $_POST['billing_postcode'] );
}
}
}
return $args;
}
public function is_custom_form_active() {
return $this->get_option( 'form_type' ) === 'custom_form';
}
public function is_postal_code_enabled() {
global $wp;
if ( is_checkout() && empty( $wp->query_vars['order-pay'] ) ) {
return $this->is_active( 'postal_field_enabled' );
} else {
return true;
}
}
public function should_display_street() {
if ( $this->is_active( 'street_enabled' ) ) {
$bool = is_add_payment_method_page() || ( wcs_braintree_active() && WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment ) ||
( wc_braintree_subscriptions_active() && wcs_braintree_is_change_payment_method_request() );
} else {
$bool = false;
}
return apply_filters( 'wc_braintree_can_display_street', $bool, $this );
}
public function get_custom_form_fields() {
$fields = array(
'number' => array(
'label' => __( 'Card Number', 'woo-payment-gateway' ),
'placeholder' => __( 'Card Number', 'woo-payment-gateway' ),
'id' => 'wc-braintree-card-number',
'type' => 'number',
),
'exp_date' => array(
'label' => __( 'Exp Date', 'woo-payment-gateway' ),
'placeholder' => __( 'MM / YY', 'woo-payment-gateway' ),
'id' => 'wc-braintree-expiration-date',
'type' => 'expirationDate',
),
'exp_month' => array(
'label' => __( 'Exp Month', 'woo-payment-gateway' ),
'placeholder' => __( 'MM', 'woo-payment-gateway' ),
'id' => 'wc-braintree-expiration-month',
'type' => 'expirationMonth',
),
'exp_year' => array(
'label' => __( 'Exp Year', 'woo-payment-gateway' ),
'placeholder' => __( 'YY', 'woo-payment-gateway' ),
'id' => 'wc-braintree-expiration-year',
'type' => 'expirationYear',
),
'cvv' => array(
'label' => __( 'CVV', 'woo-payment-gateway' ),
'placeholder' => __( 'CVV', 'woo-payment-gateway' ),
'id' => 'wc-braintree-cvv',
'type' => 'cvv',
),
'postal_code' => array(
'label' => __( 'Postal Code', 'woo-payment-gateway' ),
'placeholder' => __( 'Postal Code', 'woo-payment-gateway' ),
'id' => 'wc-braintree-postal-code',
'type' => 'postalCode',
),
'save' => array( 'label' => __( 'Save Card', 'woo-payment-gateway' ) ),
'street' => array( 'label' => __( 'Street Address', 'woo-payment-gateway' ) ),
);
return apply_filters( 'wc_braintree_get_custom_form_fields', $fields, $this );
}
public function get_payment_method_from_transaction( $transaction ) {
return $transaction->creditCardDetails;
}
protected function get_hosted_form_styles() {
$styles = $this->get_option( 'custom_form_styles' );
if ( is_string( $styles ) ) {
$styles = json_decode( $styles, true );
if ( ! $styles ) {
$styles = $this->form_fields['custom_form_styles']['default'];
}
}
return apply_filters( 'wc_braintree_get_hosted_form_styles', $styles, $this );
}
public function get_localized_params() {
return array_merge( $this->settings, array(
'style_options' => array(
'appearance',
'color',
'direction',
'font-family',
'font-size',
'font-stretch',
'font-style',
'font-weight',
'font letter-spacing',
'line-height',
'opacity',
'margin',
'padding',
'text-shadow'
)
) );
}
public function has_enqueued_scripts( $scripts, $context = 'checkout' ) {
switch ( $context ) {
case 'checkout':
return wp_script_is( $scripts->get_handle( 'hosted-fields' ) ) || wp_script_is( $scripts->get_handle( 'dropin-v3' ) );
}
}
}