Skip to content

Commit

Permalink
Add option for payment modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent committed Mar 18, 2024
1 parent 2713da2 commit b84c2b5
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.2.16</version>
<version>2.2.17</version>
<authors>
<author>
<name>Vincent Lopes-Vicente</name>
Expand Down
10 changes: 10 additions & 0 deletions Controller/Front/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
class CheckoutController extends BaseFrontOpenApiController
{
const PAYMENT_MODULE_OPTION_CHOICES_SESSION_KEY = 'payment_module_option_choices';
/**
* @Route("", name="set_checkout", methods="POST")
* @OA\Post(
Expand Down Expand Up @@ -110,6 +111,11 @@ public function setCheckout(
$orderEvent
);

if (isset($decodedContent['paymentOptionChoices'])) {
// Save payment module option choices in session (for the next step)
$session->set(self::PAYMENT_MODULE_OPTION_CHOICES_SESSION_KEY, $decodedContent['paymentOptionChoices']);
}

$responseCheckout = $checkout
->createFromOrder($orderEvent->getOrder());

Expand Down Expand Up @@ -141,6 +147,10 @@ public function getCheckout(

$checkout->setPickupAddress($request->getSession()->get(OpenApi::PICKUP_ADDRESS_SESSION_KEY));

$checkout->setPaymentOptionChoices(
$request->getSession()->get(self::PAYMENT_MODULE_OPTION_CHOICES_SESSION_KEY)
);

return OpenApiService::jsonResponse($checkout);
}

Expand Down
13 changes: 12 additions & 1 deletion Controller/Front/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace OpenApi\Controller\Front;

use OpenApi\Annotations as OA;
use OpenApi\Events\OpenApiEvents;
use OpenApi\Events\PaymentModuleOptionEvent;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Model\Api\PaymentModule;
use OpenApi\Service\OpenApiService;
Expand Down Expand Up @@ -103,12 +105,21 @@ protected function getPaymentModule(
TheliaEvents::MODULE_PAYMENT_IS_VALID
);

$paymentModuleOptionEvent = new PaymentModuleOptionEvent($paymentModule, $cart);

$dispatcher->dispatch(
$paymentModuleOptionEvent,
OpenApiEvents::MODULE_PAYMENT_GET_OPTIONS
);

/** @var PaymentModule $paymentModule */
$paymentModule = $modelFactory->buildModel('PaymentModule', $paymentModule);

$paymentModule->setValid($isValidPaymentEvent->isValidModule())
->setCode($moduleInstance->getCode())
->setMinimumAmount($isValidPaymentEvent->getMinimumAmount())
->setMaximumAmount($isValidPaymentEvent->getMaximumAmount());
->setMaximumAmount($isValidPaymentEvent->getMaximumAmount())
->setOptionGroups($paymentModuleOptionEvent->getPaymentModuleOptionGroups());

return $paymentModule;
}
Expand Down
3 changes: 3 additions & 0 deletions EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\HttpKernel\KernelEvents;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;

class ExceptionListener implements EventSubscriberInterface
{
Expand All @@ -37,6 +38,8 @@ public function catchAllException(ExceptionEvent $event): void
return;
}

Tlog::getInstance()->error($event->getThrowable()->getTraceAsString());

