Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #39 from heidelpay/develop
Browse files Browse the repository at this point in the history
Archive old repository
  • Loading branch information
Simon Gabriel authored Nov 25, 2020
2 parents 4fefa7a + a2995b6 commit b74b470
Show file tree
Hide file tree
Showing 29 changed files with 201 additions and 99 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# *** ATTENTION ***
Due to the recent re-branding of the company `heidelpay GmbH` to `Unzer GmbH`this repository is replaced by https://github.com/unzerdev/shopware6
and the old heidelpay repository will soon be archived.

Please switch to the Unzer Shopware 6 plugin https://packagist.org/packages/unzerdev/shopware6

# HeidelPayment

Heidelpay payment integration for Shopware 6.2 including the following payment methods:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "heidelpay/shopware-6",
"description": "Heidelpay payment integration for Shopware 6",
"version": "0.0.1.2",
"version": "0.0.1.4",
"type": "shopware-platform-plugin",
"license": "MIT",
"minimum-stability": "dev",
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/RegisterWebhookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(WebhookRegistrator $webhookRegistrator, EntityReposi
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setName('heidelpay:register-webhooks');
$this->setDescription('Registers the heidelpay webhook');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public function pay(
$currentRequest = $this->getCurrentRequestFromStack($transaction->getOrderTransaction()->getId());

if (!$this->isPaymentAllowed($transaction->getOrderTransaction()->getId())) {
throw new AsyncPaymentProcessException(
$transaction->getOrderTransaction()->getId(),
'SEPA direct debit mandate has not been accepted by the customer.'
);
throw new AsyncPaymentProcessException($transaction->getOrderTransaction()->getId(), 'SEPA direct debit mandate has not been accepted by the customer.');
}

$registerDirectDebit = $this->pluginConfig->get(ConfigReader::CONFIG_KEY_REGISTER_DIRECT_DEBIT, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function pay(
parent::pay($transaction, $dataBag, $salesChannelContext);

if (!$this->isPaymentAllowed($transaction->getOrderTransaction()->getId())) {
throw new AsyncPaymentProcessException(
$transaction->getOrderTransaction()->getId(),
'SEPA direct debit mandate has not been accepted by the customer.'
);
throw new AsyncPaymentProcessException($transaction->getOrderTransaction()->getId(), 'SEPA direct debit mandate has not been accepted by the customer.');
}

$registerDirectDebit = $this->pluginConfig->get(ConfigReader::CONFIG_KEY_REGISTER_DIRECT_DEBIT, false);
Expand Down
5 changes: 1 addition & 4 deletions src/Components/PaymentHandler/HeidelIdealPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public function pay(

return new RedirectResponse($returnUrl);
} catch (HeidelpayApiException $apiException) {
throw new AsyncPaymentProcessException(
$transaction->getOrderTransaction()->getId(),
$apiException->getClientMessage()
);
throw new AsyncPaymentProcessException($transaction->getOrderTransaction()->getId(), $apiException->getClientMessage());
}
}
}
94 changes: 77 additions & 17 deletions src/Components/ResourceHydrator/BasketResourceHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use heidelpayPHP\Resources\EmbeddedResources\BasketItem;
use InvalidArgumentException;
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct;
use Shopware\Core\Checkout\Promotion\Cart\PromotionProcessor;
Expand All @@ -31,33 +32,44 @@ public function hydrateObject(
$currencyPrecision = $transaction->getOrder()->getCurrency() !== null ? $transaction->getOrder()->getCurrency()->getDecimalPrecision() : 4;
$currencyPrecision = min($currencyPrecision, 4);

$amountTotalVat = round($transaction->getOrder()->getAmountTotal() - $transaction->getOrder()->getAmountNet(), $currencyPrecision);

if ($transaction instanceof AsyncPaymentTransactionStruct) {
$transactionId = $transaction->getOrderTransaction()->getId();
} else {
$transactionId = $transaction->getId();
}

$amountTotalDiscount = 0;
$amountTotalGross = 0;
$amountTotalVat = 0;

$heidelBasket = new Basket(
$transactionId,
round($transaction->getOrder()->getAmountTotal(), $currencyPrecision),
$channelContext->getCurrency()->getIsoCode()
);

$heidelBasket->setAmountTotalVat($amountTotalVat);
$heidelBasket->setAmountTotalDiscount($amountTotalDiscount);

if (null === $transaction->getOrder()->getLineItems()) {
return $heidelBasket;
}

/** @var OrderLineItemEntity $lineItem */
foreach ($transaction->getOrder()->getLineItems() as $lineItem) {
$amountDiscount = 0;
$amountGross = 0;

$type = $lineItem->getType();

if ($lineItem->getPrice() === null) {
$heidelBasket->addBasketItem(new BasketItem(
$lineItem->getLabel(),
round($this->getAmountByItemType($lineItem->getType(), $lineItem->getTotalPrice()), $currencyPrecision),
round($this->getAmountByItemType($lineItem->getType(), $lineItem->getUnitPrice()), $currencyPrecision),
$lineItem->getQuantity())
$heidelBasket->addBasketItem(
new BasketItem(
$lineItem->getLabel(),
round($this->getAmountByItemType($type, $lineItem->getTotalPrice()), $currencyPrecision),
round($this->getAmountByItemType($type, $lineItem->getUnitPrice()), $currencyPrecision),
$lineItem->getQuantity()
)
);

continue;
Expand All @@ -66,13 +78,33 @@ public function hydrateObject(
$amountTax = 0;
$taxRate = 0.0;
foreach ($lineItem->getPrice()->getCalculatedTaxes() as $tax) {
$amountTax += round($this->getAmountByItemType($lineItem->getType(), $tax->getTax()), $currencyPrecision);
$amountTax += round($this->getAmountByItemType($type, $tax->getTax()), $currencyPrecision);
$taxRate += $tax->getTaxRate();
}

$unitPrice = round($this->getAmountByItemType($lineItem->getType(), $lineItem->getUnitPrice()), $currencyPrecision);
$amountGross = round($this->getAmountByItemType($lineItem->getType(), $lineItem->getTotalPrice()), $currencyPrecision);
$amountNet = round($amountGross - $amountTax, $currencyPrecision);
if ($this->isPromotionLineItemType($type)) {
$unitPrice = 0;
$amountGross = 0;
$amountNet = 0;
$amountTax = 0;
$taxRate = 0;
$amountDiscount = round($this->getAmountByItemType($type, $lineItem->getTotalPrice()), $currencyPrecision);
} else {
$unitPrice = round($this->getAmountByItemType($type, $lineItem->getUnitPrice()), $currencyPrecision);
$amountGross = round($this->getAmountByItemType($type, $lineItem->getTotalPrice()), $currencyPrecision);
$amountNet = round($amountGross - $amountTax, $currencyPrecision);

if ($lineItem->getProduct() !== null) {
$product = $lineItem->getProduct();
$amountDiscount = round(($product->getPrice() - $lineItem->getTotalPrice()) * -1, $currencyPrecision);
} else {
$amountDiscount = 0;
}
}

$amountTotalDiscount += $amountDiscount;
$amountTotalGross += $amountGross;
$amountTotalVat += $amountTax;

$basketItem = new BasketItem(
$lineItem->getLabel(),
Expand All @@ -82,9 +114,10 @@ public function hydrateObject(
);

$basketItem->setVat($taxRate);
$basketItem->setType($this->getMappedLineItemType($lineItem->getType()));
$basketItem->setType($this->getMappedLineItemType($type));
$basketItem->setAmountVat($amountTax);
$basketItem->setAmountGross($amountGross);
$basketItem->setAmountDiscount($amountDiscount);
$basketItem->setImageUrl($lineItem->getCover() ? $lineItem->getCover()->getUrl() : null);

$heidelBasket->addBasketItem($basketItem);
Expand All @@ -94,15 +127,22 @@ public function hydrateObject(
$transaction,
$heidelBasket,
$currencyPrecision,
$channelContext->getShippingMethod()->getName()
$channelContext->getShippingMethod()->getName(),
$amountTotalDiscount,
$amountTotalGross,
$amountTotalVat
);

$heidelBasket->setAmountTotalDiscount($amountTotalDiscount);
$heidelBasket->setAmountTotalGross($amountTotalGross);
$heidelBasket->setAmountTotalVat($amountTotalVat);

return $heidelBasket;
}

protected function getAmountByItemType(string $type, float $price): float
{
if ($type === PromotionProcessor::LINE_ITEM_TYPE) {
if ($this->isPromotionLineItemType($type)) {
return $price * -1;
}

Expand All @@ -111,18 +151,30 @@ protected function getAmountByItemType(string $type, float $price): float

protected function getMappedLineItemType(string $type): string
{
if ($type === PromotionProcessor::LINE_ITEM_TYPE) {
if ($this->isPromotionLineItemType($type)) {
return BasketItemTypes::VOUCHER;
}

return BasketItemTypes::GOODS;
}

private function isPromotionLineItemType(string $type): bool
{
return $type === PromotionProcessor::LINE_ITEM_TYPE;
}

/**
* @param AsyncPaymentTransactionStruct|OrderTransactionEntity $transaction
*/
private function hydrateShippingCosts($transaction, Basket $basket, int $currencyPrecision, string $shippingMethodName): void
{
private function hydrateShippingCosts(
$transaction,
Basket $basket,
int $currencyPrecision,
string $shippingMethodName,
float &$amountTotalDiscount,
float &$amountTotalGross,
float &$amountTotalVat
): void {
$shippingCosts = $transaction->getOrder()->getShippingCosts();

if ($transaction->getOrder()->getTaxStatus() === CartPrice::TAX_STATE_FREE) {
Expand All @@ -134,6 +186,10 @@ private function hydrateShippingCosts($transaction, Basket $basket, int $currenc
$dispatchBasketItem->setAmountNet(round($shippingCosts->getTotalPrice(), $currencyPrecision));
$dispatchBasketItem->setQuantity($shippingCosts->getQuantity());

$amountTotalDiscount += $dispatchBasketItem->getAmountDiscount();
$amountTotalGross += $dispatchBasketItem->getAmountGross();
$amountTotalVat += $dispatchBasketItem->getAmountVat();

$basket->addBasketItem($dispatchBasketItem);

return;
Expand All @@ -156,6 +212,10 @@ private function hydrateShippingCosts($transaction, Basket $basket, int $currenc
$dispatchBasketItem->setQuantity($shippingCosts->getQuantity());
$dispatchBasketItem->setVat($tax->getTaxRate());

$amountTotalDiscount += $dispatchBasketItem->getAmountDiscount();
$amountTotalGross += $dispatchBasketItem->getAmountGross();
$amountTotalVat += $dispatchBasketItem->getAmountVat();

$basket->addBasketItem($dispatchBasketItem);
}
}
Expand Down
30 changes: 28 additions & 2 deletions src/Components/ResourceHydrator/MetadataResourceHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,52 @@

use heidelpayPHP\Resources\AbstractHeidelpayResource;
use heidelpayPHP\Resources\Metadata;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin\PluginEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class MetadataResourceHydrator implements ResourceHydratorInterface
{
public const PLUGIN_NAME = 'HeidelPayment6';

/** @var string */
private $shopwareVersion;

public function __construct(string $shopwareVersion)
/** @var EntityRepositoryInterface */
private $pluginRepository;

public function __construct(string $shopwareVersion, EntityRepositoryInterface $pluginRepository)
{
$this->shopwareVersion = $shopwareVersion;
$this->shopwareVersion = $shopwareVersion;
$this->pluginRepository = $pluginRepository;
}

public function hydrateObject(
SalesChannelContext $channelContext,
$transaction = null
): AbstractHeidelpayResource {
$pluginData = $this->getPluginData($channelContext->getContext());

$heidelMetadata = new Metadata();
$heidelMetadata->setShopType('Shopware 6');
$heidelMetadata->setShopVersion($this->shopwareVersion);

if ($pluginData !== null) {
$heidelMetadata->addMetadata('pluginVersion', $pluginData->getVersion());
}
$heidelMetadata->addMetadata('pluginType', self::PLUGIN_NAME);

return $heidelMetadata;
}

protected function getPluginData(Context $context): ?PluginEntity
{
$pluginSearchCriteria = new Criteria();
$pluginSearchCriteria->addFilter(new EqualsFilter('name', self::PLUGIN_NAME));

return $this->pluginRepository->search($pluginSearchCriteria, $context)->first();
}
}
11 changes: 5 additions & 6 deletions src/Components/WebhookRegistrator/WebhookRegistrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use HeidelPayment6\Components\ClientFactory\ClientFactoryInterface;
use heidelpayPHP\Exceptions\HeidelpayApiException;
use heidelpayPHP\Heidelpay;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
Expand All @@ -30,8 +29,8 @@ class WebhookRegistrator implements WebhookRegistratorInterface
/** @var null|RequestContext */
protected $context;

/** @var Heidelpay */
private $client;
/** @var ClientFactoryInterface */
private $clientFactory;

/** @var Router */
private $router;
Expand All @@ -48,7 +47,7 @@ public function __construct(
EntityRepositoryInterface $salesChannelRepository,
LoggerInterface $logger
) {
$this->client = $clientFactory->createClient();
$this->clientFactory = $clientFactory;
$this->router = $router;
$this->salesChannelRepository = $salesChannelRepository;
$this->logger = $logger;
Expand All @@ -70,7 +69,7 @@ public function registerWebhook(RequestDataBag $salesChannelDomains): array

try {
$url = $this->router->generate('heidelpay.webhook.execute', [], UrlGeneratorInterface::ABSOLUTE_URL);
$result = $this->client->createWebhook($url, 'all');
$result = $this->clientFactory->createClient()->createWebhook($url, 'all');

$returnData[$salesChannelDomain->get('url', '')] = [
'success' => true,
Expand Down Expand Up @@ -112,7 +111,7 @@ public function clearWebhooks(RequestDataBag $salesChannelDomains): array
}

try {
$this->client->deleteAllWebhooks();
$this->clientFactory->createClient()->deleteAllWebhooks();

$returnData[$salesChannelDomain->get('url', '')] = [
'success' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;

/**
* @method void add(HeidelpayPaymentDeviceEntity $entity)
* @method void set(string $key, HeidelpayPaymentDeviceEntity $entity)
* @method void add(HeidelpayPaymentDeviceEntity $entity)
* @method void set(string $key, HeidelpayPaymentDeviceEntity $entity)
* @method HeidelpayPaymentDeviceEntity[] getIterator()
* @method HeidelpayPaymentDeviceEntity[] getElements()
* @method null|HeidelpayPaymentDeviceEntity get(string $key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;

/**
* @method void add(HeidelpayTransferInfoEntity $entity)
* @method void set(string $key, HeidelpayTransferInfoEntity $entity)
* @method void add(HeidelpayTransferInfoEntity $entity)
* @method void set(string $key, HeidelpayTransferInfoEntity $entity)
* @method HeidelpayTransferInfoEntity[] getIterator()
* @method HeidelpayTransferInfoEntity[] getElements()
* @method null|HeidelpayTransferInfoEntity get(string $key)
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/hydrators.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="HeidelPayment6\Components\ResourceHydrator\BasketResourceHydrator"/>

<service id="HeidelPayment6\Components\ResourceHydrator\BusinessCustomerResourceHydrator"/>

<service id="HeidelPayment6\Components\ResourceHydrator\CustomerResourceHydrator"/>

<service id="HeidelPayment6\Components\ResourceHydrator\MetadataResourceHydrator">
<argument>%kernel.shopware_version%</argument>
<argument type="service" id="plugin.repository" />
</service>

<service id="HeidelPayment6\Components\ResourceHydrator\BusinessCustomerResourceHydrator"/>

<service id="HeidelPayment6\Components\ArrayHydrator\PaymentArrayHydrator" />
</services>
</container>
Loading

0 comments on commit b74b470

Please sign in to comment.