1 | <?php |
||
2 | |||
3 | /** |
||
4 | * MÓDULO DE EMISIÓN ELECTRÓNICA F72X |
||
5 | * UBL 2.1 |
||
6 | * Version 1.0 |
||
7 | * |
||
8 | * Copyright 2019, Jaime Cruz |
||
9 | */ |
||
10 | |||
11 | namespace F72X\Sunat; |
||
12 | |||
13 | use Exception; |
||
14 | use F72X\Repository; |
||
15 | use F72X\Exception\SunatException; |
||
16 | |||
17 | class ServiceGateway |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | * @param string $documentName El nombre del documento electrónico. |
||
23 | * @return array |
||
24 | */ |
||
25 | public static function sendBill($documentName) |
||
26 | { |
||
27 | $contentFile = Repository::getZipContent($documentName); |
||
28 | try { |
||
29 | $soapService = SunatSoapClient::getService(); |
||
30 | $soapService->__soapCall('sendBill', [['fileName' => "$documentName.zip", 'contentFile' => $contentFile]]); |
||
31 | $serverResponse = $soapService->__getLastResponse(); |
||
32 | } catch (Exception $exc) { |
||
33 | throw new SunatException($exc->getMessage(), $exc->getCode()); |
||
34 | } |
||
35 | |||
36 | // Save Constancia de recepción |
||
37 | self::saveCdr($serverResponse, $documentName); |
||
38 | // Get Response info |
||
39 | return Repository::getCdrInfo($documentName); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @param string $documentName El nombre del documento electrónico. |
||
45 | * @return array |
||
46 | */ |
||
47 | public static function sendSummary($documentName) |
||
48 | { |
||
49 | $contentFile = Repository::getZipContent($documentName); |
||
50 | try { |
||
51 | $soapService = SunatSoapClient::getService(); |
||
52 | $soapService->__soapCall('sendSummary', [['fileName' => "$documentName.zip", 'contentFile' => $contentFile]]); |
||
53 | $serverResponse = $soapService->__getLastResponse(); |
||
54 | } catch (Exception $exc) { |
||
55 | throw new SunatException($exc->getMessage(), $exc->getCode()); |
||
56 | } |
||
57 | |||
58 | // Save and return ticket |
||
59 | $ticket = self::saveTicket($serverResponse, $documentName); |
||
60 | return $ticket; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
61 | } |
||
62 | |||
63 | public static function getStatus($documentName) |
||
64 | { |
||
65 | $ticket = Repository::getTicketInfo($documentName); |
||
66 | try { |
||
67 | $soapService = SunatSoapClient::getService(); |
||
68 | $soapService->__soapCall('getStatus', [['ticket' => $ticket]]); |
||
69 | $serverResponse = $soapService->__getLastResponse(); |
||
70 | } catch (Exception $exc) { |
||
71 | throw new SunatException($exc->getMessage(), $exc->getCode()); |
||
72 | } |
||
73 | // Save Constancia de recepción |
||
74 | return self::saveStatusResponse($serverResponse, $documentName); |
||
75 | } |
||
76 | |||
77 | private static function saveCdr($response, $documentName) |
||
78 | { |
||
79 | $xml = simplexml_load_string($response); |
||
80 | $appResp = $xml->xpath("//applicationResponse")[0]; |
||
81 | // CDR |
||
82 | $cdr = base64_decode($appResp); |
||
83 | Repository::saveCdr($documentName, $cdr); |
||
84 | } |
||
85 | |||
86 | private static function saveStatusResponse($response, $documentName) |
||
87 | { |
||
88 | $xml = simplexml_load_string($response); |
||
89 | $status = (array)$xml->xpath("//status")[0]; |
||
90 | |||
91 | // Status code only 0 and 99 |
||
92 | if ($status['statusCode'] == '0' || $status['statusCode'] == '99') { |
||
93 | $statusContent = $status['content']; |
||
94 | $cdr = base64_decode($statusContent); |
||
95 | Repository::saveCdr($documentName, $cdr); |
||
96 | $status['cdr'] = Repository::getCdrInfo($documentName); |
||
97 | $status['message'] = null; |
||
98 | } else { |
||
99 | $status['cdr'] = null; |
||
100 | $status['message'] = $status['content']; |
||
101 | } |
||
102 | unset($status['content']); |
||
103 | return $status; |
||
104 | } |
||
105 | |||
106 | private static function saveTicket($response, $documentName) |
||
107 | { |
||
108 | $xmlObj = simplexml_load_string($response); |
||
109 | // Ticket |
||
110 | $ticket = (string) $xmlObj->xpath("//ticket")[0]; |
||
111 | Repository::saveTicket($documentName, $ticket); |
||
112 | return $ticket; |
||
113 | } |
||
114 | } |
||
115 |