1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Giansalex |
5
|
|
|
* Date: 15/07/2017 |
6
|
|
|
* Time: 22:56 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Greenter\Ws\Services; |
10
|
|
|
use Greenter\Helper\SunatErrorHelper; |
11
|
|
|
use Greenter\Helper\ZipHelper; |
12
|
|
|
use Greenter\Model\Response\BillResult; |
13
|
|
|
use Greenter\Model\Response\CdrResponse; |
14
|
|
|
use Greenter\Model\Response\Error; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FeSunat |
18
|
|
|
* @package Greenter\Ws\Services |
19
|
|
|
*/ |
20
|
|
|
class FeSunat extends BaseSunat |
21
|
|
|
{ |
22
|
|
|
const BETA = 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService'; |
23
|
|
|
const HOMOLOGACION = 'https://www.sunat.gob.pe/ol-ti-itcpgem-sqa/billService'; |
24
|
|
|
const PRODUCCION = 'https://e-factura.sunat.gob.pe/ol-ti-itcpfegem/billService'; |
25
|
|
|
const WSDL_ENDPOINT = 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl'; |
26
|
|
|
|
27
|
6 |
|
public function __construct($user, $password) |
28
|
|
|
{ |
29
|
6 |
|
parent::__construct($user, $password); |
30
|
6 |
|
$this->setUrlWsdl(FeSunat::WSDL_ENDPOINT); |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $filename |
35
|
|
|
* @param $content |
36
|
|
|
* @return BillResult |
37
|
|
|
*/ |
38
|
2 |
|
public function send($filename, $content) |
39
|
|
|
{ |
40
|
2 |
|
$client = $this->getClient(); |
41
|
2 |
|
$result = new BillResult(); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$params = [ |
45
|
2 |
|
'fileName' => $filename, |
46
|
2 |
|
'contentFile' => $content, |
47
|
2 |
|
]; |
48
|
2 |
|
$response = $client->__soapCall('sendBill', [ 'parameters' => $params ]); |
49
|
|
|
|
50
|
2 |
|
$cdrZip = $response->applicationResponse; |
51
|
|
|
$result |
52
|
2 |
|
->setCdrResponse($this->extractResponse($cdrZip)) |
53
|
2 |
|
->setCdrZip($cdrZip) |
54
|
2 |
|
->setSuccess(true); |
55
|
|
|
} |
56
|
2 |
|
catch (\SoapFault $e) { |
57
|
|
|
$result->setError($this->getErrorFromFault($e)); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function sendSummary($filename, $content) |
64
|
|
|
{ |
65
|
2 |
|
$client = $this->getClient(); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$params = [ |
69
|
2 |
|
'fileName' => $filename, |
70
|
2 |
|
'contentFile' => $content, |
71
|
2 |
|
]; |
72
|
2 |
|
$response = $client->__soapCall('sendSummary', [ 'parameters' => $params ]); |
73
|
2 |
|
return $response->ticket; |
74
|
|
|
} |
75
|
|
|
catch (\Exception $e) { |
76
|
|
|
// $client->__getLastResponse() |
|
|
|
|
77
|
|
|
return $e->getMessage(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
public function getStatus($ticket) |
82
|
|
|
{ |
83
|
2 |
|
$client = $this->getClient(); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$params = [ |
87
|
2 |
|
'ticket' => $ticket, |
88
|
2 |
|
]; |
89
|
2 |
|
$response = $client->__soapCall('getStatus', [ 'parameters' => $params ]); |
90
|
|
|
return $response->statusResponse; |
91
|
|
|
} |
92
|
2 |
|
catch (\SoapFault $fault) { |
93
|
|
|
// $client->__getLastResponse() |
|
|
|
|
94
|
|
|
// $fault->faultstring; |
95
|
2 |
|
return $fault->faultcode; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get error from Fault Exception. |
101
|
|
|
* |
102
|
|
|
* @param \SoapFault $fault |
103
|
|
|
* @return Error |
104
|
|
|
*/ |
105
|
|
|
private function getErrorFromFault(\SoapFault $fault) |
106
|
|
|
{ |
107
|
|
|
$err = new Error(); |
108
|
|
|
$code = $fault->faultcode; |
|
|
|
|
109
|
|
|
if ($code) { |
110
|
|
|
$err->setCode($code); |
111
|
|
|
$err->setMessage($fault->faultstring); |
|
|
|
|
112
|
|
|
return $err; |
113
|
|
|
} |
114
|
|
|
$code = preg_replace('/[^0-9]+/', '', $code); |
115
|
|
|
$search = new SunatErrorHelper(); |
116
|
|
|
$msg = $search->getMessageByCode($code); |
117
|
|
|
$err->setCode($code); |
118
|
|
|
$err->setMessage($msg); |
119
|
|
|
return $err; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
2 |
|
private function extractResponse($zipContent) |
124
|
|
|
{ |
125
|
2 |
|
$zip = new ZipHelper(); |
126
|
2 |
|
$xml = $zip->decompressLastFile($zipContent); |
127
|
2 |
|
$doc = new \DOMDocument(); |
128
|
2 |
|
$doc->loadXML($xml); |
129
|
2 |
|
$xp = new \DOMXPath($doc); |
130
|
2 |
|
$xp->registerNamespace('cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'); |
131
|
2 |
|
$xp->registerNamespace('xr', 'urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2'); |
132
|
2 |
|
$resp = $xp->query('/xr:ApplicationResponse/cac:DocumentResponse/cac:Response'); |
133
|
|
|
|
134
|
2 |
|
$obj = $resp[0]->childNodes; |
135
|
2 |
|
$cdr = new CdrResponse(); |
136
|
2 |
|
$cdr->setId($obj[0]->nodeValue) |
137
|
2 |
|
->setCode($obj[1]->nodeValue) |
138
|
2 |
|
->setDescription($obj[2]->nodeValue); |
139
|
|
|
|
140
|
2 |
|
return $cdr; |
141
|
|
|
} |
142
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.