Skip to content

Commit

Permalink
Push all data when updating an order
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed Nov 13, 2020
1 parent d4caa4b commit c1c8109
Showing 1 changed file with 107 additions and 107 deletions.
214 changes: 107 additions & 107 deletions src/Module/KlarnaCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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".
*
Expand Down

0 comments on commit c1c8109

Please sign in to comment.