1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\eCurring\Http\Resource; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use LauLamanApps\eCurring\Http\Resource\Exception\UnPostableEntityException; |
9
|
|
|
use LauLamanApps\eCurring\Resource\Customer; |
10
|
|
|
use LauLamanApps\eCurring\Resource\Subscription; |
11
|
|
|
use LauLamanApps\eCurring\Resource\Transaction; |
12
|
|
|
|
13
|
|
|
final class CreateParser implements CreateParserInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @throws UnPostableEntityException |
17
|
|
|
*/ |
18
|
|
|
public function parse(Creatable $object): array |
19
|
|
|
{ |
20
|
|
|
if ($object instanceof Customer) { |
21
|
|
|
return $this->getCustomerPostData($object); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($object instanceof Subscription) { |
25
|
|
|
return $this->getSubscriptionPostData($object); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($object instanceof Transaction) { |
29
|
|
|
return $this->getTransactionPostData($object); |
30
|
|
|
} |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
private function getCustomerPostData(Customer $customer): array |
35
|
|
|
{ |
36
|
|
|
$data = [ |
37
|
|
|
'first_name' => $customer->getFirstName(), |
38
|
|
|
'last_name' => $customer->getLastName(), |
39
|
|
|
'email' => $customer->getEmail(), |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
if ($customer->getGender()) { |
43
|
|
|
$data['gender'] = $customer->getGender(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($customer->getMiddleName()) { |
47
|
|
|
$data['middle_name'] = $customer->getMiddleName(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($customer->getCompanyName()) { |
51
|
|
|
$data['company_name'] = $customer->getCompanyName(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($customer->getVatNumber()) { |
55
|
|
|
$data['vat_number'] = $customer->getVatNumber(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($customer->getPostalcode()) { |
59
|
|
|
$data['postalcode'] = $customer->getPostalcode(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($customer->getHouseNumber()) { |
63
|
|
|
$data['house_number'] = $customer->getHouseNumber(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($customer->getHouseNumberAdd()) { |
67
|
|
|
$data['house_number_add'] = $customer->getHouseNumberAdd(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($customer->getStreet()) { |
71
|
|
|
$data['street'] = $customer->getStreet(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($customer->getCity()) { |
75
|
|
|
$data['city'] = $customer->getCity(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($customer->getCountryCode()) { |
79
|
|
|
$data['country_iso2'] = $customer->getCountryCode(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($customer->getLanguage()) { |
83
|
|
|
$data['language'] = $customer->getLanguage(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($customer->getTelephone()) { |
87
|
|
|
$data['telephone '] = $customer->getTelephone(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return [ |
91
|
|
|
'data' => [ |
92
|
|
|
'type' => 'customer', |
93
|
|
|
'attributes' => $data |
94
|
|
|
] |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws UnPostableEntityException |
100
|
|
|
*/ |
101
|
|
|
private function getSubscriptionPostData(Subscription $subscription): array |
102
|
|
|
{ |
103
|
|
|
if (!$subscription->getCustomer()) { |
104
|
|
|
throw new UnPostableEntityException('Missing required field `customer`. Make sure you create a new subscription using Subscription::new()'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!$subscription->getSubscriptionPlan()) { |
108
|
|
|
throw new UnPostableEntityException('Missing required field `subscriptionPlan`. Make sure you create a new subscription using Subscription::new()'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$data = [ |
112
|
|
|
'customer_id' => $subscription->getCustomer()->getId(), |
|
|
|
|
113
|
|
|
'subscription_plan_id' => $subscription->getSubscriptionPlan()->getId(), |
|
|
|
|
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
if ($subscription->getMandate()) { |
117
|
|
|
$data['mandate_code'] = $subscription->getMandate()->getCode(); |
118
|
|
|
$data['mandate_accepted'] = $subscription->getMandate()->isAccepted(); |
119
|
|
|
$data['mandate_accepted_date'] = $subscription->getMandate()->getAcceptedDate()->format(DateTime::ATOM); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($subscription->getStartDate()) { |
123
|
|
|
$data['start_date '] = $subscription->getStartDate()->format(DateTime::ATOM); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($subscription->getStatus()) { |
127
|
|
|
$data['status '] = $subscription->getStatus()->getValue(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($subscription->getCancelDate()) { |
131
|
|
|
$data['cancel_date '] = $subscription->getCancelDate()->format(DateTime::ATOM); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($subscription->getResumeDate()) { |
135
|
|
|
$data['resume_date '] = $subscription->getResumeDate()->format(DateTime::ATOM); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($subscription->isConfirmationSent() !== null) { |
|
|
|
|
139
|
|
|
$data['confirmation_sent '] = $subscription->isConfirmationSent(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($subscription->getSubscriptionWebhookUrl()) { |
143
|
|
|
$data['subscription_webhook_url '] = $subscription->getSubscriptionWebhookUrl(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($subscription->getTransactionWebhookUrl()) { |
147
|
|
|
$data['transaction_webhook_url '] = $subscription->getTransactionWebhookUrl(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($subscription->getSuccessRedirectUrl()) { |
151
|
|
|
$data['success_redirect_url '] = $subscription->getSuccessRedirectUrl(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return [ |
155
|
|
|
'data' => [ |
156
|
|
|
'type' => 'subscription', |
157
|
|
|
'attributes' => $data |
158
|
|
|
] |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @throws UnPostableEntityException |
164
|
|
|
*/ |
165
|
|
|
private function getTransactionPostData(Transaction $transaction): array |
166
|
|
|
{ |
167
|
|
|
if (!$transaction->getSubscription()) { |
168
|
|
|
throw new UnPostableEntityException('No Subscription found on Transaction. Make sure you create a new transaction using Transaction::new()'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$data = [ |
172
|
|
|
'subscription_id' => $transaction->getSubscription()->getId(), |
173
|
|
|
'amount' => ((float)$transaction->getAmount()->getAmount()/100.00), |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
if ($transaction->getDueDate()) { |
177
|
|
|
$data['due_date'] = $transaction->getDueDate()->format(DateTime::ATOM); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $data; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: