woocommerce-multilingual
/
classes
/
multi-currency
/
payment-gateways
/
class-wcml-payment-gateway-stripe.php
class-wcml-currencies-payment-gateways.php
1 month ago
class-wcml-not-supported-payment-gateway.php
1 month ago
class-wcml-payment-gateway-bacs.php
1 month ago
class-wcml-payment-gateway-paypal-v2.php
1 month ago
class-wcml-payment-gateway-paypal.php
1 month ago
class-wcml-payment-gateway-stripe.php
1 month ago
class-wcml-payment-gateway.php
1 month ago
class-wcml-payment-gateway-stripe.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Payment_Gateway_Stripe extends WCML_Payment_Gateway { |
| 4 | |
| 5 | const ID = 'stripe'; |
| 6 | |
| 7 | public function get_output_model() { |
| 8 | return [ |
| 9 | 'id' => $this->get_id(), |
| 10 | 'title' => $this->get_title(), |
| 11 | 'isSupported' => true, |
| 12 | 'settings' => $this->get_currencies_details(), |
| 13 | 'tooltip' => '', |
| 14 | 'strings' => [ |
| 15 | 'labelCurrency' => __( 'Currency', 'woocommerce-multilingual' ), |
| 16 | 'labelLivePublishableKey' => __( 'Live Publishable Key', 'woocommerce-multilingual' ), |
| 17 | 'labelLiveSecretKey' => __( 'Live Secret Key', 'woocommerce-multilingual' ), |
| 18 | 'labelTestPublishableKey' => __( 'Test Publishable Key', 'woocommerce-multilingual' ), |
| 19 | 'labelTestSecretKey' => __( 'Test Secret Key', 'woocommerce-multilingual' ), |
| 20 | ], |
| 21 | ]; |
| 22 | } |
| 23 | |
| 24 | public function add_hooks() { |
| 25 | $client_currency = $this->woocommerce_wpml->multi_currency->get_client_currency(); |
| 26 | |
| 27 | if ( $this->is_client_currency_supported( $client_currency ) ) { |
| 28 | add_filter( 'wc_stripe_generate_create_intent_request', [ |
| 29 | $this, |
| 30 | 'convert_stripe_payment_request' |
| 31 | ], 10, 3 ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function convert_stripe_payment_request( $request, $order, $source ) { |
| 36 | $client_currency = $request ['currency'] ?? null; |
| 37 | $client_currency = strtoupper( $client_currency ); |
| 38 | |
| 39 | $convert_to_currency = $this->maybe_convert_currency( $client_currency ); |
| 40 | if ( null === $convert_to_currency ) { |
| 41 | return $request; |
| 42 | } |
| 43 | |
| 44 | if ( $client_currency !== $convert_to_currency ) { |
| 45 | $convert_price = $this->get_convert_price_callable( $convert_to_currency ); |
| 46 | |
| 47 | $request = $convert_price( $request ); |
| 48 | } |
| 49 | |
| 50 | return $request; |
| 51 | } |
| 52 | |
| 53 | public function get_currencies_details() { |
| 54 | $currencies_details = []; |
| 55 | $default_currency = wcml_get_woocommerce_currency_option(); |
| 56 | $active_currencies = get_woocommerce_currencies(); |
| 57 | |
| 58 | foreach ( $active_currencies as $code => $currency ) { |
| 59 | |
| 60 | if ( $default_currency === $code ) { |
| 61 | $currencies_details[ $code ]['currency'] = $code; |
| 62 | $currencies_details[ $code ]['publishable_key'] = $this->get_gateway()->settings['publishable_key']; |
| 63 | $currencies_details[ $code ]['secret_key'] = $this->get_gateway()->settings['secret_key']; |
| 64 | $currencies_details[ $code ]['test_publishable_key'] = $this->get_gateway()->settings['test_publishable_key']; |
| 65 | $currencies_details[ $code ]['test_secret_key'] = $this->get_gateway()->settings['test_secret_key']; |
| 66 | } else { |
| 67 | $currency_gateway_setting = $this->get_setting( $code ); |
| 68 | $currencies_details[ $code ]['currency'] = $currency_gateway_setting['currency'] ?? ''; |
| 69 | $currencies_details[ $code ]['publishable_key'] = $currency_gateway_setting['publishable_key'] ?? ''; |
| 70 | $currencies_details[ $code ]['secret_key'] = $currency_gateway_setting['secret_key'] ?? ''; |
| 71 | $currencies_details[ $code ]['test_publishable_key'] = $currency_gateway_setting['test_publishable_key'] ?? ''; |
| 72 | $currencies_details[ $code ]['test_secret_key'] = $currency_gateway_setting['test_secret_key'] ?? ''; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $currencies_details; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | public static function filter_stripe_settings( $settings ) { |
| 81 | if ( is_admin() ) { |
| 82 | return $settings; |
| 83 | } |
| 84 | |
| 85 | global $woocommerce_wpml; |
| 86 | |
| 87 | $client_currency = $woocommerce_wpml->multi_currency->get_client_currency(); |
| 88 | $gateway_settings = get_option( self::OPTION_KEY . self::ID, [] ); |
| 89 | |
| 90 | if ( $gateway_settings && isset( $gateway_settings[ $client_currency ] ) ) { |
| 91 | $gateway_setting = $gateway_settings[ $client_currency ]; |
| 92 | if ( ! empty( $gateway_setting['publishable_key'] ) && ! empty( $gateway_setting['secret_key'] ) ) { |
| 93 | $settings['publishable_key'] = $gateway_setting['publishable_key']; |
| 94 | $settings['secret_key'] = $gateway_setting['secret_key']; |
| 95 | } |
| 96 | if ( ! empty( $gateway_setting['test_publishable_key'] ) && ! empty( $gateway_setting['test_secret_key'] ) ) { |
| 97 | $settings['test_publishable_key'] = $gateway_setting['test_publishable_key']; |
| 98 | $settings['test_secret_key'] = $gateway_setting['test_secret_key']; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $settings; |
| 103 | } |
| 104 | |
| 105 | private function get_convert_price_callable( $convert_to_currency ): callable { |
| 106 | return function ( array $price_params ) use ( $convert_to_currency ): array { |
| 107 | $value = $price_params['amount']; |
| 108 | $currency = strtoupper( $price_params['currency'] ); |
| 109 | |
| 110 | $price_default = $this->woocommerce_wpml->multi_currency->prices->unconvert_price_amount( $value, $currency ); |
| 111 | $price_converted = $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $price_default, $convert_to_currency ); |
| 112 | |
| 113 | $price_params['amount'] = (int) $price_converted; |
| 114 | $price_params['currency'] = strtolower( $convert_to_currency ); |
| 115 | |
| 116 | return $price_params; |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | private function maybe_convert_currency( string $client_currency ) { |
| 121 | $gateway_setting = $this->get_setting( $client_currency ); |
| 122 | |
| 123 | return $gateway_setting['currency'] ?? null; |
| 124 | } |
| 125 | |
| 126 | private function is_client_currency_supported( string $client_currency ): bool { |
| 127 | $newCurrency = $this->maybe_convert_currency( $client_currency ); |
| 128 | |
| 129 | if ( null === $newCurrency ) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 |