-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsgw.class.php
More file actions
141 lines (112 loc) · 5.41 KB
/
sgw.class.php
File metadata and controls
141 lines (112 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/*
* SGW processor for Swedbank SGW service able to
* # send task to SGW
* # fetch task from SGW
* # purge task from SGW
*
* Author: Deniss Gaplevsky slim@inbox.lv
*
*/
class SGW {
private $gw_ok_header = 'X-Gateway-Message';
private $gw_header_CorrelationID = 'CorrelationID';
private $gw_header_RequestId = 'RequestId';
private $gw_header_TrackingID = 'TrackingID';
private $key_pass = '';
public $gw = 'https://dev.hansagateway.net';
public $keyfile = null;
public $certfile = null;
public $transport_cert = null;
public $status = null;
public $errors = array();
public $debug = array();
function __construct($keyfile = null, $certfile = null) {
if ($keyfile) $this->keyfile = $keyfile;
if ($certfile) $this->certfile = $certfile;
}
function setKeyCert($keyfile,$certfile) {
$this->keyfile = $keyfile;
$this->certfile = $certfile;
}
function setKeyPass($key_pass = null) {
if ($key_pass) {
if (!$this->keyfile) throw new Exception('keyfile is not set');
$res = openssl_pkey_get_private('file://'.$this->keyfile, $key_pass);
if (!$res) throw new Exception('passphrase is not valid for ',$this->keyfile);
$this->key_pass = $key_pass;
openssl_pkey_free($res);
} else {
throw new Exception('passphrase is empty');
}
}
function send($task) {
$this->errors = array();
if (empty($task->inputCDOC)) throw new Exception('CDOC is not set');
list($code, $headers, $body) = $this->callCurl('PUT',$task->inputCDOC, [ $this->gw_header_CorrelationID.': '.$task->CorrelationID]);
if ( $code == '500' ) throw new Exception('error 500: '.$headers.$body);
if ($headers[$this->gw_ok_header] != '1') throw new Exception('No '.$this->gw_ok_header.' in output set: '.print_r($headers,1));
if (empty($headers[$this->gw_header_RequestId])) throw new Exception('No '.$this->gw_header_RequestId.' in output set: '.print_r($headers,1));
$task->RequestId = $headers[$this->gw_header_RequestId];
return true;
}
function receive($task) {
$this->errors = array();
list($code, $headers,$body) = $this->callCurl('GET');
if ( $code == '500' || $code == '400' ) throw new Exception('error '.$code.': '.$headers.$body);
if ( $code == '404' ) {
$this->errors[] = 'code '.$code.' No messages: '.print_r($headers,1).$body;
return null;
}
$task->CorrelationID = $headers[$this->gw_header_CorrelationID];
$task->outputCDOC = $body;
$task->TrackingID = $headers[$this->gw_header_TrackingID];
return true;
}
function purge($task) {
$this->errors = array();
if ( empty($task->TrackingID) ) return false;
$this->callCurl('DELETE', '', [ $this->gw_header_TrackingID.': '.$task->TrackingID ]);
return true;
}
function getError() { return implode("\r\n",$this->errors); }
private function callCurl($method, $content = null, $headers = array()) {
$this->debug[] = "callCurl($method, $content)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->gw);
curl_setopt($ch, CURLOPT_HEADER, 1); # to include the header in the output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); # to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
curl_setopt($ch, CURLOPT_CERTINFO, 1); # to output SSL certification information to STDERR on secure transfers
curl_setopt($ch, CURLOPT_SSLCERT, $this->certfile); # The name of a file containing a PEM formatted certificate.
curl_setopt($ch, CURLOPT_SSLKEY, $this->keyfile); # The name of a file containing a private SSL key.
if ($this->key_pass) curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->key_pass);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); # to track the handle's request string
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->transport_cert ? 1 : 0 ); #FALSE to stop cURL from verifying the peer's certificate.
if ($this->transport_cert) {
curl_setopt($ch, CURLOPT_CAINFO, $this->transport_cert ); # The name of a file holding one or more certificates to verify the peer with
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ( $method != 'GET') {
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array('Content-Length: ' . strlen($content),'Expect:'), $headers) );
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
$response = curl_exec($ch);
if ($response === false ) throw new Exception('curl error: '.curl_error($ch));
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->debug[] = 'CURL headers send: '. curl_getinfo($ch,CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$_headers = explode("\n",$header);
$headers = array();
foreach ($_headers as $header) {
if ( strpos($header,':') === false ) continue;
list($key,$val) = explode(':',$header);
$headers[trim($key)] = trim($val);
}
$this->debug[] = "curl_exec: ".$response;
$this->debug[] = "headers: ".print_r($headers,1);
return [$code, $headers,$body];
}
}