PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/SwissPaymentSlip/SwissPaymentSlip/SwissPaymentSlipData.php

https://github.com/ravage84/SwissPaymentSlip
PHP | 1543 lines | 734 code | 139 blank | 670 comment | 111 complexity | cf10b132692708ef72018bdd77fef79d MD5 | raw file
  1. <?php
  2. /**
  3. * Swiss Payment Slip
  4. *
  5. * PHP version >= 5.3.0
  6. *
  7. * @licence MIT
  8. * @copyright 2012-2013 Some nice Swiss guys
  9. * @author Manuel Reinhard <manu@sprain.ch>
  10. * @author Peter Siska <pesche@gridonic.ch>
  11. * @author Marc Würth ravage@bluewin.ch
  12. * @link https://github.com/sprain/class.Einzahlungsschein.php
  13. * @version: 0.5.0
  14. */
  15. namespace SwissPaymentSlip\SwissPaymentSlip;
  16. use InvalidArgumentException;
  17. /**
  18. * Creates data containers for standard Swiss payment slips with or without reference number.
  19. * It doesn't actually do much. It's mostly a data container class to keep
  20. * including classes from having to care about how SwissPaymentSlip works.
  21. * But it provides a flexibility of which data it holds, because not always
  22. * all slip fields are needed in an application.
  23. *
  24. * Glossary:
  25. * SwissPaymentSlip = Einzahlungsschein mit Referenznummer
  26. * Payment slip with reference number
  27. * Summary term for orange payment slips in Switzerland
  28. * BESR = Banken-Einzahlungsschein mit Referenznummer
  29. * Banking payment slip with reference number
  30. * Orange payment slip for paying into a bank account (in contrast to a post cheque account with a VESR)
  31. * VESR = Verfahren für Einzahlungsschein mit Referenznummer
  32. * Procedure for payment slip with reference number
  33. * Orange payment slip for paying into a post cheque account (in contrast to a banking account with a BESR)
  34. * (B|V)SwissPaymentSlip+ = Einzahlungsschein mit Referenznummer ohne Betragsangabe
  35. * Payment slip with reference number without amount specification
  36. * An payment slip can be issued without a predefined payment amount
  37. * ES = Einzahlungsschein
  38. * Payment slip
  39. * Red payment slip for paying into a post cheque or bank account without reference number but message box
  40. *
  41. * @link https://www.postfinance.ch/content/dam/pf/de/doc/consult/manual/dlserv/inpayslip_isr_man_de.pdf German manual
  42. * @link http://www.six-interbank-clearing.com/en/home/standardization/dta.html
  43. *
  44. * @todo implement full red slip support (code line + additional code line)
  45. * @todo implement currency (CHF, EUR), means different prefixes in code line
  46. * @todo implement payment on own account, means different prefixes in code line --> edge case!
  47. * @todo implement cash on delivery (Nachnahme), means different prefixes in code line --> do it on demand
  48. * @todo implement amount check for unrounded (.05) cents, document why (see manual)
  49. */
  50. class SwissPaymentSlipData
  51. {
  52. /**
  53. * Constant for orange payment slips
  54. */
  55. const ORANGE = 'orange';
  56. /**
  57. * Constant for red payment slips
  58. */
  59. const RED = 'red';
  60. /**
  61. * Consists the array table for calculating the check digit by modulo 10
  62. *
  63. * @var array Table for calculating the check digit by modulo 10
  64. */
  65. private $moduloTable = array(0,9,4,6,8,2,7,1,3,5);
  66. /**
  67. * Determines the payment slip type.
  68. * Either orange or red
  69. *
  70. * @var string Orange or red payment slip
  71. */
  72. protected $type = self::ORANGE;
  73. /**
  74. * Determines if the payment slip must not be used for payment (XXXed out)
  75. *
  76. * @var bool Normally false, true if not for payment
  77. */
  78. protected $notForPayment = false;
  79. /**
  80. * Determines if the payment slip has a recipient bank. Can be disabled for preprinted payment slips
  81. *
  82. * @var bool True if yes, false if no
  83. */
  84. protected $withBank = true;
  85. /**
  86. * Determines if the payment slip has a account number. Can be disabled for preprinted payment slips
  87. *
  88. * @var bool True if yes, false if no
  89. */
  90. protected $withAccountNumber = true;
  91. /**
  92. * Determines if the payment slip has a recipient. Can be disabled for preprinted payment slips
  93. *
  94. * @var bool True if yes, false if no
  95. */
  96. protected $withRecipient = true;
  97. /**
  98. * Determines if it's an SwissPaymentSlip or an SwissPaymentSlip+
  99. *
  100. * @var bool True for SwissPaymentSlip, false for SwissPaymentSlip+
  101. */
  102. protected $withAmount = true;
  103. /**
  104. * Determines if the payment slip has a reference number. Can be disabled for preprinted payment slips
  105. *
  106. * @var bool True if yes, false if no
  107. */
  108. protected $withReferenceNumber = true;
  109. /**
  110. * Determines if the payment slip's reference number should contain the banking customer id.
  111. * Can be disabled for recipients who don't need this
  112. *
  113. * @var bool True if yes, false if no
  114. */
  115. protected $withBankingCustomerId = true;
  116. /**
  117. * Determines if the payment slip has a payer. Can be disabled for preprinted payment slips
  118. *
  119. * @var bool True if yes, false if no
  120. */
  121. protected $withPayer = true;
  122. /**
  123. * Determines if the payment slip has a IBAN specified. Can be disabled for preprinted payment slips
  124. * Only possible for ES, but not for SwissPaymentSlip
  125. *
  126. * @var bool True if yes, false if no
  127. */
  128. protected $withIban = false;
  129. /**
  130. * Determines if the payment slip has a payment reason. Can be disabled for preprinted payment slips
  131. * Only possible for ES, but not for SwissPaymentSlip
  132. *
  133. * @var bool True if yes, false if no
  134. */
  135. protected $withPaymentReason = true;
  136. /**
  137. * The name of the bank
  138. *
  139. * @var string The name of the bank
  140. */
  141. protected $bankName = '';
  142. /**
  143. * The postal code and city of the bank
  144. *
  145. * @var string The postal code and city of the bank
  146. */
  147. protected $bankCity = '';
  148. /**
  149. * The bank or post cheque account where the money will be transferred to
  150. *
  151. * @var string The bank or post cheque account
  152. */
  153. protected $accountNumber = '';
  154. /**
  155. * The first line of the recipient, e.g. "My Company Ltd."
  156. *
  157. * @var string The first line of the recipient
  158. */
  159. protected $recipientLine1 = '';
  160. /**
  161. * The second line of the recipient, e.g. "Examplestreet 61"
  162. *
  163. * @var string The second line of the recipient
  164. */
  165. protected $recipientLine2 = '';
  166. /**
  167. * The third line of the recipient, e.g. "8000 Zürich"
  168. *
  169. * @var string The third line of the recipient
  170. */
  171. protected $recipientLine3 = '';
  172. /**
  173. * The fourth line of the recipient, if needed
  174. *
  175. * @var string The fourth line of the recipient
  176. */
  177. protected $recipientLine4 = '';
  178. /**
  179. * The amount to be payed into. Can be disabled with withAmount = false for SwissPaymentSlip+ slips
  180. *
  181. * @var float The amount to be payed into
  182. */
  183. protected $amount = 0.0;
  184. /**
  185. * The reference number, without banking customer id and check digit
  186. *
  187. * @var string The reference number
  188. */
  189. protected $referenceNumber = '';
  190. /**
  191. * The banking customer id, which will be prepended to the reference number
  192. *
  193. * @var string The banking customer id
  194. */
  195. protected $bankingCustomerId = '';
  196. /**
  197. * The first line of the payer, e.g. "Hans Mustermann"
  198. *
  199. * @var string The first line of the payer
  200. */
  201. protected $payerLine1 = '';
  202. /**
  203. * The second line of the payer, e.g. "Main Street 11"
  204. *
  205. * @var string The second line of the payer
  206. */
  207. protected $payerLine2 = '';
  208. /**
  209. * The third line of the payer, e.g. "4052 Basel"
  210. *
  211. * @var string The third line of the payer
  212. */
  213. protected $payerLine3 = '';
  214. /**
  215. * The fourth line of the payer, if needed
  216. *
  217. * @var string The fourth line of the payer
  218. */
  219. protected $payerLine4 = '';
  220. /**
  221. * The IBAN of the recipient of a ES. Not available on a SwissPaymentSlip
  222. *
  223. * @var string The IBAN of the recipient
  224. */
  225. protected $iban = '';
  226. /**
  227. * The first line of the payment reason of a ES. Not available on a SwissPaymentSlip
  228. *
  229. * @var string The first line of the payment reason
  230. */
  231. protected $paymentReasonLine1 = '';
  232. /**
  233. * The second line of the payment reason of a ES. Not available on a SwissPaymentSlip
  234. *
  235. * @var string The second line of the payment reason
  236. */
  237. protected $paymentReasonLine2 = '';
  238. /**
  239. * The third line of the payment reason of a ES. Not available on a SwissPaymentSlip
  240. *
  241. * @var string The third line of the payment reason
  242. */
  243. protected $paymentReasonLine3 = '';
  244. /**
  245. * The fourth line of the payment reason of a ES. Not available on a SwissPaymentSlip
  246. *
  247. * @var string The fourth line of the payment reason
  248. */
  249. protected $paymentReasonLine4 = '';
  250. /**
  251. * Construct an empty payment slip
  252. *
  253. * By default it creates an empty orange payment slip.
  254. * Can be changed to a red one by supplying red as parameter.
  255. *
  256. * @param string $type The slip type, either orange or red
  257. */
  258. public function __construct($type = self::ORANGE)
  259. {
  260. $this->setType($type, true);
  261. }
  262. /**
  263. * Set payment slip type. Resets settings and data if changing the type or being forced to
  264. *
  265. * @param string $type The slip type
  266. * @param bool $forceReset Force a data reset according to the given type
  267. * @throws \InvalidArgumentException
  268. * @return bool True if successful
  269. */
  270. public function setType($type = self::ORANGE, $forceReset = false)
  271. {
  272. if (!is_string($type)) {
  273. throw new InvalidArgumentException('Type parameter is not a string!');
  274. }
  275. if (!is_bool($forceReset)) {
  276. throw new InvalidArgumentException('ForceReset parameter is not a bool!');
  277. }
  278. $type = strtolower($type);
  279. if ($type !== self::ORANGE && $type !== self::RED) {
  280. throw new InvalidArgumentException('Invalid type parameter ' . $type . '!');
  281. }
  282. if ($this->type != $type || $forceReset) {
  283. $this->type = $type;
  284. if ($type == self::ORANGE) {
  285. $this->setOrangeDefaults();
  286. } elseif ($type == self::RED) {
  287. $this->setRedDefaults();
  288. }
  289. }
  290. return true;
  291. }
  292. /**
  293. * Set the default values for the orange payment slip
  294. */
  295. protected function setOrangeDefaults() {
  296. $this->setWithBank(true);
  297. $this->setWithAccountNumber(true);
  298. $this->setWithRecipient(true);
  299. $this->setWithAmount(true);
  300. $this->setWithReferenceNumber(true);
  301. $this->setWithBankingCustomerId(true);
  302. $this->setWithPayer(true);
  303. $this->setWithIban(false);
  304. $this->setWithPaymentReason(false);
  305. }
  306. /**
  307. * Set the default values for the red payment slip
  308. */
  309. protected function setRedDefaults() {
  310. $this->setWithBank(true);
  311. $this->setWithAccountNumber(true);
  312. $this->setWithRecipient(true);
  313. $this->setWithAmount(true);
  314. $this->setWithReferenceNumber(false);
  315. $this->setWithBankingCustomerId(false);
  316. $this->setWithPayer(true);
  317. $this->setWithIban(true);
  318. $this->setWithPaymentReason(true);
  319. }
  320. /**
  321. * Get payment slip type
  322. *
  323. * @return string The slip type
  324. */
  325. public function getType()
  326. {
  327. return $this->type;
  328. }
  329. /**
  330. * Set payment slip for not to be used for payment
  331. *
  332. * XXXes out all fields to prevent people using the payment slip.
  333. *
  334. * @param boolean $notForPayment True if not for payment, else false
  335. */
  336. public function setNotForPayment($notForPayment = true)
  337. {
  338. $this->notForPayment = $notForPayment;
  339. if ($notForPayment === true) {
  340. $this->setBankData('XXXXXX', 'XXXXXX');
  341. $this->setAccountNumber('XXXXXX');
  342. $this->setRecipientData('XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX');
  343. $this->setPayerData('XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX');
  344. $this->setAmount('XXXXXXXX.XX');
  345. $this->setReferenceNumber('XXXXXXXXXXXXXXXXXXXX');
  346. $this->setBankingCustomerId('XXXXXX');
  347. }
  348. }
  349. /**
  350. * Get whether this payment slip must not be used for payment
  351. *
  352. * @return bool
  353. */
  354. public function getNotForPayment() {
  355. return $this->notForPayment;
  356. }
  357. /**
  358. * Set if payment slip has a bank specified
  359. *
  360. * @param bool $withBank True for yes, false for no
  361. * @return bool True if successful, else false
  362. */
  363. public function setWithBank($withBank = true)
  364. {
  365. if (is_bool($withBank)) {
  366. $this->withBank = $withBank;
  367. if (!$withBank) {
  368. $this->bankName = '';
  369. $this->bankCity = '';
  370. }
  371. return true;
  372. }
  373. return false;
  374. }
  375. /**
  376. * Get if payment slip has recipient specified
  377. *
  378. * @return bool True if payment slip has the recipient specified, else false
  379. */
  380. public function getWithBank()
  381. {
  382. return $this->withBank;
  383. }
  384. /**
  385. * Set if payment slip has an account number specified
  386. *
  387. * @param bool $withAccountNumber True if yes, false if no
  388. * @return bool True if successful, else false
  389. */
  390. public function setWithAccountNumber($withAccountNumber = true)
  391. {
  392. if (is_bool($withAccountNumber)) {
  393. $this->withAccountNumber = $withAccountNumber;
  394. if (!$withAccountNumber) {
  395. $this->accountNumber = '';
  396. }
  397. return true;
  398. }
  399. return false;
  400. }
  401. /**
  402. * Get if payment slip has an account number specified
  403. *
  404. * @return bool True if payment slip has an account number specified, else false
  405. */
  406. public function getWithAccountNumber()
  407. {
  408. return $this->withAccountNumber;
  409. }
  410. /**
  411. * Set if payment slip has a recipient specified
  412. *
  413. * @param bool $withRecipient True if yes, false if no
  414. * @return bool True if successful, else false
  415. */
  416. public function setWithRecipient($withRecipient = true)
  417. {
  418. if (is_bool($withRecipient)) {
  419. $this->withRecipient = $withRecipient;
  420. if (!$withRecipient) {
  421. $this->recipientLine1 = '';
  422. $this->recipientLine2 = '';
  423. $this->recipientLine3 = '';
  424. $this->recipientLine4 = '';
  425. }
  426. return true;
  427. }
  428. return false;
  429. }
  430. /**
  431. * Get if payment slip has a recipient specified
  432. *
  433. * @return bool True if payment slip has a recipient specified, else false
  434. */
  435. public function getWithRecipient()
  436. {
  437. return $this->withRecipient;
  438. }
  439. /**
  440. * Set if payment slip has an amount specified
  441. *
  442. * @param bool $withAmount True for yes, false for no
  443. * @return bool True if successful, else false
  444. */
  445. public function setWithAmount($withAmount = true)
  446. {
  447. if (is_bool($withAmount)) {
  448. $this->withAmount = $withAmount;
  449. if (!$withAmount) {
  450. $this->amount = 0.0;
  451. }
  452. return true;
  453. }
  454. return false;
  455. }
  456. /**
  457. * Get if payment slip has an amount specified
  458. *
  459. * @return bool True if payment slip has an amount specified, else false
  460. */
  461. public function getWithAmount()
  462. {
  463. return $this->withAmount;
  464. }
  465. /**
  466. * Set if payment slip has a reference number specified
  467. *
  468. * Resets reference number if disabled
  469. *
  470. * @param bool $withReferenceNumber True if yes, false if no
  471. * @return bool True if successful, else false
  472. */
  473. public function setWithReferenceNumber($withReferenceNumber = true)
  474. {
  475. if ($this->getType() == self::ORANGE) {
  476. if (is_bool($withReferenceNumber)) {
  477. $this->withReferenceNumber = $withReferenceNumber;
  478. if (!$withReferenceNumber) {
  479. $this->referenceNumber = '';
  480. }
  481. return true;
  482. }
  483. } else {
  484. $this->withReferenceNumber = false;
  485. $this->referenceNumber = '';
  486. }
  487. return false;
  488. }
  489. /**
  490. * Get if payment slip has a reference number specified
  491. *
  492. * @return bool True if payment slip has a reference number specified, else false
  493. */
  494. public function getWithReferenceNumber()
  495. {
  496. return $this->withReferenceNumber;
  497. }
  498. /**
  499. * Set if the payment slip's reference number should contain the banking customer id
  500. *
  501. * @param bool $withBankingCustomerId True if successful, else false
  502. * @return bool True if successful, else false
  503. */
  504. public function setWithBankingCustomerId($withBankingCustomerId = true)
  505. {
  506. if ($this->getType() == self::ORANGE) {
  507. if (is_bool($withBankingCustomerId)) {
  508. $this->withBankingCustomerId = $withBankingCustomerId;
  509. if (!$withBankingCustomerId) {
  510. $this->bankingCustomerId = '';
  511. }
  512. return true;
  513. }
  514. } else {
  515. $this->withBankingCustomerId = false;
  516. $this->bankingCustomerId = '';
  517. }
  518. return false;
  519. }
  520. /**
  521. * Get if the payment slip's reference number should contain the banking customer id.
  522. *
  523. * @return bool True if payment slip has the recipient specified, else false
  524. */
  525. public function getWithBankingCustomerId()
  526. {
  527. return $this->withBankingCustomerId;
  528. }
  529. /**
  530. * Set if payment slip has a payer specified
  531. *
  532. * @param bool $withPayer True if yes, false if no
  533. * @return bool True if successful, else false
  534. */
  535. public function setWithPayer($withPayer = true)
  536. {
  537. if (is_bool($withPayer)) {
  538. $this->withPayer = $withPayer;
  539. if (!$withPayer) {
  540. $this->payerLine1 = '';
  541. $this->payerLine2 = '';
  542. $this->payerLine3 = '';
  543. $this->payerLine4 = '';
  544. }
  545. return true;
  546. }
  547. return false;
  548. }
  549. /**
  550. * Get if payment slip has a payer specified
  551. *
  552. * @return bool True if payment slip has a payer specified, else false
  553. */
  554. public function getWithPayer()
  555. {
  556. return $this->withPayer;
  557. }
  558. /**
  559. * Set if payment slip has an IBAN specified.
  560. * Only available for red payment slips
  561. *
  562. * @param bool $withIban True if yes, false if no
  563. * @return bool True if successful, else false
  564. */
  565. public function setWithIban($withIban = false)
  566. {
  567. if ($this->getType() == self::RED) {
  568. if (is_bool($withIban)) {
  569. $this->withIban = $withIban;
  570. if (!$withIban) {
  571. $this->iban = '';
  572. }
  573. return true;
  574. }
  575. } else {
  576. $this->withIban = false;
  577. $this->iban = '';
  578. }
  579. return false;
  580. }
  581. /**
  582. * Get if payment slip has an IBAN specified
  583. *
  584. * @return bool True if payment slip has an IBAN specified, else false
  585. */
  586. public function getWithIban()
  587. {
  588. return $this->withIban;
  589. }
  590. /**
  591. * Set if payment slip has a payment reason specified.
  592. * Only available for red payment slips
  593. *
  594. * @param bool $withPaymentReason
  595. * @return bool True if successful, else false
  596. */
  597. public function setWithPaymentReason($withPaymentReason = false)
  598. {
  599. if ($this->getType() == self::RED) {
  600. if (is_bool($withPaymentReason)) {
  601. $this->withPaymentReason = $withPaymentReason;
  602. if (!$withPaymentReason) {
  603. $this->paymentReasonLine1 = '';
  604. $this->paymentReasonLine2 = '';
  605. $this->paymentReasonLine3 = '';
  606. $this->paymentReasonLine4 = '';
  607. }
  608. return true;
  609. }
  610. } else {
  611. $this->withPaymentReason = false;
  612. $this->paymentReasonLine1 = '';
  613. $this->paymentReasonLine2 = '';
  614. $this->paymentReasonLine3 = '';
  615. $this->paymentReasonLine4 = '';
  616. }
  617. return false;
  618. }
  619. /**
  620. * Get if payment slip has a payment reason specified
  621. *
  622. * @return bool True if payment slip has a payment reason specified, else false
  623. */
  624. public function getWithPaymentReason()
  625. {
  626. return $this->withPaymentReason;
  627. }
  628. /**
  629. * Sets the name, city and account number of the bank
  630. *
  631. * @param string $bankName Name of the bank
  632. * @param string $bankCity City of the bank
  633. * @return bool True if successful, else false
  634. */
  635. public function setBankData($bankName, $bankCity)
  636. {
  637. if (!$this->setBankName($bankName)) {
  638. return false;
  639. }
  640. if (!$this->setBankCity($bankCity)) {
  641. return false;
  642. }
  643. return true;
  644. }
  645. // TODO create a getBankData with formatting parameter
  646. /**
  647. * Set the name of the bank
  648. *
  649. * @param string $bankName The name of the bank
  650. * @return bool True if successful, else false
  651. *
  652. * @todo Implement max length check
  653. */
  654. protected function setBankName($bankName)
  655. {
  656. if ($this->getWithBank()) {
  657. $this->bankName = $bankName;
  658. return true;
  659. }
  660. return false;
  661. }
  662. /**
  663. * Get the name of the bank
  664. *
  665. * @return string|bool The name of the bank or false if withBank = false
  666. */
  667. public function getBankName()
  668. {
  669. if ($this->getWithBank()) {
  670. return $this->bankName;
  671. }
  672. return false;
  673. }
  674. /**
  675. * Set the postal code and city of the bank
  676. *
  677. * @param string $bankCity The postal code and city of the bank
  678. * @return bool True if successful, else false
  679. *
  680. * @todo Implement max length check
  681. */
  682. protected function setBankCity($bankCity)
  683. {
  684. if ($this->getWithBank()) {
  685. $this->bankCity = $bankCity;
  686. return true;
  687. }
  688. return false;
  689. }
  690. /**
  691. * Get the postal code and city of the bank
  692. *
  693. * @return string|bool The postal code and city of the bank or false if withBank = false
  694. */
  695. public function getBankCity()
  696. {
  697. if ($this->getWithBank()) {
  698. return $this->bankCity;
  699. }
  700. return false;
  701. }
  702. /**
  703. * Set the bank or post cheque account where the money will be transferred to
  704. *
  705. * @param string $accountNumber The bank or post cheque account
  706. * @return bool True if successful, else false
  707. *
  708. * @todo Implement parameter validation (two hyphens, min & max length)
  709. */
  710. public function setAccountNumber($accountNumber)
  711. {
  712. if ($this->getWithAccountNumber()) {
  713. $this->accountNumber = $accountNumber;
  714. return true;
  715. }
  716. return false;
  717. }
  718. /**
  719. * Get the bank or post cheque account where the money will be transferred to
  720. *
  721. * @return string|bool The bank or post cheque account or false if withAccountNumber = false
  722. */
  723. public function getAccountNumber()
  724. {
  725. if ($this->getWithAccountNumber()) {
  726. return $this->accountNumber;
  727. }
  728. return false;
  729. }
  730. /**
  731. * Sets the four lines of the recipient
  732. *
  733. * @param string $recipientLine1 The first line of the recipient, e.g. "My Company Ltd."
  734. * @param string $recipientLine2 The second line of the recipient, e.g. "Examplestreet 61"
  735. * @param string $recipientLine3 The third line of the recipient, e.g. "8000 Zürich"
  736. * @param string $recipientLine4 The fourth line of the recipient, if needed
  737. * @return bool True if successful, else false
  738. */
  739. public function setRecipientData($recipientLine1, $recipientLine2, $recipientLine3 = '', $recipientLine4 = '')
  740. {
  741. if (!$this->setRecipientLine1($recipientLine1) ||
  742. !$this->setRecipientLine2($recipientLine2) ||
  743. !$this->setRecipientLine3($recipientLine3) ||
  744. !$this->setRecipientLine4($recipientLine4)) {
  745. return false;
  746. }
  747. return true;
  748. }
  749. // TODO create a getRecipientData with formatting parameter
  750. /**
  751. * Set the first line of the recipient
  752. *
  753. * @param string $recipientLine1 The first line of the recipient, e.g. "My Company Ltd."
  754. * @return bool True if successful, else false
  755. */
  756. protected function setRecipientLine1($recipientLine1)
  757. {
  758. if ($this->getWithRecipient()) {
  759. $this->recipientLine1 = $recipientLine1;
  760. return true;
  761. }
  762. return false;
  763. }
  764. /**
  765. * Get the first line of the recipient
  766. *
  767. * @return string|bool The first line of the recipient or false if withRecipient = false
  768. */
  769. public function getRecipientLine1()
  770. {
  771. if ($this->getWithRecipient()) {
  772. return $this->recipientLine1;
  773. }
  774. return false;
  775. }
  776. /**
  777. * Set the second line of the recipient
  778. *
  779. * @param string $recipientLine2 The second line of the recipient, e.g. "Examplestreet 61"
  780. * @return bool True if successful, else false
  781. */
  782. protected function setRecipientLine2($recipientLine2)
  783. {
  784. if ($this->getWithRecipient()) {
  785. $this->recipientLine2 = $recipientLine2;
  786. return true;
  787. }
  788. return false;
  789. }
  790. /**
  791. * Get the second line of the recipient
  792. *
  793. * @return string|bool The second line of the recipient or false if withRecipient = false
  794. */
  795. public function getRecipientLine2()
  796. {
  797. if ($this->getWithRecipient()) {
  798. return $this->recipientLine2;
  799. }
  800. return false;
  801. }
  802. /**
  803. * Set the third line of the recipient
  804. *
  805. * @param string $recipientLine3 The third line of the recipient, e.g. "8000 Zürich"
  806. * @return bool True if successful, else false
  807. */
  808. protected function setRecipientLine3($recipientLine3)
  809. {
  810. if ($this->getWithRecipient()) {
  811. $this->recipientLine3 = $recipientLine3;
  812. return true;
  813. }
  814. return false;
  815. }
  816. /**
  817. * Get the third line of the recipient
  818. *
  819. * @return string|bool The third line of the recipient or false if withRecipient = false
  820. */
  821. public function getRecipientLine3()
  822. {
  823. if ($this->getWithRecipient()) {
  824. return $this->recipientLine3;
  825. }
  826. return false;
  827. }
  828. /**
  829. * Set the fourth line of the recipient
  830. *
  831. * @param string $recipientLine4 The fourth line of the recipient, if needed
  832. * @return bool True if successful, else false
  833. */
  834. protected function setRecipientLine4($recipientLine4)
  835. {
  836. if ($this->getWithRecipient()) {
  837. $this->recipientLine4 = $recipientLine4;
  838. return true;
  839. }
  840. return false;
  841. }
  842. /**
  843. * Get the fourth line of the recipient
  844. *
  845. * @return string|bool The fourth line of the recipient or false if withRecipient = false
  846. */
  847. public function getRecipientLine4()
  848. {
  849. if ($this->getWithRecipient()) {
  850. return $this->recipientLine4;
  851. }
  852. return false;
  853. }
  854. /**
  855. * Set the amount of the payment slip. Only possible if it's not a SwissPaymentSlip+
  856. *
  857. * @param float $amount The amount to be payed into
  858. * @return bool True if successful, else false
  859. */
  860. public function setAmount($amount = 0.0)
  861. {
  862. if ($this->getWithAmount()) {
  863. $this->amount = $amount;
  864. return true;
  865. }
  866. return false;
  867. }
  868. /**
  869. * Get the amount to be payed into
  870. *
  871. * @return float The amount to be payed into
  872. */
  873. public function getAmount()
  874. {
  875. if ($this->getWithAmount()) {
  876. return $this->amount;
  877. }
  878. return false;
  879. }
  880. /**
  881. * Set the reference number
  882. *
  883. * @param string $referenceNumber The reference number
  884. * @return bool True if successful, else false
  885. */
  886. public function setReferenceNumber($referenceNumber)
  887. {
  888. if ($this->getWithReferenceNumber()) {
  889. // TODO validate reference number
  890. $this->referenceNumber = $referenceNumber;
  891. return true;
  892. }
  893. return false;
  894. }
  895. /**
  896. * Get the reference number
  897. *
  898. * @return string|bool The reference number or false if withReferenceNumber is false
  899. */
  900. public function getReferenceNumber()
  901. {
  902. if ($this->getWithReferenceNumber()) {
  903. return $this->referenceNumber;
  904. }
  905. return false;
  906. }
  907. /**
  908. * Set the banking customer id
  909. *
  910. * @param string $bankingCustomerId The banking customer id
  911. * @return bool True if successful, else false
  912. */
  913. public function setBankingCustomerId($bankingCustomerId)
  914. {
  915. if ($this->getWithBankingCustomerId()) {
  916. // TODO check length (exactly 6)
  917. $this->bankingCustomerId = $bankingCustomerId;
  918. return true;
  919. }
  920. return false;
  921. }
  922. /**
  923. * Get the banking customer id
  924. *
  925. * @return string|bool The banking customer id or false if withBankingCustomerId is false
  926. */
  927. public function getBankingCustomerId()
  928. {
  929. if ($this->getWithBankingCustomerId()) {
  930. return $this->bankingCustomerId;
  931. }
  932. return false;
  933. }
  934. /**
  935. * Sets the four lines of the payer
  936. *
  937. * @param string $payerLine1 The first line of the payer, e.g. "Hans Mustermann"
  938. * @param string $payerLine2 The second line of the payer, e.g. "Main Street 11"
  939. * @param string $payerLine3 The third line of the payer, e.g. "4052 Basel"
  940. * @param string $payerLine4 The fourth line of the payer, if needed
  941. * @return bool True if successful, else false
  942. */
  943. public function setPayerData($payerLine1, $payerLine2, $payerLine3 = '', $payerLine4 = '')
  944. {
  945. if (!$this->setPayerLine1($payerLine1) ||
  946. !$this->setPayerLine2($payerLine2) ||
  947. !$this->setPayerLine3($payerLine3) ||
  948. !$this->setPayerLine4($payerLine4)) {
  949. return false;
  950. }
  951. return true;
  952. }
  953. /**
  954. * Set the first line of the payer
  955. *
  956. * @param string $payerLine1 The first line of the payer, e.g. "Hans Mustermann"
  957. * @return bool True if successful, else false
  958. */
  959. protected function setPayerLine1($payerLine1)
  960. {
  961. if ($this->getWithPayer()) {
  962. $this->payerLine1 = $payerLine1;
  963. return true;
  964. }
  965. return false;
  966. }
  967. /**
  968. * Get the first line of the payer
  969. *
  970. * @return string|bool The first line of the payer or false if withPayer = false
  971. */
  972. public function getPayerLine1()
  973. {
  974. if ($this->getWithPayer()) {
  975. return $this->payerLine1;
  976. }
  977. return false;
  978. }
  979. /**
  980. * Set the second line of the payer
  981. *
  982. * @param string $payerLine2 The second line of the payer, e.g. "Main Street 11"
  983. * @return bool True if successful, else false
  984. */
  985. protected function setPayerLine2($payerLine2)
  986. {
  987. if ($this->getWithPayer()) {
  988. $this->payerLine2 = $payerLine2;
  989. return true;
  990. }
  991. return false;
  992. }
  993. /**
  994. * Get the second line of the payer
  995. *
  996. * @return string|bool The second line of the payer or false if withPayer = false
  997. */
  998. public function getPayerLine2()
  999. {
  1000. if ($this->getWithPayer()) {
  1001. return $this->payerLine2;
  1002. }
  1003. return false;
  1004. }
  1005. /**
  1006. * Set the third line of the payer
  1007. *
  1008. * @param string $payerLine3 The third line of the payer, e.g. "4052 Basel"
  1009. * @return bool True if successful, else false
  1010. */
  1011. protected function setPayerLine3($payerLine3)
  1012. {
  1013. if ($this->getWithPayer()) {
  1014. $this->payerLine3 = $payerLine3;
  1015. return true;
  1016. }
  1017. return false;
  1018. }
  1019. /**
  1020. * Get the third line of the payer
  1021. *
  1022. * @return string|bool The third line of the payer or false if withPayer = false
  1023. */
  1024. public function getPayerLine3()
  1025. {
  1026. if ($this->getWithPayer()) {
  1027. return $this->payerLine3;
  1028. }
  1029. return false;
  1030. }
  1031. /**
  1032. * Set the fourth line of the payer
  1033. *
  1034. * @param string $payerLine4 The fourth line of the payer, if needed
  1035. * @return bool True if successful, else false
  1036. */
  1037. protected function setPayerLine4($payerLine4)
  1038. {
  1039. if ($this->getWithPayer()) {
  1040. $this->payerLine4 = $payerLine4;
  1041. return true;
  1042. }
  1043. return false;
  1044. }
  1045. /**
  1046. * Get the fourth line of the payer
  1047. *
  1048. * @return string|bool The fourth line of the payer or false if withPayer = false
  1049. */
  1050. public function getPayerLine4()
  1051. {
  1052. if ($this->getWithPayer()) {
  1053. return $this->payerLine4;
  1054. }
  1055. return false;
  1056. }
  1057. /**
  1058. * Set the IBAN
  1059. *
  1060. * @param $iban String The IBAN
  1061. * @return bool True if successful, else false
  1062. */
  1063. public function setIban($iban)
  1064. {
  1065. if ($this->getWithIban()) {
  1066. // TODO plausible IBAN method http://www.six-interbank-clearing.com/de/tkicch_financialinstitutions_ibanipi.htm
  1067. // TODO check if to implement http://code.google.com/p/php-iban/ (composer!)
  1068. // TODO At least strip spaces (may be more?)
  1069. $this->iban = $iban;
  1070. return true;
  1071. }
  1072. return false;
  1073. }
  1074. /**
  1075. * Get the IBAN
  1076. *
  1077. * @return string|bool The IBAN or false if withIban is false
  1078. */
  1079. public function getIban()
  1080. {
  1081. if ($this->getWithIban()) {
  1082. return $this->iban;
  1083. }
  1084. return false;
  1085. }
  1086. /**
  1087. * Set payment reason lines
  1088. *
  1089. * @param string $paymentReasonLine1 The first line of the payment reason
  1090. * @param string $paymentReasonLine2 The second line of the payment reason
  1091. * @param string $paymentReasonLine3 The third line of the payment reason
  1092. * @param string $paymentReasonLine4 The fourth line of the payment reason
  1093. * @return bool True if successful, else false
  1094. */
  1095. public function setPaymentReasonData($paymentReasonLine1 = '',$paymentReasonLine2 = '',
  1096. $paymentReasonLine3 = '', $paymentReasonLine4 = '')
  1097. {
  1098. if (!$this->setPaymentReasonLine1($paymentReasonLine1) ||
  1099. !$this->setPaymentReasonLine2($paymentReasonLine2) ||
  1100. !$this->setPaymentReasonLine3($paymentReasonLine3) ||
  1101. !$this->setPaymentReasonLine4($paymentReasonLine4)) {
  1102. return false;
  1103. }
  1104. return true;
  1105. }
  1106. // TODO create a getPaymentReason with formatting parameter
  1107. /**
  1108. * Set the first line of the payment reason
  1109. *
  1110. * @param string $paymentReasonLine1 The first line of the payment reason
  1111. * @return bool True if successful, else false
  1112. */
  1113. protected function setPaymentReasonLine1($paymentReasonLine1)
  1114. {
  1115. if ($this->getWithPaymentReason()) {
  1116. $this->paymentReasonLine1 = $paymentReasonLine1;
  1117. return true;
  1118. }
  1119. return false;
  1120. }
  1121. /**
  1122. * Get the first line of the payment reason
  1123. *
  1124. * @return string|bool The first line of the payment reason or false if withPaymentReason = false
  1125. */
  1126. public function getPaymentReasonLine1()
  1127. {
  1128. if ($this->getWithPaymentReason()) {
  1129. return $this->paymentReasonLine1;
  1130. }
  1131. return false;
  1132. }
  1133. /**
  1134. * Set the second line of the payment reason
  1135. *
  1136. * @param string $paymentReasonLine2 The second line of the payment reason
  1137. * @return bool True if successful, else false
  1138. */
  1139. protected function setPaymentReasonLine2($paymentReasonLine2)
  1140. {
  1141. if ($this->getWithPaymentReason()) {
  1142. $this->paymentReasonLine2 = $paymentReasonLine2;
  1143. return true;
  1144. }
  1145. return false;
  1146. }
  1147. /**
  1148. * Get the second line of the payment reason
  1149. *
  1150. * @return string|bool The second line of the payment reason or false if withPaymentReason = false
  1151. */
  1152. public function getPaymentReasonLine2()
  1153. {
  1154. if ($this->getWithPaymentReason()) {
  1155. return $this->paymentReasonLine2;
  1156. }
  1157. return false;
  1158. }
  1159. /**
  1160. * Set the third line of the payment reason
  1161. *
  1162. * @param string $paymentReasonLine3 The third line of the payment reason
  1163. * @return bool True if successful, else false
  1164. */
  1165. protected function setPaymentReasonLine3($paymentReasonLine3)
  1166. {
  1167. if ($this->getWithPaymentReason()) {
  1168. $this->paymentReasonLine3 = $paymentReasonLine3;
  1169. return true;
  1170. }
  1171. return false;
  1172. }
  1173. /**
  1174. * Get the third line of the payment reason
  1175. *
  1176. * @return string|bool The third line of the payment reason or false if withPaymentReason = false
  1177. */
  1178. public function getPaymentReasonLine3()
  1179. {
  1180. if ($this->getWithPaymentReason()) {
  1181. return $this->paymentReasonLine3;
  1182. }
  1183. return false;
  1184. }
  1185. /**
  1186. * Set the fourth line of the payment reason
  1187. *
  1188. * @param string $paymentReasonLine4 The fourth line of the payment reason
  1189. * @return bool True if successful, else false
  1190. */
  1191. protected function setPaymentReasonLine4($paymentReasonLine4)
  1192. {
  1193. if ($this->getWithPaymentReason()) {
  1194. $this->paymentReasonLine4 = $paymentReasonLine4;
  1195. return true;
  1196. }
  1197. return false;
  1198. }
  1199. /**
  1200. * Get the fourth line of the payment reason
  1201. *
  1202. * @return string|bool The fourth line of the payment reason or false if withPaymentReason = false
  1203. */
  1204. public function getPaymentReasonLine4()
  1205. {
  1206. if ($this->getWithPaymentReason()) {
  1207. return $this->paymentReasonLine4;
  1208. }
  1209. return false;
  1210. }
  1211. /**
  1212. * Get complete reference number
  1213. *
  1214. * @param bool $formatted Should the returned reference be formatted in blocks of five (for better readability)
  1215. * @param bool $fillZeros Fill up with leading zeros, only applies to the case where no banking customer id is used
  1216. * @return string|bool The complete (with/without bank customer id), formatted reference number with check digit
  1217. * or false if withReferenceNumber is false
  1218. */
  1219. public function getCompleteReferenceNumber($formatted = true, $fillZeros = true)
  1220. {
  1221. if ($this->getWithReferenceNumber()) {
  1222. if ($this->getWithBankingCustomerId()) {
  1223. // Get reference number and fill with zeros
  1224. $completeReferenceNumber = str_pad($this->getReferenceNumber(), 20 ,'0', STR_PAD_LEFT);
  1225. // Add banking customer identification code
  1226. $completeReferenceNumber = $this->getBankingCustomerId() . $completeReferenceNumber;
  1227. } else {
  1228. if ($fillZeros) {
  1229. // Get reference number and fill with zeros
  1230. $completeReferenceNumber = str_pad($this->getReferenceNumber(), 26 ,'0', STR_PAD_LEFT);
  1231. } else{
  1232. // Get just reference number
  1233. $completeReferenceNumber = $this->getReferenceNumber();
  1234. }
  1235. }
  1236. // Add check digit
  1237. if ($this->getNotForPayment()) {
  1238. $completeReferenceNumber .= 'X';
  1239. } else {
  1240. $completeReferenceNumber .= $this->modulo10($completeReferenceNumber);
  1241. }
  1242. if ($formatted) {
  1243. $completeReferenceNumber = $this->breakStringIntoBlocks($completeReferenceNumber);
  1244. }
  1245. return $completeReferenceNumber;
  1246. }
  1247. return false;
  1248. }
  1249. /**
  1250. * Get IBAN number in human readable format.
  1251. * Not valid for electronic transactions.
  1252. *
  1253. * @return string|bool Formatted IBAN or false if withIban is false
  1254. * @link http://en.wikipedia.org/wiki/International_Bank_Account_Number#Practicalities
  1255. */
  1256. public function getFormattedIban()
  1257. {
  1258. if ($this->getWithIban()) {
  1259. return $this->breakStringIntoBlocks($this->getIban(), 4, false);
  1260. }
  1261. return false;
  1262. }
  1263. /**
  1264. * Get the full code line at the bottom of the SwissPaymentSlip
  1265. *
  1266. * @param bool $fillZeros Fill up with leading zeros
  1267. * @return string|bool Either the full code line or false if something was wrong
  1268. *
  1269. * @todo implement red slip support
  1270. */
  1271. public function getCodeLine($fillZeros = true)
  1272. {
  1273. $francs = $this->getAmountFrancs();
  1274. $cents = $this->getAmountCents();
  1275. $referenceNumber = $this->getCompleteReferenceNumber(false, $fillZeros);
  1276. if ($referenceNumber === false) {
  1277. return false;
  1278. }
  1279. $accountNumber = $this->getAccountDigits();
  1280. if ($accountNumber === false) {
  1281. return false;
  1282. }
  1283. if ($this->getWithAmount()) {
  1284. $francs = str_pad($francs, 8 ,'0', STR_PAD_LEFT);
  1285. $cents = str_pad($cents, 2 ,'0', STR_PAD_RIGHT);
  1286. $amountPrefix = '01';
  1287. $amountPart = $francs . $cents;
  1288. $amountCheck = $this->modulo10($amountPrefix . $amountPart);
  1289. } else {
  1290. $amountPrefix = '04';
  1291. $amountPart = '';
  1292. $amountCheck = '2';
  1293. }
  1294. if ($fillZeros) {
  1295. $referenceNumberPart = str_pad($referenceNumber, 27 ,'0', STR_PAD_LEFT);
  1296. } else {
  1297. $referenceNumberPart = $referenceNumber;
  1298. }
  1299. $accountNumberPart = substr($accountNumber,0, 2) .
  1300. str_pad(substr($accountNumber, 2), 7 ,'0', STR_PAD_LEFT);
  1301. if ($this->getNotForPayment()) {
  1302. $amountPrefix = 'XX';
  1303. $amountCheck = 'X';
  1304. }
  1305. $codeLine = sprintf('%s%s%s>%s+ %s>',
  1306. $amountPrefix,
  1307. $amountPart,
  1308. $amountCheck,
  1309. $referenceNumberPart,
  1310. $accountNumberPart
  1311. );
  1312. return $codeLine;
  1313. }
  1314. /**
  1315. * Clear the account of the two hyphens
  1316. */
  1317. protected function getAccountDigits()
  1318. {
  1319. if ($this->getWithAccountNumber()) {
  1320. if ($this->getNotForPayment()) {
  1321. return 'XXXXXXXXX';
  1322. }
  1323. $accountNumber = $this->getAccountNumber();
  1324. if ($accountNumber) {
  1325. $accountDigits = str_replace('-', '', $accountNumber, $replacedHyphens);
  1326. if ($replacedHyphens == 2) {
  1327. return $accountDigits;
  1328. }
  1329. }
  1330. }
  1331. return false;
  1332. }
  1333. /**
  1334. * Returns francs amount without cents
  1335. *
  1336. * @return bool|int Francs amount without cents
  1337. */
  1338. public function getAmountFrancs() {
  1339. $amount = $this->getAmount();
  1340. if ($this->getNotForPayment()) {
  1341. return 'XXXXXXXX';
  1342. }
  1343. if ($amount === false) {
  1344. return false;
  1345. }
  1346. $francs = intval($amount);
  1347. return $francs;
  1348. }
  1349. /**
  1350. * Returns zero filled, right padded, two digits long cents amount
  1351. *
  1352. * @return bool|string Amount of Cents, zero filled, right padded, two digits long
  1353. */
  1354. public function getAmountCents() {
  1355. $amount = $this->getAmount();
  1356. if ($this->getNotForPayment()) {
  1357. return 'XX';
  1358. }
  1359. if ($amount === false) {
  1360. return false;
  1361. }
  1362. $francs = intval($amount);
  1363. $cents = round(($amount - $francs) * 100);
  1364. return str_pad($cents, 2 ,'0', STR_PAD_RIGHT);
  1365. }
  1366. /**
  1367. * Creates Modulo10 recursive check digit
  1368. *
  1369. * @copyright As found on http://www.developers-guide.net/forums/5431,modulo10-rekursiv (thanks, dude!)
  1370. * @param string $number Number to create recursive check digit off
  1371. * @return int Recursive check digit
  1372. */
  1373. private function modulo10($number)
  1374. {
  1375. $next = 0;
  1376. for ($i=0; $i < strlen($number); $i++) {
  1377. $next = $this->moduloTable[($next + substr($number, $i, 1)) % 10];
  1378. }
  1379. return (10 - $next) % 10;
  1380. }
  1381. /**
  1382. * Returns a given string in blocks of a certain size
  1383. * Example: 000000000000000 becomes more readable 00000 00000 00000
  1384. *
  1385. * @param string $string To be formatted string
  1386. * @param int $blockSize Block size of choice
  1387. * @param bool $alignFromRight Right aligned, blocks are build from right
  1388. * @return string Given string divided in blocks of given block size separated by one space
  1389. */
  1390. private function breakStringIntoBlocks($string, $blockSize = 5, $alignFromRight = true)
  1391. {
  1392. // Lets reverse the string (because we want the block to be aligned from the right)
  1393. if ($alignFromRight) {
  1394. $string = strrev($string);
  1395. }
  1396. // Chop it into blocks
  1397. $string = trim(chunk_split($string, $blockSize, ' '));
  1398. // Re-reverse
  1399. if ($alignFromRight) {
  1400. $string = strrev($string);
  1401. }
  1402. return $string;
  1403. }
  1404. }