getAttribute('oid')); /** @var OrderEntity */ $order = $this->dispatcher->dispatch($cmd); } catch (OrderNotFoundException $th) { return new RedirectResponse('/app'); } $ws = $order->getWorkspace(); // Current subscription, cancel after new subscription is created $sub = $ws->getSubscription(); try { $gateway = $this->factory->create( $request->getAttribute('gateway') ); } catch (GatewayNotFoundException $th) { return new RedirectResponse('/app'); } $params = $request->getQueryParams(); if ($request->getMethod() == RequestMethod::POST->value) { $params = [...$params, ...json_decode( json_encode($request->getParsedBody() ?: []), true )]; } try { $id = $gateway->completePurchase($order, $params); } catch (PaymentException $th) { return new RedirectResponse( '/app/billing/orders/' . $order->getId()->getValue() . '/receipt' ); } try { $cmd = new PayOrderCommand( $order, $request->getAttribute('gateway'), $id ); $this->dispatcher->dispatch($cmd); } catch (AlreadyFulfilledException $th) { return new RedirectResponse( '/app/billing/orders/' . $order->getId()->getValue() . '/receipt' ); } catch (AlreadyPaidException $th) { // Already paid, go to next step } try { $cmd = new FulfillOrderCommand($order); $this->dispatcher->dispatch($cmd); } catch (AlreadyFulfilledException $th) { // Already fulfilled, go to next step } // Cancel current subscription if ($sub && $order->getPlan()->getBillingCycle()->isRenewable()) { $cmd = new CancelSubscriptionCommand($sub); $this->dispatcher->dispatch($cmd); } return new RedirectResponse( '/app/billing/orders/' . $order->getId()->getValue() . '/receipt' ); } }