Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Oct 31, 2014
0 parents commit d87a722
Show file tree
Hide file tree
Showing 29 changed files with 1,148 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#v11
This is a php library to read ESR v11 files, Swiss banking payments.<br>
(BESR/VESR/ESR - Einzahlungsscheine mit Referenznummer einlesen)

## Installation

Add v11 in your composer.json:

```js
{
"require": {
"ticketpark/v11": "~0.1"
}
}
```

Now tell composer to download the bundle by running the command:

``` bash
$ php composer.phar update ticketpark/v11
```

## Example
See [example.php](example/example.php)


## License
This bundle is under the MIT license. See the complete license in the bundle:

[Resources/meta/LICENSE](Resources/meta/LICENSE)
Binary file not shown.
19 changes: 19 additions & 0 deletions Resources/meta/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014 Ticketpark GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
150 changes: 150 additions & 0 deletions Tests/v11/Tests/v11Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Ticketpark\v11\Tests;

use Ticketpark\v11\v11;

class v11Test extends \PHPUnit_Framework_TestCase
{
public function testValidate()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$this->assertTrue($v11->validate());
}

public function testValidateTransactions()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102514102600000000000000000000120",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000000",
"002012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$v11->setParticipantIdentifier('123456');

$this->assertSame('002', $v11->getTransactionRecords()[0]->getTransactionCode()->getTransactionCode());
$this->assertSame('credit', $v11->getTransactionRecords()[0]->getTransactionCode()->getTransactionType());
$this->assertSame('esr', $v11->getTransactionRecords()[0]->getTransactionCode()->getReceiptType());
$this->assertSame('banking', $v11->getTransactionRecords()[0]->getTransactionCode()->getPaymentType());

$this->assertSame('012000002', $v11->getTransactionRecords()[0]->getBankingAccount());
$this->assertSame('123456000000001291146290519', $v11->getTransactionRecords()[0]->getReferenceNumber());
$this->assertSame('12345600000000129114629051', $v11->getTransactionRecords()[0]->getReferenceNumberWithoutCheckDigit());
$this->assertSame('00000000129114629051', $v11->getTransactionRecords()[0]->getCustomReferenceNumber());
$this->assertSame(129114629051, $v11->getTransactionRecords()[0]->getCustomReferenceNumber(true));
$this->assertSame(75, $v11->getTransactionRecords()[0]->getAmount());
$this->assertSame('0000000000', $v11->getTransactionRecords()[0]->getInternalBankReference());
$this->assertSame('2014-10-24', $v11->getTransactionRecords()[0]->getDatePaid()->format('Y-m-d'));
$this->assertSame('2014-10-25', $v11->getTransactionRecords()[0]->getDateProcessed()->format('Y-m-d'));
$this->assertSame('2014-10-26', $v11->getTransactionRecords()[0]->getDateCreditNote()->format('Y-m-d'));
$this->assertSame('000000000', $v11->getTransactionRecords()[0]->getMicrofilmReference());
$this->assertSame('0', $v11->getTransactionRecords()[0]->getRejectCode());
$this->assertNull($v11->getTransactionRecords()[0]->getDateValuta());
$this->assertSame(1.2, $v11->getTransactionRecords()[0]->getFee());

$this->assertSame('999', $v11->getTotalRecord()->getTransactionCode()->getTransactionCode());
$this->assertSame('012000002', $v11->getTotalRecord()->getBankingAccount());
$this->assertSame('999999999999999999999999999', $v11->getTotalRecord()->getSortingKey());
$this->assertSame(4, $v11->getTotalRecord()->getNumberOfTransactions());
$this->assertSame('2014-10-27', $v11->getTotalRecord()->getDateFileCreation()->format('Y-m-d'));
$this->assertSame(1.2, $v11->getTotalRecord()->getTotalFees());
}


public function testValidateTotalLineTooShort()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"99901200000299999999999999999999999999900000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('The total line contains 86 instead of 87 characters', $v11->getError());
}

public function testValidateTotalLineBadTransactionCode()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"111012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('The total line does not contain a valid transaction code', $v11->getError());
}

public function testValidateTotalLineInvalidContents()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"99901200000299999999999999999999999999900A000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('The total line contains invalid characters. It may only contain digits and spaces', $v11->getError());
}

public function testValidateTransactionLineTooShort()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 255000000000001410241410241410240000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('Line number 3 contains 99 instead of 100 characters', $v11->getError());
}

public function testValidateTransactionLineBadTransactionCode()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"007012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('Line number 3 contains does not contain a valid transaction code', $v11->getError());
}

public function testValidateTransactionLineInvalidContents()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 255000000A000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('Line number 3 contains invalid characters. It may only contain digits and spaces', $v11->getError());
}

