Skip to content
Merged
104 changes: 103 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:

PayPlug\SyliusPayPlugPlugin\:
resource: '../src/*'
exclude: '../src/{ApiClient,DependencyInjection,Entity,Exception,Gateway,Model,Repository,PayPlugSyliusPayPlugPlugin.php}'
exclude: '../src/{ApiClient,DependencyInjection,Entity,Exception,Model,Repository,PayPlugSyliusPayPlugPlugin.php}'

PayPlug\SyliusPayPlugPlugin\Repository\PaymentRepositoryInterface:
class: PayPlug\SyliusPayPlugPlugin\Repository\PaymentRepository
Expand All @@ -33,3 +33,105 @@ services:

payplug_sylius_payplug_plugin.action.notify:
class: PayPlug\SyliusPayPlugPlugin\Action\NotifyAction

## Default Payplug Gateway ##
payplug_sylius_payplug_plugin.command_provider.payplug:
class: Sylius\Bundle\PaymentBundle\CommandProvider\ActionsCommandProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.command_provider.payplug
index_by: 'action'
tags:
- name: sylius.payment_request.command_provider
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\PayPlugGatewayFactory::FACTORY_NAME
payplug_sylius_payplug_plugin.provider.order_pay.http_response.payplug:
class: Sylius\Bundle\PaymentBundle\Provider\ActionsHttpResponseProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.http_response_provider.payplug
index_by: action
tags:
- name: sylius.payment_request.provider.http_response
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\PayPlugGatewayFactory::FACTORY_NAME

## Oney Payplug Gateway ##
payplug_sylius_payplug_plugin.command_provider.payplug_oney:
class: Sylius\Bundle\PaymentBundle\CommandProvider\ActionsCommandProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.command_provider.payplug_oney
index_by: 'action'
tags:
- name: sylius.payment_request.command_provider
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\OneyGatewayFactory::FACTORY_NAME
payplug_sylius_payplug_plugin.provider.order_pay.http_response.payplug_oney:
class: Sylius\Bundle\PaymentBundle\Provider\ActionsHttpResponseProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.http_response_provider.payplug_oney
index_by: action
tags:
- name: sylius.payment_request.provider.http_response
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\OneyGatewayFactory::FACTORY_NAME


## Bancontact Payplug Gateway ##
payplug_sylius_payplug_plugin.command_provider.payplug_bancontact:
class: Sylius\Bundle\PaymentBundle\CommandProvider\ActionsCommandProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.command_provider.payplug_bancontact
index_by: 'action'
tags:
- name: sylius.payment_request.command_provider
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\BancontactGatewayFactory::FACTORY_NAME
payplug_sylius_payplug_plugin.provider.order_pay.http_response.payplug_bancontact:
class: Sylius\Bundle\PaymentBundle\Provider\ActionsHttpResponseProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.http_response_provider.payplug_bancontact
index_by: action
tags:
- name: sylius.payment_request.provider.http_response
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\BancontactGatewayFactory::FACTORY_NAME


## Amex Payplug Gateway ##
payplug_sylius_payplug_plugin.command_provider.payplug_american_express:
class: Sylius\Bundle\PaymentBundle\CommandProvider\ActionsCommandProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.command_provider.payplug_american_express
index_by: 'action'
tags:
- name: sylius.payment_request.command_provider
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\AmericanExpressGatewayFactory::FACTORY_NAME
payplug_sylius_payplug_plugin.provider.order_pay.http_response.payplug_american_express:
class: Sylius\Bundle\PaymentBundle\Provider\ActionsHttpResponseProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.http_response_provider.payplug_american_express
index_by: action
tags:
- name: sylius.payment_request.provider.http_response
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\AmericanExpressGatewayFactory::FACTORY_NAME

