Completed
Pull Request — master (#650)
by Jeroen De
19:07 queued 01:42
created

handleNotification()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 6
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\DonationContext\UseCases\CreditCardPaymentNotification;
6
7
use Psr\Log\LoggerInterface;
8
use WMDE\Fundraising\Frontend\DonationContext\Authorization\DonationAuthorizer;
9
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\Donation;
10
use WMDE\Fundraising\Frontend\DonationContext\Domain\Repositories\DonationRepository;
11
use WMDE\Fundraising\Frontend\DonationContext\Domain\Repositories\GetDonationException;
12
use WMDE\Fundraising\Frontend\DonationContext\Domain\Repositories\StoreDonationException;
13
use WMDE\Fundraising\Frontend\DonationContext\Infrastructure\DonationConfirmationMailer;
14
use WMDE\Fundraising\Frontend\DonationContext\Infrastructure\DonationEventLogger;
15
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\CreditCardTransactionData;
16
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PaymentType;
17
use WMDE\Fundraising\Frontend\PaymentContext\Infrastructure\CreditCardService;
18
19
/**
20
 * @license GNU GPL v2+
21
 * @author Kai Nissen < [email protected] >
22
 */
23
class CreditCardNotificationUseCase {
24
25
	private $repository;
26
	private $authorizationService;
27
	private $creditCardService;
28
	private $mailer;
29
	private $logger;
30
	private $donationEventLogger;
31
32
	public function __construct( DonationRepository $repository, DonationAuthorizer $authorizationService,
33
								 CreditCardService $creditCardService, DonationConfirmationMailer $mailer,
34
								 LoggerInterface $logger, DonationEventLogger $donationEventLogger ) {
35
		$this->repository = $repository;
36
		$this->authorizationService = $authorizationService;
37
		$this->creditCardService = $creditCardService;
38
		$this->mailer = $mailer;
39
		$this->logger = $logger;
40
		$this->donationEventLogger = $donationEventLogger;
41
	}
42
43
	/**
44
	 * @param CreditCardPaymentNotificationRequest $request
45
	 * @throws CreditCardPaymentHandlerException
46
	 */
47
	public function handleNotification( CreditCardPaymentNotificationRequest $request ) {
48
		try {
49
			$donation = $this->repository->getDonationById( $request->getDonationId() );
50
		} catch ( GetDonationException $ex ) {
51
			throw new CreditCardPaymentHandlerException( 'data set could not be retrieved from database', $ex );
52
		}
53
54
		if ( $donation === null ) {
55
			throw new CreditCardPaymentHandlerException( 'donation not found' );
56
		}
57
58
		if ( $donation->getPaymentType() !== PaymentType::CREDIT_CARD ) {
59
			throw new CreditCardPaymentHandlerException( 'payment type mismatch' );
60
		}
61
62
		if ( !$donation->getAmount()->equals( $request->getAmount() ) ) {
63
			throw new CreditCardPaymentHandlerException( 'amount mismatch' );
64
		}
65
66
		if ( !$this->authorizationService->systemCanModifyDonation( $request->getDonationId() ) ) {
67
			throw new CreditCardPaymentHandlerException( 'invalid or expired token' );
68
		}
69
70
		$this->handleRequest( $request, $donation );
71
	}
72
73
	/**
74
	 * @param CreditCardPaymentNotificationRequest $request
75
	 * @param \WMDE\Fundraising\Frontend\DonationContext\Domain\Model\Donation $donation
76
	 */
77
	private function handleRequest( CreditCardPaymentNotificationRequest $request, Donation $donation ) {
78
		try {
79
			$donation->addCreditCardData( $this->newCreditCardDataFromRequest( $request ) );
80
			$donation->confirmBooked();
81
		} catch ( \RuntimeException $e ) {
82
			throw new CreditCardPaymentHandlerException( 'data set could not be updated', $e );
83
		}
84
85
		try {
86
			$this->repository->storeDonation( $donation );
87
		}
88
		catch ( StoreDonationException $ex ) {
89
			throw new CreditCardPaymentHandlerException( 'updated data set could not be stored', $ex );
90
		}
91
92
		$this->sendConfirmationEmail( $donation );
93
		$this->donationEventLogger->log( $donation->getId(), 'mcp_handler: booked' );
94
	}
95
96
	private function sendConfirmationEmail( Donation $donation ) {
97
		if ( $donation->getDonor() !== null ) {
98
			try {
99
				$this->mailer->sendConfirmationMailFor( $donation );
100
			} catch ( \RuntimeException $ex ) {
101
				// no need to re-throw or return false, this is not a fatal error, only a minor inconvenience
102
			}
103
		}
104
	}
105
106
	private function newCreditCardDataFromRequest( CreditCardPaymentNotificationRequest $request ): CreditCardTransactionData {
107
		return ( new CreditCardTransactionData() )
108
			->setTransactionId( $request->getTransactionId() )
109
			->setTransactionStatus( 'processed' )
110
			->setTransactionTimestamp( new \DateTime() )
111
			->setCardExpiry( $this->creditCardService->getExpirationDate( $request->getCustomerId() ) )
112
			->setAmount( $request->getAmount() )
113
			->setCustomerId( $request->getCustomerId() )
114
			->setSessionId( $request->getSessionId() )
115
			->setAuthId( $request->getAuthId() )
116
			->setTitle( $request->getTitle() )
117
			->setCountryCode( $request->getCountry() )
118
			->setCurrencyCode( $request->getCurrency() );
119
	}
120
121
}
122