|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\App\Controllers\Payment; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use WMDE\Euro\Euro; |
|
11
|
|
|
use WMDE\Fundraising\DonationContext\UseCases\CreditCardPaymentNotification\CreditCardNotificationResponse; |
|
12
|
|
|
use WMDE\Fundraising\DonationContext\UseCases\CreditCardPaymentNotification\CreditCardPaymentNotificationRequest; |
|
13
|
|
|
use WMDE\Fundraising\Frontend\Factories\FunFunFactory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @license GPL-2.0-or-later |
|
17
|
|
|
*/ |
|
18
|
|
|
class CreditCardPaymentNotificationController { |
|
19
|
|
|
|
|
20
|
|
|
private const MSG_NOT_HANDLED = 'Credit card request "%s" not handled'; |
|
21
|
|
|
|
|
22
|
|
|
public function index( FunFunFactory $ffFactory, Request $request ): Response { |
|
23
|
|
|
$queryParams = $request->query; |
|
24
|
|
|
$donationId = $queryParams->get( 'donation_id', '' ); |
|
25
|
|
|
|
|
26
|
|
|
if ( !$this->requestIsForPaymentCompletion( $queryParams ) ) { |
|
27
|
|
|
return $this->handleInvalidNotificationType( $ffFactory, $queryParams, $donationId ); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->handleBillingNotification( $ffFactory, $queryParams, $donationId, $request->getClientIp() ); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function requestIsForPaymentCompletion( ParameterBag $query ): bool { |
|
34
|
|
|
return $query->get( 'function', '' ) === 'billing'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function handleInvalidNotificationType( FunFunFactory $ffFactory, ParameterBag $queryParams, string $donationId ): Response { |
|
38
|
|
|
$ffFactory->getCreditCardLogger()->info( |
|
39
|
|
|
sprintf( self::MSG_NOT_HANDLED, $queryParams->get( 'function' ) ), |
|
40
|
|
|
$queryParams->all() |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
return new Response( $ffFactory->newCreditCardNotificationPresenter()->present( |
|
44
|
|
|
new CreditCardNotificationResponse( |
|
45
|
|
|
false, |
|
46
|
|
|
sprintf( 'Function "%s" not supported by this end point', $queryParams->get( 'function' ) ) |
|
47
|
|
|
), |
|
48
|
|
|
$donationId, |
|
49
|
|
|
$queryParams->get( 'token', '' ) |
|
50
|
|
|
) ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function handleBillingNotification( FunFunFactory $ffFactory, ParameterBag $queryParams, string $donationId, string $clientIp ): Response { |
|
54
|
|
|
$response = $ffFactory->newCreditCardNotificationUseCase( $queryParams->get( 'utoken', '' ) ) |
|
55
|
|
|
->handleNotification( |
|
56
|
|
|
( new CreditCardPaymentNotificationRequest() ) |
|
57
|
|
|
->setTransactionId( $queryParams->get( 'transactionId', '' ) ) |
|
58
|
|
|
->setDonationId( (int)$donationId ) |
|
59
|
|
|
->setAmount( Euro::newFromCents( (int)$queryParams->get( 'amount' ) ) ) |
|
60
|
|
|
->setCustomerId( $queryParams->get( 'customerId', '' ) ) |
|
61
|
|
|
->setSessionId( $queryParams->get( 'sessionId', '' ) ) |
|
62
|
|
|
->setAuthId( $queryParams->get( 'auth', '' ) ) |
|
63
|
|
|
->setTitle( $queryParams->get( 'title', '' ) ) |
|
64
|
|
|
->setCountry( $queryParams->get( 'country', '' ) ) |
|
65
|
|
|
->setCurrency( $queryParams->get( 'currency', '' ) ) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$loggingContext = $response->getLowLevelError() === null ? [] : [ 'exception' => $response->getLowLevelError() ]; |
|
69
|
|
|
if ( !$response->isSuccessful() ) { |
|
70
|
|
|
$loggingContext['queryParams'] = $queryParams->all(); |
|
71
|
|
|
$loggingContext['clientIP'] = $clientIp; |
|
72
|
|
|
$ffFactory->getLogger()->error( 'Credit Card Notification Error: ' . $response->getErrorMessage(), $loggingContext ); |
|
73
|
|
|
} elseif ( $loggingContext !== [] ) { |
|
74
|
|
|
$ffFactory->getLogger()->warning( 'Failed to send conformation email for credit card notification', $loggingContext ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return new Response( $ffFactory->newCreditCardNotificationPresenter()->present( |
|
78
|
|
|
$response, |
|
79
|
|
|
$donationId, |
|
80
|
|
|
$queryParams->get( 'token', '' ) |
|
81
|
|
|
) ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|