woocommerce-multilingual
/
classes
/
multi-currency
/
payment-gateways
/
class-wcml-payment-gateway-paypal.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-paypal.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Payment_Gateway_PayPal extends WCML_Payment_Gateway { |
| 4 | |
| 5 | const ID = 'paypal'; |
| 6 | const SUPPORTED_CURRENCIES = [ |
| 7 | 'AUD', |
| 8 | 'BRL', |
| 9 | 'CAD', |
| 10 | 'MXN', |
| 11 | 'NZD', |
| 12 | 'HKD', |
| 13 | 'SGD', |
| 14 | 'USD', |
| 15 | 'EUR', |
| 16 | 'JPY', |
| 17 | 'TRY', |
| 18 | 'NOK', |
| 19 | 'CZK', |
| 20 | 'DKK', |
| 21 | 'HUF', |
| 22 | 'ILS', |
| 23 | 'MYR', |
| 24 | 'PHP', |
| 25 | 'PLN', |
| 26 | 'SEK', |
| 27 | 'CHF', |
| 28 | 'TWD', |
| 29 | 'THB', |
| 30 | 'GBP', |
| 31 | 'RMB', |
| 32 | 'RUB', |
| 33 | 'INR', |
| 34 | ]; |
| 35 | |
| 36 | public function get_output_model() { |
| 37 | return [ |
| 38 | 'id' => $this->get_id(), |
| 39 | 'title' => $this->get_title(), |
| 40 | 'isSupported' => true, |
| 41 | 'settings' => $this->get_currencies_details(), |
| 42 | 'tooltip' => '', |
| 43 | 'strings' => [ |
| 44 | 'labelCurrency' => __( 'Currency', 'woocommerce-multilingual' ), |
| 45 | 'labelPayPalEmail' => __( 'PayPal Email', 'woocommerce-multilingual' ), |
| 46 | // translators: %s is currency code. |
| 47 | 'tooltipNotSupported' => __( 'This gateway does not support %s. To show this gateway please select another currency.', 'woocommerce-multilingual' ), |
| 48 | ], |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | public function is_valid_for_use( $currency ) { |
| 53 | |
| 54 | $filter_removed = remove_filter( 'woocommerce_paypal_supported_currencies', [ 'WCML_Payment_Gateway_PayPal', 'filter_supported_currencies' ] ); |
| 55 | |
| 56 | $is_valid = in_array( |
| 57 | $currency, |
| 58 | apply_filters( |
| 59 | 'woocommerce_paypal_supported_currencies', |
| 60 | self::SUPPORTED_CURRENCIES |
| 61 | ), |
| 62 | true |
| 63 | ); |
| 64 | |
| 65 | if ( $filter_removed ) { |
| 66 | add_filter( 'woocommerce_paypal_supported_currencies', [ 'WCML_Payment_Gateway_PayPal', 'filter_supported_currencies' ] ); |
| 67 | } |
| 68 | |
| 69 | return $is_valid; |
| 70 | } |
| 71 | |
| 72 | public function get_currencies_details() { |
| 73 | |
| 74 | $currencies_details = []; |
| 75 | $default_currency = wcml_get_woocommerce_currency_option(); |
| 76 | $woocommerce_currencies = get_woocommerce_currencies(); |
| 77 | |
| 78 | foreach ( $woocommerce_currencies as $code => $currency ) { |
| 79 | |
| 80 | if ( $default_currency === $code ) { |
| 81 | $currencies_details[ $code ]['value'] = $this->get_gateway()->settings['email'] ?? ''; |
| 82 | $currencies_details[ $code ]['currency'] = $code; |
| 83 | $currencies_details[ $code ]['isValid'] = $this->is_valid_for_use( $default_currency ); |
| 84 | } else { |
| 85 | $currency_gateway_setting = $this->get_setting( $code ); |
| 86 | $currencies_details[ $code ]['value'] = $currency_gateway_setting ? $currency_gateway_setting['value'] : ''; |
| 87 | $currencies_details[ $code ]['currency'] = $currency_gateway_setting ? $currency_gateway_setting['currency'] : $code; |
| 88 | $currencies_details[ $code ]['isValid'] = $this->is_valid_for_use( $code ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $currencies_details; |
| 93 | |
| 94 | } |
| 95 | |
| 96 | public function add_hooks() { |
| 97 | add_filter( 'woocommerce_paypal_args', [ $this, 'filter_paypal_args' ], 10, 2 ); |
| 98 | } |
| 99 | |
| 100 | public function filter_paypal_args( $args, $order ) { |
| 101 | |
| 102 | $order_data = $order->get_data(); |
| 103 | $client_currency = $order_data['currency']; |
| 104 | $gateway_setting = $this->get_setting( $client_currency ); |
| 105 | |
| 106 | if ( $gateway_setting ) { |
| 107 | |
| 108 | if ( $gateway_setting['value'] ) { |
| 109 | $args['business'] = $gateway_setting['value']; |
| 110 | } |
| 111 | |
| 112 | if ( $client_currency !== $gateway_setting['currency'] ) { |
| 113 | $args['currency_code'] = $gateway_setting['currency']; |
| 114 | $cart_items = WC()->cart->get_cart_contents(); |
| 115 | $item_id = 1; |
| 116 | |
| 117 | foreach ( $cart_items as $item ) { |
| 118 | $item_product_id = $item['variation_id'] ?: $item['product_id']; |
| 119 | $args[ 'amount_' . $item_id ] = $this->woocommerce_wpml->multi_currency->prices->get_product_price_in_currency( $item_product_id, $gateway_setting['currency'] ); |
| 120 | $item_id ++; |
| 121 | } |
| 122 | |
| 123 | $args['shipping_1'] = $this->woocommerce_wpml->cart->convert_cart_shipping_to_currency( $gateway_setting['currency'] ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return $args; |
| 128 | } |
| 129 | |
| 130 | public static function filter_supported_currencies( $supported_currencies ) { |
| 131 | global $woocommerce_wpml; |
| 132 | |
| 133 | $client_currency = $woocommerce_wpml->multi_currency->get_client_currency(); |
| 134 | |
| 135 | if ( ! in_array( $client_currency, self::SUPPORTED_CURRENCIES, true ) ) { |
| 136 | $gateway_settings = get_option( self::OPTION_KEY . self::ID, [] ); |
| 137 | |
| 138 | if ( $gateway_settings && isset( $gateway_settings[ $client_currency ] ) ) { |
| 139 | |
| 140 | if ( in_array( $gateway_settings[ $client_currency ]['currency'], self::SUPPORTED_CURRENCIES, true ) ) { |
| 141 | $supported_currencies[] = $client_currency; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return $supported_currencies; |
| 147 | } |
| 148 | |
| 149 | } |
| 150 |