Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate amount #194

Merged
merged 1 commit into from Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/routes.php
Expand Up @@ -25,6 +25,7 @@
use WMDE\Fundraising\Frontend\UseCases\CancelDonation\CancelDonationRequest;
use WMDE\Fundraising\Frontend\UseCases\ListComments\CommentListingRequest;
use WMDE\Fundraising\Frontend\UseCases\AddSubscription\SubscriptionRequest;
use WMDE\Fundraising\Frontend\Validation\ConstraintViolation;

$app->get(
'validate-email',
Expand All @@ -37,6 +38,26 @@ function( Request $request ) use ( $app, $ffFactory ) {
}
);

$app->post(
'validate-amount',
function( Request $request ) use ( $app, $ffFactory ) {

$amount = (float) $ffFactory->newDecimalNumberFormatter()->parse( $request->get( 'amount', 0 ) );
$amountValidator = $ffFactory->newAmountValidator();
$validationResult = $amountValidator->validate( $amount, (string) $request->get( 'paymentType', '' ) );

if ( $validationResult->isSuccessful() ) {
return $app->json( [ 'status' => 'OK' ] );
}
else {
$errors = array_map( function ( ConstraintViolation $constraintViolation ) {
return $constraintViolation->getMessage();
}, $validationResult->getViolations() );
return $app->json( [ 'status' => 'ERR', 'message' => implode( "\n", $errors ) ] );
}
}
);

$app->get(
'list-comments.json',
function( Request $request ) use ( $app, $ffFactory ) {
Expand Down
7 changes: 6 additions & 1 deletion src/FunFunFactory.php
Expand Up @@ -19,6 +19,7 @@
use Monolog\Handler\BufferHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use NumberFormatter;
use Psr\Log\LoggerInterface;
use Swift_MailTransport;
use Symfony\Component\Translation\TranslatorInterface;
Expand Down Expand Up @@ -610,7 +611,7 @@ public function getDonationRepository(): DonationRepository {
return $this->pimple['donation_repository'];
}

private function newAmountValidator(): AmountValidator {
public function newAmountValidator(): AmountValidator {
return new AmountValidator(
0.01,
[
Expand All @@ -620,6 +621,10 @@ private function newAmountValidator(): AmountValidator {
);
}

public function newDecimalNumberFormatter(): NumberFormatter {
return new NumberFormatter( $this->config['locale'], NumberFormatter::DECIMAL );
}

public function newAddCommentUseCase(): AddCommentUseCase {
return new AddCommentUseCase(
$this->getCommentRepository()
Expand Down
43 changes: 43 additions & 0 deletions tests/System/Routes/ValidateAmountRouteTest.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types = 1);

namespace WMDE\Fundraising\Frontend\Tests\System\Routes;

use WMDE\Fundraising\Frontend\Tests\System\WebRouteTestCase;
use WMDE\Fundraising\Frontend\Domain\Model\PaymentType;

/**
* @licence GNU GPL v2+
* @author Gabriel Birke < gabriel.birke@wikimedia.de >
*/
class ValidateAmountRouteTest extends WebRouteTestCase {

public function testGivenValidAmount_successResponseIsReturned() {
$client = $this->createClient();

$client->request(
'POST',
'/validate-amount',
[ 'amount' => '23', 'paymentType' => PaymentType::BANK_TRANSFER ]
);

$this->assertJsonSuccessResponse(
[ 'status' => 'OK' ],
$client->getResponse()
);
}

public function testGivenInvalidAmount_failureResponseIsReturned() {
$client = $this->createClient();

$client->request(
'POST',
'/validate-amount',
[ 'amount' => '-1', 'paymentType' => PaymentType::BANK_TRANSFER ]
);

$this->assertErrorJsonResponse( $client->getResponse() );
}

}