Passed
Pull Request — master (#122)
by
unknown
02:41
created

Atipay::verify()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 24
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 35
rs 9.536
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
        $amount = $this->invoice->getAmount();
74
        if ($this->settings->currency == 'T') { //convert amount to rial, payment gateways need rial
75
            $amount = $amount * 10;
76
        }
77
        
78
        $order_id = $this->invoice->getUuid();
79
        $mobile = $this->extractDetails('mobile');
80
        $description = $this->extractDetails('description');
0 ignored issues
show
Unused Code introduced by
The assignment to $description is dead and can be removed.
Loading history...
81
        $apikey = $this->settings->apikey;
82
        $redirectUrl = $this->settings->callbackUrl;
83
        
84
        $token_params = array('apiKey'=>$apikey,
85
            'redirectUrl'=>$redirectUrl,
86
            'invoiceNumber'=>$order_id,
87
            'amount'=>$amount,
88
            'cellNumber'=>$mobile,
89
        );
90
91
        $r = fn_atipay_get_token($token_params);
92
        if ($r['success'] == 1) {
93
            $token = $r['token'];
94
            $this->invoice->transactionId($token);
95
            return $this->invoice->getTransactionId();
96
        } else {
97
            $error_message = $r['errorMessage'];
98
            throw new PurchaseFailedException($error_message);
99
        }
100
    }
101
102
    /**
103
     * Pay the Invoice
104
     *
105
     * @return RedirectionForm
106
     */
107
    public function pay(): RedirectionForm
108
    {
109
        $token = $this->invoice->getTransactionId();
110
        $payUrl = $this->settings->atipayRedirectGatewayUrl;
111
        return $this->redirectWithForm($payUrl, ['token'=>$token], 'POST');
112
    }
113
114
    /**
115
     * Verify payment
116
     *
117
     * @return ReceiptInterface
118
     *
119
     * @throws InvalidPaymentException
120
     * @throws \GuzzleHttp\Exception\GuzzleException
121
     */
122
    public function verify(): ReceiptInterface
123
    {
124
        $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...
125
        $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...
126
        if ($result['success'] == 1) { //will verify here
127
            $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...
128
            $amount = $this->invoice->getAmount();
129
            if ($this->settings->currency == 'T') { //convert amount to rial, payment gateways need rial
130
                $amount = $amount * 10;
131
            }
132
            $verify_params = array('apiKey' => $apiKey,
133
                'referenceNumber' => $params['referenceNumber']
134
            );
135
            
136
            $r = fn_atipay_verify_payment($verify_params, $amount);
137
            if ($r['success'] == 0) { //veriy failed
138
                $error_message = $r['errorMessage'];
139
                throw new InvalidPaymentException($error_message);
140
            } else { //success
141
                $receipt =  $this->createReceipt($data['referenceNumber']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
142
                $receipt->detail([
143
                    'referenceNo' => $params['referenceNumber'],
144
                    'rrn' => Request::input('rrn'),
145
                    'pan' => $params['maskedPan']
146
                ]);
147
148
                return $receipt;
149
            }
150
        } else {
151
            $error_message = $result['error'];
152
            throw new InvalidPaymentException($error_message);
153
        }
154
155
156
        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...
157
    }
158
159
    /**
160
     * Generate the payment's receipt
161
     *
162
     * @param $referenceId
163
     *
164
     * @return Receipt
165
     */
166
    protected function createReceipt($referenceId)
167
    {
168
        $receipt = new Receipt('Atipay', $referenceId);
169
170
        return $receipt;
171
    }
172
}
173