Skip to content

Commit

Permalink
Revert "PISHPS-265: remove ideal issuer dropdown (#743)"
Browse files Browse the repository at this point in the history
This reverts commit 09ab3b4.
  • Loading branch information
Vitalij Mik committed May 21, 2024
1 parent 8c86aaa commit 253b494
Show file tree
Hide file tree
Showing 30 changed files with 862 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/Controller/StoreApi/iDEAL/Response/IssuersResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\iDEAL\Response;

use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\StoreApiResponse;

class IssuersResponse extends StoreApiResponse
{
/**
* @var ArrayStruct<mixed, mixed>
*/
protected $object;


/**
* @param array<mixed> $terminals
*/
public function __construct(array $terminals)
{
$this->object = new ArrayStruct(
[
'issuers' => $terminals,
],
'mollie_payments_ideal_issuers'
);

parent::__construct($this->object);
}
}
29 changes: 29 additions & 0 deletions src/Controller/StoreApi/iDEAL/Response/StoreIssuerResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\iDEAL\Response;

use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\StoreApiResponse;

class StoreIssuerResponse extends StoreApiResponse
{
/**
* @var ArrayStruct<mixed, mixed>
*/
protected $object;

/**
* @param bool $success
*/
public function __construct(bool $success)
{
$this->object = new ArrayStruct(
[
'success' => $success,
],
'mollie_payments_ideal_issuer_stored'
);

parent::__construct($this->object);
}
}
98 changes: 98 additions & 0 deletions src/Controller/StoreApi/iDEAL/iDealControllerBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Kiener\MolliePayments\Controller\StoreApi\iDEAL;

use Kiener\MolliePayments\Controller\StoreApi\iDEAL\Response\IssuersResponse;
use Kiener\MolliePayments\Controller\StoreApi\iDEAL\Response\StoreIssuerResponse;
use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
use Kiener\MolliePayments\Handler\Method\iDealPayment;
use Kiener\MolliePayments\Service\CustomerService;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\StoreApiResponse;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\Routing\Annotation\Route;

class iDealControllerBase
{
/**
* @var CustomerService
*/
private $customerService;

/**
* @var MollieGatewayInterface
*/
private $mollieGateway;


/**
* @param CustomerService $customerService
* @param MollieGatewayInterface $mollieGateway
*/
public function __construct(CustomerService $customerService, MollieGatewayInterface $mollieGateway)
{
$this->customerService = $customerService;
$this->mollieGateway = $mollieGateway;
}


/**
*
* @param SalesChannelContext $context
* @return StoreApiResponse
*/
public function getIssuers(SalesChannelContext $context): StoreApiResponse
{
$this->mollieGateway->switchClient($context->getSalesChannelId());

$issuers = $this->mollieGateway->getIDealIssuers();

$issuerArray = [];

foreach ($issuers as $issuer) {
$issuerArray[] = [
'id' => $issuer->getId(),
'name' => $issuer->getName(),
'images' => [
'1x' => $issuer->getImage1x(),
'2x' => $issuer->getImage2x(),
'svg' => $issuer->getSvg(),
],
];
}

return new IssuersResponse($issuerArray);
}

/**
*
* @param string $customerId
* @param string $issuerId
* @param SalesChannelContext $context
* @throws \Exception
* @return StoreApiResponse
*/
public function saveIssuer(string $customerId, string $issuerId, SalesChannelContext $context): StoreApiResponse
{
$customer = $this->customerService->getCustomer($customerId, $context->getContext());

if (!$customer instanceof CustomerEntity) {
throw new \Exception('Customer with ID ' . $customerId . ' not found in Shopware');
}

# if we have a "reset" value, then empty our stored issuer
if ($issuerId === iDealPayment::ISSUER_RESET_VALUE) {
$issuerId = '';
}

$result = $this->customerService->setIDealIssuer(
$customer,
$issuerId,
$context->getContext()
);

return new StoreIssuerResponse($result !== null);
}
}
63 changes: 63 additions & 0 deletions src/Controller/Storefront/iDEAL/iDealControllerBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Kiener\MolliePayments\Controller\Storefront\iDEAL;

