Response::getTransactionReference()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Omnipay\Myfatoorah\Message;
4
5
class Response implements \Omnipay\Common\Message\ResponseInterface {
6
7
    public function __construct($request, $response) {
8
        $this->request  = $request;
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
9
        $this->response = $response;
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
10
    }
11
12
    public function getRequest() {
13
        $this->request;
14
    }
15
16
    public function isSuccessful() {
17
        $json = json_decode($this->response->getBody()->getContents(), true);
18
        return $json; 
19
        return ($this->response->getStatusCode() >= 200 && $this->response->getStatusCode() <= 299 && isset($json['IsSuccess']) && $json['IsSuccess']);
0 ignored issues
show
Unused Code introduced by
return $this->response->...e && $json['IsSuccess'] 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...
20
    }
21
22
    public function isRedirect() {
23
        $this->response->getBody()->rewind();
24
        $json = json_decode($this->response->getBody()->getContents(), true);
25
        if (!empty($json['Data']['InvoiceURL'])) {
26
            return true;
27
        }
28
        return false;
29
    }
30
31
    /**
32
     * Gets the redirect target url.
33
     *
34
     * @return string
35
     */
36
    public function getRedirectUrl() {
37
        $this->response->getBody()->rewind();
38
        $json = json_decode($this->response->getBody()->getContents(), true);
39
        if (!empty($json['Data']['InvoiceURL'])) {
40
            return $json['Data']['InvoiceURL'];
41
        } else {
42
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
43
        }
44
    }
45
46
    public function isCancelled() {
47
        return false;
48
    }
49
50
    public function getMessage() {
51
        $this->response->getBody()->rewind();
52
        $res  = ($this->response->getBody()->getContents());
53
        $json = json_decode($res);
54
55
        if (isset($json->IsSuccess) && $json->IsSuccess == true) {
56
            return $res;
57
        }
58
        if (isset($json->ValidationErrors) || isset($json->FieldsErrors)) {
59
            $errorsObj = isset($json->ValidationErrors) ? $json->ValidationErrors : $json->FieldsErrors;
60
            $blogDatas = array_column($errorsObj, 'Error', 'Name');
61
            $err       = implode(', ', array_map(function ($k, $v) {
62
                        return "$k: $v";
63
                    }, array_keys($blogDatas), array_values($blogDatas)));
64
        } else if (isset($json->Data->ErrorMessage)) {
65
            $err = $json->Data->ErrorMessage;
66
        }
67
68
        if (empty($err)) {
69
            $err = (isset($json->Message)) ? $json->Message : (!empty($res) ? $res : 'Kindly, review your Myfatoorah API configuration due to a wrong entry.');
70
        }
71
72
        return $err;
73
    }
74
75
    public function getCode() {
76
        return $this->response->getStatusCode();
77
    }
78
79
    public function getTransactionReference() {
80
        $this->response->getBody()->rewind();
81
        $json = json_decode($this->response->getBody()->getContents(), true);
82
        return $json['Data']['InvoiceId'];
83
    }
84
85
    public function getData() {
86
        return $this->request->getData();
87
    }
88
89
    public function getPaymentStatus($orderId, $paymentId, $KeyType = 'PaymentId') {
90
        $res  = $this->getMessage();
91
        $json = json_decode($res);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        $json = json_decode(/** @scrutinizer ignore-type */ $res);
Loading history...
92
        if ($orderId && $json->Data->CustomerReference != $orderId) {
93
            return 'Trying to call data of another order';
94
        } else if ($json->Data->InvoiceStatus == 'DuplicatePayment') {
95
            return 'Duplicate Payment'; //success with Duplicate
96
        }
97
98
        if ($KeyType == 'PaymentId') {
99
            foreach ($json->Data->InvoiceTransactions as $transaction) {
100
                if ($transaction->PaymentId == $paymentId && $transaction->Error && $json->Data->InvoiceStatus != 'Paid') {
101
                    return 'Failed with Error (' . $transaction->Error . ')'; //faild order
102
                }
103
            }
104
        }
105
        if ($json->Data->InvoiceStatus != 'Paid') {
106
            //------------------
107
            //case 1:
108
            $lastInvoiceTransactions = end($json->Data->InvoiceTransactions);
109
            if ($lastInvoiceTransactions && $lastInvoiceTransactions->Error) {
110
                return 'Failed with Error (' . $lastInvoiceTransactions->Error . ')'; //faild order
111
            }
112
113
            //------------------
114
            //case 2:
115
            //all myfatoorah gateway is set to Asia/Kuwait
116
            $ExpiryDate  = new \DateTime($json->Data->ExpiryDate, new \DateTimeZone('Asia/Kuwait'));
117
            $ExpiryDate->modify('+1 day'); ///????????????$ExpiryDate without any hour so for i added the 1 day just in case. this should be changed after adding the tome to the expire date
118
            $currentDate = new \DateTime('now', new \DateTimeZone('Asia/Kuwait'));
119
120
            if ($ExpiryDate < $currentDate) {
121
                return'Invoice is expired since: ' . $ExpiryDate->format('Y-m-d'); //cancelled order
122
            }
123
124
            //------------------
125
            //case 3:
126
            //payment is pending .. user has not paid yet and the invoice is not expired
127
            return 'Payment is pending';
128
        }
129
130
        return print_r($json->Data, 1);
131
    }
132
133
    public function getRefundInfo() {
134
        return $this->getMessage();
135
    }
136
137
}
138