Skip to content

Commit

Permalink
Merge pull request #23 from logeecom/master
Browse files Browse the repository at this point in the history
PISYL-148: PISYL-207 && PISYL-240 && PISYL-241 && PISYL-244 && PISYL-245
  • Loading branch information
hwysoszynski authored Jun 21, 2024
2 parents 6fa6e98 + 1d8d55f commit e11165b
Show file tree
Hide file tree
Showing 53 changed files with 1,781 additions and 102 deletions.
53 changes: 40 additions & 13 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Ensure that you have `wkhtmltopdf` installed, and that you have the proper path
#### 2. Require Mollie plugin with composer:

```bash
composer require mollie/sylius-plugin --no-scripts -w
composer require mollie/sylius-plugin --no-scripts -W
```

#### 3. Update the GatewayConfig entity class with the following code:
Expand Down Expand Up @@ -79,7 +79,7 @@ If you don't use annotations, you can also define new Entity mapping inside your
```
For an example, check [tests/Application/src/Resources/config/doctrine/GatewayConfig.orm.xml](/tests/Application/src/Resources/config/doctrine/GatewayConfig.orm.xml) file.

Override GatewayConfig resource:
Ensure that the GatewayConfig resource is overridden in the Sylius configuration file:
```yaml
# config/packages/_sylius.yaml
...
Expand All @@ -91,7 +91,7 @@ sylius_payum:
model: App\Entity\Payment\GatewayConfig
```
#### 4. Update the Order entity class with the following code:
#### 4. Update the Order entity class with the following code: (do this also when updating to plugin version 5.4.0)
```php
<?php
Expand All @@ -100,11 +100,13 @@ declare(strict_types=1);

namespace App\Entity\Order;

use SyliusMolliePlugin\Entity\MolliePaymentIdOrderTrait;
use SyliusMolliePlugin\Entity\OrderInterface;
use SyliusMolliePlugin\Entity\MollieSubscriptionInterface;
use SyliusMolliePlugin\Entity\AbandonedEmailOrderTrait;
use SyliusMolliePlugin\Entity\QRCodeOrderTrait;
use SyliusMolliePlugin\Entity\RecurringOrderTrait;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Order as BaseOrder;
use Sylius\Component\Core\Model\OrderItemInterface;

Expand All @@ -116,25 +118,39 @@ class Order extends BaseOrder implements OrderInterface
{
use AbandonedEmailOrderTrait;
use RecurringOrderTrait;
use QRCodeOrderTrait;
use MolliePaymentIdOrderTrait;

/**
* @var bool
* @ORM\Column(type="boolean", name="abandoned_email")
*/
protected $abandonedEmail = false;
protected bool $abandonedEmail = false;

/**
* @var ?int
* @ORM\Column(type="integer", name="recurring_sequence_index", nullable=true)
*/
protected $recurringSequenceIndex;
protected ?int $recurringSequenceIndex = null;

/**
* @var string|null
* @ORM\Column(type="text", name="qr_code", nullable=true)
*/
protected ?string $qrCode = null;

/**
* @var string|null
* @ORM\Column(type="text", name="mollie_payment_id", nullable=true)
*/
protected ?string $molliePaymentId = null;

/**
* @var MollieSubscriptionInterface|null
* @ORM\ManyToOne(targetEntity="SyliusMolliePlugin\Entity\MollieSubscription")
* @ORM\JoinColumn(name="subscription_id", fieldName="subscription", onDelete="RESTRICT")
*/
protected $subscription = null;
protected ?MollieSubscriptionInterface $subscription = null;

public function getRecurringItems(): Collection
{
Expand Down Expand Up @@ -172,6 +188,7 @@ class Order extends BaseOrder implements OrderInterface
return 0 < $this->getNonRecurringItems()->count();
}
}

```

If you don't use annotations, you can also define new Entity mapping inside your `src/Resources/config/doctrine` directory.
Expand All @@ -188,7 +205,7 @@ If you don't use annotations, you can also define new Entity mapping inside your
</mapped-superclass>
</doctrine-mapping>
```
Override Order resource:
Ensure that the Order resource is overridden in the Sylius configuration file:

```yaml
# config/packages/_sylius.yaml
Expand All @@ -214,6 +231,7 @@ use Doctrine\ORM\Mapping as ORM;
use SyliusMolliePlugin\Entity\ProductInterface;
use SyliusMolliePlugin\Entity\ProductTrait;
use Sylius\Component\Core\Model\Product as BaseProduct;
use SyliusMolliePlugin\Entity\ProductType;

/**
* @ORM\Entity
Expand All @@ -227,7 +245,7 @@ class Product extends BaseProduct implements ProductInterface
* @ORM\ManyToOne(targetEntity="SyliusMolliePlugin\Entity\ProductType")
* @ORM\JoinColumn(name="product_type_id", fieldName="productType", onDelete="SET NULL")
*/
protected $productType;
protected ?ProductType $productType = null;
}
```

