Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions controllers/front/applePayDirectAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ public function postProcess()
switch ($action) {
case 'mollie_apple_pay_validation':
$this->getApplePaySession();
// no break
break;
case 'mollie_apple_pay_update_shipping_contact':
$this->updateAppleShippingContact();
// no break
break;
case 'mollie_apple_pay_update_shipping_method':
$this->updateShippingMethod();
// no break
break;
case 'mollie_apple_pay_create_order':
$this->createApplePayOrder();
// no break
break;
case 'mollie_apple_pay_get_total_price':
$this->getTotalApplePayCartPrice();
// no break
break;
case 'mollie_apple_pay_remove_from_cart':
$this->removeProductFromCart();
break;
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));
Expand Down Expand Up @@ -268,4 +269,5 @@ private function getWantedCartProducts(int $cartId)

return $products;
}

}
26 changes: 26 additions & 0 deletions src/Application/CommandHandler/CreateApplePayOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public function __construct(
public function handle(CreateApplePayOrder $command): array
{
$cart = new Cart($command->getCartId());

if ($cart->id_address_delivery == $cart->id_address_invoice) {
$invoiceAddress = $this->duplicateAddress($cart->id_address_invoice);
$cart->id_address_invoice = $invoiceAddress->id;
$cart->update();
}

$this->updateCardInfo($cart->id_address_delivery, $command->getOrder()->getShippingContent());
$this->updateCardInfo($cart->id_address_invoice, $command->getOrder()->getBillingContent());
$this->updateCustomer($cart->id_customer, $command->getOrder()->getShippingContent());
Expand Down Expand Up @@ -229,6 +236,25 @@ private function createMollieTransaction(Cart $cart, string $cardToken)
return $this->mollieOrderCreationService->createMollieApplePayDirectOrder($paymentData, $paymentMethodObj);
}

private function duplicateAddress(int $addressId): Address
{
$original = new Address($addressId);
$copy = new Address();
$copy->id_customer = $original->id_customer;
$copy->alias = $original->alias;
$copy->firstname = $original->firstname;
$copy->lastname = $original->lastname;
$copy->address1 = $original->address1;
$copy->address2 = $original->address2;
$copy->city = $original->city;
$copy->postcode = $original->postcode;
$copy->id_country = $original->id_country;
$copy->country = $original->country;
$copy->add();

return $copy;
}

private function deleteAddress(int $addressId)
{
$address = new Address($addressId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public function __construct(

public function handle(UpdateApplePayShippingContact $command): array
{
$customer = $this->createCustomer($command->getCustomerId());
$deliveryAddress = $this->createAddress($customer->id, $command);
$invoiceAddress = $this->createAddress($customer->id, $command);
$cart = $this->updateCart($customer, $deliveryAddress->id, $invoiceAddress->id, $command->getCartId());
$cart = new Cart($command->getCartId());
$customer = $this->getOrCreateCustomer($command->getCustomerId(), $cart);
$deliveryAddress = $this->getOrCreateAddress($cart->id_address_delivery, $customer->id, $command);
$invoiceAddress = $this->getOrCreateAddress($cart->id_address_invoice, $customer->id, $command);
$this->updateCart($cart, $customer, $deliveryAddress->id, $invoiceAddress->id);
$this->addProductToCart($cart, $command);
$cart = new Cart($cart->id);
$this->updateContext($cart, $customer);
Expand Down Expand Up @@ -93,8 +94,22 @@ public function handle(UpdateApplePayShippingContact $command): array
];
}

private function createAddress(int $customerId, UpdateApplePayShippingContact $command): Address
private function getOrCreateAddress(int $existingAddressId, int $customerId, UpdateApplePayShippingContact $command): Address
{
if ($existingAddressId) {
$address = new Address($existingAddressId);
if ($address->id && $address->alias === 'applePay') {
$address->postcode = $command->getPostalCode();
$address->id_country = Country::getByIso($command->getCountryCode());
$address->country = $command->getCountry();
$address->city = $command->getLocality();
$address->id_customer = $customerId;
$address->update();

return $address;
}
}

$address = new Address();
$address->address1 = 'ApplePay';
$address->lastname = 'ApplePay';
Expand All @@ -110,12 +125,16 @@ private function createAddress(int $customerId, UpdateApplePayShippingContact $c
return $address;
}

private function createCustomer(int $customerId): Customer
private function getOrCreateCustomer(int $customerId, Cart $cart): Customer
{
if ($customerId) {
return new Customer($customerId);
}

if ($cart->id_customer) {
return new Customer($cart->id_customer);
}

if (!Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) {
throw GuestCheckoutNotAvailableException::guestCheckoutDisabled();
}
Expand All @@ -124,23 +143,20 @@ private function createCustomer(int $customerId): Customer
$customer->is_guest = true;
$customer->firstname = 'applePay';
$customer->lastname = 'applePay';
$customer->email = 'applePay@mollie.com';
$customer->email = 'applepay-' . (int) $cart->id . '@mollie.com';
$customer->passwd = Tools::hash(microtime());
$customer->add();

return $customer;
}

private function updateCart(Customer $customer, int $deliveryAddressId, int $invoiceAddressId, int $cartId): cart
private function updateCart(Cart $cart, Customer $customer, int $deliveryAddressId, int $invoiceAddressId): void
{
$cart = new Cart($cartId);
$cart->secure_key = $customer->secure_key;
$cart->id_address_delivery = $deliveryAddressId;
$cart->id_address_invoice = $invoiceAddressId;
$cart->id_customer = $customer->id;
$cart->update();

return $cart;
}

private function addProductToCart(Cart $cart, UpdateApplePayShippingContact $command)
Expand Down
Loading