Passed
Push — main ( 9fe0be...bbd723 )
by Iain
04:40
created

AddCardByTokenDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
rs 10
c 1
b 0
f 1
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\PaymentDetails;
16
17
use Obol\Model\Customer as ObolCustomer;
18
use Obol\Provider\ProviderInterface;
19
use Parthenon\Billing\Entity\CustomerInterface;
20
use Parthenon\Billing\Obol\CustomerConverterInterface;
21
use Parthenon\Billing\Repository\CustomerRepositoryInterface;
22
23
class AddCardByTokenDriver implements AddCardByTokenDriverInterface
24
{
25
    public function __construct(
26
        private ProviderInterface $provider,
27
        private CustomerRepositoryInterface $customerRepository,
28
        private CustomerConverterInterface $customerConverter,
29
    ) {
30
    }
31
32
    public function startTokenProcess(CustomerInterface $customer): string
33
    {
34
        $billingDetails = $this->customerConverter->convertToBillingDetails($customer);
35
        if (!$customer->hasExternalCustomerReference()) {
36
            $obolCustomer = new ObolCustomer();
37
            $obolCustomer->setEmail($customer->getBillingEmail());
38
            $obolCustomer->setAddress($billingDetails->getAddress());
39
            $customerCreation = $this->provider->customers()->create($obolCustomer);
40
41
            $customer->setExternalCustomerReference($customerCreation->getReference());
42
            $customer->setPaymentProviderDetailsUrl($customerCreation->getDetailsUrl());
43
        }
44
45
        $tokenData = $this->provider->payments()->startFrontendCreateCardOnFile($billingDetails);
46
        if ($tokenData->hasCustomerCreation()) {
47
            $customer->setPaymentProviderDetailsUrl($tokenData->getCustomerCreation()->getDetailsUrl());
48
            $customer->setExternalCustomerReference($tokenData->getCustomerReference());
49
        }
50
        $this->customerRepository->save($customer);
51
52
        return $tokenData->getToken();
53
    }
54
}
55