Expand All @@ -254,7 +272,7 @@ If you don't use annotations, you can also define new Entity mapping inside your
</entity>
</doctrine-mapping>
```
Override Product resource:
Ensure that the Product resource is overridden in the Sylius configuration file:

```yaml
# config/packages/_sylius.yaml
Expand Down Expand Up @@ -384,7 +402,7 @@ If you don't use annotations, you can also define new Entity mapping inside your
</mapped-superclass>
</doctrine-mapping>
```
Override ProductVariant resource:
Ensure that the ProductVariant resource is overridden in the Sylius configuration file:

```yaml
# config/packages/_sylius.yaml
Expand Down Expand Up @@ -431,7 +449,6 @@ winzou_state_machine:
```

#### 10. Add image directory parameter in `config/packages/_sylius.yaml`:

```yaml
# config/packages/_sylius.yaml
Expand Down Expand Up @@ -467,13 +484,17 @@ bin/console doctrine:migrations:migrate
```
#### 13. Copy Sylius templates overridden in plugin to your templates directory (e.g templates/bundles/):
**Note:** Some directories may already exist in your project
```
mkdir -p templates/bundles/SyliusAdminBundle/
mkdir -p templates/bundles/SyliusShopBundle/
mkdir -p templates/bundles/SyliusUiBundle/
mkdir -p templates/bundles/SyliusRefundPlugin/
```
**Note:** Ba aware that the following commands will override your existing templates!
```
cp -R vendor/mollie/sylius-plugin/tests/Application/templates/bundles/SyliusAdminBundle/* templates/bundles/SyliusAdminBundle/
cp -R vendor/mollie/sylius-plugin/tests/Application/templates/bundles/SyliusShopBundle/* templates/bundles/SyliusShopBundle/
cp -R vendor/mollie/sylius-plugin/tests/Application/templates/bundles/SyliusUiBundle/* templates/bundles/SyliusUiBundle/
Expand Down Expand Up @@ -587,7 +608,7 @@ nvm use 12
Ensure you have the following packages installed:

```bash
yarn add babel-preset-env bazinga-translator intl-messageformat lodash.get [email protected] [email protected] webpack-notifier
yarn add @babel/preset-env bazinga-translator intl-messageformat lodash.get [email protected] [email protected] webpack-notifier
yarn add --dev @babel/[email protected] @babel/[email protected] @babel/[email protected] @symfony/[email protected]
```

Expand All @@ -610,3 +631,9 @@ Update the scheme, since webpack and asset require new tables that are not in th
```bash
php bin/console doctrine:schema:update --force
```

If you are missing translations, just clear the cache:

```bash
php bin/console cache:clear
```
1 change: 0 additions & 1 deletion src/Action/Api/CreateOnDemandPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function execute($request): void
try {
$paymentSettings = [
'method' => $details['metadata']['molliePaymentMethods'],
'issuer' => $details['metadata']['selected_issuer'] ?? null,
'cardToken' => $details['metadata']['cartToken'],
'amount' => $details['amount'],
'customerId' => $details['customerId'] ?? null,
Expand Down
1 change: 0 additions & 1 deletion src/Action/Api/CreateOnDemandSubscriptionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function execute($request): void
try {
$paymentSettings = [
'method' => $details['metadata']['molliePaymentMethods'],
'issuer' => $details['metadata']['selected_issuer'] ?? null,
'cardToken' => $details['metadata']['cartToken'],
'amount' => $details['amount'],
'customerId' => $details['customerId'] ?? null,
Expand Down
2 changes: 0 additions & 2 deletions src/Action/Api/CreateOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function execute($request): void
{
$details = ArrayObject::ensureArrayObject($request->getModel());

$issuer = $details['metadata']['selected_issuer'] ?? null;
$customerId = $details['metadata']['customer_mollie_id'] ?? null;
$method = $details['metadata']['molliePaymentMethods'] ?? '';

Expand All @@ -56,7 +55,6 @@ public function execute($request): void
$order = $this->mollieApiClient->orders->create([
'method' => $method,
'payment' => [
'issuer' => $issuer,
'cardToken' => $details['metadata']['cartToken'],
'customerId' => $customerId,
'webhookUrl' => $details['webhookUrl'],
Expand Down
1 change: 0 additions & 1 deletion src/Action/Api/CreatePaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function execute($request): void
Assert::keyExists($details['metadata'], 'molliePaymentMethods');
$paymentDetails = [
'method' => $details['metadata']['molliePaymentMethods'],
'issuer' => $details['metadata']['selected_issuer'] ?? null,
'cardToken' => $details['metadata']['cartToken'],
'amount' => $details['amount'],
'customerId' => $details['customerId'] ?? null,
Expand Down
120 changes: 118 additions & 2 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

namespace SyliusMolliePlugin\Action;

use App\Entity\Payment\Payment;
use Payum\Core\Reply\HttpRedirect;
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
use SyliusMolliePlugin\Entity\OrderInterface;
use SyliusMolliePlugin\Payments\PaymentTerms\Options;
use SyliusMolliePlugin\Request\Api\CreateCustomer;
use SyliusMolliePlugin\Request\Api\CreateInternalRecurring;
Expand All @@ -21,20 +26,55 @@
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use Psr\Log\InvalidArgumentException;
use SyliusMolliePlugin\Resolver\MollieApiClientKeyResolverInterface;

final class CaptureAction extends BaseApiAwareAction implements CaptureActionInterface
{
const PAYMENT_FAILED_STATUS = 'failed';
const PAYMENT_CANCELLED_STATUS = 'cancelled';
const PAYMENT_NEW_STATUS = 'new';

use GatewayAwareTrait;

/** @var GenericTokenFactoryInterface|null */
private $tokenFactory;

