Passed
Pull Request — master (#127)
by
unknown
02:37
created

Fanavacard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
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
        $this->invoice->uuid(crc32($this->invoice->getUuid()));
61
        $token  = $this->getToken();
62
        $this->invoice->transactionId($token['Token']);
63
64
        return $this->invoice->getTransactionId();
65
    }
66
67
    /**
68
     * Pay the Invoice
69
     *
70
     * @return RedirectionForm
71
     */
72
    public function pay(): RedirectionForm
73
    {
74
        $url = rtrim($this->settings->baseUri,'/')."/{$this->settings->apiPaymentUrl}";
75
        return $this->redirectWithForm($url, [
76
            'token' => $this->invoice->getTransactionId(),
77
            'language' => 'fa'
78
        ], 'POST');
79
    }
80
81
    /**
82
     * Verify payment
83
     *
84
     * @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...
85
     *
86
     * @throws PurchaseFailedException
87
     * @throws InvalidPaymentException
88
     */
89
    public function verify(): ReceiptInterface
90
    {
91
        $transaction_amount = Request::input('transactionAmount');
92
93
        if($this->invoice->getAmount()*10 == $transaction_amount){
94
95
            $param = ['Token'=>Request::input('token'), 'RefNum'=>Request::input('RefNum')];
96
            $response = $this->client->post($this->settings->apiVerificationUrl,[
97
                'json'=> array_merge(
98
                    ['WSContext'=> $this->getWsContext()],
99
                    $param
100
                )]);
101
102
            $response_data = json_decode($response->getBody()->getContents());
103
            if($response_data->Result != 'erSucceed'){
104
                return throw new InvalidPaymentException($response_data->Result);
105
            }elseif($this->invoice->getAmount()*10 != $response_data->Amount){
106
                $response = $this->client->post($this->settings->apiReverseAmountUrl,
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
107
                                                [
108
                                                    'json'=> [
109
                                                        'WSContext'=> $this->getWsContext(),
110
                                                        $param
111
                                                    ]
112
                                                ]
113
                );
114
                return throw new InvalidPaymentException('مبلغ تراکنش برگشت داده شد');
115
116
            }
117
        }
118
119
        return $this->createReceipt(Request::input('ResNum'));
120
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
     */
152
    public function getToken(): array
153
    {
154
155
        $response = $this->client->request('POST',$this->settings->apiPurchaseUrl, [
156
            'json'=>[
157
                'WSContext'=> $this->getWsContext(),
158
                'TransType'=>'EN_GOODS',
159
                'ReserveNum'=>crc32($this->invoice->getUuid()),
160
                'Amount'=> $this->invoice->getAmount() * 10,
161
                'RedirectUrl'=>$this->settings->callbackUrl,
162
            ]]);
163
164
        if($response->getStatusCode() != 200)
165
            return throw new PurchaseFailedException("cant get token |  $response->getBody()->getContents()", $response->getStatusCode());
0 ignored issues
show
Bug introduced by
Accessing getBody on the interface Psr\Http\Message\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
166
167
        $response_data = json_decode($response->getBody()->getContents());
168
        if($response_data->Result != 'erSucceed')
169
            return throw new PurchaseFailedException("cant get token |  $response->getBody()->getContents()", $response->getStatusCode());
170
171
        return (array) $response_data;
172
    }
173
174
    private function httpClientInit(): void {
175
        $this->client = new Client([
176
                                       'curl'=>[\CURLOPT_SSL_CIPHER_LIST=>'DEFAULT@SECLEVEL=1',],
177
                                       'verify' => false,
178
                                       'base_uri' => $this->settings->baseUri,
179
                                       'headers' => ['Content-Type' => 'application/json',],
180
                                   ]);
181
    }
182
183
    private function getWsContext(): array {
184
        return ['UserId' => $this->settings->username, 'Password' => $this->settings->password];
185
    }
186
}
187