Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BDE] create donation form page (#10150)
- Loading branch information
Showing
16 changed files
with
500 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace App\BesoinDEurope\Donation; | ||
|
||
use App\Address\Address; | ||
use App\Entity\Adherent; | ||
use App\Recaptcha\RecaptchaChallengeInterface; | ||
use App\Recaptcha\RecaptchaChallengeTrait; | ||
use App\Validator\Recaptcha as AssertRecaptcha; | ||
use App\Validator\StrictEmail; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* @AssertRecaptcha(api="friendly_captcha") | ||
*/ | ||
class DonationRequest implements RecaptchaChallengeInterface | ||
{ | ||
use RecaptchaChallengeTrait; | ||
|
||
/** | ||
* @Assert\NotBlank | ||
* @Assert\Range(min=10, max=4600) | ||
*/ | ||
public ?int $amount = null; | ||
|
||
/** | ||
* @Assert\NotBlank | ||
* @StrictEmail(dnsCheck=false) | ||
*/ | ||
public ?string $email = null; | ||
|
||
/** | ||
* @Assert\NotBlank | ||
* @Assert\Choice(callback={"App\ValueObject\Genders", "all"}, message="common.invalid_choice") | ||
*/ | ||
public ?string $civility = null; | ||
|
||
/** | ||
* @Assert\NotBlank | ||
* @Assert\Length( | ||
* allowEmptyString=true, | ||
* min=2, | ||
* max=50, | ||
* minMessage="common.first_name.min_length", | ||
* maxMessage="common.first_name.max_length" | ||
* ) | ||
*/ | ||
public ?string $firstName = null; | ||
|
||
/** | ||
* @Assert\NotBlank | ||
* @Assert\Length( | ||
* allowEmptyString=true, | ||
* min=1, | ||
* max=50, | ||
* minMessage="common.last_name.min_length", | ||
* maxMessage="common.last_name.max_length" | ||
* ) | ||
*/ | ||
public ?string $lastName = null; | ||
|
||
/** | ||
* @Assert\Valid | ||
*/ | ||
public ?Address $address = null; | ||
|
||
/** | ||
* @Assert\Country | ||
*/ | ||
public ?string $nationality = null; | ||
|
||
public ?string $utmSource = null; | ||
public ?string $utmCampaign = null; | ||
|
||
public function updateFromAdherent(Adherent $user): void | ||
{ | ||
$this->email = $user->getEmailAddress(); | ||
$this->firstName = $user->getFirstName(); | ||
$this->lastName = $user->getLastName(); | ||
$this->civility = $user->getGender(); | ||
$this->nationality = $user->getNationality(); | ||
$this->address = Address::createFromAddress($user->getPostAddress()); | ||
} | ||
|
||
public function hasAmount(): bool | ||
{ | ||
return $this->amount >= 10; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace App\Controller\BesoinDEurope; | ||
|
||
use App\BesoinDEurope\Donation\DonationRequest; | ||
use App\Donation\Systempay\RequestParamsBuilder; | ||
use App\Form\BesoinDEurope\DonationRequestType; | ||
use App\Security\Http\Session\AnonymousFollowerSession; | ||
use App\Utils\UtmParams; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; | ||
|
||
#[Route(path: '/don', name: 'app_bde_donation', methods: ['GET', 'POST'])] | ||
class DonationController extends AbstractController | ||
{ | ||
public function __construct( | ||
private readonly CsrfTokenManagerInterface $csrfTokenManager, | ||
private readonly RequestParamsBuilder $requestParamsBuilder, | ||
private readonly AnonymousFollowerSession $anonymousFollowerSession, | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
if ($response = $this->anonymousFollowerSession->start($request)) { | ||
return $response; | ||
} | ||
|
||
$donationRequest = $this->getDonationRequest($request); | ||
|
||
$form = $this | ||
->createForm(DonationRequestType::class, $donationRequest) | ||
->handleRequest($request) | ||
; | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
return $this->render('besoindeurope/donation/payment.html.twig', [ | ||
'params' => $this->requestParamsBuilder->build($donationRequest), | ||
]); | ||
} | ||
|
||
return $this->renderForm('besoindeurope/donation/form.html.twig', [ | ||
'form' => $form, | ||
'email_validation_token' => $this->csrfTokenManager->getToken('email_validation_token'), | ||
'step' => $donationRequest->hasAmount() ? 1 : 0, | ||
]); | ||
} | ||
|
||
private function getDonationRequest(Request $request): DonationRequest | ||
{ | ||
$donationRequest = new DonationRequest(); | ||
|
||
if ($user = $this->getUser()) { | ||
$donationRequest->updateFromAdherent($user); | ||
} | ||
|
||
$donationRequest->email = $request->query->get('email'); | ||
$donationRequest->amount = $request->query->getInt('amount'); | ||
|
||
if ($request->query->has(UtmParams::UTM_SOURCE)) { | ||
$donationRequest->utmSource = UtmParams::filterUtmParameter($request->query->get(UtmParams::UTM_SOURCE)); | ||
$donationRequest->utmCampaign = UtmParams::filterUtmParameter($request->query->get(UtmParams::UTM_CAMPAIGN)); | ||
} | ||
|
||
return $donationRequest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Donation\Systempay; | ||
|
||
use App\BesoinDEurope\Donation\DonationRequest; | ||
use App\ValueObject\Genders; | ||
|
||
class RequestParamsBuilder | ||
{ | ||
public function __construct( | ||
private readonly string $systemPaySiteId, | ||
private readonly string $systemPayMode, | ||
private readonly string $systemPayKey, | ||
) { | ||
} | ||
|
||
public function build(DonationRequest $donationRequest): array | ||
{ | ||
$params = [ | ||
'vads_site_id' => $this->systemPaySiteId, | ||
'vads_ctx_mode' => $this->systemPayMode, | ||
'vads_trans_id' => sprintf("%'.06d", (strtotime('tomorrow') - time()) * 10 + ((microtime(true) * 10) % 10)), | ||
'vads_trans_date' => date('YmdHis'), | ||
'vads_amount' => $donationRequest->amount * 100, | ||
'vads_currency' => '978', | ||
'vads_action_mode' => 'INTERACTIVE', | ||
'vads_page_action' => 'PAYMENT', | ||
'vads_version' => 'V2', | ||
'vads_payment_config' => 'SINGLE', | ||
'vads_capture_delay' => '0', | ||
'vads_validation_mode' => '0', | ||
'vads_cust_title' => match ($donationRequest->civility) { | ||
Genders::MALE => 'Monsieur', | ||
Genders::FEMALE => 'Madame', | ||
default => '', | ||
}, | ||
'vads_cust_first_name' => $donationRequest->firstName, | ||
'vads_cust_last_name' => $donationRequest->lastName, | ||
'vads_cust_email' => $donationRequest->email, | ||
'vads_cust_address' => $donationRequest->address->getAddress(), | ||
'vads_cust_address2' => $donationRequest->address->getAdditionalAddress(), | ||
'vads_cust_zip' => $donationRequest->address->getPostalCode(), | ||
'vads_cust_city' => $donationRequest->address->getCityName(), | ||
'vads_cust_country' => $donationRequest->address->getCountry(), | ||
'vads_ext_info_nationalite' => $donationRequest->nationality, | ||
'vads_ext_info_utm_source' => $donationRequest->utmSource, | ||
'vads_ext_info_utm_campagne' => $donationRequest->utmCampaign, | ||
]; | ||
ksort($params); | ||
$data = implode('+', $params); | ||
$data .= '+'.$this->systemPayKey; | ||
|
||
$signData = base64_encode(hash_hmac('sha256', $data, $this->systemPayKey, true)); | ||
|
||
$params['signature'] = $signData; | ||
|
||
return $params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Form\BesoinDEurope; | ||
|
||
use App\Address\AddressInterface; | ||
use App\Form\AutocompleteAddressType; | ||
use App\Form\CivilityType; | ||
use App\Form\RequiredCheckboxType; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CountryType; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class DonationRequestType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('amount', HiddenType::class) | ||
->add('email', EmailType::class) | ||
->add('civility', CivilityType::class) | ||
->add('firstName', TextType::class) | ||
->add('lastName', TextType::class) | ||
->add('nationality', CountryType::class, ['preferred_choices' => [AddressInterface::FRANCE]]) | ||
->add('address', AutocompleteAddressType::class, ['with_additional_address' => true]) | ||
->add('autorisations', RequiredCheckboxType::class) | ||
; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'donation_request'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.