## Apple Pay Payplug Gateway ##
payplug_sylius_payplug_plugin.command_provider.payplug_apple_pay:
class: Sylius\Bundle\PaymentBundle\CommandProvider\ActionsCommandProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.command_provider.payplug_apple_pay
index_by: 'action'
tags:
- name: sylius.payment_request.command_provider
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\ApplePayGatewayFactory::FACTORY_NAME
payplug_sylius_payplug_plugin.provider.order_pay.http_response.payplug_apple_pay:
class: Sylius\Bundle\PaymentBundle\Provider\ActionsHttpResponseProvider
arguments:
- !tagged_locator
tag: payplug_sylius_payplug_plugin.http_response_provider.payplug_apple_pay
index_by: action
tags:
- name: sylius.payment_request.provider.http_response
gateway_factory: !php/const PayPlug\SyliusPayPlugPlugin\Gateway\ApplePayGatewayFactory::FACTORY_NAME
50 changes: 0 additions & 50 deletions config/services/gateway.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults autoconfigure="true" autowire="true" public="false"/>
<!-- Gateway PayPlug -->
<service id="payplug_sylius_payplug_plugin.gateway_factory.payplug"
class="Payum\Core\Bridge\Symfony\Builder\GatewayFactoryBuilder">
<argument type="string">PayPlug\SyliusPayPlugPlugin\Gateway\PayPlugGatewayFactory</argument>
<tag name="payum.gateway_factory_builder"
factory="payplug" />
</service>
<service id="payplug_sylius_payplug_plugin.form.type.payplug_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Type\PayPlugGatewayConfigurationType">
<tag name="sylius.gateway_configuration_type"
type="payplug"
label="payplug_sylius_payplug_plugin.ui.payplug_gateway_label" />
<tag name="form.type" />
</service>
<service id="payplug_sylius_payplug_plugin.form.type_extension.payplug_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Extension\PayPlugGatewayConfigurationTypeExtension">
<tag name="form.type_extension" />
</service>

<!-- Gateway Oney By PayPlug -->
<service id="payplug_sylius_payplug_plugin.gateway_factory.oney"
Expand All @@ -31,17 +13,6 @@
<tag name="payum.gateway_factory_builder"
factory="payplug_oney"/>
</service>
<service id="payplug_sylius_payplug_plugin.form.type.oney_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Type\OneyGatewayConfigurationType">
<tag name="sylius.gateway_configuration_type"
type="payplug_oney"
label="payplug_sylius_payplug_plugin.ui.oney_gateway_label" />
<tag name="form.type" />
</service>
<service id="payplug_sylius_payplug_plugin.form.type_extension.oney_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Extension\OneyGatewayConfigurationTypeExtension">
<tag name="form.type_extension" />
</service>

<!-- Gateway Bancontact by PayPlug -->
<service id="payplug_sylius_payplug_plugin.gateway_factory.bancontact"
Expand All @@ -50,13 +21,6 @@
<tag name="payum.gateway_factory_builder"
factory="payplug_bancontact"/>
</service>
<service id="payplug_sylius_payplug_plugin.form.type.bancontact_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Type\BancontactGatewayConfigurationType">
<tag name="sylius.gateway_configuration_type"
type="payplug_bancontact"
label="payplug_sylius_payplug_plugin.ui.bancontact_gateway_label" />
<tag name="form.type" />
</service>

<!-- Gateway Apple Pay by PayPlug -->
<service id="payplug_sylius_payplug_plugin.gateway_factory.apple_pay"
Expand All @@ -65,13 +29,6 @@
<tag name="payum.gateway_factory_builder"
factory="payplug_apple_pay"/>
</service>
<service id="payplug_sylius_payplug_plugin.form.type.apple_pay_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Type\ApplePayGatewayConfigurationType">
<tag name="sylius.gateway_configuration_type"
type="payplug_apple_pay"
label="payplug_sylius_payplug_plugin.ui.apple_pay_gateway_label" />
<tag name="form.type" />
</service>

<!-- Gateway American Express by PayPlug -->
<service id="payplug_sylius_payplug_plugin.gateway_factory.american_express"
Expand All @@ -80,13 +37,6 @@
<tag name="payum.gateway_factory_builder"
factory="payplug_american_express"/>
</service>
<service id="payplug_sylius_payplug_plugin.form.type.american_express_gateway_configuration"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Form\Type\AmericanExpressGatewayConfigurationType">
<tag name="sylius.gateway_configuration_type"
type="payplug_american_express"
label="payplug_sylius_payplug_plugin.ui.american_express_gateway_label" />
<tag name="form.type" />
</service>

<service id="payplug_sylius_payplug_plugin.is_oney_enabled_validator"
class="PayPlug\SyliusPayPlugPlugin\Gateway\Validator\Constraints\IsOneyEnabledValidator">
Expand Down
2 changes: 1 addition & 1 deletion src/Action/ConvertPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function execute($request): void
/** @var PaymentInterface $payment */
$payment = $request->getSource();

$details = $this->paymentDataCreator->create($payment, $this->payPlugApiClient->getGatewayFactoryName());
$details = $this->paymentDataCreator->create($payment);

$request->setResult((array) $details);
}
Expand Down
3 changes: 3 additions & 0 deletions src/ApiClient/PayPlugApiClientFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace PayPlug\SyliusPayPlugPlugin\ApiClient;

