Skip to content

Commit f769700

Browse files
authored
Merge pull request #42 from shopware/feat/context-gateway
feat: context gateway SDK support
2 parents 1e3fa81 + e807e4a commit f769700

33 files changed

+1032
-5
lines changed

src/Context/ContextResolver.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Shopware\App\SDK\Context\ActionButton\ActionButtonAction;
1010
use Shopware\App\SDK\Context\Cart\Cart;
1111
use Shopware\App\SDK\Context\Gateway\Checkout\CheckoutGatewayAction;
12+
use Shopware\App\SDK\Context\Gateway\Context\ContextGatewayAction;
1213
use Shopware\App\SDK\Context\Gateway\InAppFeatures\FilterAction;
1314
use Shopware\App\SDK\Context\InAppPurchase\InAppPurchaseProvider;
1415
use Shopware\App\SDK\Context\Module\ModuleAction;
@@ -288,6 +289,24 @@ public function assembleCheckoutGatewayRequest(RequestInterface $request, ShopIn
288289
);
289290
}
290291

292+
public function assembleContextGatewayRequest(RequestInterface $request, ShopInterface $shop): ContextGatewayAction
293+
{
294+
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
295+
$request->getBody()->rewind();
296+
297+
if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source']) || !isset($body['data']) || !\is_array($body['data'])) {
298+
throw new MalformedWebhookBodyException();
299+
}
300+
301+
return new ContextGatewayAction(
302+
$shop,
303+
$this->parseSource($body['source'], $shop),
304+
new Cart($body['cart']),
305+
new SalesChannelContext($body['salesChannelContext']),
306+
$body['data'],
307+
);
308+
}
309+
291310
public function assembleInAppPurchasesFilterRequest(RequestInterface $request, ShopInterface $shop): FilterAction
292311
{
293312
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Context\Gateway\Context;
6+
7+
use Shopware\App\SDK\Context\ActionSource;
8+
use Shopware\App\SDK\Context\Cart\Cart;
9+
use Shopware\App\SDK\Context\SalesChannelContext\SalesChannelContext;
10+
use Shopware\App\SDK\Shop\ShopInterface;
11+
12+
class ContextGatewayAction
13+
{
14+
/**
15+
* @param array<string, mixed> $data - Additional data to be passed to the action.
16+
*/
17+
public function __construct(
18+
public readonly ShopInterface $shop,
19+
public readonly ActionSource $source,
20+
public readonly Cart $cart,
21+
public readonly SalesChannelContext $context,
22+
public readonly array $data = [],
23+
) {
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Context\Response\Customer;
6+
7+
use Shopware\App\SDK\Context\Response\ResponseStruct;
8+
9+
class AddressResponseStruct extends ResponseStruct
10+
{
11+
public ?string $title = null;
12+
public string $firstName;
13+
public string $lastName;
14+
public ?string $salutationId = null;
15+
public string $street;
16+
public string $zipcode;
17+
public string $city;
18+
public ?string $company = null;
19+
public ?string $department = null;
20+
public ?string $countryStateId = null;
21+
public string $countryId;
22+
public ?string $additionalAddressLine1 = null;
23+
public ?string $additionalAddressLine2 = null;
24+
public ?string $phoneNumber = null;
25+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Context\Response\Customer;
6+
7+
use Shopware\App\SDK\Context\Response\ResponseStruct;
8+
9+
class CustomerResponseStruct extends ResponseStruct
10+
{
11+
public ?string $title;
12+
13+
/**
14+
* @var 'private'|'business'|null
15+
*/
16+
public ?string $accountType = null;
17+
18+
public string $firstName;
19+
20+
public string $lastName;
21+
22+
public string $email;
23+
24+
public ?string $salutationId = null;
25+
26+
public bool $guest = true;
27+
28+
/**
29+
* You find available domains in the sales channel context -> sales channel -> domains
30+
*/
31+
public string $storefrontUrl;
32+
33+
public ?string $requestedGroupId = null;
34+
35+
public ?string $affiliateCode = null;
36+
37+
public ?string $campaignCode = null;
38+
39+
public ?int $birthdayDay = null;
40+
41+
public ?int $birthdayMonth = null;
42+
43+
public ?int $birthdayYear = null;
44+
45+
/**
46+
* You'll need to set a password if you want to create a non-guest customer
47+
* Be aware to supply a plain password, it will be hashed before it is stored by the shop instance
48+
*/
49+
public ?string $password = null;
50+
51+
public AddressResponseStruct $billingAddress;
52+
53+
public ?AddressResponseStruct $shippingAddress = null;
54+
55+
/**
56+
* @var string[]
57+
*/
58+
public array $vatIds = [];
59+
60+
public bool $acceptedDataProtection = false;
61+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Context\Response;
6+
7+
use JsonSerializable;
8+
9+
abstract class ResponseStruct implements \JsonSerializable
10+
{
11+
/**
12+
* @return array<string, mixed>
13+
*/
14+
public function jsonSerialize(): array
15+
{
16+
$data = \get_object_vars($this);
17+
18+
foreach ($data as $key => $value) {
19+
if ($value instanceof JsonSerializable) {
20+
$data[$key] = $value->jsonSerialize();
21+
}
22+
}
23+
24+
return $data;
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Gateway\Context\Command;
6+
7+
use Shopware\App\SDK\Gateway\Context\ContextGatewayCommand;
8+
9+
/**
10+
* Adds an error message to be displayed to the customer in the Storefront via FlashBag messages.
11+
*/
12+
class AddCustomerMessageCommand extends ContextGatewayCommand
13+
{
14+
final public const KEY = 'context_add-customer-message';
15+
16+
/**
17+
* @param string $message - The message to be displayed to the customer.
18+
*/
19+
public function __construct(
20+
public string $message,
21+
) {
22+
$this->keyName = self::KEY;
23+
$this->setPayloadValue('message', $message);
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Gateway\Context\Command;
6+
7+
use Shopware\App\SDK\Gateway\Context\ContextGatewayCommand;
8+
9+
/**
10+
* Changes the billing address of the current customer context to the specified address ID.
11+
* Be aware, that the address must be available for the current customer.
12+
*/
13+
class ChangeBillingAddressCommand extends ContextGatewayCommand
14+
{
15+
final public const KEY = 'context_change-billing-address';
16+
17+
/**
18+
* @param string $addressId - The address id to set as active billing address.
19+
*/
20+
public function __construct(
21+
public string $addressId,
22+
) {
23+
$this->keyName = self::KEY;
24+
$this->setPayloadValue('addressId', $addressId);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Gateway\Context\Command;
6+
7+
use Shopware\App\SDK\Gateway\Context\ContextGatewayCommand;
8+
9+
/**
10+
* Changes the active currency for logged in or newly registered customers
11+
* Be aware that the currency must be available in the sales channel
12+
*/
13+
class ChangeCurrencyCommand extends ContextGatewayCommand
14+
{
15+
final public const KEY = 'context_change-currency';
16+
17+
/**
18+
* @param string $iso - The ISO 4217 currency code, e.g. EUR, USD, GBP
19+
*/
20+
public function __construct(
21+
public string $iso,
22+
) {
23+
$this->keyName = self::KEY;
24+
$this->setPayloadValue('iso', $iso);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Gateway\Context\Command;
6+
7+
use Shopware\App\SDK\Gateway\Context\ContextGatewayCommand;
8+
9+
/**
10+
* Changes the active language for logged in customers
11+
* Be aware that the language must be available in the sales channel
12+
*/
13+
class ChangeLanguageCommand extends ContextGatewayCommand
14+
{
15+
final public const KEY = 'context_change-language';
16+
17+
/**
18+
* @param string $iso - The BCP 47 language tag, e.g. en-US, de-DE, en-GB
19+
*/
20+
public function __construct(
21+
public string $iso,
22+
) {
23+
$this->keyName = self::KEY;
24+
$this->setPayloadValue('iso', $iso);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\App\SDK\Gateway\Context\Command;
6+
7+
use Shopware\App\SDK\Gateway\Context\ContextGatewayCommand;
8+
9+
/**
10+
* Changes the active payment method in the context.
11+
* Beware that the payment method should be valid for the current context.
12+
*/
13+
class ChangePaymentMethodCommand extends ContextGatewayCommand
14+
{
15+
final public const KEY = 'context_change-payment-method';
16+
17+
/**
18+
* @param string $technicalName - The technical name of the payment method to be set.
19+
*/
20+
public function __construct(
21+
public string $technicalName,
22+
) {
23+
$this->keyName = self::KEY;
24+
$this->setPayloadValue('technicalName', $technicalName);
25+
}
26+
}

0 commit comments

Comments
 (0)