|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright Iain Cambridge 2020-2023. |
|
7
|
|
|
* |
|
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
|
9
|
|
|
* |
|
10
|
|
|
* Change Date: TBD ( 3 years after 2.2.0 release ) |
|
11
|
|
|
* |
|
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Parthenon\Billing\Subscription; |
|
16
|
|
|
|
|
17
|
|
|
use Obol\Model\Events\AbstractCharge; |
|
18
|
|
|
use Obol\Model\PaymentDetails; |
|
19
|
|
|
use Obol\Provider\ProviderInterface; |
|
20
|
|
|
use Parthenon\Billing\Entity\Payment; |
|
21
|
|
|
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface; |
|
22
|
|
|
use Parthenon\Common\Exception\NoEntityFoundException; |
|
23
|
|
|
|
|
24
|
|
|
class PaymentLinker implements PaymentLinkerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
private ProviderInterface $provider, |
|
28
|
|
|
private SubscriptionRepositoryInterface $subscriptionRepository, |
|
29
|
|
|
) { |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function linkPaymentDetailsToSubscription(Payment $payment, PaymentDetails $charge): void |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$charge->getInvoiceReference()) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$invoice = $this->provider->invoices()->fetch($charge->getInvoiceReference()); |
|
39
|
|
|
|
|
40
|
|
|
if (!$invoice) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
foreach ($invoice->getLines() as $line) { |
|
45
|
|
|
if (!$line->hasReferences()) { |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
try { |
|
49
|
|
|
$subscription = $this->subscriptionRepository->getForMainAndChildExternalReference($line->getMainSubscriptionReference(), $line->getChildSubscriptionReference()); |
|
50
|
|
|
$payment->addSubscription($subscription); |
|
51
|
|
|
} catch (NoEntityFoundException $e) { |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function linkToSubscription(Payment $payment, AbstractCharge $charge): void |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$charge->hasExternalInvoiceId()) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$invoice = $this->provider->invoices()->fetch($charge->getExternalInvoiceId()); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
if (!$invoice) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($invoice->getLines() as $line) { |
|
69
|
|
|
if (!$line->hasReferences()) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
try { |
|
73
|
|
|
$subscription = $this->subscriptionRepository->getForMainAndChildExternalReference($line->getMainSubscriptionReference(), $line->getChildSubscriptionReference()); |
|
74
|
|
|
$payment->addSubscription($subscription); |
|
75
|
|
|
} catch (NoEntityFoundException $e) { |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|