Skip to content

Commit

Permalink
finish up the first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorchi committed Aug 7, 2018
1 parent db943c3 commit 9d6e707
Show file tree
Hide file tree
Showing 25 changed files with 654 additions and 101 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build
composer.lock
docs
vendor
coverage
coverage
temp
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:30:"PHPUnit\Runner\TestResultCache":562:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:15:{s:22:"instanceOfCsdConverter";d:0.028;s:22:"fileExistsOrIsReadable";d:0.003;s:14:"fileIsNotEmpty";d:0.002;s:15:"convertCerToPem";d:0.014;s:31:"convertCerToPemIfIsNotConverted";d:0.015;s:13:"cerIsValidCsd";d:0.016;s:15:"getSerialNumber";d:0.015;s:8:"getTaxId";d:0.015;s:24:"datesAreInstanceOfCarbon";d:0.014;s:7:"isValid";d:0.015;s:31:"convertKeyToPemIfIsNotConverted";d:0.019;s:17:"assertSeeFilename";d:0.022;s:16:"saveToAGivenPath";d:0.023;s:28:"saveToAGivenPathWithFilename";d:0.018;s:16:"encryptPemInDes3";d:0.029;}}}
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Very short description of the package
# Convert pem required files for CFDI

[![Latest Version on Packagist](https://img.shields.io/packagist/v/feimx/csd_converter.svg?style=flat-square)](https://packagist.org/packages/feimx/csd_converter)
[![Build Status](https://img.shields.io/travis/feimx/csd_converter/master.svg?style=flat-square)](https://travis-ci.org/feimx/csd_converter)
[![Quality Score](https://img.shields.io/scrutinizer/g/feimx/csd_converter.svg?style=flat-square)](https://scrutinizer-ci.com/g/feimx/csd_converter)
[![Total Downloads](https://img.shields.io/packagist/dt/feimx/csd_converter.svg?style=flat-square)](https://packagist.org/packages/feimx/csd_converter)

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
The `feimx/csd_converter` package provide a simple way for create .pem files for your CSD's.

## Installation

Expand All @@ -17,11 +17,69 @@ composer require feimx/csd_converter

## Usage

Firt need create a new instance of `CsdConverter`:

``` php
$converter = new FeiMx\Csd\CsdConverter();
echo $converter->echoPhrase('Hello, FeiMx!');
echo $converter->serial_number();
```

Thats all, now you can access the cer info and save the news files to given path

``` php
echo $converter->serial_number;
echo $converter->tax_id;
echo $converter->valid_from; // instance of Carbon
echo $converter->valid_to; // instance of Carbon
echo $converter->getStatus();
```

`valid_from` and `valid_to` are instances of Carbon, so you can modify and format the dates:

``` php
echo $converter->valid_from->format('d/m/Y h:i a');
echo $converter->valid_to->format('d/m/Y h:i a');
```

`getStatus()` returns a expired, valid or invalid status:

``` php
if($converter->getStatus() === CsdConverter::VALID){}
if($converter->getStatus() === CsdConverter::INVALID){}
if($converter->getStatus() === CsdConverter::EXPIRED){}

```

You can verify if the files are a valid CSD:

``` php
var_dump($converter->isValidCsd()); // true or false
```

Now you can save the created files to a given path and assigne a optional filename:

``` php
$path = __DIR__.'/temp/';
$filename = 'VALIDCSD';
$converter->save($path, $filename);
//this create 4 files:
//__DIR__.'/temp/VALIDCSD.cer'
//__DIR__.'/temp/VALIDCSD.cer.pem'
//__DIR__.'/temp/VALIDCSD.key'
//__DIR__.'/temp/VALIDCSD.key.pem'
```

And last, but not less important, you can encryp the converted key into des3:

``` php
$file = __DIR__.'/temp/VALIDCSD.key.pem';
$password = 'secret';
$converter->encryptKey($file, $password);
//__DIR__.'/temp/VALIDCSD.enc.key'
```

_Note:_ This is very useful with third party provider such as Finkok for stamp the CFDI

### Testing

``` bash
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
}
],
"require": {
"php": "^7.1"
"php": "^7.1",
"nesbot/carbon": "^1.32"
},
"require-dev": {
"larapack/dd": "^1.0",
Expand Down
130 changes: 130 additions & 0 deletions src/CsdConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace FeiMx\Csd;

use Carbon\Carbon;
use FeiMx\Csd\Strategies\Cer;
use FeiMx\Csd\Strategies\Key;
use InvalidArgumentException;
use RuntimeException;

class CsdConverter
{
const VALID = 1;

const INVALID = 0;

const EXPIRED = -1;

public $cerStrategy;

public $keyStrategy;

public $data = [];

public $version;

public $serial_number;

public $tax_id;

public $name;

public $valid_from;

public $valid_to;

/**
* Create a new CsdConverter Instance.
*/
public function __construct(string $cer_file, string $key_file, string $password)
{
$this->assertValidFile($cer_file);
$this->cerStrategy = new Cer($cer_file);

$this->assertValidFile($key_file);
$this->keyStrategy = new Key($key_file, $password);

$this->setData();
}

protected function assertValidFile(string $cer_file)
{
if (!file_exists($cer_file) || !is_readable($cer_file)) {
throw new InvalidArgumentException("File {$cer_file} does not exists or is not readable");
}
}

protected function setData()
{
$this->data = openssl_x509_parse($this->cerStrategy->pem_content, true);
if (!is_array($this->data)) {
throw new RuntimeException("Cannot parse the certificate file {$this->cerStrategy->filename}");
}

$this->mapDataToProps($this->data);
}

public function isValidCsd()
{
return isset($this->data['subject']['OU']) && !empty($this->data['subject']['OU']);
}

public function getStatus()
{
$now = Carbon::now();
if ($now > $this->valid_to) {
return self::EXPIRED;
}

if ($now < $this->valid_from) {
return self::INVALID;
}

if ($now > $this->valid_from && $now < $this->valid_to) {
return self::VALID;
}
}

protected function mapDataToProps($data)
{
$this->version = $data['version'];

$this->serial_number = $this->paserSeriaNumberHex($data['serialNumberHex']);

$this->tax_id = preg_split('/\s/', $data['subject']['x500UniqueIdentifier'])[0];

$this->name = $data['subject']['name'];

$this->valid_from = Carbon::createFromTimestamp($data['validFrom_time_t']);

$this->valid_to = Carbon::createFromTimestamp($data['validTo_time_t']);
}

public function paserSeriaNumberHex($serialNumber)
{
$parsedSerialNumber = '';
for ($i = 0; $i < strlen($serialNumber); ++$i) {
if (0 != $i % 2) {
$parsedSerialNumber .= substr($serialNumber, $i, 1);
}
}

return $parsedSerialNumber;
}

public function save(string $path, string $filename = null)
{
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$this->cerStrategy->save($path, $filename);

$this->keyStrategy->save($path, $filename);
}

public function encryptKey(string $pemfile, string $password)
{
return $this->keyStrategy->encrypt($pemfile, $password);
}
}
24 changes: 24 additions & 0 deletions src/CsdServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace FeiMx\Csd;

use Illuminate\Support\ServiceProvider;

class CsdServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
}
}

/**
* Register the application services.
*/
public function register()
{
}
}
25 changes: 0 additions & 25 deletions src/SkeletonClass.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/SkeletonFacade.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/SkeletonServiceProvider.php

This file was deleted.

Loading

0 comments on commit 9d6e707

Please sign in to comment.