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 $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws UnPostableEntityException |
95
|
|
|
*/ |
96
|
|
|
private function getSubscriptionPostData(Subscription $subscription): array |
97
|
|
|
{ |
98
|
|
|
if (!$subscription->getCustomer()) { |
99
|
|
|
throw new UnPostableEntityException('Missing required field `customer`. Make sure you create a new subscription using Subscription::new()'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$subscription->getSubscriptionPlan()) { |
103
|
|
|
throw new UnPostableEntityException('Missing required field `subscriptionPlan`. Make sure you create a new subscription using Subscription::new()'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$data = [ |
107
|
|
|
'customer_id' => $subscription->getCustomer()->getId(), |
|
|
|
|
108
|
|
|
'subscription_plan_id' => $subscription->getSubscriptionPlan()->getId(), |
|
|
|
|
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
if ($subscription->getMandate()) { |
112
|
|
|
$data['mandate_code'] = $subscription->getMandate()->getCode(); |
113
|
|
|
$data['mandate_accepted'] = $subscription->getMandate()->isAccepted(); |
114
|
|
|
$data['mandate_accepted_date'] = $subscription->getMandate()->getAcceptedDate()->format(DateTime::ATOM); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($subscription->getStartDate()) { |
118
|
|
|
$data['start_date '] = $subscription->getStartDate()->format(DateTime::ATOM); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($subscription->getStatus()) { |
122
|
|
|
$data['status '] = $subscription->getStatus()->getValue(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($subscription->getCancelDate()) { |
126
|
|
|
$data['cancel_date '] = $subscription->getCancelDate()->format(DateTime::ATOM); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($subscription->getResumeDate()) { |
130
|
|
|
$data['resume_date '] = $subscription->getResumeDate()->format(DateTime::ATOM); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($subscription->isConfirmationSent() !== null) { |
|
|
|
|
134
|
|
|
$data['confirmation_sent '] = $subscription->isConfirmationSent(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($subscription->getSubscriptionWebhookUrl()) { |
138
|
|
|
$data['subscription_webhook_url '] = $subscription->getSubscriptionWebhookUrl(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($subscription->getTransactionWebhookUrl()) { |
142
|
|
|
$data['transaction_webhook_url '] = $subscription->getTransactionWebhookUrl(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($subscription->getSuccessRedirectUrl()) { |
146
|
|
|
$data['success_redirect_url '] = $subscription->getSuccessRedirectUrl(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $data; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @throws UnPostableEntityException |
154
|
|
|
*/ |
155
|
|
|
private function getTransactionPostData(Transaction $transaction): array |
156
|
|
|
{ |
157
|
|
|
if (!$transaction->getSubscription()) { |
158
|
|
|
throw new UnPostableEntityException('No Subscription found on Transaction. Make sure you create a new transaction using Transaction::new()'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$data = [ |
162
|
|
|
'subscription_id' => $transaction->getSubscription()->getId(), |
163
|
|
|
'amount' => ((float)$transaction->getAmount()->getAmount()/100.00), |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
if ($transaction->getDueDate()) { |
167
|
|
|
$data['due_date'] = $transaction->getDueDate()->format(DateTime::ATOM); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $data; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
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: