Passed
Pull Request — master (#129)
by
unknown
02:31
created

Fanavacard::pay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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