|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace App\Services; |
|
20
|
|
|
|
|
21
|
|
|
use App\Entity\PaymentOrder; |
|
22
|
|
|
use RuntimeException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This service generates a payment reference for payment orders. |
|
26
|
|
|
*/ |
|
27
|
|
|
class PaymentReferenceGenerator |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Generates a payment Reference (via generatePaymentReference) and sets the reference value in the payment order. |
|
31
|
|
|
* Database is NOT flushed yet. |
|
32
|
|
|
* |
|
33
|
|
|
* @see PaymentReferenceGenerator::generatePaymentReference() |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setPaymentReference(PaymentOrder $paymentOrder): void |
|
36
|
|
|
{ |
|
37
|
|
|
$paymentOrder->getBankInfo() |
|
38
|
|
|
->setReference($this->generatePaymentReference($paymentOrder)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns a payment reference for the given payment order. |
|
43
|
|
|
* It contains the project name, the FSR name, and the funding ID (if existing) and the ZA-ID. |
|
44
|
|
|
* The length of all values are cut the way, that the reference does not exceed 140 chars. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function generatePaymentReference(PaymentOrder $paymentOrder): string |
|
47
|
|
|
{ |
|
48
|
|
|
//Max 140 chars are allowed for a payment reference |
|
49
|
|
|
//Format: [ProjectName 70] [FSR Name 45] [?Funding ID 11] ZA[PaymentOrder ID] |
|
50
|
|
|
|
|
51
|
|
|
//Project name |
|
52
|
|
|
$tmp = mb_strimwidth($paymentOrder->getProjectName(), 0, 70, ''); |
|
53
|
|
|
$tmp .= ' '; |
|
54
|
|
|
//FSR Name |
|
55
|
|
|
$tmp .= mb_strimwidth($paymentOrder->getDepartment()->getName(), 0, 45, ''); |
|
56
|
|
|
$tmp .= ' '; |
|
57
|
|
|
//Funding ID if existing |
|
58
|
|
|
if (!empty($paymentOrder->getFundingId())) { |
|
59
|
|
|
$tmp .= mb_strimwidth($paymentOrder->getFundingId(), 0, 11, ''); |
|
60
|
|
|
$tmp .= ' '; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
//ZA + ID |
|
64
|
|
|
if (null === $paymentOrder->getId()) { |
|
|
|
|
|
|
65
|
|
|
throw new RuntimeException('ID is null. You have to persist the PaymentOrder before using this function!'); |
|
66
|
|
|
} |
|
67
|
|
|
$tmp .= $paymentOrder->getIDString(); |
|
68
|
|
|
|
|
69
|
|
|
if (mb_strlen($tmp) > 140) { |
|
70
|
|
|
return new RuntimeException('Generated Payment reference exceeds 140 characters! This should not happen unless you have a very long ID...'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $tmp; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|