55namespace PayPlug \SyliusPayPlugPlugin \Action ;
66
77use ArrayAccess ;
8+ use Payplug \Exception \BadRequestException ;
89use Payplug \Resource \Payment ;
910use PayPlug \SyliusPayPlugPlugin \Action \Api \ApiAwareTrait ;
1011use PayPlug \SyliusPayPlugPlugin \ApiClient \PayPlugApiClientInterface ;
12+ use PayPlug \SyliusPayPlugPlugin \Exception \UnknownApiErrorException ;
1113use Payum \Core \Action \ActionInterface ;
1214use Payum \Core \ApiAwareInterface ;
1315use Payum \Core \Bridge \Spl \ArrayObject ;
2123use Payum \Core \Security \GenericTokenFactoryInterface ;
2224use Payum \Core \Security \TokenInterface ;
2325use Psr \Log \LoggerInterface ;
26+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBagInterface ;
27+ use Symfony \Contracts \Translation \TranslatorInterface ;
2428
2529final class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
2630{
@@ -32,9 +36,20 @@ final class CaptureAction implements ActionInterface, ApiAwareInterface, Gateway
3236 /** @var LoggerInterface */
3337 private $ logger ;
3438
35- public function __construct (LoggerInterface $ logger )
36- {
39+ /** @var FlashBagInterface */
40+ private $ flashBag ;
41+
42+ /** @var TranslatorInterface */
43+ private $ translator ;
44+
45+ public function __construct (
46+ LoggerInterface $ logger ,
47+ FlashBagInterface $ flashBag ,
48+ TranslatorInterface $ translator
49+ ) {
3750 $ this ->logger = $ logger ;
51+ $ this ->flashBag = $ flashBag ;
52+ $ this ->translator = $ translator ;
3853 }
3954
4055 public function setGenericTokenFactory (GenericTokenFactoryInterface $ genericTokenFactory = null ): void
@@ -48,6 +63,13 @@ public function execute($request): void
4863
4964 $ details = ArrayObject::ensureArrayObject ($ request ->getModel ());
5065
66+ if (isset ($ details ['status ' ]) && PayPlugApiClientInterface::FAILED === $ details ['status ' ]) {
67+ // Unset current status to allow to use payplug to change payment method
68+ unset($ details ['status ' ]);
69+
70+ return ;
71+ }
72+
5173 if (isset ($ details ['status ' ], $ details ['payment_id ' ])) {
5274 if (PayPlugApiClientInterface::STATUS_CREATED !== $ details ['status ' ]) {
5375 return ;
@@ -88,11 +110,28 @@ public function execute($request): void
88110 unset($ details ['status ' ]);
89111 }
90112
91- $ payment = $ this ->createPayment ($ details );
113+ try {
114+ $ payment = $ this ->createPayment ($ details );
115+ $ details ['status ' ] = PayPlugApiClientInterface::STATUS_CREATED ;
116+
117+ throw new HttpRedirect ($ payment ->hosted_payment ->payment_url );
118+ } catch (BadRequestException $ badRequestException ) {
119+ $ errorObject = $ badRequestException ->getErrorObject ();
120+ if (null === $ errorObject || [] === $ errorObject ) {
121+ $ this ->flashBag ->add ('error ' , 'payplug_sylius_payplug_plugin.error.api_unknow_error ' );
122+
123+ return ;
124+ }
92125
93- $ details[ ' status ' ] = PayPlugApiClientInterface:: STATUS_CREATED ;
126+ $ this -> notifyErrors ( $ details, $ errorObject ) ;
94127
95- throw new HttpRedirect ($ payment ->hosted_payment ->payment_url );
128+ throw new HttpRedirect ($ details ['hosted_payment ' ]['cancel_url ' ]);
129+ } catch (UnknownApiErrorException $ unknownApiErrorException ) {
130+ $ details ['status ' ] = PayPlugApiClientInterface::FAILED ;
131+ $ this ->flashBag ->add ('error ' , $ this ->translator ->trans ('payplug_sylius_payplug_plugin.error.api_unknow_error ' ));
132+
133+ throw new HttpRedirect ($ details ['hosted_payment ' ]['cancel_url ' ]);
134+ }
96135 }
97136
98137 public function supports ($ request ): bool
@@ -120,14 +159,58 @@ private function addNotificationUrl(TokenInterface $token, ArrayObject $details)
120159
121160 private function createPayment (ArrayObject $ details ): Payment
122161 {
123- $ payment = $ this ->payPlugApiClient ->createPayment ($ details ->getArrayCopy ());
124- $ details ['payment_id ' ] = $ payment ->id ;
125- $ details ['is_live ' ] = $ payment ->is_live ;
162+ try {
163+ $ payment = $ this ->payPlugApiClient ->createPayment ($ details ->getArrayCopy ());
164+ $ details ['payment_id ' ] = $ payment ->id ;
165+ $ details ['is_live ' ] = $ payment ->is_live ;
166+
167+ $ this ->logger ->debug ('[PayPlug] Create payment ' , [
168+ 'payment_id ' => $ payment ->id ,
169+ ]);
170+
171+ return $ payment ;
172+ } catch (BadRequestException $ badRequestException ) {
173+ $ details ['status ' ] = PayPlugApiClientInterface::FAILED ;
174+
175+ throw $ badRequestException ;
176+ } catch (\Throwable $ throwable ) {
177+ $ details ['status ' ] = PayPlugApiClientInterface::FAILED ;
178+
179+ throw new UnknownApiErrorException (
180+ 'payplug_sylius_payplug_plugin.error.api_unknow_error ' ,
181+ $ throwable ->getCode (),
182+ $ throwable ,
183+ );
184+ }
185+ }
186+
187+ private function notifyErrors (ArrayObject $ details , array $ errorDetails ): void
188+ {
189+ if (!isset ($ errorDetails ['details ' ])) {
190+ return ;
191+ }
192+
193+ if (isset ($ errorDetails ['details ' ]['billing ' ]['postcode ' ])) {
194+ $ this ->flashBag ->add (
195+ 'error ' ,
196+ $ this ->translator ->trans ('payplug_sylius_payplug_plugin.ui.error.billing.postcode ' , [
197+ '%postalCode% ' => $ details ['billing ' ]['postcode ' ],
198+ ])
199+ );
200+ }
126201
127- $ this ->logger ->debug ('[PayPlug] Create payment ' , [
128- 'payment_id ' => $ payment ->id ,
129- ]);
202+ if (isset ($ errorDetails ['details ' ]['shipping ' ]['postcode ' ])) {
203+ $ this ->flashBag ->add (
204+ 'error ' ,
205+ $ this ->translator ->trans ('payplug_sylius_payplug_plugin.ui.error.shipping.postcode ' , [
206+ '%postalCode% ' => $ details ['shipping ' ]['postcode ' ],
207+ ])
208+ );
209+ }
130210
131- return $ payment ;
211+ if (!isset ($ errorDetails ['details ' ]['billing ' ]['postcode ' ]) &&
212+ !isset ($ errorDetails ['details ' ]['shipping ' ]['postcode ' ])) {
213+ $ this ->flashBag ->add ('error ' , 'payplug_sylius_payplug_plugin.error.api_unknow_error ' );
214+ }
132215 }
133216}
0 commit comments