/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var MollieApiClientKeyResolverInterface */
private $apiClientKeyResolver;

/** @var PaymentRepositoryInterface */
private $paymentRepository;

/**
* @param OrderRepositoryInterface $orderRepository
* @param MollieApiClientKeyResolverInterface $apiClientKeyResolver
* @param PaymentRepositoryInterface $paymentRepository
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
MollieApiClientKeyResolverInterface $apiClientKeyResolver,
PaymentRepositoryInterface $paymentRepository
)
{
$this->orderRepository = $orderRepository;
$this->apiClientKeyResolver = $apiClientKeyResolver;
$this->paymentRepository = $paymentRepository;
}

public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
{
$this->tokenFactory = $genericTokenFactory;
}

/** @param Capture|mixed $request */
/**
* @param $request
*
* @return void
* @throws \Exception
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);
Expand All @@ -43,7 +83,28 @@ public function execute($request): void

if (true === isset($details['payment_mollie_id']) ||
true === isset($details['subscription_mollie_id']) ||
true === isset($details['order_mollie_id'])) {
true === isset($details['order_mollie_id']) ||
$request->getFirstModel()->getOrder()->getQrCode() ||
$request->getFirstModel()->getOrder()->getMolliePaymentId()) {
$qrCodeValue = $request->getFirstModel()->getOrder()->getQrCode();
$molliePaymentId = $request->getFirstModel()->getOrder()->getMolliePaymentId();
if ($qrCodeValue || $molliePaymentId) {
$this->setQrCodeOnOrder($request->getFirstModel()->getOrder());
$payment = $request->getFirstModel();

if ($payment->getState() === self::PAYMENT_FAILED_STATUS ||
$payment->getState() === self::PAYMENT_CANCELLED_STATUS) {
$this->paymentRepository->add($this->createNewPayment($payment));
}

$this->mollieApiClient->setApiKey($this->apiClientKeyResolver->getClientWithKey()->getApiKey());
$molliePayment = $this->mollieApiClient->payments->get($molliePaymentId);

if ($checkoutUrl = $molliePayment->getCheckoutUrl()) {
throw new HttpRedirect($checkoutUrl);
}
}

return;
}

Expand Down Expand Up @@ -93,16 +154,71 @@ public function execute($request): void
$this->gateway->execute(new CreatePayment($details));
}

if (isset($details['metadata']['methodType']) && Options::ORDER_API === $details['metadata']['methodType']) {
if (in_array($details['metadata']['molliePaymentMethods'], Options::getOnlyPaymentAPIMethods(), true)) {
throw new InvalidArgumentException(sprintf(
'Method %s is not allowed to use %s',
$details['metadata']['molliePaymentMethods'],
Options::ORDER_API
));
}

$this->gateway->execute(new CreatePayment($details));
}

if (isset($details['metadata']['methodType']) && Options::ORDER_API === $details['metadata']['methodType']) {
$this->gateway->execute(new CreateOrder($details));
}
}
}

/**
* @param $request
*
* @return bool
*/
public function supports($request): bool
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess;
}

/**
* @param Payment $payment
*
* @return Payment
* @throws \Exception
*/
private function createNewPayment(Payment $payment): Payment
{
$newPayment = new Payment();
$newPayment->setMethod($payment->getMethod());
$newPayment->setOrder($payment->getOrder());
$newPayment->setCurrencyCode($payment->getCurrencyCode());
$newPayment->setAmount($payment->getAmount());
$newPayment->setState(self::PAYMENT_NEW_STATUS);
$newPayment->setDetails([]);
$paymentDate = new \DateTime('now', $payment->getCreatedAt()->getTimezone());
$newPayment->setCreatedAt($paymentDate);
$newPayment->setUpdatedAt($paymentDate);

return $newPayment;
}

/**
* @param OrderInterface $order
* @param string|null $qrCode
*
* @return void
*/
private function setQrCodeOnOrder(OrderInterface $order, ?string $qrCode = null)
{
try {
$order->setQrCode($qrCode);
$this->orderRepository->add($order);
} catch (\Exception $exception) {

}
}
}
Loading

0 comments on commit e11165b

Please sign in to comment.