-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetPaymentMethods.php
More file actions
65 lines (54 loc) · 1.98 KB
/
GetPaymentMethods.php
File metadata and controls
65 lines (54 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace MatchBot\Application\Actions;
use JetBrains\PhpStorm\Pure;
use MatchBot\Application\Auth\PersonManagementAuthMiddleware;
use MatchBot\Application\Auth\PersonWithPasswordAuthMiddleware;
use MatchBot\Application\Security\Security;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Stripe\StripeClient;
class GetPaymentMethods extends Action
{
#[Pure]
public function __construct(
private StripeClient $stripeClient,
private Security $securityService,
LoggerInterface $logger
) {
parent::__construct($logger);
}
/**
* @see PersonWithPasswordAuthMiddleware
*/
#[\Override]
protected function action(Request $request, Response $response, array $args): Response
{
$donor = $this->securityService->requireAuthenticatedDonorAccountWithPassword($request);
$paymentMethods = $this->stripeClient->customers->allPaymentMethods(
$donor->stripeCustomerId->stripeCustomerId,
['type' => 'card'],
);
$paymentMethodArray = $paymentMethods->toArray()['data'];
\assert(is_array($paymentMethodArray));
$regularGivingPaymentMethod = null;
$nonRegularGivingMethods = array_values(array_filter(
$paymentMethodArray,
static function (array $paymentMethod) use ($donor, &$regularGivingPaymentMethod): bool {
if ($paymentMethod['id'] === $donor->getRegularGivingPaymentMethod()?->stripePaymentMethodId) {
$regularGivingPaymentMethod = $paymentMethod;
return false;
}
return true;
}
));
return $this->respondWithData(
$response,
[
'data' => $nonRegularGivingMethods,
'regularGivingPaymentMethod' => $regularGivingPaymentMethod
]
);
}
}