Skip to content

Commit 79c58cd

Browse files
authored
Fix/configuration and payment process (Sylius#381)
| Q | A | --------------- | ----- | Branch? | 1.7 (fixes) | Bug fix? | yes Solves problems - when the customer clicks on PayPal checkout, he is not taken to the -Complete step with his PayPal account details, but to the Select Address step; - When changing the PayPal settings in payment methods, the hidden data in the form are deleted.
2 parents d752825 + dd4300d commit 79c58cd

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/Controller/ProcessPayPalOrderAction.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Sylius\Abstraction\StateMachine\WinzouStateMachineAdapter;
2020
use Sylius\Component\Core\Factory\AddressFactoryInterface;
2121
use Sylius\Component\Core\Model\CustomerInterface;
22-
use Sylius\Component\Core\Model\OrderInterface;
2322
use Sylius\Component\Core\Model\PaymentInterface;
2423
use Sylius\Component\Core\Model\PaymentMethodInterface;
2524
use Sylius\Component\Core\OrderCheckoutTransitions;
@@ -89,10 +88,6 @@ public function __invoke(Request $request): Response
8988
$orderId = $request->request->getInt('orderId');
9089
$order = $this->orderProvider->provideOrderById($orderId);
9190

92-
if ($order->getState() !== OrderInterface::STATE_NEW) {
93-
return new JsonResponse(['orderID' => $orderId]);
94-
}
95-
9691
/** @var PaymentInterface|null $payment */
9792
$payment = $order->getLastPayment(PaymentInterface::STATE_CART);
9893

@@ -157,9 +152,6 @@ public function __invoke(Request $request): Response
157152
return new JsonResponse(['orderID' => $orderId]);
158153
}
159154

160-
$this->paymentStateManager->create($payment);
161-
$this->paymentStateManager->process($payment);
162-
163155
return new JsonResponse(['orderID' => $orderId]);
164156
}
165157

src/Form/Type/PayPalConfigurationType.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,54 @@
1717
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
1818
use Symfony\Component\Form\Extension\Core\Type\TextType;
1919
use Symfony\Component\Form\FormBuilderInterface;
20+
use Symfony\Component\Form\FormEvent;
21+
use Symfony\Component\Form\FormEvents;
2022

2123
final class PayPalConfigurationType extends AbstractType
2224
{
25+
private const HIDDEN_FIELDS = [
26+
'merchant_id',
27+
'sylius_merchant_id',
28+
'partner_attribution_id',
29+
'use_authorize',
30+
];
31+
2332
public function buildForm(FormBuilderInterface $builder, array $options): void
2433
{
34+
$originalData = [];
35+
2536
$builder
26-
->add('client_id', TextType::class, ['label' => 'sylius.pay_pal.client_id', 'attr' => ['readonly' => true]])
27-
->add('client_secret', TextType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]])
28-
->add('merchant_id', HiddenType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]])
29-
->add('sylius_merchant_id', HiddenType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]])
30-
->add('partner_attribution_id', HiddenType::class, ['label' => 'sylius.pay_pal.partner_attribution_id', 'attr' => ['readonly' => true]])
37+
->add('client_id', TextType::class, ['label' => 'sylius_paypal.client_id', 'attr' => ['readonly' => true]])
38+
->add('client_secret', TextType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
39+
->add('merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
40+
->add('sylius_merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
41+
->add('partner_attribution_id', HiddenType::class, ['label' => 'sylius_paypal.partner_attribution_id', 'attr' => ['readonly' => true]])
3142
// we need to force Sylius Payum integration to postpone creating an order, it's the easiest way
3243
->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]])
33-
->add('reports_sftp_username', TextType::class, ['label' => 'sylius.pay_pal.sftp_username', 'required' => false])
34-
->add('reports_sftp_password', TextType::class, ['label' => 'sylius.pay_pal.sftp_password', 'required' => false])
44+
->add('reports_sftp_username', TextType::class, ['label' => 'sylius_paypal.sftp_username', 'required' => false])
45+
->add('reports_sftp_password', TextType::class, ['label' => 'sylius_paypal.sftp_password', 'required' => false])
3546
;
47+
48+
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use (&$originalData): void {
49+
$data = $event->getData();
50+
if (is_array($data)) {
51+
$originalData = $data;
52+
}
53+
});
54+
55+
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use (&$originalData): void {
56+
$submitted = $event->getData() ?? [];
57+
58+
foreach (self::HIDDEN_FIELDS as $field) {
59+
if (
60+
!array_key_exists($field, $submitted) &&
61+
array_key_exists($field, $originalData)
62+
) {
63+
$submitted[$field] = $originalData[$field];
64+
}
65+
}
66+
67+
$event->setData($submitted);
68+
});
3669
}
3770
}

0 commit comments

Comments
 (0)