Passed
Push — main ( 0d1c63...8584cf )
by Iain
04:42
created

RefundManager::issueFullRefundForSubscription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 20
rs 9.7998
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\Refund;
16
17
use Obol\Model\Refund\IssueRefund;
18
use Obol\Provider\ProviderInterface;
19
use Parthenon\Billing\Entity\BillingAdminInterface;
20
use Parthenon\Billing\Entity\Refund;
21
use Parthenon\Billing\Entity\Subscription;
22
use Parthenon\Billing\Repository\PaymentRepositoryInterface;
23
use Parthenon\Billing\Repository\RefundRepositoryInterface;
24
25
class RefundManager implements RefundManagerInterface
26
{
27
    public function __construct(
28
        private ProviderInterface $provider,
29
        private PaymentRepositoryInterface $paymentRepository,
30
        private RefundRepositoryInterface $refundRepository,
31
    ) {
32
    }
33
34
    public function issueFullRefundForSubscription(Subscription $subscription, BillingAdminInterface $billingAdmin): void
35
    {
36
        $payment = $this->paymentRepository->getLastPaymentForSubscription($subscription);
37
38
        $issueRefund = new IssueRefund();
39
        $issueRefund->setAmount($subscription->getMoneyAmount());
40
        $issueRefund->setPaymentExternalReference($payment->getPaymentReference());
41
42
        $refund = $this->provider->refunds()->issueRefund($issueRefund);
43
44
        $refundEn = new Refund();
45
        $refundEn->setAmount($refund->getAmount());
46
        $refundEn->setCurrency($refund->getCurrency());
47
        $refundEn->setExternalReference($refund->getId());
48
        $refundEn->setStatus('refunded');
49
        $refundEn->setBillingAdmin($billingAdmin);
50
        $refundEn->setPayment($payment);
51
        $refundEn->setCustomer($subscription->getCustomer());
52
53
        $this->refundRepository->save($refundEn);
54
    }
55
}
56