Passed
Push — master ( 1aa6bf...61950f )
by mahdi
02:59
created

Fanavacard   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 68
c 1
b 0
f 1
dl 0
loc 180
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createReceipt() 0 14 1
A pay() 0 8 1
A getWsContext() 0 3 1
A getToken() 0 27 3
A httpClientInit() 0 7 1
A purchase() 0 7 1
A verify() 0 30 4
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Fanavacard;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Multipay\Abstracts\Driver;
7
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
8
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
9
use Shetabit\Multipay\Contracts\ReceiptInterface;
10
use Shetabit\Multipay\Invoice;
11
use Shetabit\Multipay\Receipt;
12
use Shetabit\Multipay\RedirectionForm;
13
use Shetabit\Multipay\Request;
14
15
class Fanavacard extends Driver
16
{
17
    /**
18
     * client
19
     *
20
     * @var Client
21
     */
22
    protected $client;
23
24
    /**
25
     * Invoice
26
     *
27
     * @var Invoice
28
     */
29
    protected $invoice;
30
31
    /**
32
     * Driver settings
33
     *
34
     * @var object
35
     */
36
    protected $settings;
37
38
    /**
39
     * Etebarino constructor.
40
     * Construct the class with the relevant settings.
41
     *
42
     * @param Invoice $invoice
43
     * @param $settings
44
     */
45
    public function __construct(Invoice $invoice, $settings)
46
    {
47
        $this->invoice($invoice);
48
        $this->settings = (object)$settings;
49
        $this->httpClientInit();
50
    }
51
52
    /**
53
     * Purchase Invoice
54
     *
55
     * @return string
56
     *
57
     * @throws PurchaseFailedException
58
     */
59
    public function purchase()
60
    {
61
        $this->invoice->uuid(crc32($this->invoice->getUuid()));
62
        $token  = $this->getToken();
63
        $this->invoice->transactionId($token['Token']);
64
65
        return $this->invoice->getTransactionId();
66
    }
67
68
    /**
69
     * Pay the Invoice
70
     *
71
     * @return RedirectionForm
72
     */
73
    public function pay(): RedirectionForm
74
    {
75
        $url = rtrim($this->settings->baseUri, '/')."/{$this->settings->apiPaymentUrl}";
76
77
        return $this->redirectWithForm($url, [
78
            'token' => $this->invoice->getTransactionId(),
79
            'language' => 'fa'
80
        ], 'POST');
81
    }
82
83
    /**
84
     * Verify payment
85
     *
86
     * @return mixed|Receipta
0 ignored issues
show
Bug introduced by
The type Shetabit\Multipay\Drivers\Fanavacard\Receipta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
     *
88
     * @throws PurchaseFailedException
89
     * @throws InvalidPaymentException
90
     */
91
    public function verify(): ReceiptInterface
92
    {
93
        $transaction_amount = Request::input('transactionAmount');
94
95
        if ($this->invoice->getAmount()*10 == $transaction_amount) {
96
            $param = ['Token'=>Request::input('token'), 'RefNum'=>Request::input('RefNum')];
97
            $response = $this->client->post($this->settings->apiVerificationUrl, [
98
                'json'=> array_merge(
99
                    ['WSContext'=> $this->getWsContext()],
100
                    $param
101
                )]);
102
103
            $response_data = json_decode($response->getBody()->getContents());
104
            if ($response_data->Result != 'erSucceed') {
105
                throw new InvalidPaymentException($response_data->Result);
106
            } elseif ($this->invoice->getAmount()*10 != $response_data->Amount) {
107
                $this->client->post(
108
                    $this->settings->apiReverseAmountUrl,
109
                    [
110
                        'json'=> [
111
                            'WSContext'=> $this->getWsContext(),
112
                            $param
113
                        ]
114
                    ]
115
                );
116
                throw new InvalidPaymentException('مبلغ تراکنش برگشت داده شد');
117
            }
118
        }
119
120
        return $this->createReceipt(Request::input('ResNum'));
121
    }
122
123
124
    /**
125
     * Generate the payment's receipt
126
     *
127
     * @param $referenceId
128
     *
129
     * @return Receipt
130
     */
131
    protected function createReceipt($referenceId): Receipt
132
    {
133
        $receipt = new Receipt('fanavacard', $referenceId);
134
        $receipt->detail([
135
                             'ResNum'=>Request::input('ResNum'),
136
                             'RefNum'=>Request::input('RefNum'),
137
                             'token'=>Request::input('token'),
138
                             'CustomerRefNum'=>Request::input('CustomerRefNum'),
139
                             'CardMaskPan'=>Request::input('CardMaskPan'),
140
                             'transactionAmount'=>Request::input('transactionAmount'),
141
                             'emailAddress'=>Request::input('emailAddress'),
142
                             'mobileNo'=>Request::input('mobileNo'),
143
                         ]);
144
        return $receipt;
145
    }
146
147
    /**
148
     * call create token request
149
     *
150
     * @return array
151
     * @throws PurchaseFailedException
152
     */
153
    public function getToken(): array
154
    {
155
        $response = $this->client->request('POST', $this->settings->apiPurchaseUrl, [
156
            'json'=>[
157
                'WSContext'=> $this->getWsContext(),
158
                'TransType'=>'EN_GOODS',
159
                'ReserveNum'=>$this->invoice->getDetail('invoice_number') ?? crc32($this->invoice->getUuid()),
160
                'Amount'=> $this->invoice->getAmount() * 10,
161
                'RedirectUrl'=>$this->settings->callbackUrl,
162
            ]]);
163
164
        if ($response->getStatusCode() != 200) {
165
            throw new PurchaseFailedException(
166
                "cant get token |  {$response->getBody()->getContents()}",
167
                $response->getStatusCode()
168
            );
169
        }
170
171
        $response_data = json_decode($response->getBody()->getContents());
172
        if ($response_data->Result != 'erSucceed') {
173
            throw new PurchaseFailedException(
174
                "cant get token |  {$response->getBody()->getContents()}",
175
                $response->getStatusCode()
176
            );
177
        }
178
179
        return (array) $response_data;
180
    }
181
182
    private function httpClientInit(): void
183
    {
184
        $this->client = new Client([
185
                                       'curl'=>[\CURLOPT_SSL_CIPHER_LIST=>'DEFAULT@SECLEVEL=1',],
186
                                       'verify' => false,
187
                                       'base_uri' => $this->settings->baseUri,
188
                                       'headers' => ['Content-Type' => 'application/json',],
189
                                   ]);
190
    }
191
192
    private function getWsContext(): array
193
    {
194
        return ['UserId' => $this->settings->username, 'Password' => $this->settings->password];
195
    }
196
}
197