/** @var Error $error */
$error = $this->modelFactory->buildModel(
'Error',
Expand Down
1 change: 1 addition & 0 deletions Events/OpenApiEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
class OpenApiEvents
{
const MODULE_DELIVERY_GET_OPTIONS = 'thelia.module.delivery.options';
const MODULE_PAYMENT_GET_OPTIONS = 'thelia.module.payment.options';
}
124 changes: 124 additions & 0 deletions Events/PaymentModuleOptionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace OpenApi\Events;

use OpenApi\Model\Api\PaymentModuleOption;
use OpenApi\Model\Api\PaymentModuleOptionGroup;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Address;
use Thelia\Model\Base\CountryQuery;
use Thelia\Model\Cart;
use Thelia\Model\Country;
use Thelia\Model\Module;
use Thelia\Model\State;

class PaymentModuleOptionEvent extends ActionEvent
{
protected Module $module;

protected Cart $cart;

protected ?Address $address = null;

protected ?Country $country = null;

protected ?State $state = null;

protected array $paymentModuleOptionGroups = [];

public function __construct(
Module $module,
Cart $cart = null,
) {
$this->module = $module;
$address = $cart?->getAddressRelatedByAddressInvoiceId() ?? $cart?->getAddressRelatedByAddressDeliveryId() ?? null;
$this->address = $address;
$this->cart = $cart;
$this->country = $address?->getCountry() ?? CountryQuery::create()->filterByByDefault(true)->findOne();
$this->state = $address?->getState() ?? $this->country?->getStates()?->getFirst() ?? null;

if (!$module->isPayementModule()) {
throw new \Exception(Translator::getInstance()->trans($module->getTitle().' is not a payment module.'));
}
}

public function getPaymentModuleOptionGroups()
{
return $this->paymentModuleOptionGroups;
}

public function setPaymentModuleOptionGroups($paymentModuleOptionGroups)
{
$this->paymentModuleOptionGroups = $paymentModuleOptionGroups;

return $this;
}

public function appendPaymentModuleOptionGroups(PaymentModuleOptionGroup $paymentModuleOptionGroup)
{
$this->paymentModuleOptionGroups[] = $paymentModuleOptionGroup;

return $this;
}

public function getModule()
{
return $this->module;
}

public function setModule($module)
{
$this->module = $module;

return $this;
}

public function getCart()
{
return $this->cart;
}

public function setCart($cart)
{
$this->cart = $cart;

return $this;
}

public function getAddress()
{
return $this->address;
}

public function setAddress($address)
{
$this->address = $address;

return $this;
}

public function getCountry()
{
return $this->country;
}

public function setCountry($country)
{
$this->country = $country;

return $this;
}

public function getState()
{
return $this->state;
}

public function setState($state)
{
$this->state = $state;

return $this;
}
}
20 changes: 20 additions & 0 deletions Model/Api/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ class Checkout extends BaseApiModel
*/
protected $pickupAddress;

/**
* @var array
* @OA\Property(
* type="array",
* @OA\Items(
* ref="#/components/schemas/PaymentModuleOptionChoice"
* )
* )
*/
protected $paymentOptionChoices = [];

/**
* @var bool
* @OA\Property(
Expand Down Expand Up @@ -305,5 +316,14 @@ public function setDeliveryModuleOptionCode($deliveryModuleOptionCode)
return $this;
}

public function getPaymentOptionChoices(): array
{
return $this->paymentOptionChoices;
}

public function setPaymentOptionChoices(?array $paymentOptionChoices = []): Checkout
{
$this->paymentOptionChoices = $paymentOptionChoices;
return $this;
}
}
29 changes: 29 additions & 0 deletions Model/Api/PaymentModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ class PaymentModule extends BaseApiModel
*/
protected $maximumAmount;

/**
* @var array
* @OA\Property(
* type="array",
* @OA\Items(
* ref="#/components/schemas/PaymentModuleOptionGroup"
* )
* )
*/
protected $optionGroups;

/**
* @var array
* @OA\Property(
Expand Down Expand Up @@ -170,6 +181,24 @@ public function setMaximumAmount($maximumAmount)
return $this;
}

/**
* @return array
*/
public function getOptionGroups()
{
return $this->optionGroups;
}

/**
* @return PaymentModule
*/
public function setOptionGroups($optionGroups)
{
$this->optionGroups = $optionGroups;

return $this;
}

/**
* @return array
*/
Expand Down
83 changes: 83 additions & 0 deletions Model/Api/PaymentModuleOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace OpenApi\Model\Api;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
use Symfony\Component\Validator\Constraints\NotBlank;


#[Schema(description: "An option for payment module")]
class PaymentModuleOption extends BaseApiModel
{
#[Property(type: "string")]
#[NotBlank(groups: ["read"])]
protected string $code;

#[Property(type: "boolean")]
protected bool $valid;

#[Property(type: "string")]
protected string $title;

#[Property(type: "string")]
protected string $description;

#[Property(description: "Option logo url", type: "string")]
protected string $image;

public function getCode(): string
{
return $this->code;
}

public function setCode(string $code): PaymentModuleOption
{
$this->code = $code;
return $this;
}

public function isValid(): bool
{
return $this->valid;
}

public function setValid(bool $valid): PaymentModuleOption
{
$this->valid = $valid;
return $this;
}

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): PaymentModuleOption
{
$this->title = $title;
return $this;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): PaymentModuleOption
{
$this->description = $description;
return $this;
}

public function getImage(): string
{
return $this->image;
}

public function setImage(string $image): PaymentModuleOption
{
$this->image = $image;
return $this;
}
}
Loading

0 comments on commit b84c2b5

Please sign in to comment.