Valentin Harrang
Valentin Harrang

Reputation: 1191

Symfony - Redirect with post data in a controller

The goal is to register an idle user on my website and redirect it to a payment platform by sending the user data entered in the form via the POST method.

I have a form in POST that I use to create an inactive user if this form is valid. I would then like to redirect my user to an external URL while sending the data from this form in POST to this URL. This URL accepts only specific variable names (these variables are in the associative array of my RedirectResponse) and only in POST.

The user will make his payment on the external link and if the payment is successful I will activate the user later. I identify with the payment platform by sending her 'ID' and she has authorized my domain name.

I tried to use RedirectResponse with status 307 but I think it is not possible to send it POST data.

     * @Route("/{id<\d+>?1}/{slug}", methods={"GET", "POST"}, name="show")
     * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')")
     */
    public function show(Offre $offre, $slug, Request $request): Response
    {
        if ($offre->getSlug() !== $slug) {
            return $this->redirectToRoute('site_devenir_vip_show', [
                'id'   => $offre->getId(),
                'slug' => $offre->getSlug(),
            ], Response::HTTP_MOVED_PERMANENTLY);
        }

        $utilisateur = new User();

        $form = $this->createForm(UserType::class, $utilisateur);
        $form->handleRequest($request);

        if ($form->isSubmitted() === true) {
            if ($form->isValid() === true) {
                // TODO: create the inactive User here

                // TODO: redirect the user on the payment platform
                return new RedirectResponse('https://mywebsite.com', 307, [
                    'NOM'        => $utilisateur->getNom(),
                    'PRENOM'     => $utilisateur->getPrenom(),
                    'TEL'        => $utilisateur->getTel(),
                    'EMAIL'      => $utilisateur->getEmail(),
                    'PAYS'       => $utilisateur->getPays(),
                    'ID'         => 'XXXX',
                    'ABONNEMENT' => 'XXXX',
                ]);
            }

            $this->addFlash('error', 'Le formulaire comporte des erreurs.');
        }

        return $this->render('site/pages/devenir_vip/show.html.twig', [
            'offre' => $offre,
            'form'  => $form->createView(),
        ]);
    }

I am currently redirected to the external link that is in the RedirectResponse but it does not get the parameters. Do you have an idea ?

Upvotes: 1

Views: 9707

Answers (1)

Somrlik
Somrlik

Reputation: 740

POST is a request method, not a response "method". Requests have methods (GET, POST, PUT, PATCH, HEAD, etc.) and responses have status codes (200, 404, 500, etc.).

You probably need to create the payment by sending the data to gate using an HTTP client from php and then redirecting the user, if the payment was created. Payment gates often respond with the url you should redirect your user to. Refer to the API documentation for your gate.

Symfony does not include an HTTP client by default (not a proper one anyways). I recommend Guzzle.

Mock code:

if ($form->isSubmitted() === true) {
    if ($form->isValid() === true) {
        $httpClient = new Client();

        $response = $httpClient->post('https://paymentgate.com', [
            'key' => 'value',
        ]);

        // Validate response somehow
        if ($response->statusCode() !== 200) {
            $this->addFlash('error', 'Payment gate failed to respond');
        } else {
            // Let's assume that the gate returns a json with key 'redirect' containing the url
            $json = json_decode($response->getContent());
            return new RedirectResponse($json->redirect);
        }
    }

    $this->addFlash('error', 'Le formulaire comporte des erreurs.');
}

Upvotes: 2

Related Questions