Skip to content

Commit

Permalink
Merge pull request #550 from mollie/release/2.15.0
Browse files Browse the repository at this point in the history
Release/2.15.0
  • Loading branch information
Marvin-Magmodules authored Jul 31, 2022
2 parents 895eaa2 + 03abb93 commit f1954cd
Show file tree
Hide file tree
Showing 21 changed files with 732 additions and 257 deletions.
90 changes: 90 additions & 0 deletions Block/Applepay/Shortcut/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Mollie\Payment\Block\Applepay\Shortcut;

use Magento\Catalog\Block\ShortcutInterface;
use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\ScopeInterface;
use Mollie\Payment\Config;

class Button extends Template implements ShortcutInterface
{
protected $_template = 'Mollie_Payment::applepay/minicart/applepay-button.phtml';

/**
* @var Session
*/
private $checkoutSession;

/**
* @var Config
*/
private $config;

public function __construct(
Template\Context $context,
Session $checkoutSession,
Config $config,
array $data = []
) {
parent::__construct($context, $data);
$this->checkoutSession = $checkoutSession;
$this->config = $config;
}

/**
* @return string
*/
public function getAlias(): string
{
return 'mollie.applepay.mini-cart';
}

/**
* @return float|null
*/
public function getBaseGrandTotal(): ?float
{
return $this->checkoutSession->getQuote()->getBaseGrandTotal();
}

/**
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return string
*/
public function getStoreName(): string
{
return $this->_storeManager->getStore()->getName();
}

/**
* @return string
*/
public function getStoreCountry(): string
{
return $this->_scopeConfig->getValue('general/country/default', ScopeInterface::SCOPE_STORE);
}

/**
* @return string
*/
public function getStoreCurrency(): string
{
return $this->_storeManager->getStore()->getCurrentCurrencyCode();
}

public function getButtonClasses(): string
{
$classes = [];
$classes[] = 'mollie-applepay-minicart-button';
$classes[] = 'apple-pay-button';
$classes[] = 'apple-pay-button-color-' . $this->config->applePayMinicartColor();

if ($this->config->applePayMinicartText()) {
$classes[] = 'apple-pay-button-text-' . $this->config->applePayMinicartText();
}

return implode(' ', $classes);
}
}
3 changes: 2 additions & 1 deletion Block/Product/View/ApplePay.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ public function isEnabled(): bool
public function getButtonClasses()
{
$classes = [];
$classes[] = 'mollie-product-page-apple-pay-button';
$classes[] = 'apple-pay-button';
$classes[] = 'apple-pay-button-color-' . $this->config->applePayBuyNowColor();

if ($text = $this->config->applePayBuyNowText()) {
if ($this->config->applePayBuyNowText()) {
$classes[] = 'apple-pay-button-text-' . $this->config->applePayBuyNowText();
}

Expand Down
30 changes: 30 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Config
const PAYMENT_APPLEPAY_BUY_NOW_BUTTON_COLOR = 'payment/mollie_methods_applepay/buy_now_button_color';
const PAYMENT_APPLEPAY_BUY_NOW_BUTTON_TEXT = 'payment/mollie_methods_applepay/buy_now_button_text';
const PAYMENT_APPLEPAY_INTEGRATION_TYPE = 'payment/mollie_methods_applepay/integration_type';
const PAYMENT_APPLEPAY_ENABLE_MINICART_BUTTON = 'payment/mollie_methods_applepay/enable_minicart_button';
const PAYMENT_APPLEPAY_MINICART_BUTTON_COLOR = 'payment/mollie_methods_applepay/minicart_button_color';
const PAYMENT_APPLEPAY_MINICART_BUTTON_TEXT = 'payment/mollie_methods_applepay/minicart_button_text';
const PAYMENT_CREDITCARD_USE_COMPONENTS = 'payment/mollie_methods_creditcard/use_components';
const PAYMENT_CREDITCARD_ENABLE_CUSTOMERS_API = 'payment/mollie_methods_creditcard/enable_customers_api';
const PAYMENT_BANKTRANSFER_STATUS_PENDING = 'payment/mollie_methods_banktransfer/order_status_pending';
Expand Down Expand Up @@ -348,6 +351,33 @@ public function applePayBuyNowText($storeId = null)
return $this->getPath(static::PAYMENT_APPLEPAY_BUY_NOW_BUTTON_TEXT, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
*/
public function applePayEnableMinicartButton($storeId = null)
{
return $this->isSetFlag(static::PAYMENT_APPLEPAY_ENABLE_MINICART_BUTTON, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
*/
public function applePayMinicartColor($storeId = null)
{
return $this->getPath(static::PAYMENT_APPLEPAY_MINICART_BUTTON_COLOR, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
*/
public function applePayMinicartText($storeId = null)
{
return $this->getPath(static::PAYMENT_APPLEPAY_MINICART_BUTTON_TEXT, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
Expand Down
132 changes: 1 addition & 131 deletions Controller/ApplePay/BuyNowPlaceOrder.php
Original file line number Diff line number Diff line change
@@ -1,138 +1,8 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Controller\ApplePay;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\GuestCartRepositoryInterface;
use Magento\Quote\Model\QuoteManagement;
use Magento\Sales\Api\OrderRepositoryInterface;
use Mollie\Payment\Api\Webapi\PaymentTokenRequestInterface;
use Mollie\Payment\Service\PaymentToken\Generate;

class BuyNowPlaceOrder extends Action
class BuyNowPlaceOrder extends PlaceOrder
{
/**
* @var GuestCartRepositoryInterface
*/
private $guestCartRepository;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var QuoteManagement
*/
private $quoteManagement;

/**
* @var Session
*/
private $checkoutSession;

/**
* @var PaymentTokenRequestInterface
*/
private $paymentTokenRequest;

/**
* @var Generate
*/
private $paymentToken;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

public function __construct(
Context $context,
GuestCartRepositoryInterface $guestCartRepository,
CartRepositoryInterface $cartRepository,
QuoteManagement $quoteManagement,
Session $checkoutSession,
Generate $paymentToken,
OrderRepositoryInterface $orderRepository
) {
parent::__construct($context);

$this->guestCartRepository = $guestCartRepository;
$this->cartRepository = $cartRepository;
$this->quoteManagement = $quoteManagement;
$this->checkoutSession = $checkoutSession;
$this->paymentToken = $paymentToken;
$this->orderRepository = $orderRepository;
}

public function execute()
{
$cart = $this->guestCartRepository->get($this->getRequest()->getParam('cartId'));

$shippingAddress = $cart->getShippingAddress();
$this->updateAddress($shippingAddress, $this->getRequest()->getParam('shippingAddress'));
$this->updateAddress($cart->getBillingAddress(), $this->getRequest()->getParam('billingAddress'));

$cart->setCustomerEmail($this->getRequest()->getParam('shippingAddress')['emailAddress']);

$shippingAddress->setShippingMethod($this->getRequest()->getParam('shippingMethod')['identifier']);

$cart->setPaymentMethod('mollie_methods_applepay');
$cart->setCustomerIsGuest(true);

$cart->collectTotals();
$this->cartRepository->save($cart);
$cart->getPayment()->addData(['method' => 'mollie_methods_applepay']);

$order = $this->quoteManagement->submit($cart);
$order->getPayment()->setAdditionalInformation(
'applepay_payment_token',
$this->getRequest()->getParam('applePayPaymentToken')
);

$this->orderRepository->save($order);

$paymentToken = $this->paymentToken->forOrder($order);

$url = $this->_url->getUrl('mollie/checkout/redirect', ['paymentToken' => $paymentToken->getToken()]);

$this->checkoutSession->clearHelperData();
$this->checkoutSession
->setLastQuoteId($cart->getId())
->setLastSuccessQuoteId($cart->getId())
->setLastOrderId($order->getId());

$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
return $response->setData(['url' => $url]);
}

private function updateAddress(AddressInterface $address, array $input)
{
$address->addData([
AddressInterface::KEY_STREET => implode(PHP_EOL, $input['addressLines']),
AddressInterface::KEY_COUNTRY_ID => $input['countryCode'],
AddressInterface::KEY_LASTNAME => $input['familyName'],
AddressInterface::KEY_FIRSTNAME => $input['givenName'],
AddressInterface::KEY_CITY => $input['locality'],
AddressInterface::KEY_POSTCODE => $input['postalCode'],
]);

if (isset($input['phoneNumber'])) {
$address->setTelephone($input['phoneNumber']);
}

if ($address->getAddressType() == \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_BILLING) {
$input = $this->getRequest()->getParam('shippingAddress');
$address->setTelephone($input['phoneNumber']);
}
}
}
Loading

0 comments on commit f1954cd

Please sign in to comment.