Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEXT-30813 - Checkout gateway #18

Merged
merged 1 commit into from
May 24, 2024
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
},
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"lcobucci/jwt": "^4.0 || ^5.0",
"lcobucci/clock": "^3",
"lcobucci/jwt": "^4.0 || ^5.0",
"php-http/discovery": "^1.17",
"psr/clock-implementation": "*",
"psr/event-dispatcher": "^1.0",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "*",
"psr/http-factory": "^1.0",
"psr/http-factory-implementation": "*",
"psr/http-message": "^1.0 || ^2.0",
"psr/simple-cache": "^3.0",
"psr/clock-implementation": "*"
"psr/simple-cache": "^3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.16",
Expand Down
2 changes: 1 addition & 1 deletion infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
'Shopware\\App\\SDK\\Registration\\RegistrationService::registerConfirm',
'Shopware\\App\\SDK\\Shop\\ShopResolver::resolveFromSource'
]
}
},
}
}
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
Expand Down
8 changes: 2 additions & 6 deletions src/Authentication/RequestVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,15 @@ public function authenticateStorefrontRequest(RequestInterface $request, ShopInt
*/
private function getSignatureFromQuery(RequestInterface $request): string
{
parse_str($request->getUri()->getQuery(), $queries);
\parse_str($request->getUri()->getQuery(), $queries);

if (!isset($queries[self::SHOPWARE_SHOP_SIGNATURE_HEADER])) {
throw new SignatureNotFoundException($request);
}

/** @var string $header */
$header = $queries[self::SHOPWARE_SHOP_SIGNATURE_HEADER];

if (!is_string($header)) {
/** @infection-ignore-all */
throw new SignatureNotFoundException($request);
}

return $header;
}

Expand Down
10 changes: 9 additions & 1 deletion src/Context/ArrayStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Shopware\App\SDK\Context;

abstract class ArrayStruct
abstract class ArrayStruct implements \JsonSerializable
{
/**
* @param array<mixed> $data
Expand All @@ -20,4 +20,12 @@ public function toArray(): array
{
return $this->data;
}

/**
* @return array<mixed>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}
52 changes: 36 additions & 16 deletions src/Context/Cart/CalculatedPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\App\SDK\Context\Cart;

use Shopware\App\SDK\Context\ArrayStruct;
use Shopware\App\SDK\Framework\Collection;

class CalculatedPrice extends ArrayStruct
{
Expand All @@ -27,38 +28,57 @@ public function getQuantity(): int
}

/**
* @return array<CalculatedTax>
* @return Collection<CalculatedTax>
*/
public function getCalculatedTaxes(): array
public function getCalculatedTaxes(): Collection
{
\assert(is_array($this->data['calculatedTaxes']));
return array_map(static function (array $calculatedTax): CalculatedTax {
return new CalculatedTax($calculatedTax);
}, $this->data['calculatedTaxes']);

return new Collection(
\array_map(
static fn (array $tax) => new CalculatedTax($tax),
$this->data['calculatedTaxes']
)
);
}

/**
* @return array<TaxRule>
* @return Collection<TaxRule>
*/
public function getTaxRules(): array
public function getTaxRules(): Collection
{
\assert(is_array($this->data['taxRules']));
return array_map(static function (array $taxRule): TaxRule {
return new TaxRule($taxRule);
}, $this->data['taxRules']);

return new Collection(
\array_map(
static fn (array $rule) => new TaxRule($rule),
$this->data['taxRules']
)
);
}

/**
* @param array<CalculatedPrice> $prices
* @param Collection<CalculatedPrice> $prices
*/
public static function sum(array $prices): CalculatedPrice
public static function sum(Collection $prices): CalculatedPrice
{
/** @var array<array<CalculatedTax>> $allTaxes */
$allTaxes = $prices->map(static fn (CalculatedPrice $price) => $price->getCalculatedTaxes()->all());

$taxSum = CalculatedTax::sum(new Collection(array_merge(...$allTaxes)));

$rules = [];

foreach ($prices as $price) {
$rules = array_merge($rules, $price->getTaxRules()->jsonSerialize());
}

return new CalculatedPrice([
'unitPrice' => array_sum(array_map(static fn (CalculatedPrice $price): float => $price->getUnitPrice(), $prices)),
'totalPrice' => array_sum(array_map(static fn (CalculatedPrice $price): float => $price->getTotalPrice(), $prices)),
'unitPrice' => \array_sum($prices->map(static fn (CalculatedPrice $price): float => $price->getUnitPrice())),
'totalPrice' => \array_sum($prices->map(static fn (CalculatedPrice $price): float => $price->getTotalPrice())),
'quantity' => 1,
'calculatedTaxes' => array_map(static fn (CalculatedTax $tax) => $tax->toArray(), CalculatedTax::sum(array_merge(...array_map(static fn (CalculatedPrice $price): array => $price->getCalculatedTaxes(), $prices)))),
'taxRules' => array_map(static fn (TaxRule $rule) => $rule->toArray(), array_merge(...array_map(static fn (CalculatedPrice $price): array => $price->getTaxRules(), $prices))),
'calculatedTaxes' => $taxSum->map(static fn (CalculatedTax $tax) => $tax->toArray()),
'taxRules' => $rules,
]);
}
}
9 changes: 5 additions & 4 deletions src/Context/Cart/CalculatedTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\App\SDK\Context\Cart;

use Shopware\App\SDK\Context\ArrayStruct;
use Shopware\App\SDK\Framework\Collection;

class CalculatedTax extends ArrayStruct
{
Expand All @@ -27,10 +28,10 @@ public function getTax(): float
}

/**
* @param array<CalculatedTax> $calculatedTaxes
* @return array<CalculatedTax>
* @param Collection<CalculatedTax> $calculatedTaxes
* @return Collection<CalculatedTax>
*/
public static function sum(array $calculatedTaxes): array
public static function sum(Collection $calculatedTaxes): Collection
{
$new = [];

Expand All @@ -49,6 +50,6 @@ public static function sum(array $calculatedTaxes): array
]);
}

