From c1c81094a8376a85a8a1328a3685209c115f6a44 Mon Sep 17 00:00:00 2001 From: Richard Henkenjohann Date: Fri, 13 Nov 2020 10:32:46 +0100 Subject: [PATCH] Push all data when updating an order --- src/Module/KlarnaCheckout.php | 214 +++++++++++++++++----------------- 1 file changed, 107 insertions(+), 107 deletions(-) diff --git a/src/Module/KlarnaCheckout.php b/src/Module/KlarnaCheckout.php index 6f5bfa3..db95e46 100644 --- a/src/Module/KlarnaCheckout.php +++ b/src/Module/KlarnaCheckout.php @@ -47,6 +47,7 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\RouterInterface; class KlarnaCheckout extends Module { @@ -183,16 +184,7 @@ protected function compile() // Resume, just make sure the cart is up to date $klarnaCheckout = new KlarnaOrder($connector, $klarnaOrderId); try { - $klarnaCheckout->update( - [ - 'order_amount' => (int) round($this->cart->getTotal() * 100, 0), - 'order_tax_amount' => (int) round( - ($this->cart->getTotal() - $this->cart->getTaxFreeTotal()) * 100, - 0 - ), - 'order_lines' => $this->orderLines(), - ] - ); + $klarnaCheckout->update($this->klarnaOrderData()); } catch (RequestException $e) { $klarnaCheckout = null; } catch (ConnectorException $e) { @@ -202,104 +194,8 @@ protected function compile() try { if (null === $klarnaCheckout) { - // Load addresses from address book for logged in members - $shippingAddress = null; - $billingAddress = null; - if (FE_USER_LOGGED_IN) { - if ($address = Address::findDefaultBillingForMember($this->user->id)) { - $shippingAddress = $this->getApiDataFromAddress($address); - } - - if ($address = Address::findDefaultShippingForMember($this->user->id)) { - $billingAddress = $this->getApiDataFromAddress($address); - } - } - $klarnaCheckout = new KlarnaOrder($connector); - $klarnaCheckout->create( - [ - 'purchase_country' => $this->config->country, - 'purchase_currency' => $this->config->currency, - 'locale' => $this->request->getLocale(), - 'order_amount' => (int) round($this->cart->getTotal() * 100, 0), - 'order_tax_amount' => (int) round( - ($this->cart->getTotal() - $this->cart->getTaxFreeTotal()) * 100, - 0 - ), - 'order_lines' => $this->orderLines(), - 'merchant_urls' => [ - 'terms' => $this->uri($this->klarna_terms_page), - 'cancellation_terms' => $this->uri($this->klarna_cancellation_terms_page), - 'checkout' => $this->uri($this->klarna_checkout_page), - 'confirmation' => $this->uri($this->klarna_confirmation_page) - . '?klarna_order_id={checkout.order.id}', - 'push' => urldecode( - System::getContainer()->get('router')->generate( - 'richardhj.klarna_checkout.push', - ['orderId' => '{checkout.order.id}'], - UrlGeneratorInterface::ABSOLUTE_URL - ) - ), - 'shipping_option_update' => urldecode( - System::getContainer()->get('router')->generate( - 'richardhj.klarna_checkout.callback.shipping_option_update', - ['orderId' => '{checkout.order.id}'], - UrlGeneratorInterface::ABSOLUTE_URL - ) - ), - 'address_update' => urldecode( - System::getContainer()->get('router')->generate( - 'richardhj.klarna_checkout.callback.address_update', - ['orderId' => '{checkout.order.id}'], - UrlGeneratorInterface::ABSOLUTE_URL - ) - ), - 'country_change' => urldecode( - System::getContainer()->get('router')->generate( - 'richardhj.klarna_checkout.callback.country_change', - ['orderId' => '{checkout.order.id}'], - UrlGeneratorInterface::ABSOLUTE_URL - ) - ), - 'validation' => System::getContainer()->get('router')->generate( - 'richardhj.klarna_checkout.callback.order_validation', - [], - UrlGeneratorInterface::ABSOLUTE_URL - ), - ], - 'billing_address' => $billingAddress, - 'shipping_address' => $shippingAddress, - 'shipping_options' => $this->shippingOptions( - StringUtil::deserialize($this->iso_shipping_modules, true) - ), - 'shipping_countries' => $this->config->getShippingCountries(), - 'external_payment_methods' => $this->externalPaymentMethods(), - 'options' => [ - 'allow_separate_shipping_address' => [] !== $this->config->getShippingFields(), - 'color_button' => $this->klarna_color_button - ? '#' . $this->klarna_color_button - : null, - 'color_button_text' => $this->klarna_color_button_text - ? '#' . $this->klarna_color_button_text - : null, - 'color_checkbox' => $this->klarna_color_checkbox - ? '#' . $this->klarna_color_checkbox - : null, - 'color_checkbox_checkmark' => $this->klarna_color_checkbox_checkmark - ? '#' . $this->klarna_color_checkbox_checkmark - : null, - 'color_header' => $this->klarna_color_header - ? '#' . $this->klarna_color_header - : null, - 'color_link' => $this->klarna_color_link - ? '#' . $this->klarna_color_link - : null, - 'require_validate_callback_success' => true, - 'show_subtotal_detail' => (bool) $this->klarna_show_subtotal_detail, - ], - 'merchant_data' => http_build_query(['member' => $this->user->id ?? null]), - ] - ); + $klarnaCheckout->create($this->klarnaOrderData()); $this->cart->klarna_checkout_module = $this->id; } @@ -322,6 +218,110 @@ protected function compile() $this->Template->gui = $klarnaCheckout['html_snippet']; } + private function klarnaOrderData() + { + // Load addresses from address book for logged in members + $shippingAddress = null; + $billingAddress = null; + if (FE_USER_LOGGED_IN) { + if ($address = Address::findDefaultBillingForMember($this->user->id)) { + $shippingAddress = $this->getApiDataFromAddress($address); + } + + if ($address = Address::findDefaultShippingForMember($this->user->id)) { + $billingAddress = $this->getApiDataFromAddress($address); + } + } + + /** @var RouterInterface $router */ + $router = System::getContainer()->get('router'); + + return [ + 'purchase_country' => $this->config->country, + 'purchase_currency' => $this->config->currency, + 'locale' => $this->request->getLocale(), + 'order_amount' => (int) round($this->cart->getTotal() * 100, 0), + 'order_tax_amount' => (int) round( + ($this->cart->getTotal() - $this->cart->getTaxFreeTotal()) * 100, + 0 + ), + 'order_lines' => $this->orderLines(), + 'merchant_urls' => [ + 'terms' => $this->uri($this->klarna_terms_page), + 'cancellation_terms' => $this->uri($this->klarna_cancellation_terms_page), + 'checkout' => $this->uri($this->klarna_checkout_page), + 'confirmation' => sprintf( + '%s?klarna_order_id={checkout.order.id}', + $this->uri($this->klarna_confirmation_page) + ), + 'push' => urldecode( + $router->generate( + 'richardhj.klarna_checkout.push', + ['orderId' => '{checkout.order.id}'], + UrlGeneratorInterface::ABSOLUTE_URL + ) + ), + 'shipping_option_update' => urldecode( + $router->generate( + 'richardhj.klarna_checkout.callback.shipping_option_update', + ['orderId' => '{checkout.order.id}'], + UrlGeneratorInterface::ABSOLUTE_URL + ) + ), + 'address_update' => urldecode( + $router->generate( + 'richardhj.klarna_checkout.callback.address_update', + ['orderId' => '{checkout.order.id}'], + UrlGeneratorInterface::ABSOLUTE_URL + ) + ), + 'country_change' => urldecode( + $router->generate( + 'richardhj.klarna_checkout.callback.country_change', + ['orderId' => '{checkout.order.id}'], + UrlGeneratorInterface::ABSOLUTE_URL + ) + ), + 'validation' => $router->generate( + 'richardhj.klarna_checkout.callback.order_validation', + [], + UrlGeneratorInterface::ABSOLUTE_URL + ), + ], + 'billing_address' => $billingAddress, + 'shipping_address' => $shippingAddress, + 'shipping_options' => $this->shippingOptions( + StringUtil::deserialize($this->iso_shipping_modules, true) + ), + 'shipping_countries' => $this->config->getShippingCountries(), + 'external_payment_methods' => $this->externalPaymentMethods(), + 'options' => [ + 'allow_separate_shipping_address' => [] !== $this->config->getShippingFields(), + 'color_button' => $this->klarna_color_button + ? '#' . $this->klarna_color_button + : null, + 'color_button_text' => $this->klarna_color_button_text + ? '#' . $this->klarna_color_button_text + : null, + 'color_checkbox' => $this->klarna_color_checkbox + ? '#' . $this->klarna_color_checkbox + : null, + 'color_checkbox_checkmark' => $this->klarna_color_checkbox_checkmark + ? '#' . $this->klarna_color_checkbox_checkmark + : null, + 'color_header' => $this->klarna_color_header + ? '#' . $this->klarna_color_header + : null, + 'color_link' => $this->klarna_color_link + ? '#' . $this->klarna_color_link + : null, + 'require_validate_callback_success' => true, + 'show_subtotal_detail' => (bool) $this->klarna_show_subtotal_detail, + ], + 'merchant_data' => http_build_query(['member' => $this->user->id ?? null]), + ]; + } + /** * Process external payment methods. That means, we are listening to the queries "step=complete" und "step=process". *