|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Query; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\Output\BankingInfosType; |
|
8
|
|
|
use Application\Api\Scalar\MoneyType; |
|
9
|
|
|
use Application\Model\User; |
|
10
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
|
11
|
|
|
use Ecodev\Felix\Service\Bvr; |
|
12
|
|
|
use GraphQL\Type\Definition\Type; |
|
13
|
|
|
use Laminas\Http\Client; |
|
14
|
|
|
use Laminas\Http\Client\Exception\RuntimeException; |
|
15
|
|
|
use Laminas\Http\Request; |
|
16
|
|
|
use Laminas\Http\Response; |
|
17
|
|
|
use Money\Money; |
|
18
|
|
|
|
|
19
|
|
|
abstract class BankingInfos implements FieldInterface |
|
20
|
|
|
{ |
|
21
|
1 |
|
public static function build(): iterable |
|
22
|
|
|
{ |
|
23
|
1 |
|
yield 'bankingInfos' => fn () => [ |
|
24
|
1 |
|
'type' => Type::nonNull(_types()->get(BankingInfosType::class)), |
|
25
|
1 |
|
'description' => 'Info to top-up the current user account by bank transfer', |
|
26
|
1 |
|
'args' => [ |
|
27
|
1 |
|
'user' => Type::nonNull(_types()->getId(User::class)), |
|
28
|
1 |
|
'amount' => _types()->get(MoneyType::class), |
|
29
|
1 |
|
], |
|
30
|
1 |
|
'resolve' => function ($root, array $args): array { |
|
31
|
|
|
global $container; |
|
32
|
|
|
$config = $container->get('config'); |
|
33
|
|
|
$banking = $config['banking']; |
|
34
|
|
|
$iban = $banking['iban']; |
|
35
|
|
|
$paymentTo = $banking['paymentTo']; |
|
36
|
|
|
$paymentFor = $banking['paymentFor']; |
|
37
|
|
|
|
|
38
|
|
|
$user = $args['user']->getEntity(); |
|
39
|
|
|
$amount = $args['amount'] ?? null; |
|
40
|
|
|
|
|
41
|
|
|
$referenceNumber = Bvr::getReferenceNumber('000000', (string) $user->getId()); |
|
42
|
|
|
|
|
43
|
|
|
$result = [ |
|
44
|
|
|
'iban' => $iban, |
|
45
|
|
|
'paymentTo' => $paymentTo, |
|
46
|
|
|
'paymentFor' => $paymentFor, |
|
47
|
|
|
'referenceNumber' => $referenceNumber, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
$qrCodeField = self::qrBill($user, $amount, $iban, $paymentFor, false); |
|
51
|
|
|
$qrBillField = self::qrBill($user, $amount, $iban, $paymentFor, true); |
|
52
|
|
|
$result = array_merge($result, $qrCodeField, $qrBillField); |
|
53
|
|
|
|
|
54
|
|
|
return $result; |
|
55
|
1 |
|
}, |
|
56
|
1 |
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Lazy resolve qrBill or qrCode fields for banking infos query. |
|
61
|
|
|
*/ |
|
62
|
|
|
protected static function qrBill(User $user, ?Money $amount, string $iban, string $paymentFor, bool $paymentPart): array |
|
63
|
|
|
{ |
|
64
|
|
|
$resolve = function () use ($user, $amount, $iban, $paymentFor, $paymentPart): ?string { |
|
65
|
|
|
global $container; |
|
66
|
|
|
|
|
67
|
|
|
if (!Bvr::isQrIban($iban)) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$config = $container->get('config'); |
|
72
|
|
|
$request = new Request(); |
|
73
|
|
|
$request->setUri('https://qrbill.ecodev.ch/qr-code'); |
|
74
|
|
|
$request->getHeaders()->addHeaders([ |
|
75
|
|
|
'Content-Type' => 'application/json', |
|
76
|
|
|
'Accept' => $paymentPart ? 'application/json application/pdf' : 'application/json image/svg+xml', |
|
77
|
|
|
'Referer' => 'https://' . $config['hostname'], |
|
78
|
|
|
]); |
|
79
|
|
|
$request->setMethod(Request::METHOD_POST); |
|
80
|
|
|
|
|
81
|
|
|
$creditorLines = explode("\n", (string) $paymentFor); |
|
82
|
|
|
$creditor = [ |
|
83
|
|
|
'name' => $creditorLines[0], |
|
84
|
|
|
]; |
|
85
|
|
|
if (count($creditorLines) > 2) { |
|
86
|
|
|
$creditor['addressLine1'] = $creditorLines[1]; |
|
87
|
|
|
$creditor['addressLine2'] = $creditorLines[2]; |
|
88
|
|
|
} else { |
|
89
|
|
|
$creditor['addressLine2'] = $creditorLines[1]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$attrs = [ |
|
93
|
|
|
'creditor' => $creditor, |
|
94
|
|
|
'customerIdentification' => null, |
|
95
|
|
|
'referenceNumber' => (string) $user->getId(), |
|
96
|
|
|
'iban' => $iban, |
|
97
|
|
|
'amount' => $amount, |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
if (!empty($user->getPostcode()) && !empty($user->getLocality())) { |
|
101
|
|
|
$debtor = [ |
|
102
|
|
|
'name' => $user->getName(), |
|
103
|
|
|
'addressLine2' => implode(' ', array_filter([$user->getPostcode(), $user->getLocality()])), |
|
104
|
|
|
]; |
|
105
|
|
|
$attrs['ultimateDebtor'] = $debtor; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$request->setContent(json_encode($attrs)); |
|
109
|
|
|
$client = new Client(); |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
/** @var Response $response */ |
|
113
|
|
|
$response = $client->dispatch($request); |
|
114
|
|
|
if (!$response->isSuccess()) { |
|
115
|
|
|
_log()->error('Erreur de génération du QR code: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); |
|
116
|
|
|
|
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
} catch (RuntimeException $e) { |
|
120
|
|
|
_log()->error($e->getMessage(), $attrs); |
|
121
|
|
|
|
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$content = json_decode($response->getBody()); |
|
126
|
|
|
|
|
127
|
|
|
return $content->qrcode; |
|
128
|
|
|
}; |
|
129
|
|
|
|
|
130
|
|
|
return [ |
|
131
|
|
|
$paymentPart ? 'qrBill' : 'qrCode' => $resolve, |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|