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 443 444 445 446 447 448 449 450 451 452 453 454 455
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Braintree_Payment_Gateway' ) ) {
return;
}
class WC_Braintree_ApplePay_Payment_Gateway extends WC_Braintree_Payment_Gateway {
protected $has_digital_wallet = true;
private $api_error;
public function __construct() {
$this->id = 'braintree_applepay';
$this->deprecated_id = 'braintree_applepay_payments';
$this->template = 'applepay.php';
$this->token_type = 'ApplePay';
$this->method_title = __( 'Braintree Apple Pay Gateway', 'woo-payment-gateway' );
$this->tab_title = __( 'Apple Pay', 'woo-payment-gateway' );
$this->method_description = __( 'Gateway that integrates Apple Pay with your Braintree account.', 'woo-payment-gateway' );
$this->icon = braintree()->assets_path() . 'img/applepay/apple_pay_mark.svg';
parent::__construct();
}
public function add_hooks() {
parent::add_hooks();
add_filter( 'woocommerce_payment_methods_list_item', array( $this, 'payment_methods_list_item' ), 10, 2 );
add_filter( 'wc_braintree_update_address_error', array( $this, 'update_address_error' ), 10, 3 );
add_filter( 'wc_braintree_mini_cart_deps', array( $this, 'get_mini_cart_dependencies' ), 10, 2 );
}
public function set_supports() {
parent::set_supports();
$this->supports[] = 'wc_braintree_cart_checkout';
$this->supports[] = 'wc_braintree_banner_checkout';
$this->supports[] = 'wc_braintree_product_checkout';
$this->supports[] = 'wc_braintree_mini_cart';
}
public function enqueue_admin_scripts() {
wp_localize_script(
'wc-braintree-admin-settings',
'wc_braintree_applepay_params',
array(
'routes' => array( 'domain_association' => braintree()->rest_api->rest_url() . 'applepay/domain-association' ),
)
);
}
public function get_line_items( $encode = false ) {
return $this->get_display_items();
}
protected function add_recurring_display_items( &$data, $incl_tax = false ) {
}
public function get_shipping_methods() {
return $this->get_formatted_shipping_methods();
}
public function get_localized_standard_params() {
$data = array_merge_recursive(
parent::get_localized_standard_params(),
array(
'store_name' => $this->get_option( 'store_name' ),
'button_html' => wc_braintree_get_template_html(
'applepay-button.php',
array(
'gateway' => $this,
'button' => $this->get_option( 'button' ),
'type' => $this->get_option( 'button_type_checkout' ),
'style' => $this->get_applepay_button_style()
)
),
'banner_enabled' => $this->banner_checkout_enabled(),
'messages' => array(
'errors' => array( 'invalid_administrativeArea' => __( 'Invalid state provided. Use uppercase state abbreviation. Example: California = CA' ) ),
),
'routes' => array( 'applepay_payment_method' => WC_Braintree_Rest_API::get_endpoint( braintree()->rest_api->applepay->rest_uri() . '/payment-method' ) ),
)
);
return $data;
}
public function localize_applepay_params() {
return $this->get_localized_standard_params();
}
public function enqueue_checkout_scripts( $scripts ) {
$scripts->enqueue_script(
'applepay',
$scripts->assets_url( 'js/frontend/applepay.js' ),
array(
$scripts->get_handle( 'client-manager' ),
$scripts->get_handle( 'data-collector-v3' ),
$scripts->get_handle( 'applepay-v3' ),
)
);
$scripts->localize_script( 'applepay', $this->get_localized_standard_params() );
}
public function enqueue_cart_scripts( $scripts ) {
$scripts->enqueue_script(
'applepay-cart',
$scripts->assets_url( 'js/frontend/applepay-cart.js' ),
array(
$scripts->get_handle( 'client-manager' ),
$scripts->get_handle( 'data-collector-v3' ),
$scripts->get_handle( 'applepay-v3' ),
)
);
$scripts->localize_script( 'applepay-cart', $this->get_localized_standard_params() );
}
public function enqueue_product_scripts( $scripts ) {
$scripts->enqueue_script(
'applepay-product',
$scripts->assets_url( 'js/frontend/applepay-product.js' ),
array(
$scripts->get_handle( 'client-manager' ),
$scripts->get_handle( 'data-collector-v3' ),
$scripts->get_handle( 'applepay-v3' ),
)
);
$scripts->localize_script( 'applepay-product', $this->get_localized_standard_params() );
}
public function get_payment_method_from_transaction( $transaction ) {
return $transaction->applePayCardDetails;
}
public function update_shipping_address_response( $data ) {
$data['shippingContactUpdate'] = array(
'newLineItems' => $this->get_display_items(),
'newTotal' => array(
'type' => 'final',
'label' => $this->get_option( 'store_name' ),
'amount' => WC()->cart->total,
),
'newShippingMethods' => $this->get_formatted_shipping_methods(),
);
return $data;
}
public function get_cart_total() {
return WC()->cart->total;
}
public function validate_wallet_address( $address ) {
$states = WC()->countries->get_states( $address['country'] );
$state = isset( $address['state'] ) ? $address['state'] : null;
if ( $states && $state ) {
if ( ! isset( $states[ $state ] ) ) {
$this->set_error_callback(
new WP_Error(
'shippingContactInvalid',
sprintf( __( '%s is not a valid state. Use uppercase state abbreviation. California = CA', 'woo-payment-gateway' ), $state ),
array(
'status' => 200,
'newTotal' => array(
'type' => 'final',
'label' => $this->get_option( 'store_name' ),
'amount' => wc_format_decimal( WC()->cart->total, 2 )
),
'newShippingMethods' => $this->get_formatted_shipping_methods(),
'contactField' => 'administrativeArea',
)
)
);
return false;
}
}
return true;
}
private function set_error_callback( $error ) {
$this->api_error = $error;
add_action( 'rest_request_before_callbacks', array( $this, 'request_before_callbacks' ) );
}
public function request_before_callbacks( $response ) {
return rest_ensure_response( $this->api_error );
}
public function update_address_error( $error, $payment_method, $request ) {
if ( $payment_method === $this->id ) {
if ( $error->get_error_code() === 'shipping-address' ) {
$error = new WP_Error(
'addressUnserviceable',
$error->get_error_message(),
array(
'status' => 200,
'shippingContactUpdate' => array(
'newLineItems' => $this->get_display_items(),
'newTotal' => array(
'type' => 'final',
'label' => $this->get_option( 'store_name' ),
'amount' => wc_format_decimal( WC()->cart->total, 2 ),
),
'newShippingMethods' => $this->get_formatted_shipping_methods(),
),
)
);
}
}
return $error;
}
public function update_shipping_method_response( $data ) {
$data['shippingMethodUpdate'] = array(
'newLineItems' => $this->get_display_items(),
'newTotal' => array(
'type' => 'final',
'label' => $this->get_option( 'store_name' ),
'amount' => wc_format_decimal( WC()->cart->total, 2 )
),
);
return $data;
}
public function payment_methods_list_item( $item, $payment_token ) {
if ( 'Braintree_ApplePay' !== $payment_token->get_type() ) {
return $item;
}
$item['method']['brand'] = $payment_token->get_payment_method_title( $this->get_option( 'method_format' ) );
$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 output_line_items() {
}
public function add_to_cart_response( $data ) {
$data[ $this->id ] = array(
'lineItems' => $this->get_display_items()
);
return $data;
}
public function get_formatted_shipping_methods() {
$methods = parent::get_formatted_shipping_methods();
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods', array() );
$chosen_method_ids = array();
foreach ( $chosen_shipping_methods as $idx => $method_id ) {
$chosen_method_ids[] = $this->get_shipping_method_id( $method_id, $idx );
}
usort( $methods, function ( $a, $b ) use ( $chosen_method_ids ) {
return in_array( $a['identifier'], $chosen_method_ids ) ? - 1 : 1;
} );
return $methods;
}
public function get_formatted_shipping_method( $method, $index, $selected, $amount, $decimals ) {
return array(
'label' => $method->get_label(),
'detail' => '',
'amount' => wc_format_decimal( $amount, $decimals ),
'identifier' => $this->get_shipping_method_id( $method, $index ),
);
}
public function get_display_item_for_cart( $total, $label, $type, $cart, ...$args ) {
return array(
'type' => 'final',
'label' => $label,
'amount' => $total
);
}
public function get_display_item_for_order( $total, $label, $type, $order, ...$args ) {
return array(
'type' => 'final',
'label' => $label,
'amount' => $total
);
}
public function get_display_item_for_product( $product, $decimals = 2 ) {
return array(
'type' => 'final',
'label' => $product->get_name(),
'amount' => wc_format_decimal( $product->get_price(), $decimals )
);
}
public function get_mini_cart_dependencies( $deps, $scripts ) {
if ( $this->mini_cart_enabled() ) {
$deps[] = $scripts->get_handle( 'applepay-v3' );
}
return $deps;
}
public function get_product_admin_options() {
$args = wp_parse_args( array(
'button_type_product' => $this->form_fields['button_type_product'],
'button' => $this->form_fields['button']
), parent::get_product_admin_options() );
$args['button_type_product']['options'] = wp_parse_args( array(
'check-out' => __( 'Checkout With Apple Pay', 'woo-payment-gateway' ),
'subscribe' => __( 'Subscribe With Apple Pay', 'woo-payment-gateway' )
), $args['button_type_product']['options'] );
$args['button_type_product']['default'] = $this->get_option( 'button_type_product' );
$args['button']['description'] = '';
$args['button']['default'] = $this->get_option( 'button' );
return $args;
}
public function has_enqueued_scripts( $scripts, $context = 'checkout' ) {
switch ( $context ) {
case 'checkout':
return wp_script_is( $scripts->get_handle( 'applepay' ) );
}
}
public function get_applepay_button_style() {
$style = $this->get_option( 'button' );
switch ( $style ) {
case 'apple-pay-button-white':
return 'white';
case 'apple-pay-button-white-with-line':
return 'white-outline';
default:
return 'black';
}
}
}