| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Payplug\PayplugWoocommerce\Gateway\Blocks; |
| 4 | 4 | |
| 5 | +use Payplug\PayplugWoocommerce\Gateway\Oney\OneyAccountMapper; | |
| 5 | 6 | use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 6 | 7 | |
| 7 | 8 | class PayplugOney extends PayplugGenericBlock |
| 8 | 9 | { |
| @@ -19,8 +20,27 @@ | ||
| 19 | 20 | |
| 20 | 21 | protected $total_price; |
| 21 | 22 | |
| 22 | 23 | /** |
| 24 | + * Returns an array of scripts/handles to be registered for this payment method. | |
| 25 | + * | |
| 26 | + * The bundled JS calls the official Oney widget (window.loadOneyWidget / | |
| 27 | + * window.oneyMerchantApp), which is exposed by a separate external loader script - not | |
| 28 | + * something webpack bundles. Register and prepend it here. Execution order between the | |
| 29 | + * loader and the bundle doesn't matter: the bundle only calls loadOneyWidget() from inside | |
| 30 | + * a useEffect, which fires after the checkout page has finished mounting - i.e. well after | |
| 31 | + * every enqueued script has already executed. | |
| 32 | + * | |
| 33 | + * @return array | |
| 34 | + */ | |
| 35 | + public function get_payment_method_script_handles() | |
| 36 | + { | |
| 37 | + wp_register_script('payplug-oney-loader', PayplugWoocommerceHelper::get_oney_loader_url(), [], PAYPLUG_GATEWAY_VERSION, true); | |
| 38 | + | |
| 39 | + return array_merge(['payplug-oney-loader'], parent::get_payment_method_script_handles()); | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 23 | 43 | * Returns an associative array of data to be exposed for the payment method's client side. |
| 24 | 44 | */ |
| 25 | 45 | public function get_payment_method_data() |
| 26 | 46 | { |
| @@ -30,11 +50,17 @@ | ||
| 30 | 50 | $this->cart = WC()->cart; |
| 31 | 51 | $this->total_price = floatval(WC()->cart->total); |
| 32 | 52 | } |
| 33 | 53 | |
| 54 | + $account = PayplugWoocommerceHelper::get_account_data_from_options(); | |
| 55 | + $oney_settings = $this->gateway->get_configuration()->get_option('payment_methods.configuration.oney'); | |
| 56 | + $country = PayplugWoocommerceHelper::getISOCountryCode(); | |
| 57 | + $mapper = OneyAccountMapper::map((array) $account, $oney_settings, $country); | |
| 58 | + $fee_mode = !empty($oney_settings['with_fees']) ? 'with_fees' : 'without_fees'; | |
| 59 | + | |
| 34 | 60 | if (PayplugWoocommerceHelper::is_cart_block() && is_cart() && !PayplugWoocommerceHelper::is_subscription()) { |
| 35 | 61 | $data['oney_cart_label'] = __('OR PAY IN', 'payplug'); |
| 36 | - if ($this->gateway->settings['payment_methods']['configuration']['oney']['with_fees']) { | |
| 62 | + if ('with_fees' === $fee_mode) { | |
| 37 | 63 | $data['oney_cart_logo'] = esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/lg-3x4xoney.png'); |
| 38 | 64 | } else { |
| 39 | 65 | $data['oney_cart_logo'] = esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/Oneywithoutfees3x4x.png'); |
| 40 | 66 | } |
| @@ -45,29 +71,21 @@ | ||
| 45 | 71 | 'class' => 'payplug-payment-icon', |
| 46 | 72 | 'alt' => $this->gateway->title, |
| 47 | 73 | ]; |
| 48 | 74 | $data['description'] = $this->gateway->description; |
| 49 | - $data['oney_response'] = $this->gateway->payplug_api->simulate_oney_payment($this->total_price, 'with_fees'); | |
| 50 | - // Normalize total_cost from cents to euros | |
| 51 | - if (is_array($data['oney_response'])) { | |
| 52 | - foreach ($data['oney_response'] as &$plan) { | |
| 53 | - if (isset($plan['total_cost'])) { | |
| 54 | - $plan['total_cost'] = $plan['total_cost'] / 100; | |
| 55 | - } | |
| 56 | - } | |
| 57 | - unset($plan); | |
| 58 | - } | |
| 59 | 75 | $data['currency'] = get_woocommerce_currency_symbol(get_option('woocommerce_currency')); |
| 76 | + $data['allowed_country_codes'] = $this->gateway->allowed_country_codes; | |
| 60 | 77 | |
| 61 | - $data['translations']['bring'] = __('Bring', 'payplug'); | |
| 62 | - $data['translations']['oney_financing_cost'] = __('oney_financing_cost', 'payplug'); | |
| 63 | - $data['translations']['1st_monthly_payment'] = __('1st monthly payment', 'payplug'); | |
| 64 | - $data['translations']['2nd_monthly_payment'] = __('2nd monthly payment', 'payplug'); | |
| 65 | - $data['translations']['oney_total'] = __('oney_total', 'payplug'); | |
| 66 | - $data['allowed_country_codes'] = $this->gateway->allowed_country_codes; | |
| 78 | + $data['oney_widget'] = [ | |
| 79 | + 'merchant_guid' => $mapper->get_merchant_guid(), | |
| 80 | + 'business_transaction_code' => $mapper->business_transaction_code_for_gateway($this->gateway->id), | |
| 81 | + 'business_transaction_codes' => $mapper->business_transaction_codes($fee_mode), | |
| 82 | + 'country' => $country, | |
| 83 | + 'language' => strtoupper(substr(get_locale(), 0, 2)), | |
| 84 | + 'payment_amount' => $this->total_price, | |
| 85 | + ]; | |
| 67 | 86 | |
| 68 | - $oney_cfg = $this->gateway->get_configuration()->get_option('payment_methods.configuration.oney'); | |
| 69 | - $oney_amount = json_decode($oney_cfg['custom_amounts'], true); | |
| 87 | + $oney_amount = json_decode($oney_settings['custom_amounts'], true); | |
| 70 | 88 | $data['requirements'] = [ |
| 71 | 89 | 'max_quantity' => $this->gateway::ONEY_PRODUCT_QUANTITY_MAXIMUM, |
| 72 | 90 | 'min_threshold' => $oney_amount['min'], |
| 73 | 91 | 'max_threshold' => $oney_amount['max'], |