use Kiener\MolliePayments\Controller\Storefront\AbstractStoreFrontController;
use Kiener\MolliePayments\Handler\Method\iDealPayment;
use Kiener\MolliePayments\Service\CustomerService;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\JsonResponse;

class iDealControllerBase extends AbstractStoreFrontController
{
/**
* @var CustomerService
*/
private $customerService;


/**
* @param CustomerService $customerService
*/
public function __construct(CustomerService $customerService)
{
$this->customerService = $customerService;
}

/**
*
* @param SalesChannelContext $context
* @param string $customerId
* @param string $issuerId
*
* @return JsonResponse
*/
public function storeIssuer(SalesChannelContext $context, string $customerId, string $issuerId): JsonResponse
{
$result = null;

# if we have a "reset" value, then empty our stored issuer
if ($issuerId === iDealPayment::ISSUER_RESET_VALUE) {
$issuerId = '';
}

$customer = $this->customerService->getCustomer($customerId, $context->getContext());

if ($customer instanceof CustomerEntity) {
$writtenEvent = $this->customerService->setIDealIssuer(
$customer,
$issuerId,
$context->getContext()
);

$result = $writtenEvent->getErrors();
}

return new JsonResponse([
'success' => (bool)$result,
'customerId' => $customerId,
'result' => $result,
]);
}
}
88 changes: 88 additions & 0 deletions src/Gateway/Mollie/Model/Issuer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Kiener\MolliePayments\Gateway\Mollie\Model;

class Issuer
{
/**
* @var string
*/
private $id;

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

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

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

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


/**
* @param string $id
* @param string $name
* @param string $image1x
* @param string $image2x
* @param string $svg
*/
public function __construct(string $id, string $name, string $image1x, string $image2x, string $svg)
{
$this->id = $id;
$this->name = $name;
$this->image1x = $image1x;
$this->image2x = $image2x;
$this->svg = $svg;
}

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @return string
*/
public function getImage1x(): string
{
return $this->image1x;
}

/**
* @return string
*/
public function getImage2x(): string
{
return $this->image2x;
}

/**
* @return string
*/
public function getSvg(): string
{
return $this->svg;
}
}
30 changes: 30 additions & 0 deletions src/Gateway/Mollie/MollieGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Kiener\MolliePayments\Gateway\Mollie;

use Kiener\MolliePayments\Factory\MollieApiFactory;
use Kiener\MolliePayments\Gateway\Mollie\Model\Issuer;
use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Method;
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\Profile;
use Mollie\Api\Resources\Subscription;
use Mollie\Api\Resources\Terminal;
use Mollie\Api\Types\PaymentMethod;

class MollieGateway implements MollieGatewayInterface
{
Expand Down Expand Up @@ -85,7 +88,34 @@ public function getOrganizationId(): string
return (string)$orgId;
}

/**
* @throws \Mollie\Api\Exceptions\ApiException
* @return Issuer[]
*/
public function getIDealIssuers(): array
{
$parameters = [
'include' => 'issuers',
];

/** @var Method $iDeal */
$iDeal = $this->apiClient->methods->get(PaymentMethod::IDEAL, $parameters);

$issuers = [];

/** @var \Mollie\Api\Resources\Issuer $issuer */
foreach ($iDeal->issuers as $issuer) {
$issuers[] = new Issuer(
$issuer->id,
$issuer->name,
$issuer->image->size1x,
$issuer->image->size2x,
$issuer->image->svg
);
}

return $issuers;
}

/**
* @throws \Mollie\Api\Exceptions\ApiException
Expand Down
5 changes: 5 additions & 0 deletions src/Gateway/MollieGatewayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kiener\MolliePayments\Gateway;

use Kiener\MolliePayments\Gateway\Mollie\Model\Issuer;
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\Subscription;
Expand All @@ -24,6 +25,10 @@ public function getOrganizationId(): string;
*/
public function getProfileId(): string;

/**
* @return Issuer[]
*/
public function getIDealIssuers(): array;

/**
* @return Terminal[]
Expand Down
Loading

0 comments on commit 253b494

Please sign in to comment.