xint0/sw-php-client

SmarterWeb API PHP client

0.2.0 2023-11-15 17:35 UTC

This package is auto-updated.

Last update: 2024-05-18 06:06:51 UTC


README

Description

SmarterWeb fiscal voucher stamping and cancellation web service client.

Installation

Quick installation with Guzzle 7 adapter:

composer require xint0/sw-php-client php-http/guzzle7-adapter

The SmarterWeb PHP client requires a PSR-18 HTTP client, you can use any component that provides psr/http-client-implementation.

If your project already requires a PSR-18 HTTP client implementation you can only install SmarterWeb PHP client:

composer require xint0/sw-php-client

Usage

Once you have a token for using the SmarterWeb web services you can create an instance of the Xint0\SmarterWeb\Api class:

Stamp

use Xint0\SmarterWeb\Api;
use Xint0\SmarterWeb\Exceptions\ApiException;

$token = 'your_smarterweb_security_token';
$api = new Api($token);

$xml = file_get_contents('path_to_signed_fiscal_voucher.xml');

try {
    /*
     * Calls stamp method requesting version 1 response
     * see https://developers.sw.com.mx/knowledge-base/versiones-de-respuesta-timbrado/
     */
    $response = $api->stamp($xml);

    if ($response->success) {
        $fiscal_voucher_stamp_xml_fragment = $response->data['tfd'];
    } else {
        /**
         * In case service returns an error response more information is available
         * in message and messageDetail properties.
         * @see https://developers.sw.com.mx/knowledge-base/cancelacion-cfdi/
         */
        $message = $response->message;
        $messageDetail = $response->messageDetail;
    }
} catch (ApiException $exception) {
    /**
     * In case an error occurred when sending the request or receiving the response
     * an ApiException is thrown
     */
    $message = $exception->getMessage();
}

Cancellation

use Xint0\SmarterWeb\Api;
use Xint0\SmarterWeb\Exceptions\ApiException;

$token = 'your_smarterweb_security_token';
$api = new Api($token);

$cancellation_request_xml = fopen('path_to_signed_cancellation_request.xml');

try {
    $response = $api->cancel($cancellation_request_xml);

    if ($response->success) {
        $data = $response->data;
        /**
         * The response data array has to keys: acuse and uuid.
         * The acuse key value is the cancellation request acknowledgement XML.
         * The uuid key value is and array with each of the fiscal voucher folios
         * included in the request as key and the cancellation request code as value.
         * @see https://developers.sw.com.mx/knowledge-base/cancelacion-cfdi/
         */
        $fiscal_voucher_cancellation_request_acknowledgement_xml = $data['acuse'];
        $individual_fiscal_voucher_cancellation_request_code = $data['uuid']['3EAEABC9-EA41-4627-9609-C6856B78E2B1'];
    } else {
        /**
         * In case service returns an error response more information is available
         * in message and messageDetail properties.
         * @see https://developers.sw.com.mx/knowledge-base/cancelacion-cfdi/
         */
        $message = $response->message;
        $messageDetail = $response->messageDetail;
    }
} catch (ApiException $exception) {
    /**
     * In case an error occurred when sending the request or receiving the response
     * an ApiException is thrown
     */
    $message = $exception->getMessage();
} finally {
    fclose($cancellation_request_xml);
}

Support

If you need assistance you can open an issue at: https://gitlab.com/xint0-open-source/sw-php-client/-/issues

Roadmap

MethodIs implemented
stampYes
cancelYes

Contributing

You can fork the project and submit a merge request.

Authors and acknowledgment

Show your appreciation to those who have contributed to the project.

License

This component is open source licensed under the MIT license.