return $new;
return new Collection($new);
}
}
34 changes: 19 additions & 15 deletions src/Context/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\App\SDK\Context\Cart;

use Shopware\App\SDK\Context\ArrayStruct;
use Shopware\App\SDK\Framework\Collection;

class Cart extends ArrayStruct
{
Expand Down Expand Up @@ -33,39 +34,42 @@ public function getCampaignCode(): ?string
}

/**
* @return array<LineItem>
* @return Collection<LineItem>
*/
public function getLineItems(): array
public function getLineItems(): Collection
{
\assert(is_array($this->data['lineItems']));
return array_map(
\assert(\is_array($this->data['lineItems']));

return new Collection(\array_map(
static fn (array $lineItem) => new LineItem($lineItem),
$this->data['lineItems']
);
));
}

/**
* @return array<Delivery>
* @return Collection<Delivery>
*/
public function getDeliveries(): array
public function getDeliveries(): Collection
{
\assert(is_array($this->data['deliveries']));
return array_map(
\assert(\is_array($this->data['deliveries']));

return new Collection(\array_map(
static fn (array $delivery) => new Delivery($delivery),
$this->data['deliveries']
);
));
}

/**
* @return array<CartTransaction>
* @return Collection<CartTransaction>
*/
public function getTransactions(): array
public function getTransactions(): Collection
{
\assert(is_array($this->data['transactions']));
return array_map(
\assert(\is_array($this->data['transactions']));

return new Collection(\array_map(
static fn (array $transaction) => new CartTransaction($transaction),
$this->data['transactions']
);
));
}

public function getPrice(): CartPrice
Expand Down
20 changes: 11 additions & 9 deletions src/Context/Cart/CartPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\App\SDK\Context\Cart;

use Shopware\App\SDK\Context\ArrayStruct;
use Shopware\App\SDK\Framework\Collection;

class CartPrice extends ArrayStruct
{
Expand All @@ -25,14 +26,15 @@ public function getTotalPrice(): float
}

/**
* @return array<CalculatedTax>
* @return Collection<CalculatedTax>
*/
public function getCalculatedTaxes(): array
public function getCalculatedTaxes(): Collection
{
\assert(is_array($this->data['calculatedTaxes']));
return array_map(static function (array $calculatedTax): CalculatedTax {

return new Collection(\array_map(static function (array $calculatedTax): CalculatedTax {
return new CalculatedTax($calculatedTax);
}, $this->data['calculatedTaxes']);
}, $this->data['calculatedTaxes']));
}

public function getTaxStatus(): string
Expand All @@ -42,15 +44,15 @@ public function getTaxStatus(): string
}

/**
* @return array<TaxRule>
* @return Collection<TaxRule>
*/
public function getTaxRules(): array
public function getTaxRules(): Collection
{
\assert(is_array($this->data['taxRules']));
\assert(\is_array($this->data['taxRules']));

return array_map(static function (array $taxRule): TaxRule {
return new Collection(\array_map(static function (array $taxRule): TaxRule {
return new TaxRule($taxRule);
}, $this->data['taxRules']);
}, $this->data['taxRules']));
}

public function getPositionPrice(): float
Expand Down
9 changes: 5 additions & 4 deletions src/Context/Cart/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
use Shopware\App\SDK\Context\ArrayStruct;
use Shopware\App\SDK\Context\SalesChannelContext\ShippingLocation;
use Shopware\App\SDK\Context\SalesChannelContext\ShippingMethod;
use Shopware\App\SDK\Framework\Collection;

class Delivery extends ArrayStruct
{
/**
* @return array<DeliveryPosition>
* @return Collection<DeliveryPosition>
*/
public function getPositions(): array
public function getPositions(): Collection
{
\assert(is_array($this->data['positions']));

return array_map(static function (array $position) {
return new Collection(\array_map(static function (array $position) {
return new DeliveryPosition($position);
}, $this->data['positions']);
}, $this->data['positions']));
}

public function getLocation(): ShippingLocation
Expand Down
12 changes: 12 additions & 0 deletions src/Context/Cart/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Shopware\App\SDK\Context\Cart;

final class Error
{
public const LEVEL_NOTICE = 0;
public const LEVEL_WARNING = 10;
public const LEVEL_ERROR = 20;
}
7 changes: 4 additions & 3 deletions src/Context/Cart/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\App\SDK\Context\Cart;

use Shopware\App\SDK\Context\ArrayStruct;
use Shopware\App\SDK\Framework\Collection;

class LineItem extends ArrayStruct
{
Expand Down Expand Up @@ -82,12 +83,12 @@ public function getStates(): array
}

/**
* @return array<LineItem>
* @return Collection<LineItem>
*/
public function getChildren(): array
public function getChildren(): Collection
{
\assert(is_array($this->data['children']));

return array_map(static fn (array $child): LineItem => new LineItem($child), $this->data['children']);
return new Collection(\array_map(static fn (array $child): LineItem => new LineItem($child), $this->data['children']));
}
}
Loading
Loading