|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Carl Owens ([email protected]) |
|
4
|
|
|
* Company: PartFire Ltd (www.partfire.co.uk) |
|
5
|
|
|
* Copyright © 2017 PartFire Ltd. All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* User: Carl Owens |
|
8
|
|
|
* Date: 18/01/2017 |
|
9
|
|
|
* Time: 16:18 |
|
10
|
|
|
* File: CardQuery.php |
|
11
|
|
|
**/ |
|
12
|
|
|
|
|
13
|
|
|
namespace PartFire\MangoPayBundle\Models\Adapters; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
use MangoPay\CardRegistration; |
|
17
|
|
|
use MangoPay\CardRegistrationStatus; |
|
18
|
|
|
use MangoPay\MangoPayApi; |
|
19
|
|
|
use PartFire\MangoPayBundle\Models\CardRegistrationsQueryInterface; |
|
20
|
|
|
use PartFire\MangoPayBundle\Models\DTOs\CardRegistration as CardRegistrationDto; |
|
21
|
|
|
use PartFire\MangoPayBundle\Models\DTOs\Translators\CardTranslator; |
|
22
|
|
|
use Symfony\Bridge\Monolog\Logger; |
|
23
|
|
|
use PartFire\MangoPayBundle\Models\Exception as PartFireException; |
|
24
|
|
|
use MangoPay\Libraries\Exception; |
|
25
|
|
|
use MangoPay\Libraries\ResponseException; |
|
26
|
|
|
|
|
27
|
|
|
class CardRegistrationsQuery extends AbstractQuery implements CardRegistrationsQueryInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var CardTranslator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $cardTranslator; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
$clientId, |
|
36
|
|
|
$clientPassword, |
|
37
|
|
|
$baseUrl, |
|
38
|
|
|
MangoPayApi $mangoPayApi, |
|
39
|
|
|
Logger $logger, |
|
40
|
|
|
CardTranslator $cardTranslator |
|
41
|
|
|
) { |
|
42
|
|
|
parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger); |
|
43
|
|
|
$this->cardTranslator = $cardTranslator; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function create(string $userId, string $currency, string $cardType, string $tag): ? CardRegistrationDto |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
$CardRegistration = new CardRegistration(); |
|
50
|
|
|
$CardRegistration->Tag = $tag; |
|
51
|
|
|
$CardRegistration->UserId = $userId; |
|
52
|
|
|
$CardRegistration->Currency = $currency; |
|
53
|
|
|
$CardRegistration->CardType = $cardType; |
|
54
|
|
|
|
|
55
|
|
|
$cardRegistration = $this->mangoPayApi->CardRegistrations->Create($CardRegistration); |
|
56
|
|
|
return $this->cardTranslator->translateMangoCardRegistrationDataToDto($cardRegistration); |
|
57
|
|
|
} catch (ResponseException $e) { |
|
58
|
|
|
$this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]); |
|
59
|
|
|
throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
|
60
|
|
|
} catch (Exception $e) { |
|
61
|
|
|
$this->logger->addError($e->getMessage()); |
|
62
|
|
|
throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function update(string $cardRegisteredId, string $registrationData): ? CardRegistrationDto |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$cardRegister = $this->mangoPayApi->CardRegistrations->Get($cardRegisteredId); |
|
70
|
|
|
if ($cardRegister instanceof CardRegistration) { |
|
71
|
|
|
$cardRegister->RegistrationData = 'data=' . $registrationData; |
|
72
|
|
|
$updatedCardRegister = $this->mangoPayApi->CardRegistrations->Update($cardRegister); |
|
73
|
|
|
|
|
74
|
|
|
if ($updatedCardRegister instanceof CardRegistration) { |
|
75
|
|
|
if ($updatedCardRegister->Status != CardRegistrationStatus::Validated || |
|
76
|
|
|
!isset($updatedCardRegister->CardId)) { |
|
77
|
|
|
$this->logger->addCritical("Cannot create card. Payment has not been created."); |
|
78
|
|
|
throw new PartFireException("Cannot create card. Payment has not been created."); |
|
79
|
|
|
} |
|
80
|
|
|
return $this->cardTranslator->translateMangoCardRegistrationDataToDto($updatedCardRegister); |
|
81
|
|
|
} |
|
82
|
|
|
$this->logger->addCritical("Card Registration failed when trying to update via API."); |
|
83
|
|
|
throw new PartFireException("Card Registration failed when trying to update via API."); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->logger->addCritical("Card Registration ID: $cardRegisteredId not found when querying API."); |
|
87
|
|
|
throw new PartFireException("Card Registration ID: $cardRegisteredId not found when querying API."); |
|
88
|
|
|
} catch (ResponseException $e) { |
|
89
|
|
|
$this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]); |
|
90
|
|
|
throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
|
91
|
|
|
} catch (Exception $e) { |
|
92
|
|
|
$this->logger->addError($e->getMessage()); |
|
93
|
|
|
throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function get(string $cardRegisteredId): ? CardRegistrationDto |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
|
|
$cardRegister = $this->mangoPayApi->CardRegistrations->Get($cardRegisteredId); |
|
101
|
|
|
if ($cardRegister instanceof CardRegistration) { |
|
102
|
|
|
return $this->cardTranslator->translateMangoCardRegistrationDataToDto($cardRegister); |
|
103
|
|
|
} |
|
104
|
|
|
$this->logger->addCritical("Card Registration ID: $cardRegisteredId not found when querying API."); |
|
105
|
|
|
throw new PartFireException("Card Registration ID: $cardRegisteredId not found when querying API."); |
|
106
|
|
|
} catch (ResponseException $e) { |
|
107
|
|
|
$this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]); |
|
108
|
|
|
throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
|
109
|
|
|
} catch (Exception $e) { |
|
110
|
|
|
$this->logger->addError($e->getMessage()); |
|
111
|
|
|
throw new PartFireException($e->getMessage(), $e->getCode(), $e); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|