Passed
Pull Request — master (#122)
by
unknown
07:52
created

Atipay::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
namespace Shetabit\Multipay\Drivers\Atipay;
3
4
use GuzzleHttp\Client;
5
use Shetabit\Multipay\Abstracts\Driver;
6
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
7
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
8
use Shetabit\Multipay\Contracts\ReceiptInterface;
9
use Shetabit\Multipay\Invoice;
10
use Shetabit\Multipay\Receipt;
11
use Shetabit\Multipay\RedirectionForm;
12
use Shetabit\Multipay\Request;
13
14
require_once 'Core/fn.atipay.php';
15
16
class Atipay extends Driver
17
{
18
    /**
19
     * Atipay Client.
20
     *
21
     * @var object
22
     */
23
    protected $client;
24
25
    /**
26
     * Invoice
27
     *
28
     * @var Invoice
29
     */
30
    protected $invoice;
31
32
    /**
33
     * Driver settings
34
     *
35
     * @var object
36
     */
37
    protected $settings;
38
39
    /**
40
     * Atipay constructor.
41
     * Construct the class with the relevant settings.
42
     *
43
     * @param Invoice $invoice
44
     * @param $settings
45
     */
46
    public function __construct(Invoice $invoice, $settings)
47
    {
48
        $this->invoice($invoice);
49
        $this->settings = (object) $settings;
50
        $this->client = new Client();
51
    }
52
53
    /**
54
     * Retrieve data from details using its name.
55
     *
56
     * @return string
57
     */
58
    private function extractDetails($name)
59
    {
60
        return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name];
61
    }
62
63
    /**
64
     * Purchase Invoice.
65
     *
66
     * @return string
67
     *
68
     * @throws PurchaseFailedException
69
     * @throws \GuzzleHttp\Exception\GuzzleException
70
     */
71
    public function purchase()
72
    {
73
        
74
75
        $amount = $this->invoice->getAmount();
76
        if ($this->settings->currency == 'T'){ //convert amount to rial, payment gateways need rial
77
            $amount = $amount * 10;
78
        }
79
        
80
        $order_id = $this->invoice->getUuid();
81
        $mobile = $this->extractDetails('mobile');
82
        $description = $this->extractDetails('description');
0 ignored issues
show
Unused Code introduced by
The assignment to $description is dead and can be removed.
Loading history...
83
        $apikey = $this->settings->apikey;
84
        $redirectUrl = $this->settings->callbackUrl;
85
        
86
        $token_params = array('apiKey'=>$apikey,
87
            'redirectUrl'=>$redirectUrl,
88
            'invoiceNumber'=>$order_id,
89
            'amount'=>$amount,
90
            'cellNumber'=>$mobile,
91
        );
92
93
        $r = fn_atipay_get_token($token_params);
94
        if ($r['success'] == 1) {
95
            $token = $r['token'];
96
            $this->invoice->transactionId($token);
97
            return $this->invoice->getTransactionId();
98
        }else{
99
            $error_message = $r['errorMessage'];
100
            throw new PurchaseFailedException($error_message);
101
        }
102
103
    }
104
105
    /**
106
     * Pay the Invoice
107
     *
108
     * @return RedirectionForm
109
     */
110
    public function pay(): RedirectionForm
111
    {
112
        $token = $this->invoice->getTransactionId();
113
        $payUrl = $this->settings->atipayRedirectGatewayUrl;
114
        return $this->redirectWithForm($payUrl, ['token'=>$token], 'POST');
115
    }
116
117
    /**
118
     * Verify payment
119
     *
120
     * @return ReceiptInterface
121
     *
122
     * @throws InvalidPaymentException
123
     * @throws \GuzzleHttp\Exception\GuzzleException
124
     */
125
    public function verify(): ReceiptInterface
126
    {
127
        
128
129
130
        $result = fn_check_callback_data($params);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $params does not exist. Did you maybe mean $verify_params?
Loading history...
131
        $payment_id = $params['reservationNumber'];
0 ignored issues
show
Unused Code introduced by
The assignment to $payment_id is dead and can be removed.
Loading history...
132
        if ($result['success'] == 1) { //will verify here
133
            $apiKey = $this->setting->apikey;
0 ignored issues
show
Bug introduced by
The property setting does not exist on Shetabit\Multipay\Drivers\Atipay\Atipay. Did you mean settings?
Loading history...
134
            $amount = $this->invoice->getAmount();
135
            if ($this->settings->currency == 'T'){ //convert amount to rial, payment gateways need rial
136
                $amount = $amount * 10;
137
            }
138
            $verify_params = array('apiKey' => $apiKey,
139
                'referenceNumber' => $params['referenceNumber']
140
            );
141
            
142
            $r = fn_atipay_verify_payment($verify_params, $amount);
143
            if ($r['success'] == 0) { //veriy failed
144
                $error_message = $r['errorMessage'];
145
                throw new InvalidPaymentException($error_message);
146
            } else { //success
147
                $receipt =  $this->createReceipt($data['referenceNumber']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
148
                $receipt->detail([
149
                    'referenceNo' => $params['referenceNumber'],
150
                    'rrn' => Request::input('rrn'),
151
                    'pan' => $params['maskedPan']
152
                ]);
153
154
                return $receipt;
155
            }
156
        } else {
157
            $error_message = $result['error'];
158
            throw new InvalidPaymentException($error_message);
159
        }
160
161
162
        return $this->createReceipt($body['transId']);
0 ignored issues
show
Unused Code introduced by
return $this->createReceipt($body['transId']) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
163
    }
164
165
    /**
166
     * Generate the payment's receipt
167
     *
168
     * @param $referenceId
169
     *
170
     * @return Receipt
171
     */
172
    protected function createReceipt($referenceId)
173
    {
174
        $receipt = new Receipt('Atipay', $referenceId);
175
176
        return $receipt;
177
    }
178
179
180
}
181
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
182