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
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Payment_Gateway_Stripe_Local_Payment' ) ) {
return;
}
class WC_Payment_Gateway_Stripe_Afterpay extends WC_Payment_Gateway_Stripe_Local_Payment {
use WC_Stripe_Local_Payment_Intent_Trait;
protected $payment_method_type = 'afterpay_clearpay';
public function __construct() {
$this->local_payment_type = 'afterpay_clearpay';
$this->currencies = array( 'AUD', 'CAD', 'NZD', 'GBP', 'USD' );
$this->countries = array( 'AU', 'CA', 'NZ', 'GB', 'US' );
$this->id = 'stripe_afterpay';
$this->tab_title = __( 'Afterpay', 'woo-stripe-payment' );
$this->method_title = __( 'Afterpay', 'woo-stripe-payment' );
$this->method_description = __( 'Afterpay gateway that integrates with your Stripe account.', 'woo-stripe-payment' );
$this->icon = '';
parent::__construct();
$this->template_name = 'afterpay.php';
}
public function init_supports() {
parent::init_supports();
$this->supports[] = 'wc_stripe_cart_checkout';
$this->supports[] = 'wc_stripe_product_checkout';
$this->supports[] = 'wc_stripe_mini_cart_checkout';
}
public function get_order_button_text( $text ) {
return __( 'Complete Order', 'woo-stripe-payment' );
}
public function get_local_payment_settings() {
$settings = wp_parse_args( array(
'charge_type' => array(
'type' => 'select',
'title' => __( 'Charge Type', 'woo-stripe-payment' ),
'default' => 'capture',
'class' => 'wc-enhanced-select',
'options' => array(
'capture' => __( 'Capture', 'woo-stripe-payment' ),
'authorize' => __( 'Authorize', 'woo-stripe-payment' ),
),
'desc_tip' => true,
'description' => __( 'This option determines whether the customer\'s funds are captured immediately or authorized and can be captured at a later date.',
'woo-stripe-payment' ),
),
'payment_sections' => array(
'type' => 'multiselect',
'title' => __( 'Payment Sections', 'woo-stripe-payment' ),
'class' => 'wc-enhanced-select',
'options' => array(
'product' => __( 'Product Page', 'woo-stripe-payment' ),
'cart' => __( 'Cart Page', 'woo-stripe-payment' ),
'mini_cart' => __( 'Mini Cart', 'woo-stripe-payment' ),
),
'default' => array( 'product', 'cart' ),
'description' => __( 'These are the additional sections where the Afterpay messaging will be enabled. You can control individual products via the Edit product page.',
'woo-stripe-payment' ),
),
'hide_ineligible' => array(
'title' => __( 'Hide If Ineligible', 'woo-stripe-payment' ),
'type' => 'checkbox',
'value' => 'yes',
'default' => 'no',
'desc_tip' => true,
'description' => __( 'If enabled, Afterpay won\'t show when the products in the cart are not eligible.', 'woo-stripe-payment' )
),
'checkout_styling' => array(
'type' => 'title',
'title' => __( 'Checkout Page Styling', 'woo-stripe-payments' )
),
'icon_checkout' => array(
'title' => __( 'Icon', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'black-on-mint',
'options' => array(
'black-on-mint' => __( 'Black on mint', 'woo-stripe-payment' ),
'black-on-white' => __( 'Black on white', 'woo-stripe-payment' ),
'mint-on-black' => __( 'Mint on black', 'woo-stripe-payment' ),
'white-on-black' => __( 'White on black', 'woo-stripe-payment' )
),
'desc_tip' => true,
'description' => __( 'This is the icon style that appears next to the gateway on the checkout page.', 'woo-stripe-payment' ),
),
'intro_text_checkout' => array(
'title' => __( 'Intro text', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'In',
'options' => array(
'In' => 'In',
'Or' => 'Or',
'Pay' => 'Pay',
'Pay in' => 'Pay in'
)
),
'modal_link_style_checkout' => array(
'title' => __( 'Modal link style', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'circled-info-icon',
'options' => array(
'more-info-text' => __( 'More info text', 'woo-stripe-payment' ),
'circled-info-icon' => __( 'Circled info icon', 'woo-stripe-payment' ),
'learn-more-text' => __( 'Learn more text', 'woo-stripe-payment' ),
),
'description' => __( 'This is the style of the Afterpay info link.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'modal_theme_checkout' => array(
'title' => __( 'Modal link style', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'mint',
'options' => array(
'mint' => __( 'Mint', 'woo-stripe-payment' ),
'white' => __( 'White', 'woo-stripe-payment' )
),
'description' => __( 'This is the theme color for the Afterpay info modal.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'show_interest_free_checkout' => array(
'type' => 'checkbox',
'title' => __( 'Show interest free', 'woo-stripe-payment' ),
'default' => 'no',
'value' => 'yes',
'description' => __( 'If enabled, the Afterpay message will contain the interest free text.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'cart_styling' => array(
'type' => 'title',
'title' => __( 'Cart Page Styling' )
),
'icon_cart' => array(
'title' => __( 'Icon', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'black-on-mint',
'options' => array(
'black-on-mint' => __( 'Black on mint', 'woo-stripe-payment' ),
'black-on-white' => __( 'Black on white', 'woo-stripe-payment' ),
'mint-on-black' => __( 'Mint on black', 'woo-stripe-payment' ),
'white-on-black' => __( 'White on black', 'woo-stripe-payment' )
),
'desc_tip' => true,
'description' => __( 'This is the icon style that appears next to the gateway on the checkout page.', 'woo-stripe-payment' ),
),
'intro_text_cart' => array(
'title' => __( 'Intro text', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'Or',
'options' => array(
'In' => 'In',
'Or' => 'Or',
'Pay' => 'Pay',
'Pay in' => 'Pay in'
)
),
'modal_link_style_cart' => array(
'title' => __( 'Modal link style', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'circled-info-icon',
'options' => array(
'more-info-text' => __( 'More info text', 'woo-stripe-payment' ),
'circled-info-icon' => __( 'Circled info icon', 'woo-stripe-payment' ),
'learn-more-text' => __( 'Learn more text', 'woo-stripe-payment' ),
),
'description' => __( 'This is the style of the Afterpay info link.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'modal_theme_cart' => array(
'title' => __( 'Modal link style', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'mint',
'options' => array(
'mint' => __( 'Mint', 'woo-stripe-payment' ),
'white' => __( 'White', 'woo-stripe-payment' )
),
'description' => __( 'This is the theme color for the Afterpay info modal.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'show_interest_free_cart' => array(
'type' => 'checkbox',
'title' => __( 'Show interest free', 'woo-stripe-payment' ),
'default' => 'no',
'value' => 'yes',
'description' => __( 'If enabled, the Afterpay message will contain the interest free text.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'product_styling' => array(
'type' => 'title',
'title' => __( 'Product Page Styling' )
),
'icon_product' => array(
'title' => __( 'Icon', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'black-on-mint',
'options' => array(
'black-on-mint' => __( 'Black on mint', 'woo-stripe-payment' ),
'black-on-white' => __( 'Black on white', 'woo-stripe-payment' ),
'mint-on-black' => __( 'Mint on black', 'woo-stripe-payment' ),
'white-on-black' => __( 'White on black', 'woo-stripe-payment' )
),
'desc_tip' => true,
'description' => __( 'This is the icon style that appears next to the gateway on the checkout page.', 'woo-stripe-payment' ),
),
'intro_text_product' => array(
'title' => __( 'Intro text', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'Pay in',
'options' => array(
'In' => 'In',
'Or' => 'Or',
'Pay' => 'Pay',
'Pay in' => 'Pay in'
)
),
'modal_link_style_product' => array(
'title' => __( 'Modal link style', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'circled-info-icon',
'options' => array(
'more-info-text' => __( 'More info text', 'woo-stripe-payment' ),
'circled-info-icon' => __( 'Circled info icon', 'woo-stripe-payment' ),
'learn-more-text' => __( 'Learn more text', 'woo-stripe-payment' ),
),
'description' => __( 'This is the style of the Afterpay info link.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'modal_theme_product' => array(
'title' => __( 'Modal link style', 'woo-stripe-payment' ),
'type' => 'select',
'default' => 'mint',
'options' => array(
'mint' => __( 'Mint', 'woo-stripe-payment' ),
'white' => __( 'White', 'woo-stripe-payment' )
),
'description' => __( 'This is the theme color for the Afterpay info modal.', 'woo-stripe-payment' ),
'desc_tip' => true
),
'show_interest_free_product' => array(
'type' => 'checkbox',
'title' => __( 'Show interest free', 'woo-stripe-payment' ),
'default' => 'no',
'value' => 'yes',
'description' => __( 'If enabled, the Afterpay message will contain the interest free text.', 'woo-stripe-payment' ),
'desc_tip' => true
)
), parent::get_local_payment_settings() );
unset( $settings['title_text'] );
return $settings;
}
public function enqueue_product_scripts( $scripts ) {
$scripts->enqueue_script( 'afterpay-product', $scripts->assets_url( 'js/frontend/afterpay.js' ), array(
$scripts->get_handle( 'wc-stripe' )
) );
$scripts->localize_script( 'afterpay-product', $this->get_localized_params( 'product' ) );
}
public function enqueue_cart_scripts( $scripts ) {
$scripts->enqueue_script( 'afterpay-cart', $scripts->assets_url( 'js/frontend/afterpay.js' ), array(
$scripts->get_handle( 'wc-stripe' )
) );
$scripts->localize_script( 'afterpay-cart', $this->get_localized_params( 'cart' ) );
}
public function product_fields() {
$this->enqueue_frontend_scripts( 'product' );
$this->output_display_items( 'product' );
}
public function cart_fields() {
$this->enqueue_frontend_scripts( 'cart' );
$this->output_display_items( 'cart' );
}
public function mini_cart_fields() {
$this->output_display_items( 'cart' );
}
public function get_required_parameters() {
return apply_filters( 'wc_stripe_afterpay_get_required_parameters', array(
'AUD' => array( 'AU', 1, 2000 ),
'CAD' => array( 'CA', 1, 2000 ),
'NZD' => array( 'NZ', 1, 2000 ),
'GBP' => array( 'GB', 1, 800 ),
'USD' => array( 'US', 1, 2000 )
), $this );
}
public function validate_local_payment_available( $currency, $billing_country, $total ) {
$_available = false;
$account_country = stripe_wc()->account_settings->get_option( 'country' );
if ( empty( $account_country ) && wc_stripe_mode() === 'test' ) {
$account_country = wc_get_base_location()['country'];
}
$params = $this->get_required_parameters();
$filtered_params = isset( $params[ $currency ] ) ? $params[ $currency ] : false;
if ( $filtered_params ) {
list( $country, $min_amount, $max_amount ) = $filtered_params;
$_available = $account_country === $country && $min_amount <= $total && $total <= $max_amount;
}
return $_available;
}
public function get_icon() {
return '';
}
public function get_localized_params( $context = 'checkout' ) {
$params = parent::get_localized_params();
$params['currencies'] = $this->currencies;
$params['msg_options'] = $this->get_afterpay_message_options( $context );
$params['supported_locales'] = $this->get_supported_locales();
$params['requirements'] = $this->get_required_parameters();
$params['hide_ineligible'] = $this->is_active( 'hide_ineligible' ) ? 'yes' : 'no';
$locale = get_locale();
$params['locale'] = $locale ? str_replace( '_', '-', substr( $locale, 0, 5 ) ) : 'auto';
return $params;
}
public function get_supported_locales() {
return apply_filters( 'wc_stripe_afterpay_supported_locales', array( 'en-US', 'en-CA', 'en-AU', 'en-NZ', 'en-GB', 'fr-FR', 'it-IT', 'es-ES' ) );
}
public function get_element_options( $options = array() ) {
$locale = get_locale();
$locale = wc_stripe_get_site_locale();
if ( ! in_array( $locale, $this->get_supported_locales() ) ) {
$locale = 'auto';
}
$options['locale'] = $locale;
return parent::get_element_options( $options );
}
public function get_afterpay_message_options( $context = 'checkout' ) {
$options = array(
'logoType' => 'badge',
'badgeTheme' => $this->get_option( "icon_{$context}" ),
'lockupTheme' => 'black',
'introText' => $this->get_option( "intro_text_{$context}" ),
'showInterestFree' => $this->is_active( "show_interest_free_{$context}" ),
'modalTheme' => $this->get_option( "modal_theme_{$context}" ),
'modalLinkStyle' => $this->get_option( "modal_link_style_{$context}" )
);
if ( in_array( $context, array( 'cart', 'checkout' ) ) ) {
$options['isEligible'] = WC()->cart && WC()->cart->needs_shipping();
} elseif ( $context === 'product' ) {
global $product;
if ( $product ) {
$options['isEligible'] = $product->needs_shipping();
}
}
return apply_filters( 'wc_stripe_afterpay_message_options', $options, $context, $this );
}
public function get_title() {
if ( is_checkout() ) {
$this->title = '';
}
return parent::get_title();
}
protected function get_payment_description() {
$desc = parent::get_payment_description();
if ( ( $country = stripe_wc()->account_settings->get_option( 'country' ) ) ) {
$params = $this->get_required_parameters();
foreach ( $params as $currency => $param ) {
if ( $param[0] === $country ) {
$desc = sprintf( __( 'Store currency must be %s for Afterpay to show because your Stripe account is registered in %s. This is a requirement of Afterpay.',
'woo-stripe-payment' ),
$currency,
$country );
break;
}
}
}
return $desc;
}
public function enqueue_mini_cart_scripts( $scripts ) {
if ( ! wp_script_is( $scripts->get_handle( 'mini-cart' ) ) ) {
$scripts->enqueue_script( 'mini-cart',
$scripts->assets_url( 'js/frontend/mini-cart.js' ),
apply_filters( 'wc_stripe_mini_cart_dependencies', array( $scripts->get_handle( 'wc-stripe' ) ), $scripts ) );
}
$scripts->localize_script( 'mini-cart', $this->get_localized_params( 'cart' ), 'wc_' . $this->id . '_mini_cart_params' );
}
}