-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from PAYONE-GmbH/feature/PAYONE-80
[PAYONE-80] Add secure invoice payment
- Loading branch information
Showing
48 changed files
with
1,425 additions
and
559 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
src/Components/Hydrator/LineItemHydrator/LineItemHydrator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\Components\Hydrator\LineItemHydrator; | ||
|
||
use Exception; | ||
use Shopware\Core\Checkout\Cart\LineItem\LineItem; | ||
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection; | ||
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection; | ||
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity; | ||
use Shopware\Core\Checkout\Promotion\Cart\PromotionProcessor; | ||
use Shopware\Core\System\Currency\CurrencyEntity; | ||
|
||
class LineItemHydrator implements LineItemHydratorInterface | ||
{ | ||
public const TYPE_GOODS = 'goods'; | ||
public const TYPE_VOUCHER = 'voucher'; | ||
|
||
public function mapPayoneOrderLinesByRequest( | ||
CurrencyEntity $currency, | ||
OrderLineItemCollection $orderLineItems, | ||
array $requestLines | ||
): array { | ||
$requestLineItems = []; | ||
$counter = 0; | ||
|
||
foreach ($requestLines as $orderLine) { | ||
if (!array_key_exists('id', $orderLine)) { | ||
continue; | ||
} | ||
|
||
$lineItem = $orderLineItems->get($orderLine['id']); | ||
|
||
if ($lineItem === null) { | ||
continue; | ||
} | ||
|
||
if ($this->isCustomizedProduct($lineItem)) { | ||
continue; | ||
} | ||
|
||
$taxes = $lineItem->getPrice() ? $lineItem->getPrice()->getCalculatedTaxes() : null; | ||
|
||
if (null === $taxes || null === $taxes->first()) { | ||
continue; | ||
} | ||
|
||
$requestLineItems = array_merge( | ||
$requestLineItems, | ||
$this->getLineItemRequest( | ||
++$counter, | ||
$lineItem, | ||
$currency, | ||
$taxes, | ||
$orderLine['quantity'] | ||
) | ||
); | ||
} | ||
|
||
return $requestLineItems; | ||
} | ||
|
||
public function mapOrderLines(CurrencyEntity $currency, OrderLineItemCollection $lineItemCollection): array | ||
{ | ||
$requestLineItems = []; | ||
$counter = 0; | ||
|
||
/** @var OrderLineItemEntity $lineItem */ | ||
foreach ($lineItemCollection as $lineItem) { | ||
if ($this->isCustomizedProduct($lineItem)) { | ||
continue; | ||
} | ||
|
||
$taxes = $lineItem->getPrice() ? $lineItem->getPrice()->getCalculatedTaxes() : null; | ||
|
||
if (null === $taxes || null === $taxes->first()) { | ||
continue; | ||
} | ||
|
||
$requestLineItems = array_merge( | ||
$requestLineItems, | ||
$this->getLineItemRequest( | ||
++$counter, | ||
$lineItem, | ||
$currency, | ||
$taxes, | ||
$lineItem->getQuantity() | ||
) | ||
); | ||
} | ||
|
||
return $requestLineItems; | ||
} | ||
|
||
protected function mapItemType(?string $itemType): string | ||
{ | ||
if ($itemType === LineItem::CREDIT_LINE_ITEM_TYPE) { | ||
return self::TYPE_VOUCHER; | ||
} | ||
|
||
if ($itemType === PromotionProcessor::LINE_ITEM_TYPE) { | ||
return self::TYPE_VOUCHER; | ||
} | ||
|
||
return self::TYPE_GOODS; | ||
} | ||
|
||
private function isCustomizedProduct(OrderLineItemEntity $lineItemEntity): bool | ||
{ | ||
try { | ||
/** @phpstan-ignore-next-line */ | ||
if (class_exists('Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector') && | ||
CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE === $lineItemEntity->getType( | ||
) && | ||
null === $lineItemEntity->getParentId()) { | ||
return true; | ||
} | ||
} catch (Exception $exception) { | ||
// Catch class not found if SwagCustomizedProducts plugin is not installed | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function getLineItemRequest(int $index, OrderLineItemEntity $lineItemEntity, CurrencyEntity $currencyEntity, CalculatedTaxCollection $taxCollection, int $quantity): array | ||
{ | ||
$productNumber = is_array($lineItemEntity->getPayload()) && array_key_exists('productNumber', $lineItemEntity->getPayload()) | ||
? $lineItemEntity->getPayload()['productNumber'] | ||
: $lineItemEntity->getIdentifier(); | ||
|
||
return [ | ||
'it[' . $index . ']' => $this->mapItemType($lineItemEntity->getType()), | ||
'id[' . $index . ']' => $productNumber, | ||
'pr[' . $index . ']' => (int) round($lineItemEntity->getUnitPrice() * (10 ** $currencyEntity->getDecimalPrecision())), | ||
'no[' . $index . ']' => $quantity, | ||
'de[' . $index . ']' => $lineItemEntity->getLabel(), | ||
'va[' . $index . ']' => (int) round($taxCollection->first()->getTaxRate() * (10 ** $currencyEntity->getDecimalPrecision())), | ||
]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Components/Hydrator/LineItemHydrator/LineItemHydratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\Components\Hydrator\LineItemHydrator; | ||
|
||
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection; | ||
use Shopware\Core\System\Currency\CurrencyEntity; | ||
|
||
interface LineItemHydratorInterface | ||
{ | ||
public function mapPayoneOrderLinesByRequest( | ||
CurrencyEntity $currency, | ||
OrderLineItemCollection $orderLineItems, | ||
array $requestLines | ||
): array; | ||
|
||
public function mapOrderLines(CurrencyEntity $currency, OrderLineItemCollection $lineItemCollection): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Components/RequestBuilder/SecureInvoiceRequestBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\Components\RequestBuilder; | ||
|
||
use PayonePayment\PaymentMethod\PayoneSecureInvoice; | ||
use PayonePayment\Struct\PaymentTransaction; | ||
use Shopware\Core\Framework\Context; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
|
||
class SecureInvoiceRequestBuilder extends AbstractRequestBuilder | ||
{ | ||
public function supports(string $paymentMethodId): bool | ||
{ | ||
return $paymentMethodId === PayoneSecureInvoice::UUID; | ||
} | ||
|
||
public function getAdditionalRequestParameters(PaymentTransaction $transaction, Context $context, ParameterBag $parameterBag): array | ||
{ | ||
$currency = $transaction->getOrder()->getCurrency(); | ||
$orderLines = $parameterBag->get('orderLines', []); | ||
|
||
if ($currency === null || empty($orderLines) || empty($transaction->getOrder()->getLineItems())) { | ||
return []; | ||
} | ||
|
||
return $this->lineItemHydrator->mapPayoneOrderLinesByRequest($currency, $transaction->getOrder()->getLineItems(), $orderLines); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
use PayonePayment\PaymentHandler\PayonePaypalExpressPaymentHandler; | ||
use PayonePayment\PaymentHandler\PayonePaypalPaymentHandler; | ||
use PayonePayment\PaymentHandler\PayonePrepaymentPaymentHandler; | ||
use PayonePayment\PaymentHandler\PayoneSecureInvoicePaymentHandler; | ||
use PayonePayment\PaymentHandler\PayoneSofortBankingPaymentHandler; | ||
use PayonePayment\Payone\Client\Exception\PayoneRequestException; | ||
use PayonePayment\Payone\Client\PayoneClientInterface; | ||
|
@@ -327,6 +328,28 @@ private function getPaymentParameters(string $paymentClass): array | |
]; | ||
|
||
break; | ||
case PayoneSecureInvoicePaymentHandler::class: | ||
return [ | ||
'request' => 'preauthorization', | ||
'clearingtype' => 'rec', | ||
'financingtype' => 'POV', | ||
'amount' => 10000, | ||
'currency' => 'EUR', | ||
'reference' => sprintf('%s%d', self::REFERENCE_PREFIX_TEST, random_int(1000000000000, 9999999999999)), | ||
'birthday' => '19900505', | ||
'firstname' => 'Test', | ||
'lastname' => 'Test', | ||
'country' => 'DE', | ||
'email' => '[email protected]', | ||
'street' => 'teststreet 2', | ||
'zip' => '12345', | ||
'city' => 'Test', | ||
'ip' => '127.0.0.1', | ||
'businessrelation' => 'b2c', | ||
]; | ||
|
||
break; | ||
|
||
default: | ||
$this->logger->error(sprintf('There is no test data defined for payment class %s', $paymentClass)); | ||
throw new RuntimeException(sprintf('There is no test data defined for payment class %s', $paymentClass)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.