Skip to content

Commit

Permalink
Add blink input field and capture its data inside of action
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Sep 5, 2024
1 parent f51cd8f commit d31e18d
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayGatewayConfigurationType;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;
use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType;

return function(ContainerConfigurator $container): void {
$services = $container->services();
Expand All @@ -23,4 +25,11 @@
;

$services->set(PreventSavingEmptyClientSecretListener::class);

$services->set(CompleteTypeExtension::class)
->tag(
'form.type_extension',
['extended_type' => CompleteType::class]
)
;
};
5 changes: 5 additions & 0 deletions config/services/payum/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateBlik0TransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\FetchPaymentDetailsAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction;
Expand All @@ -24,6 +25,10 @@
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_transaction'])
;

$services->set(CreateBlik0TransactionAction::class)
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_blik0_transaction'])
;

$services->set(FetchPaymentDetailsAction::class)
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.fetch_payment_details'])
;
Expand Down
23 changes: 23 additions & 0 deletions src/Form/DataTransformer/PaymentDetailsTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;

class PaymentDetailsTransformer implements DataTransformerInterface
{
public function transform($value): string
{
if (!$value || !array_key_exists('blik', $value)) {
return '';
}

return $value['blik'];
}
public function reverseTransform($value): array
{
return ['blik' => $value];
}
}
34 changes: 34 additions & 0 deletions src/Form/Type/CompleteTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;

final class CompleteTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('notes', TextareaType::class, [
'label' => 'sylius.form.notes',
'required' => false,
]);

$builder->add('others', PaymentDetailsType::class, [
'label' => 'commerce_weavers_sylius_tpay.payment.blik.token',
// TODO missing validation
'property_path' => 'payments[0].details', // TODO looks awfull and what about other payments?
// 'mapped' => false,
'required' => false,
]);
}

public static function getExtendedTypes(): iterable
{
return [CompleteType::class];
}
}
23 changes: 23 additions & 0 deletions src/Form/Type/PaymentDetailsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\PaymentDetailsTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class PaymentDetailsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new PaymentDetailsTransformer());
}

public function getParent(): string
{
return TextType::class;
}
}
63 changes: 63 additions & 0 deletions src/Payum/Action/Api/CreateBlik0TransactionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api;

use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction;
use Sylius\Component\Core\Model\PaymentInterface;
use Tpay\OpenApi\Api\TpayApi;

/**
* @property TpayApi $api
*/
final class CreateBlik0TransactionAction extends BaseApiAwareAction
{
/**
* @param CreateTransaction $request
*/
public function execute($request): void
{
/** @var PaymentInterface $model */
$model = $request->getModel();
$details = $model->getDetails();

$order = $model->getOrder();
$customer = $order->getCustomer();
$billingAddress = $order->getBillingAddress();

$blikToken = $model->getDetails()['blik'];

$response = $this->api->transactions()->createTransaction([
'amount' => number_format($model->getAmount() / 100, 2, thousands_separator: ''),
'description' => sprintf('zamówienie #%s', $order->getNumber()), // TODO: Introduce translations
'payer' => [
'email' => $customer->getEmail(),
'name' => $billingAddress->getFullName(),
],
'pay' => [
'groupId' => 150,
'blikPaymentData' => [
'blikToken' => $blikToken,
],
],
'callbacks' => [
'payerUrls' => [
'success' => $request->getAfterUrl(),
'error' => $request->getAfterUrl(),
],
],
]);

// blik token could be removed here from $details
$details['tpay']['transaction_id'] = $response['transactionId'];

$model->setDetails($details);
}

public function supports($request): bool
{
return $request instanceof CreateBlik0Transaction && $request->getModel() instanceof PaymentInterface;
}
}
14 changes: 14 additions & 0 deletions src/Payum/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action;

use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction;
use Payum\Core\Action\ActionInterface;
use Payum\Core\GatewayAwareInterface;
Expand All @@ -24,6 +25,14 @@ public function execute($request): void
/** @var PaymentInterface $model */
$model = $request->getModel();

if ($this->transactionIsBlik($model)) {
$this->gateway->execute(
new CreateBlik0Transaction($request->getToken()->getAfterUrl(), $model),
);

return;
}

$this->gateway->execute(
new CreateTransaction($request->getToken()->getAfterUrl(), $model),
);
Expand All @@ -37,4 +46,9 @@ public function supports($request): bool
{
return $request instanceof Capture && $request->getModel() instanceof PaymentInterface;
}

private function transactionIsBlik($model): bool
{
return array_key_exists('blik', $model->getDetails());
}
}
22 changes: 22 additions & 0 deletions src/Payum/Request/Api/CreateBlik0Transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api;

use Payum\Core\Request\Generic;

class CreateBlik0Transaction extends Generic
{
public function __construct (
private string $afterUrl,
mixed $model,
) {
parent::__construct($model);
}

public function getAfterUrl(): string
{
return $this->afterUrl;
}
}
1 change: 1 addition & 0 deletions templates/blik.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</div>
<p {{ sylius_test_html_attribute('payment-price') }}>{{ money.format(payment.amount, payment.currencyCode) }}</p>

{{ form_row(form.others) }}
<label for="blik-token">
{{ 'commerce_weavers_sylius_tpay.payment.blik.token'|trans }}
</label>
Expand Down

0 comments on commit d31e18d

Please sign in to comment.