use Sylius\Component\Core\Model\PaymentMethodInterface;

interface PayPlugApiClientFactoryInterface
{
public function create(string $factoryName, ?string $key = null): PayPlugApiClientInterface;
public function createForPaymentMethod(PaymentMethodInterface $paymentMethod): PayPlugApiClientInterface;
}
15 changes: 15 additions & 0 deletions src/Command/AbstractPayplugPaymentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PayPlug\SyliusPayPlugPlugin\Command;

use Sylius\Bundle\PaymentBundle\Command\PaymentRequestHashAwareInterface;
use Sylius\Bundle\PaymentBundle\Command\PaymentRequestHashAwareTrait;

abstract class AbstractPayplugPaymentRequest implements PaymentRequestHashAwareInterface
{
use PaymentRequestHashAwareTrait;

public function __construct(protected ?string $hash) {}
}
9 changes: 9 additions & 0 deletions src/Command/CapturePaymentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace PayPlug\SyliusPayPlugPlugin\Command;

class CapturePaymentRequest extends AbstractPayplugPaymentRequest
{
}
71 changes: 71 additions & 0 deletions src/Command/Handler/CapturePaymentRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace PayPlug\SyliusPayPlugPlugin\Command\Handler;

use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactoryInterface;
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface;
use PayPlug\SyliusPayPlugPlugin\Command\CapturePaymentRequest;
use PayPlug\SyliusPayPlugPlugin\Creator\PayPlugPaymentDataCreator;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Bundle\CoreBundle\OrderPay\Provider\UrlProviderInterface;
use Sylius\Bundle\PaymentBundle\Provider\PaymentRequestProviderInterface;
use Sylius\Component\Payment\PaymentRequestTransitions;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

#[AsMessageHandler]
final class CapturePaymentRequestHandler
{
public function __construct(
private PaymentRequestProviderInterface $paymentRequestProvider,
private StateMachineInterface $stateMachine,
private PayPlugApiClientFactoryInterface $apiClientFactory,
private PayPlugPaymentDataCreator $paymentDataCreator,
#[Autowire(service: 'sylius_shop.provider.order_pay.after_pay_url')]
private UrlProviderInterface $afterPayUrlProvider,
private UrlGeneratorInterface $urlGenerator,
) {}

public function __invoke(CapturePaymentRequest $capturePaymentRequest): void
{
// Retrieve the current PaymentRequest based on the hash provided in the CapturePaymentRequest command
$paymentRequest = $this->paymentRequestProvider->provide($capturePaymentRequest);
$payment = $paymentRequest->getPayment();

$client = $this->apiClientFactory->createForPaymentMethod($paymentRequest->getPayment()->getMethod());
$data = $this->paymentDataCreator->create($payment)->getArrayCopy();

$returnUrl = $this->afterPayUrlProvider->getUrl($paymentRequest, UrlGeneratorInterface::ABSOLUTE_URL);
$data['hosted_payment'] = [
'return_url' => $returnUrl,
'cancel_url' => $returnUrl . '?&' . http_build_query(['status' => PayPlugApiClientInterface::STATUS_CANCELED]),
];

$notificationUrl = $this->urlGenerator->generate('sylius_payment_method_notify', ['code' => $payment->getMethod()?->getCode()], UrlGeneratorInterface::ABSOLUTE_URL);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why a specific service to create this url can be required...

$details['notification_url'] = $notificationUrl;

$paymentRequest->setPayload($data);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you set this here, but the purpose of the payload is to serve as the input of the PaymentRequest, meaning a payload can be already set by an API call for example.
Then you can avoid setting this here but instead keep it inside the responseData or the Payment->setDetails().

$payplugPayment = $client->createPayment($data);
$arrayPayplugPayment = (array) $payplugPayment;
$payment->setDetails([
...$payment->getDetails(),
'status' => PayPlugApiClientInterface::STATUS_CREATED,
'payment_id' => $payplugPayment->__get('id'),
'payplug_response' => $arrayPayplugPayment,
]);

$paymentRequest->setResponseData(array_merge($arrayPayplugPayment, [
'payment_id' => $payplugPayment->__get('id'),
'redirect_url' => $payplugPayment->hosted_payment->payment_url
]));

$this->stateMachine->apply(
$paymentRequest,
PaymentRequestTransitions::GRAPH,
PaymentRequestTransitions::TRANSITION_COMPLETE
);
}
}
Loading
Loading