|
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\Webhook\Handler; |
|
16
|
|
|
|
|
17
|
|
|
use Obol\Model\Events\ChargeSucceeded; |
|
18
|
|
|
use Obol\Model\Events\EventInterface; |
|
19
|
|
|
use Parthenon\Billing\Enum\PaymentStatus; |
|
20
|
|
|
use Parthenon\Billing\Exception\InvalidEventException; |
|
21
|
|
|
use Parthenon\Billing\Obol\PaymentFactoryInterface; |
|
22
|
|
|
use Parthenon\Billing\Repository\CustomerRepositoryInterface; |
|
23
|
|
|
use Parthenon\Billing\Repository\PaymentRepositoryInterface; |
|
24
|
|
|
use Parthenon\Billing\Webhook\HandlerInterface; |
|
25
|
|
|
use Parthenon\Common\Exception\NoEntityFoundException; |
|
26
|
|
|
|
|
27
|
|
|
class ChargeSucceededHandler implements HandlerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
private PaymentRepositoryInterface $paymentRepository, |
|
31
|
|
|
private CustomerRepositoryInterface $customerRepository, |
|
32
|
|
|
private PaymentFactoryInterface $paymentFactory, |
|
33
|
|
|
) { |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function supports(EventInterface $event): bool |
|
37
|
|
|
{ |
|
38
|
|
|
return $event instanceof ChargeSucceeded; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param ChargeSucceeded $event |
|
43
|
|
|
*/ |
|
44
|
|
|
public function handle(EventInterface $event): void |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
$payment = $this->paymentRepository->getPaymentForReference($event->getPaymentReference()); |
|
|
|
|
|
|
48
|
|
|
} catch (NoEntityFoundException $exception) { |
|
49
|
|
|
$payment = $this->paymentFactory->fromChargeEvent($event); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
$payment->setStatus(PaymentStatus::COMPLETED); |
|
52
|
|
|
$payment->setUpdatedAt(new \DateTime('now')); |
|
53
|
|
|
/* |
|
54
|
|
|
try { |
|
55
|
|
|
$customer = $this->customerRepository->getByExternalReference($event->getExternalCustomerId()); |
|
56
|
|
|
} catch (NoEntityFoundException $e) { |
|
57
|
|
|
throw new InvalidEventException('Customer not found', previous: $e); |
|
58
|
|
|
} |
|
59
|
|
|
$payment->setCustomer($customer); |
|
60
|
|
|
*/ |
|
61
|
|
|
$this->paymentRepository->save($payment); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|