public function testValidateNotMatchingNumberOfTransactions()
{
$v11 = new v11(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));
$this->assertFalse($v11->validate());
$this->assertSame('The number of 3 transactions does not match the number of 4 transactions according to the total line', $v11->getError());
}
}
17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "ticketpark/v11",
"type": "library",
"description": "A php library to process ESR/BESR v11 files (Swiss payment slips / Einzahlungsscheine mit Referenznummer)",
"keywords": ["besr", "esr", "v11", "einzahlungsschein", "payment slip"],
"homepage": "https://github.com/Ticketpark/v11",
"license": "MIT",
"authors": [
{"name": "Manuel Reinhard", "email": "[email protected]"}
],
"require": {
"php": ">=5.4"
},
"autoload": {
"psr-4": { "Ticketpark\\v11\\": "lib/v11" }
}
}
49 changes: 49 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

include '../vendor/autoload.php';

$v11 = new \Ticketpark\v11\v11();

// Set your esr participant identifier (Teilnehmernummer)
// This is optional, but will be useful to call $record->getCustomReferenceNumber() later on.
$v11->setParticipantIdentifier('123456');

// Set content - by file or array
#$v11->setFile('/path/to/file/besr.v11');
$v11->setLines(array(
"002012000002123456000000001291146290519 7500000000000014102414102414102400000000000000000000000",
"012012000002123456000000001290507716881 50000008 000014102314102414102400552073900000000000120",
"002012000002123456000000001288602003992 2550000000000014102414102414102400000000000000000000000",
"002012000002123456000000001290068583973 50000008 000014102314102414102400489435900000000000000",
"999012000002999999999999999999999999999000000020050000000000004141027000000120000000004 ",
));

// Validate the contents
if(!$v11->validate()) {
print $v11->getError();
}

// Get the transaction records - see Ticketpark\v11\Record\TransactionRecord for more methods
foreach($v11->getTransactionRecords() as $record){
var_dump(
array(
'Transaction type' => $record->getTransactionCode()->getTransactionType(),
'Payment type' => $record->getTransactionCode()->getPaymentType(),
'Amount' => $record->getSignedAmount(),
'Full Reference number' => $record->getReferenceNumber(),
'Customer numeric reference number' => $record->getCustomReferenceNumber(true),
'Fee' => $record->getFee(),
)
);
}

// Get the total record - see Ticketpark\v11\Record\TotalRecord for more methods
$total = $v11->getTotalRecord();
var_dump(
array(
'Transaction type' => $total->getTransactionCode()->getTransactionType(),
'Amount' => $total->getSignedAmount(),
'Number of transactions' => $total->getNumberOfTransactions(),
'Total Fees' => $total->getTotalFees(),
)
);
116 changes: 116 additions & 0 deletions lib/v11/Record/TotalRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Ticketpark\v11\Record;

use Ticketpark\v11\Record\TransactionCode\TransactionCodeInterface;

/**
* TotalRecord
*
* For more information on the individual parameters see
* page 24 in Resources/docs/specifications/zkb-vesr-handbuch.pdf
*/
class TotalRecord
{
protected $transactionCode;
protected $bankingAccount;
protected $sortingKey;
protected $amount;
protected $numberOfTransactions;
protected $dateFileCreation;
protected $totalFees;

public function setTransactionCode(TransactionCodeInterface $transactionCode)
{
$this->transactionCode = $transactionCode;

return $this;
}

public function getTransactionCode()
{
return $this->transactionCode;
}

public function setBankingAccount($bankingAccount)
{
$this->bankingAccount = $bankingAccount;

return $this;
}

public function getBankingAccount()
{
return $this->bankingAccount;
}

public function setSortingKey($sortingKey)
{
$this->sortingKey = $sortingKey;

return $this;
}

public function getSortingKey()
{
return $this->sortingKey;
}

public function setAmount($amount)
{
$this->amount = $amount;

return $this;
}

public function getAmount()
{
return $this->amount;
}

public function getSignedAmount()
{
if ($this->getTransactionCode()->getTransactionType() == 'storno') {

return $this->amount * -1;
}

return $this->amount;
}

public function setNumberOfTransactions($numberOfTransactions)
{
$this->numberOfTransactions = $numberOfTransactions;

return $this;
}

public function getNumberOfTransactions()
{
return $this->numberOfTransactions;
}

public function setDateFileCreation(\DateTime $dateFileCreation = null)
{
$this->dateFileCreation = $dateFileCreation;

return $this;
}

public function getDateFileCreation()
{
return $this->dateFileCreation;
}

public function setTotalFees($totalFees)
{
$this->totalFees = $totalFees;

return $this;
}

public function getTotalFees()
{
return $this->totalFees;
}
}
Loading

0 comments on commit d87a722

Please sign in to comment.