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; |
|
|
|
|
9
|
|
|
$this->response = $response; |
|
|
|
|
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']); |
|
|
|
|
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; |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|