Home > Mobile >  Symfony 5 - A problem with the form to registrate a credit card with MangoPay
Symfony 5 - A problem with the form to registrate a credit card with MangoPay

Time:04-25

I'm trying to register a credit card with MangoPay.

I've installed the mangopay/php-sdk-v2 package.

To register a credit card, it needs three steps.

  1. Create a token of the card
  2. Post card info (using a url created by the token) that will render a string that start with data=
  3. Add the registered card to the MangoPay user
// ProfilController.php 

   /**
     * @Route("/payment/{id}", name="payment")
     * * @param int $id
     */
    public function payment(Request $request, ApiUser $ApiUser, $id): Response
    {           
            $returnUrl = "";

            $user = $this->userRepository->findOneBy(['id' => $id]);
            $userId = $user->getIdMangopay();
            $registration = $ApiUser->Registration($userId);
            
            if($request->request->count() > 0){
                $payment = new PaymentMethod(); 
                $payment->setName($request->request->get('name'));
                $payment->setCardNumber($request->request->get('cardNumber'));
        
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->persist($payment);
                $entityManager->flush();

                $registrationCard = $ApiUser->RegistrationCard($registration, $request);

                $returnUrl = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'];
                $returnUrl .= '/profil';
            }
            
            return $this->render('home/payment.html.twig', [
                    'CardRegistrationUrl' => $registration->CardRegistrationURL,
                    'Data' => $registration->PreregistrationData,
                    'AccessKeyRef' => $registration->AccessKey,
                    'returnUrl' => $returnUrl,
            ]);
    }

The Registration and ResitrationCard functions come from the ApiUser file:

// ApiUser.php

    public function Registration($UserId)
    {
        $CardRegistration = new \MangoPay\CardRegistration();
        $CardRegistration->UserId = $UserId;
        $CardRegistration->Currency = "EUR";
        $CardRegistration->CardType = "CB_VISA_MASTERCARD";
        $Result = $this->mangoPayApi->CardRegistrations->Create($CardRegistration);
      $this->registrationInfo = $Result;
      $this->CardRegistrationUrl = $Result->CardRegistrationURL;

      return $Result;
    }

    public function RegistrationCard($CardInfo)
    {
      $cardRegister = $this->mangoPayApi->CardRegistrations->Get($CardInfo->Id);

      $cardRegister->RegistrationData = $_SERVER['QUERY'];
      
      $updatedCardRegister  = $this->mangoPayApi->CardRegistrations->Update($cardRegister);
    
      return $Result;
    }

I'm able to create the token of the card and get the data= string, but the problem is that I cannot do the last step.

It seems that I cannot enter into the if statement, so it doesn't register the card on the database and I cannot update the card information (3rd step).

The returnUrl, I can simply put it outside of the if statement to make it works, but I want to change it only if the form is valid.

How can I fix the statement? Why doesn't it enter into the if?

CodePudding user response:

Please try to use the regular form validation process of Symfony, and let me know if this helps.

To do this, you need to customize the input name attribute for it to match the payment API config.

In your Type class:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        // ...
        ->add('new-input-name-goes-here', TextType::class, [
            'property_path' => '[data]'
        ]);
}
    
public function getBlockPrefix()
{
    return '';
}
  • Related