29
29
use Mollie \Api \Resources \Subscription ;
30
30
use Mollie \Payment \Api \MollieCustomerRepositoryInterface ;
31
31
use Mollie \Payment \Logger \MollieLogger ;
32
+ use Mollie \Payment \Model \Client \Payments ;
32
33
use Mollie \Payment \Model \Mollie ;
34
+ use Mollie \Payment \Service \Mollie \Order \LinkTransactionToOrder ;
33
35
use Mollie \Payment \Service \Mollie \ValidateMetadata ;
36
+ use Mollie \Payment \Service \Order \OrderCommentHistory ;
34
37
use Mollie \Payment \Service \Order \SendOrderEmails ;
35
38
use Mollie \Subscriptions \Config ;
39
+ use Mollie \Subscriptions \Service \Mollie \MollieSubscriptionApi ;
36
40
use Mollie \Subscriptions \Service \Mollie \RetryUsingOtherStoreViews ;
37
41
38
42
class Webhook extends Action implements CsrfAwareActionInterface
@@ -47,6 +51,11 @@ class Webhook extends Action implements CsrfAwareActionInterface
47
51
*/
48
52
private $ mollie ;
49
53
54
+ /**
55
+ * @var MollieSubscriptionApi
56
+ */
57
+ private $ mollieSubscriptionApi ;
58
+
50
59
/**
51
60
* @var MollieCustomerRepositoryInterface
52
61
*/
@@ -106,15 +115,27 @@ class Webhook extends Action implements CsrfAwareActionInterface
106
115
* @var MollieApiClient
107
116
*/
108
117
private $ api ;
118
+
109
119
/**
110
120
* @var ValidateMetadata
111
121
*/
112
122
private $ validateMetadata ;
113
123
124
+ /**
125
+ * @var LinkTransactionToOrder
126
+ */
127
+ private $ linkTransactionToOrder ;
128
+
129
+ /**
130
+ * @var OrderCommentHistory
131
+ */
132
+ private $ orderCommentHistory ;
133
+
114
134
public function __construct (
115
135
Context $ context ,
116
136
Config $ config ,
117
137
Mollie $ mollie ,
138
+ MollieSubscriptionApi $ mollieSubscriptionApi ,
118
139
MollieCustomerRepositoryInterface $ mollieCustomerRepository ,
119
140
CartManagementInterface $ cartManagement ,
120
141
CartRepositoryInterface $ cartRepository ,
@@ -126,12 +147,15 @@ public function __construct(
126
147
MollieLogger $ mollieLogger ,
127
148
SendOrderEmails $ sendOrderEmails ,
128
149
RetryUsingOtherStoreViews $ retryUsingOtherStoreViews ,
129
- ValidateMetadata $ validateMetadata
150
+ ValidateMetadata $ validateMetadata ,
151
+ LinkTransactionToOrder $ linkTransactionToOrder ,
152
+ OrderCommentHistory $ orderCommentHistory
130
153
) {
131
154
parent ::__construct ($ context );
132
155
133
156
$ this ->config = $ config ;
134
157
$ this ->mollie = $ mollie ;
158
+ $ this ->mollieSubscriptionApi = $ mollieSubscriptionApi ;
135
159
$ this ->mollieCustomerRepository = $ mollieCustomerRepository ;
136
160
$ this ->cartManagement = $ cartManagement ;
137
161
$ this ->cartRepository = $ cartRepository ;
@@ -144,6 +168,8 @@ public function __construct(
144
168
$ this ->sendOrderEmails = $ sendOrderEmails ;
145
169
$ this ->retryUsingOtherStoreViews = $ retryUsingOtherStoreViews ;
146
170
$ this ->validateMetadata = $ validateMetadata ;
171
+ $ this ->linkTransactionToOrder = $ linkTransactionToOrder ;
172
+ $ this ->orderCommentHistory = $ orderCommentHistory ;
147
173
}
148
174
149
175
public function execute ()
@@ -163,7 +189,7 @@ public function execute()
163
189
164
190
if ($ orders = $ this ->mollie ->getOrderIdsByTransactionId ($ id )) {
165
191
foreach ($ orders as $ orderId ) {
166
- $ this ->mollie ->processTransaction ($ orderId , ' webhook ' );
192
+ $ this ->mollie ->processTransaction ($ orderId , Payments:: TRANSACTION_TYPE_SUBSCRIPTION );
167
193
}
168
194
169
195
return $ this ->returnOkResponse ();
@@ -173,11 +199,18 @@ public function execute()
173
199
$ molliePayment = $ this ->getPayment ($ id );
174
200
$ subscription = $ this ->api ->subscriptions ->getForId ($ molliePayment ->customerId , $ molliePayment ->subscriptionId );
175
201
176
- $ customerId = $ this ->mollieCustomerRepository ->getByMollieCustomerId ($ molliePayment ->customerId )->getCustomerId ();
202
+ $ mollieCustomer = $ this ->mollieCustomerRepository ->getByMollieCustomerId ($ molliePayment ->customerId );
203
+ if (!$ mollieCustomer ) {
204
+ throw new \Exception (
205
+ 'Mollie customer with ID ' . $ molliePayment ->customerId . ' not found in database '
206
+ );
207
+ }
208
+
209
+ $ customerId = $ mollieCustomer ->getCustomerId ();
177
210
$ customer = $ this ->customerRepository ->getById ($ customerId );
178
211
179
212
$ cart = $ this ->getCart ($ customer );
180
- $ this ->addProduct ($ molliePayment , $ cart );
213
+ $ this ->addProduct ($ molliePayment , $ cart, $ subscription -> metadata -> quantity ?? 1 );
181
214
182
215
$ cart ->setBillingAddress ($ this ->formatAddress ($ this ->addressRepository ->getById ($ customer ->getDefaultBilling ())));
183
216
$ this ->setShippingAddress ($ customer , $ cart );
@@ -192,10 +225,14 @@ public function execute()
192
225
$ order ->getPayment ()->setAdditionalInformation ('subscription_created ' , $ subscription ->createdAt );
193
226
$ this ->orderRepository ->save ($ order );
194
227
195
- $ this ->mollie ->processTransactionForOrder ($ order , 'webhook ' );
228
+ $ this ->orderCommentHistory ->add ($ order , __ ('Order created by Mollie subscription %1 ' , $ molliePayment ->id ));
229
+
230
+ $ this ->linkTransactionToOrder ->execute ($ molliePayment ->id , $ order );
231
+
232
+ $ this ->mollie ->processTransactionForOrder ($ order , Payments::TRANSACTION_TYPE_SUBSCRIPTION );
196
233
return $ this ->returnOkResponse ();
197
234
} catch (ApiException $ exception ) {
198
- $ this ->mollieLogger ->addInfoLog ('ApiException occured while checking transaction ' , [
235
+ $ this ->mollieLogger ->addErrorLog ('ApiException occured while checking transaction ' , [
199
236
'id ' => $ id ,
200
237
'exception ' => $ exception ->__toString ()
201
238
]);
@@ -220,18 +257,19 @@ private function formatAddress(\Magento\Customer\Api\Data\AddressInterface $cust
220
257
$ address ->setVatId ($ customerAddress ->getVatId ());
221
258
$ address ->setSuffix ($ customerAddress ->getSuffix ());
222
259
$ address ->setPrefix ($ customerAddress ->getPrefix ());
260
+ $ address ->setRegionId ($ customerAddress ->getRegionId ());
223
261
224
262
return $ address ;
225
263
}
226
264
227
- private function addProduct (Payment $ mollieOrder , CartInterface $ cart )
265
+ private function addProduct (Payment $ mollieOrder , CartInterface $ cart, float $ quantity )
228
266
{
229
267
/** @var Subscription $subscription */
230
268
$ subscription = $ this ->api ->performHttpCallToFullUrl (MollieApiClient::HTTP_GET , $ mollieOrder ->_links ->subscription ->href );
231
269
$ sku = $ subscription ->metadata ->sku ;
232
270
$ product = $ this ->productRepository ->get ($ sku );
233
271
234
- $ cart ->addProduct ($ product );
272
+ $ cart ->addProduct ($ product, $ quantity );
235
273
}
236
274
237
275
private function setShippingAddress (CustomerInterface $ customer , CartInterface $ cart )
@@ -243,6 +281,20 @@ private function setShippingAddress(CustomerInterface $customer, CartInterface $
243
281
$ shippingAddress ->setCollectShippingRates (true );
244
282
$ shippingAddress ->collectShippingRates ();
245
283
$ shippingAddress ->setShippingMethod ($ this ->config ->getShippingMethod ());
284
+
285
+ // There are no rates available. Switch to the first available shipping method.
286
+ if ($ shippingAddress ->getShippingRateByCode ($ this ->config ->getShippingMethod ()) === false &&
287
+ count ($ shippingAddress ->getShippingRatesCollection ()->getItems ()) > 0
288
+ ) {
289
+ $ newMethod = $ shippingAddress ->getShippingRatesCollection ()->getFirstItem ()->getCode ();
290
+ $ shippingAddress ->setShippingMethod ($ newMethod );
291
+
292
+ $ this ->mollieLogger ->addInfoLog (
293
+ 'subscriptions ' ,
294
+ 'No rates available for ' . $ this ->config ->getShippingMethod () .
295
+ ', switched to ' . $ newMethod
296
+ );
297
+ }
246
298
}
247
299
248
300
private function getCart (CustomerInterface $ customer ): CartInterface
@@ -267,7 +319,7 @@ private function returnOkResponse()
267
319
public function getPayment (string $ id ): Payment
268
320
{
269
321
try {
270
- $ this ->api = $ this ->mollie -> getMollieApi ();
322
+ $ this ->api = $ this ->mollieSubscriptionApi -> loadByStore ();
271
323
272
324
return $ this ->api ->payments ->get ($ id );
273
325
} catch (ApiException $ exception ) {
0 commit comments