1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Omnipay\PlatiOnline\Message\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Omnipay\PlatiOnline\Utils\Urls; |
6
|
|
|
use Nip\Utility\Xml; |
7
|
|
|
use phpseclib\Crypt\AES; |
8
|
|
|
use phpseclib\Crypt\RSA; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait HasSoapRequestTrait |
12
|
|
|
* @package ByTIC\Omnipay\PlatiOnline\Message\Traits |
13
|
|
|
*/ |
14
|
|
|
trait HasSoapRequestTrait |
15
|
|
|
{ |
16
|
|
|
use \ByTIC\Omnipay\Common\Message\Traits\Soap\AbstractSoapRequestTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
|
|
public function getSoapEndpoint() |
22
|
|
|
{ |
23
|
|
|
return null; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
protected function getSoapAction(): string |
30
|
|
|
{ |
31
|
|
|
return 'auth-only'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function getSoapOptions(): array |
35
|
|
|
{ |
36
|
|
|
$options = $this->getSoapOptionsGeneric(); |
37
|
|
|
$options['location'] = Urls::$url; |
38
|
|
|
$options['uri'] = $this->getSoapAction(); |
39
|
|
|
return $options; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
abstract protected function getSoapRequestValidationUrl(); |
43
|
|
|
abstract protected function getSoapResponseValidationUrl(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \SoapClient $soapClient |
47
|
|
|
* @param $data |
48
|
|
|
* @return \SimpleXMLElement |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
protected function runSoapRequest($soapClient, $data, $type) |
52
|
|
|
{ |
53
|
|
|
$request = $this->setFRequest($data, $type, $this->getSoapRequestValidationUrl()); |
54
|
|
|
$response = $soapClient->__doRequest($request, Urls::$url, $this->getSoapAction(), 1); |
55
|
|
|
if (empty($response)) { |
56
|
|
|
throw new \Exception('ERROR: Nu am putut comunica cu serverul PO pentru operatiunea de autorizare!'); |
57
|
|
|
} |
58
|
|
|
Xml::validate($response, $this->getSoapResponseValidationUrl()); |
59
|
|
|
$responseObject = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA); |
60
|
|
|
return $responseObject; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* setez f_request, criptez f_request cu AES si cheia AES cu RSA |
65
|
|
|
* @param $message |
66
|
|
|
* @param $type |
67
|
|
|
* @param $validationUrl |
68
|
|
|
* @return mixed |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
protected function setFRequest($message, $type, $validationUrl) |
72
|
|
|
{ |
73
|
|
|
// aici construiesc XML din array |
74
|
|
|
$xml = new \SimpleXMLElement('<' . $type . '/>'); |
75
|
|
|
|
76
|
|
|
// test mode |
77
|
|
|
if (in_array($type, ['po_auth_request', 'po_payment_sale_by_token'])) { |
78
|
|
|
$message['f_test_request'] = ($this->getTestMode() == true) ? 1 : 0; |
|
|
|
|
79
|
|
|
$message['f_sequence'] = rand(1, 1000); |
80
|
|
|
$message['f_customer_ip'] = $this->getClientIp(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$message['f_timestamp'] = date('Y-m-d\TH:i:sP'); |
84
|
|
|
|
85
|
|
|
// set f_login |
86
|
|
|
$message['f_login'] = $this->getLoginId(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// sortez parametrii alfabetic |
89
|
|
|
ksort($message); |
90
|
|
|
|
91
|
|
|
$this->array2xml($message, $xml); |
92
|
|
|
$message = $xml->asXML(); |
93
|
|
|
|
94
|
|
|
// validez XML conform schemei (parametrul 2) |
95
|
|
|
Xml::validate($message, $validationUrl); |
96
|
|
|
|
97
|
|
|
$request = [ |
98
|
|
|
'po_request' => [ |
99
|
|
|
'f_login' => $this->getLoginId(), |
100
|
|
|
'f_message' => $this->AESEnc($message), |
101
|
|
|
'f_crypt_message' => $this->RSAEnc() |
102
|
|
|
] |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
$xml_auth_soap = Xml::fromArray($request)->asXML(); |
|
|
|
|
106
|
|
|
Xml::validate($xml_auth_soap, Urls::$requestXml); |
107
|
|
|
|
108
|
|
|
return $xml_auth_soap; |
109
|
|
|
} |
110
|
|
|
/** |
111
|
|
|
* function definition to convert array to xml |
112
|
|
|
* @param $arr |
113
|
|
|
* @param $xml_arr |
114
|
|
|
*/ |
115
|
|
|
protected function array2xml($arr, &$xml_arr) |
116
|
|
|
{ |
117
|
|
|
foreach ($arr as $key => $value) { |
118
|
|
|
if (is_array($value)) { |
119
|
|
|
if (!is_numeric($key)) { |
120
|
|
|
if (strpos($key, 'coupon') !== false) { |
121
|
|
|
$subnode = $xml_arr->addChild("coupon"); |
122
|
|
|
} else { |
123
|
|
|
$subnode = $xml_arr->addChild("$key"); |
124
|
|
|
} |
125
|
|
|
$this->array2xml($value, $subnode); |
126
|
|
|
} else { |
127
|
|
|
$subnode = $xml_arr->addChild("item"); |
128
|
|
|
$this->array2xml($value, $subnode); |
129
|
|
|
} |
130
|
|
|
} else { |
131
|
|
|
$xml_arr->addChild("$key", htmlspecialchars("$value")); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
// criptez f_request cu AES |
138
|
|
|
protected function AESEnc($message) |
139
|
|
|
{ |
140
|
|
|
$aes = new AES(); |
141
|
|
|
$aes->setIV($this->getInitialVector()); |
|
|
|
|
142
|
|
|
$aes->setKey($this->getAesKey()); |
|
|
|
|
143
|
|
|
return bin2hex(base64_encode($aes->encrypt($message))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// criptez cheia AES cu RSA |
147
|
|
|
protected function RSAEnc() |
148
|
|
|
{ |
149
|
|
|
$rsa = new RSA(); |
150
|
|
|
$rsa->loadKey($this->getPublicKey()); |
|
|
|
|
151
|
|
|
$rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); |
152
|
|
|
return base64_encode($rsa->encrypt($this->getAesKey())); |
153
|
|
|
} |
154
|
|
|
} |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.