Passed
Push — master ( ca91ee...f1c38a )
by Florian
04:54 queued 02:49
created

PaymentService::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the vseth-musikzimmer-pay project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Service;
13
14
use App\Entity\PaymentRemainder;
15
use App\Entity\User;
16
use App\Enum\PaymentRemainderStatusType;
17
use App\Model\Bill;
18
use App\Model\TransactionInfo;
19
use App\Service\Interfaces\BillServiceInterface;
20
use App\Service\Interfaces\EmailServiceInterface;
21
use App\Service\Interfaces\PaymentServiceInterface;
22
use App\Service\Payment\Interfaces\PaymentProviderServiceInterface;
23
use App\Service\Payment\PayrexxService;
24
use Doctrine\Persistence\ManagerRegistry;
25
use Symfony\Component\Routing\RouterInterface;
26
27
class PaymentService implements PaymentServiceInterface
28
{
29
    /**
30
     * @var PaymentProviderServiceInterface
31
     */
32
    private $paymentProviderService;
33
34
    /**
35
     * @var BillServiceInterface
36
     */
37
    private $billService;
38
39
    /**
40
     * @var ManagerRegistry
41
     */
42
    private $doctrine;
43
44
    /**
45
     * @var EmailServiceInterface
46
     */
47
    private $emailService;
48
49
    /**
50
     * @var RouterInterface
51
     */
52
    private $router;
53
54
    /**
55
     * UserPaymentService constructor.
56
     *
57
     * @param EmailServiceInterface $emailService
58
     */
59
    public function __construct(PayrexxService $paymentService, ManagerRegistry $doctrine, Interfaces\EmailServiceInterface $emailService, RouterInterface $router, BillServiceInterface $billService)
60
    {
61
        $this->paymentProviderService = $paymentService;
62
        $this->doctrine = $doctrine;
63
        $this->emailService = $emailService;
64
        $this->router = $router;
65
        $this->billService = $billService;
66
    }
67
68
    public function sendPaymentRemainder(User $user)
69
    {
70
        $paymentRemainder = $this->doctrine->getRepository(PaymentRemainder::class)->findActive();
71
72
        $body = $paymentRemainder->getBody();
73
        $url = $this->router->generate('login_code', ['code' => $user->getAuthenticationCode()], RouterInterface::ABSOLUTE_URL);
74
        $body = str_replace('(url)', $url, $body);
75
        $name = $user->getGivenName() . ' ' . $user->getFamilyName();
76
        $body = str_replace('(name)', $name, $body);
77
78
        $this->emailService->sendEmail($user->getEmail(), $paymentRemainder->getSubject(), $body);
79
80
        if ($user->getPaymentRemainder() !== $paymentRemainder) {
81
            $user->setPaymentRemainderStatus(PaymentRemainderStatusType::SENT);
82
        }
83
        $user->setPaymentRemainder($paymentRemainder);
84
        $this->save($user);
85
    }
86
87
    /**
88
     * @throws \Payrexx\PayrexxException
89
     */
90
    public function startPayment(User $user, Bill $bill, string $url)
91
    {
92
        $paymentInfo = $this->paymentProviderService->startPayment($bill, $url);
93
94
        $user->writePaymentInfo($paymentInfo);
95
        $user->setPaymentRemainderStatus(PaymentRemainderStatusType::PAYMENT_STARTED);
96
        $this->save($user);
97
    }
98
99
    /**
100
     * @throws \Exception
101
     */
102
    public function refreshPaymentStatus(User $user)
103
    {
104
        if ($user->getPaymentRemainderStatus() === PaymentRemainderStatusType::PAYMENT_STARTED) {
105
            /** @var TransactionInfo $transactionInfo */
106
            $successful = $this->paymentProviderService->paymentSuccessful($user->getPaymentInfo(), $transactionInfo);
107
            if ($successful) {
108
                $user->setAmountPayed($transactionInfo->getAmount());
109
                $user->setTransactionId($transactionInfo->getId());
110
                $user->setPaymentRemainderStatus(PaymentRemainderStatusType::PAYMENT_SUCCESSFUL);
111
                $this->save($user);
112
            }
113
        }
114
    }
115
116
    /**
117
     * @throws \Payrexx\PayrexxException
118
     * @throws \Exception
119
     */
120
    public function closeInvoice(User $user)
121
    {
122
        $this->paymentProviderService->closePayment($user->getPaymentInfo());
123
        $user->setPaymentRemainderStatus(PaymentRemainderStatusType::PAYMENT_ABORTED);
124
        $user->clearPaymentInfo();
125
126
        $this->save($user);
127
    }
128
129
    private function save(User $user)
130
    {
131
        $manager = $this->doctrine->getManager();
132
        $manager->persist($user);
133
        $manager->flush();
134
    }
135
}
136