Skip to content

Commit

Permalink
NEXT-36521 - in app purchases
Browse files Browse the repository at this point in the history
  • Loading branch information
lernhart committed Dec 3, 2024
1 parent 21739f6 commit 4b45503
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 17 deletions.
11 changes: 10 additions & 1 deletion src/Context/ActionSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ class ActionSource
/**
* @param string $url The shop url
* @param string $appVersion The installed App version
* @param string[] $inAppPurchases The active in-app-purchases
*/
public function __construct(public readonly string $url, public readonly string $appVersion)
public function __construct(
public readonly string $url,
public readonly string $appVersion,
public readonly array $inAppPurchases = [],
) {
}

public function hasInAppPurchase(string $inAppPurchase): bool
{
return \in_array($inAppPurchase, $this->inAppPurchases, true);
}
}
38 changes: 30 additions & 8 deletions src/Context/ContextResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use Shopware\App\SDK\Framework\Collection;
use Shopware\App\SDK\Shop\ShopInterface;

/**
* @psalm-import-type StorefrontClaimsArray from StorefrontClaims
*/
class ContextResolver
{
public function assembleWebhook(RequestInterface $request, ShopInterface $shop): WebhookAction
Expand Down Expand Up @@ -69,17 +72,28 @@ public function assembleActionButton(RequestInterface $request, ShopInterface $s

public function assembleModule(RequestInterface $request, ShopInterface $shop): ModuleAction
{
parse_str($request->getUri()->getQuery(), $params);

if (!isset($params['sw-version'], $params['sw-context-language']) || !is_string($params['sw-version']) || !is_string($params['sw-context-language']) || !isset($params['sw-user-language']) || !is_string($params['sw-user-language'])) {
\parse_str($request->getUri()->getQuery(), $params);

if (!isset($params['sw-version'], $params['sw-context-language'], $params['sw-user-language'])
|| !is_string($params['sw-version'])
|| !is_string($params['sw-context-language'])
|| !is_string($params['sw-user-language'])
|| (isset($params['in-app-purchases']) && !is_string($params['in-app-purchases']))
) {
throw new MalformedWebhookBodyException();
}

$inAppPurchaseString = $params['in-app-purchases'] ?? '';
\assert(\is_string($inAppPurchaseString));

$inAppPurchases = \explode(',', $inAppPurchaseString);

return new ModuleAction(
$shop,
$params['sw-version'],
$params['sw-context-language'],
$params['sw-user-language']
$params['sw-user-language'],
$inAppPurchases,
);
}

Expand Down Expand Up @@ -227,8 +241,8 @@ public function assembleStorefrontRequest(RequestInterface $request, ShopInterfa
throw new MalformedWebhookBodyException();
}

/** @var array<string, string> $claims */
$claims = json_decode(base64_decode($parts[1]), true, flags: JSON_THROW_ON_ERROR);
/** @var StorefrontClaimsArray $claims */
$claims = \json_decode(\base64_decode($parts[1]), true, flags: JSON_THROW_ON_ERROR);

return new StorefrontAction(
$shop,
Expand Down Expand Up @@ -261,13 +275,21 @@ public function assembleCheckoutGatewayRequest(RequestInterface $request, ShopIn
*/
private function parseSource(array $source): ActionSource
{
if (!isset($source['url'], $source['appVersion']) || !is_string($source['url']) || !is_string($source['appVersion'])) {
if (!isset($source['url'], $source['appVersion']) || !\is_string($source['url']) || !\is_string($source['appVersion'])) {
throw new MalformedWebhookBodyException();
}

if (isset($source['inAppPurchases']) && !\is_array($source['inAppPurchases'])) {
throw new MalformedWebhookBodyException();
}

/** @var string[] $inAppPurchase */
$inAppPurchase = $source['inAppPurchases'] ?? [];

return new ActionSource(
$source['url'],
$source['appVersion']
$source['appVersion'],
$inAppPurchase,
);
}
}
2 changes: 2 additions & 0 deletions src/Context/Module/ModuleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class ModuleAction
/**
* @param string $contentLanguage - The language of the Shopware content as UUID
* @param string $userLanguage - The language of the Shopware user as ISO (en-GB)
* @param string[] $inAppPurchases - The active in-app-purchases
*/
public function __construct(
public readonly ShopInterface $shop,
public readonly string $shopwareVersion,
public readonly string $contentLanguage,
public readonly string $userLanguage,
public readonly array $inAppPurchases = [],
) {
}
}
31 changes: 30 additions & 1 deletion src/Context/Storefront/StorefrontClaims.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@

use Shopware\App\SDK\Exception\MissingClaimException;

/**
* @psalm-type StorefrontClaimsArray = array{
* salesChannelId?: string,
* customerId?: string,
* currencyId?: string,
* languageId?: string,
* paymentMethodId?: string,
* shippingMethodId?: string,
* inAppPurchases?: string[],
* }
*/
class StorefrontClaims
{
/**
* @param array<string, string> $claims
* @param StorefrontClaimsArray $claims
*/
public function __construct(private readonly array $claims)
{
Expand Down Expand Up @@ -74,4 +85,22 @@ public function getShippingMethodId(): string

return $value;
}

/**
* @return string[]
*/
public function getInAppPurchases(): array
{
$value = $this->claims['inAppPurchases'] ?? [];
if (!\is_array($value)) {
throw new MissingClaimException('inAppPurchases');
}

return $value;
}

public function hasInAppPurchase(string $inAppPurchase): bool
{
return \in_array($inAppPurchase, $this->getInAppPurchases(), true);
}
}
22 changes: 21 additions & 1 deletion tests/Context/ActionSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#[CoversClass(ActionSource::class)]
class ActionSourceTest extends TestCase
{
public function testConstruct(): void
public function testConstructDefaults(): void
{
$url = 'https://example.com';
$version = '1.0.0';
Expand All @@ -20,5 +20,25 @@ public function testConstruct(): void

static::assertSame($url, $source->url);
static::assertSame($version, $source->appVersion);
static::assertSame([], $source->inAppPurchases);

static::assertFalse($source->hasInAppPurchase('purchase1'));
}

public function testConstruct(): void
{
$url = 'https://example.com';
$version = '1.0.0';
$inAppPurchases = ['purchase1', 'purchase2'];

$source = new ActionSource($url, $version, $inAppPurchases);

static::assertSame($url, $source->url);
static::assertSame($version, $source->appVersion);
static::assertSame($inAppPurchases, $source->inAppPurchases);

static::assertTrue($source->hasInAppPurchase('purchase1'));
static::assertTrue($source->hasInAppPurchase('purchase2'));
static::assertFalse($source->hasInAppPurchase('purchase3'));
}
}
Loading

0 comments on commit 4b45503

Please sign in to comment.