BitBagCommerce /
SyliusBraintreePlugin
| 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\ObtainPaymentMethodNonce; |
||
| 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 ObtainPaymentMethodNonceAction implements ActionInterface, GatewayAwareInterface |
||
| 19 | { |
||
| 20 | use GatewayAwareTrait; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | private $templateName; |
||
| 24 | |||
| 25 | protected $cardholderAuthenticationRequired; |
||
| 26 | |||
| 27 | public function __construct(string $templateName) |
||
| 28 | { |
||
| 29 | $this->templateName = $templateName; |
||
| 30 | |||
| 31 | $this->cardholderAuthenticationRequired = true; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function setCardholderAuthenticationRequired($value): void |
||
| 35 | { |
||
| 36 | $this->cardholderAuthenticationRequired = $value; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function execute($request): void |
||
| 40 | { |
||
| 41 | RequestNotSupportedException::assertSupports($this, $request); |
||
| 42 | |||
| 43 | $details = ArrayObject::ensureArrayObject($request->getModel()); |
||
| 44 | |||
| 45 | if (true == $details->offsetExists('paymentMethodNonce')) { |
||
|
0 ignored issues
–
show
|
|||
| 46 | $request->setResponse($details['paymentMethodNonce']); |
||
| 47 | |||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | $this->gateway->execute($clientHttpRequest = new GetHttpRequest()); |
||
| 52 | |||
| 53 | if (array_key_exists('payment_method_nonce', $clientHttpRequest->request)) { |
||
| 54 | $paymentMethodNonce = $clientHttpRequest->request['payment_method_nonce']; |
||
| 55 | |||
| 56 | $request->setResponse($paymentMethodNonce); |
||
| 57 | |||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (false == $details->offsetExists('clientToken')) { |
||
|
0 ignored issues
–
show
|
|||
| 62 | $this->generateClientToken($details); |
||
| 63 | } |
||
| 64 | |||
| 65 | $details->validateNotEmpty(['clientToken']); |
||
| 66 | |||
| 67 | $this->gateway->execute($template = new RenderTemplate($this->templateName, [ |
||
| 68 | 'formAction' => $clientHttpRequest->uri, |
||
| 69 | 'clientToken' => $details['clientToken'], |
||
| 70 | 'amount' => $details['amount'], |
||
| 71 | 'details' => $details, |
||
| 72 | 'obtainCardholderAuthentication' => $this->cardholderAuthenticationRequired, |
||
| 73 | ])); |
||
| 74 | |||
| 75 | throw new HttpResponse($template->getResult()); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function supports($request): bool |
||
| 79 | { |
||
| 80 | return |
||
| 81 | $request instanceof ObtainPaymentMethodNonce && |
||
| 82 | $request->getModel() instanceof \ArrayAccess |
||
| 83 | ; |
||
| 84 | } |
||
| 85 | |||
| 86 | protected function generateClientToken(ArrayObject $details): void |
||
| 87 | { |
||
| 88 | $request = new GenerateClientToken(); |
||
| 89 | |||
| 90 | $this->gateway->execute($request); |
||
| 91 | |||
| 92 | $details['clientToken'] = $request->getResponse(); |
||
| 93 | } |
||
| 94 | } |
||
| 95 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.