Passed
Push — master ( ccd39a...5e94a6 )
by Francis
02:11
created

RefundAction::getTokenHashMetadataKeyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FluxSE\PayumStripe\Action;
6
7
use ArrayObject;
8
use FluxSE\PayumStripe\Request\Api\Resource\CreateRefund;
9
use Payum\Core\Exception\RequestNotSupportedException;
10
use Payum\Core\Request\Refund;
11
use Stripe\PaymentIntent;
12
use Stripe\Refund as StripeRefund;
13
14
final class RefundAction extends AbstractPaymentIntentAwareAction
15
{
16
    /**
17
     * @param Refund $request
18
     */
19
    public function execute($request): void
20
    {
21
        RequestNotSupportedException::assertSupports($this, $request);
22
23
        /** @var ArrayObject $model */
24
        $model = $request->getModel();
25
26
        $object = $model['object'] ?? null;
27
        if (PaymentIntent::OBJECT_NAME !== $object) {
28
            return;
29
        }
30
31
        $id = $model['id'] ?? null;
32
        if (empty($id)) {
33
            return;
34
        }
35
36
        $refundModel = new ArrayObject([
37
            'payment_intent' => $id,
38
        ]);
39
        $this->embedNotifyTokenHash($refundModel, $request);
40
41
        $refundRequest = new CreateRefund($refundModel->getArrayCopy());
42
        $this->gateway->execute($refundRequest);
43
44
        /** @var StripeRefund $refund */
45
        $refund = $refundRequest->getApiResource();
46
        $model->exchangeArray($refund->toArray());
47
    }
48
49
    /**
50
     * The token hash will be stored to a different
51
     * metadata key to avoid consuming the default one.
52
     */
53
    public function getTokenHashMetadataKeyName(): string
54
    {
55
        return TokenHashKeysInterface::REFUND_TOKEN_HASH_KEY_NAME;
56
    }
57
58
    public function supports($request): bool
59
    {
60
        if (false === $request instanceof Refund) {
61
            return false;
62
        }
63
64
        return $request->getModel() instanceof ArrayObject;
65
    }
66
}
67