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
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Payment_Gateway_Stripe' ) ) {
return;
}
class WC_Payment_Gateway_Stripe_GooglePay extends WC_Payment_Gateway_Stripe {
use WC_Stripe_Payment_Intent_Trait;
protected $payment_method_type = 'card';
public function __construct() {
$this->id = 'stripe_googlepay';
$this->tab_title = __( 'Google Pay', 'woo-stripe-payment' );
$this->template_name = 'googlepay.php';
$this->token_type = 'Stripe_GooglePay';
$this->method_title = __( 'Stripe Google Pay', 'woo-stripe-payment' );
$this->method_description = __( 'Google Pay gateway that integrates with your Stripe account.', 'woo-stripe-payment' );
$this->has_digital_wallet = true;
parent::__construct();
$this->icon = stripe_wc()->assets_url( 'img/' . $this->get_option( 'icon' ) . '.svg' );
}
public function init_supports() {
parent::init_supports();
$this->supports[] = 'wc_stripe_cart_checkout';
$this->supports[] = 'wc_stripe_product_checkout';
$this->supports[] = 'wc_stripe_banner_checkout';
$this->supports[] = 'wc_stripe_mini_cart_checkout';
}
public function enqueue_checkout_scripts( $scripts ) {
$scripts->enqueue_script(
'googlepay-checkout',
$scripts->assets_url( 'js/frontend/googlepay-checkout.js' ),
array(
$scripts->get_handle( 'wc-stripe' ),
$scripts->get_handle( 'gpay' ),
),
stripe_wc()->version(),
true
);
$scripts->localize_script( 'googlepay-checkout', $this->get_localized_params() );
}
public function enqueue_product_scripts( $scripts ) {
$scripts->enqueue_script(
'googlepay-product',
$scripts->assets_url( 'js/frontend/googlepay-product.js' ),
array(
$scripts->get_handle( 'wc-stripe' ),
$scripts->get_handle( 'gpay' ),
),
stripe_wc()->version(),
true
);
$scripts->localize_script( 'googlepay-product', $this->get_localized_params() );
}
public function enqueue_cart_scripts( $scripts ) {
$scripts->enqueue_script(
'googlepay-cart',
$scripts->assets_url( 'js/frontend/googlepay-cart.js' ),
array(
$scripts->get_handle( 'wc-stripe' ),
$scripts->get_handle( 'gpay' ),
),
stripe_wc()->version(),
true
);
$scripts->localize_script( 'googlepay-cart', $this->get_localized_params() );
}
public function enqueue_admin_scripts() {
wp_register_script( 'gpay-external', stripe_wc()->scripts()->global_scripts['gpay'], array(), stripe_wc()->version(), true );
wp_enqueue_script(
'wc-stripe-gpay-admin',
stripe_wc()->assets_url( 'js/admin/googlepay.js' ),
array(
'gpay-external',
'wc-stripe-admin-settings',
),
stripe_wc()->version(),
true
);
}
public function get_localized_params() {
$data = array_merge_recursive(
parent::get_localized_params(),
array(
'environment' => wc_stripe_mode() === 'test' ? 'TEST' : 'PRODUCTION',
'merchant_id' => wc_stripe_mode() === 'test' ? '' : $this->get_option( 'merchant_id' ),
'merchant_name' => $this->get_option( 'merchant_name' ),
'processing_country' => WC()->countries ? WC()->countries->get_base_country() : wc_get_base_location()['country'],
'button_color' => $this->get_option( 'button_color' ),
'button_style' => $this->get_option( 'button_style' ),
'button_size_mode' => 'fill',
'button_locale' => $this->get_payment_button_locale(),
'total_price_label' => __( 'Total', 'woo-stripe-payment' ),
'routes' => array( 'payment_data' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->googlepay->rest_uri( 'shipping-data' ) ) ),
'messages' => array( 'invalid_amount' => __( 'Please update you product quantity before using Google Pay.', 'woo-stripe-payment' ) )
)
);
return $data;
}
protected function get_display_item_for_cart( $price, $label, $type, ...$args ) {
switch ( $type ) {
case 'tax':
$type = 'TAX';
break;
default:
$type = 'LINE_ITEM';
break;
}
return array(
'label' => $label,
'type' => $type,
'price' => wc_format_decimal( $price, 2 )
);
}
protected function get_display_item_for_product( $product ) {
return array(
'label' => esc_attr( $product->get_name() ),
'type' => 'SUBTOTAL',
'price' => wc_format_decimal( $product->get_price(), 2 )
);
}
protected function get_display_item_for_order( $price, $label, $order, $type, ...$args ) {
switch ( $type ) {
case 'tax':
$type = 'TAX';
break;
default:
$type = 'LINE_ITEM';
break;
}
return array(
'label' => $label,
'type' => $type,
'price' => wc_format_decimal( $price, 2 )
);
}
public function get_formatted_shipping_methods( $methods = array() ) {
$methods = parent::get_formatted_shipping_methods( $methods );
if ( empty( $methods ) ) {
$methods[] = array(
'id' => 'default',
'label' => __( 'Waiting...', 'woo-stripe-payment' ),
'description' => __( 'loading shipping methods...', 'woo-stripe-payment' ),
);
}
return $methods;
}
public function get_formatted_shipping_method( $price, $rate, $i, $package, $incl_tax ) {
return array(
'id' => $this->get_shipping_method_id( $rate->id, $i ),
'label' => $this->get_formatted_shipping_label( $price, $rate, $incl_tax ),
'description' => ''
);
}
protected function get_formatted_shipping_label( $price, $rate, $incl_tax ) {
$label = sprintf( '%s: %s %s', esc_attr( $rate->get_label() ), number_format( $price, 2 ), get_woocommerce_currency() );
if ( $incl_tax ) {
if ( $rate->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
$label .= ' ' . WC()->countries->inc_tax_or_vat();
}
} else {
if ( $rate->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
$label .= ' ' . WC()->countries->ex_tax_or_vat();
}
}
return $label;
}
public function get_shipping_method_label( $rate ) {
$incl_tax = wc_stripe_display_prices_including_tax();
$price = $incl_tax ? $rate->cost + $rate->get_shipping_tax() : $rate->cost;
return $this->get_formatted_shipping_label( $price, $rate, $incl_tax );
}
public function add_to_cart_response( $data ) {
$data['googlepay']['displayItems'] = $this->get_display_items();
return $data;
}
public function get_mini_cart_dependencies( $deps, $scripts ) {
if ( $this->mini_cart_enabled() ) {
$deps[] = $scripts->get_handle( 'gpay' );
}
return $deps;
}
public function has_enqueued_scripts( $scripts ) {
return wp_script_is( $scripts->get_handle( 'googlepay-checkout' ) );
}
public function get_payment_button_locale() {
$locale = wc_stripe_get_site_locale();
$button_locale = null;
if ( 'auto' !== $locale ) {
$button_locale = substr( $locale, 0, 2 );
if ( ! in_array( $button_locale, $this->get_supported_button_locales() ) ) {
$button_locale = null;
}
}
return apply_filters( 'wc_stripe_googlepay_get_button_locale', $button_locale, $this );
}
public function get_supported_button_locales() {
return apply_filters( 'wc_stripe_googlepay_supported_button_locales',
array( 'en', 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'es', 'et', 'fi', 'fr', 'hr',
'id', 'it', 'ja', 'ko', 'ms', 'nl', 'no', 'pl', 'pt', 'ru', 'sk', 'sl', 'sr', 'sv', 'th', 'tr', 'uk', 'zh'
)
);
}
}