TransactionService::get()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 14
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 24
rs 8.8333
1
<?php
2
3
namespace Squareetlabs\AuthorizeNet\Services;
4
5
use Squareetlabs\AuthorizeNet\AuthorizeNet;
6
use BadMethodCallException;
7
use Exception;
8
use Illuminate\Support\Collection;
9
use net\authorize\api\contract\v1 as AnetAPI;
10
use net\authorize\api\contract\v1\CreateTransactionResponse;
11
use net\authorize\api\controller as AnetController;
12
13
class TransactionService extends AuthorizeNet
14
{
15
    public CreateTransactionResponse $transaction;
16
17
    public ?string $id = null;
18
19
    public ?string $refId = null;
20
21
    public ?int $responseCode = null;
22
23
    /**
24
     * Transaction constructor.
25
     *
26
     * @param \Illuminate\Contracts\Auth\Authenticatable|\Illuminate\Database\Eloquent\Model $user
27
     * @param CreateTransactionResponse $transaction
28
     */
29
    public function __construct($user, CreateTransactionResponse $transaction)
30
    {
31
        parent::__construct($user);
32
        $this->transaction = $transaction;
33
        $this->setupTransaction();
34
    }
35
36
    /**
37
     * Setup transaction properties from the response.
38
     *
39
     * @return void
40
     */
41
    private function setupTransaction(): void
42
    {
43
        $transactionResponse = $this->transaction->getTransactionResponse();
44
        
45
        if ($transactionResponse !== null) {
46
            $this->id = $transactionResponse->getTransId();
47
            $this->refId = $transactionResponse->getRefTransID();
48
            $this->responseCode = $transactionResponse->getResponseCode();
49
        }
50
    }
51
52
    /**
53
     * Check if the transaction is approved.
54
     *
55
     * @return bool
56
     */
57
    public function isApproved(): bool
58
    {
59
        $transactionResponse = $this->transaction->getTransactionResponse();
60
        
61
        if ($transactionResponse === null || $transactionResponse->getMessages() === null) {
62
            return false;
63
        }
64
65
        $messages = $transactionResponse->getMessages();
66
        if (empty($messages)) {
67
            return false;
68
        }
69
70
        $responseText = $messages[0]->getDescription() ?? '';
71
72
        return $responseText === 'This transaction has been approved.';
73
    }
74
75
    /**
76
     * Check if transaction request was successfully reached and handled.
77
     * This does not indicate if the charge was successful or failed.
78
     *
79
     * @return bool
80
     */
81
    public function isRequestSuccessful(): bool
82
    {
83
        $messages = $this->transaction->getMessages();
84
        
85
        if ($messages === null) {
86
            return false;
87
        }
88
89
        return strtolower($messages->getResultCode() ?? '') === 'ok';
90
    }
91
92
    /**
93
     * Get the transaction ID.
94
     *
95
     * @return string|null
96
     */
97
    public function getId(): ?string
98
    {
99
        $transactionResponse = $this->transaction->getTransactionResponse();
100
101
        return $transactionResponse?->getTransId();
102
    }
103
104
    /**
105
     * Get all transactions for a given batch ID from authorize.net API.
106
     *
107
     * @param int $batchId
108
     * @return Collection
109
     * @throws Exception
110
     */
111
    public function get(int $batchId): Collection
112
    {
113
        $request = new AnetAPI\GetTransactionListRequest();
114
        $request->setMerchantAuthentication($this->getMerchantAuthentication());
115
        $request->setBatchId($batchId);
116
117
        $controller = new AnetController\GetTransactionListController($request);
118
        $response = $this->execute($controller);
119
120
        if ($response !== null && $response->getMessages()->getResultCode() === 'Ok') {
121
            if ($response->getTransactions() === null) {
122
                return collect();
123
            }
124
125
            return collect($response->getTransactions());
126
        }
127
128
        $messages = $response->getMessages();
129
        if ($messages !== null && $messages->getMessage() !== null && count($messages->getMessage()) > 0) {
130
            $msg = $messages->getMessage()[0];
131
            throw new Exception('Code: ' . $msg->getCode() . '. Message: ' . $msg->getText());
132
        }
133
134
        throw new Exception('Failed to retrieve transactions.');
135
    }
136
137
    /**
138
     * Magic method to call methods on the transaction response.
139
     *
140
     * @param string $method
141
     * @param array $arg
142
     * @return mixed
143
     * @throws BadMethodCallException
144
     */
145
    public function __call(string $method, array $arg): mixed
146
    {
147
        $response = $this->transaction->getTransactionResponse();
148
        
149
        if ($response !== null && method_exists($response, $method)) {
150
            return $response->$method(...$arg);
151
        }
152
153
        throw new BadMethodCallException('Method: ' . $method . ' does not exist on TransactionResponse');
154
    }
155
156
}
157