Issues (32)

src/Sunat/ServiceGateway.php (1 issue)

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
     * @param string $documentName El nombre del documento electrónico.
22
     * @return array
23
     */
24
    public static function sendBill($documentName) {
25
        $contentFile = Repository::getZipContent($documentName);
26
        try {
27
            $soapService = SunatSoapClient::getService();
28
            $soapService->__soapCall('sendBill', [['fileName' => "$documentName.zip", 'contentFile' => $contentFile]]);
29
            $serverResponse = $soapService->__getLastResponse();
30
        } catch (Exception $exc) {
31
            throw new SunatException($exc->getMessage(), $exc->getCode());
32
        }
33
34
        // Save Constancia de recepción
35
        self::saveCdr($serverResponse, $documentName);
36
        // Get Response info
37
        return Repository::getCdrInfo($documentName);
38
    }
39
40
    /**
41
     * 
42
     * @param string $documentName El nombre del documento electrónico.
43
     * @return array
44
     */
45
    public static function sendSummary($documentName) {
46
        $contentFile = Repository::getZipContent($documentName);
47
        try {
48
            $soapService = SunatSoapClient::getService();
49
            $soapService->__soapCall('sendSummary', [['fileName' => "$documentName.zip", 'contentFile' => $contentFile]]);
50
            $serverResponse = $soapService->__getLastResponse();
51
        } catch (Exception $exc) {
52
            throw new SunatException($exc->getMessage(), $exc->getCode());
53
        }
54
55
        // Save and return ticket
56
        $ticket = self::saveTicket($serverResponse, $documentName);
57
        return $ticket;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ticket returns the type string which is incompatible with the documented return type array.
Loading history...
58
    }
59
60
    public static function getStatus($documentName) {
61
        $ticket = Repository::getTicketInfo($documentName);
62
        try {
63
            $soapService = SunatSoapClient::getService();
64
            $soapService->__soapCall('getStatus', [['ticket' =>$ticket]]);
65
            $serverResponse = $soapService->__getLastResponse();
66
        } catch (Exception $exc) {
67
            throw new SunatException($exc->getMessage(), $exc->getCode());
68
        }
69
        // Save Constancia de recepción
70
        return self::saveStatusResponse($serverResponse, $documentName);
71
    }
72
73
    private static function saveCdr($response, $documentName) {
74
        $xml = simplexml_load_string($response);
75
        $appResp = $xml->xpath("//applicationResponse")[0];
76
        // CDR
77
        $cdr = base64_decode($appResp);
78
        Repository::saveCdr($documentName, $cdr);
79
    }
80
81
    private static function saveStatusResponse($response, $documentName) {
82
        $xml = simplexml_load_string($response);
83
        $status = (array)$xml->xpath("//status")[0];
84
        
85
        // Status code only 0 and 99
86
        if($status['statusCode'] == '0' || $status['statusCode'] == '99') {
87
            $statusContent = $status['content'];
88
            $cdr = base64_decode($statusContent);
89
            Repository::saveCdr($documentName, $cdr);
90
            $status['cdr'] = Repository::getCdrInfo($documentName);
91
            $status['message'] = null;
92
        }else{
93
            $status['cdr'] = null;
94
            $status['message'] = $status['content'];
95
        }
96
        unset($status['content']);
97
        return $status;
98
    }
99
100
    private static function saveTicket($response, $documentName) {
101
        $xmlObj = simplexml_load_string($response);
102
        // Ticket
103
        $ticket = (string) $xmlObj->xpath("//ticket")[0];
104
        Repository::saveTicket($documentName, $ticket);
105
        return $ticket;
106
    }
107
108
}
109