1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BitBag\SyliusBraintreePlugin\Action; |
||
6 | |||
7 | use BitBag\SyliusBraintreePlugin\Request\Api\GenerateClientToken; |
||
8 | use BitBag\SyliusBraintreePlugin\Request\ObtainCardholderAuthentication; |
||
9 | use Payum\Core\Action\ActionInterface; |
||
10 | use Payum\Core\Bridge\Spl\ArrayObject; |
||
11 | use Payum\Core\Exception\RequestNotSupportedException; |
||
12 | use Payum\Core\GatewayAwareInterface; |
||
13 | use Payum\Core\GatewayAwareTrait; |
||
14 | use Payum\Core\Reply\HttpResponse; |
||
15 | use Payum\Core\Request\GetHttpRequest; |
||
16 | use Payum\Core\Request\RenderTemplate; |
||
17 | |||
18 | final class ObtainCardholderAuthenticationAction implements ActionInterface, GatewayAwareInterface |
||
19 | { |
||
20 | use GatewayAwareTrait; |
||
21 | |||
22 | /** @var string */ |
||
23 | private $templateName; |
||
24 | |||
25 | public function __construct(string $templateName) |
||
26 | { |
||
27 | $this->templateName = $templateName; |
||
28 | } |
||
29 | |||
30 | public function execute($request): void |
||
31 | { |
||
32 | RequestNotSupportedException::assertSupports($this, $request); |
||
33 | |||
34 | $details = ArrayObject::ensureArrayObject($request->getModel()); |
||
35 | |||
36 | $details->validateNotEmpty(['paymentMethodNonce', 'paymentMethodNonceInfo']); |
||
37 | |||
38 | $paymentMethodNonceInfo = $details['paymentMethodNonceInfo']; |
||
39 | |||
40 | if (array_key_exists('threeDSecureInfo', $paymentMethodNonceInfo) && array_key_exists('status', $paymentMethodNonceInfo['threeDSecureInfo'])) { |
||
41 | return; |
||
42 | } |
||
43 | |||
44 | $this->gateway->execute($clientHttpRequest = new GetHttpRequest()); |
||
45 | |||
46 | if ('POST' == $clientHttpRequest->method && array_key_exists('threeDSecure_payment_method_nonce', $clientHttpRequest->request)) { |
||
47 | $paymentMethodNonce = $clientHttpRequest->request['threeDSecure_payment_method_nonce']; |
||
48 | |||
49 | $request->setResponse($paymentMethodNonce); |
||
50 | |||
51 | return; |
||
52 | } |
||
53 | |||
54 | if (false == $details->offsetExists('clientToken')) { |
||
0 ignored issues
–
show
|
|||
55 | $this->generateClientToken($details); |
||
56 | } |
||
57 | |||
58 | $details->validateNotEmpty(['clientToken', 'paymentMethodNonce']); |
||
59 | |||
60 | $this->gateway->execute($template = new RenderTemplate($this->templateName, [ |
||
61 | 'formAction' => $clientHttpRequest->uri, |
||
62 | 'clientToken' => $details['clientToken'], |
||
63 | 'amount' => $details['amount'], |
||
64 | 'creditCard' => $details['paymentMethodNonce'], |
||
65 | 'details' => $details, |
||
66 | ])); |
||
67 | |||
68 | throw new HttpResponse($template->getResult()); |
||
69 | } |
||
70 | |||
71 | public function supports($request): bool |
||
72 | { |
||
73 | return |
||
74 | $request instanceof ObtainCardholderAuthentication && |
||
75 | $request->getModel() instanceof \ArrayAccess |
||
76 | ; |
||
77 | } |
||
78 | |||
79 | protected function generateClientToken($details) |
||
80 | { |
||
81 | $request = new GenerateClientToken(); |
||
82 | |||
83 | $this->gateway->execute($request); |
||
84 | |||
85 | $details['clientToken'] = $request->getResponse(); |
||
86 | } |
||
87 | } |
||
88 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.