4
4
5
5
use Payum \Core \Bridge \Spl \ArrayObject ;
6
6
use PostFinanceCheckout \Sdk \ApiClient ;
7
- use PostFinanceCheckout \Sdk \Model \AddressCreate ;
8
7
use PostFinanceCheckout \Sdk \Model \LineItemCreate ;
9
8
use PostFinanceCheckout \Sdk \Model \LineItemType ;
10
9
use PostFinanceCheckout \Sdk \Model \ModelInterface ;
11
10
use PostFinanceCheckout \Sdk \Model \Transaction ;
12
11
use PostFinanceCheckout \Sdk \Model \TransactionCreate ;
12
+ use PostFinanceCheckout \Sdk \ObjectSerializer ;
13
13
use PostFinanceCheckout \Sdk \Service \TransactionIframeService ;
14
14
use PostFinanceCheckout \Sdk \Service \TransactionLightboxService ;
15
15
use PostFinanceCheckout \Sdk \Service \TransactionPaymentPageService ;
@@ -24,6 +24,11 @@ public function __construct(array $options)
24
24
$ this ->options = $ options ;
25
25
}
26
26
27
+ public function getIntegrationType (): string
28
+ {
29
+ return $ this ->options ['integrationType ' ];
30
+ }
31
+
27
32
public function getPaymentPageUrl (int $ transactionId ): string
28
33
{
29
34
return $ this ->getTransactionPaymentPageService ()->paymentPageUrl ($ this ->getSpaceId (), $ transactionId );
@@ -45,39 +50,27 @@ public function getIframeUrl(int $transactionId): string
45
50
46
51
public function prepareTransaction (ArrayObject $ details , string $ returnUrl , string $ notifyTokenHash ): Transaction
47
52
{
48
- $ transactionExtender = [];
53
+ $ transactionConfig = [];
54
+ $ detailsArray = $ details ->toUnsafeArray ();
55
+
49
56
if ($ details ->offsetExists ('transaction_extender ' )) {
50
- $ transactionExtender = $ details ['transaction_extender ' ];
57
+ $ transactionConfig = $ detailsArray ['transaction_extender ' ];
58
+ }
59
+
60
+ if (array_key_exists ('transactionCreate ' , $ transactionConfig )) {
61
+ $ transactionCreateObject = ObjectSerializer::deserialize ($ transactionConfig ['transactionCreate ' ], TransactionCreate::class);
62
+ } else {
63
+ $ transactionCreateObject = new TransactionCreate ();
51
64
}
52
65
53
- $ shippingAddress = $ this ->createPostFinanceModel (AddressCreate::class, $ transactionExtender ['shippingAddress ' ] ?? []);
54
- $ billingAddress = $ this ->createPostFinanceModel (AddressCreate::class, $ transactionExtender ['billingAddress ' ] ?? []);
55
-
56
- $ lineItem = $ this ->createPostFinanceModel (LineItemCreate::class, [
57
- 'quantity ' => 1 ,
58
- 'amountIncludingTax ' => $ transactionExtender ['amount ' ] / 100 ,
59
- 'taxes ' => $ transactionExtender ['totalTaxes ' ] ?? null ,
60
- 'uniqueId ' => $ transactionExtender ['id ' ],
61
- 'name ' => $ transactionExtender ['id ' ],
62
- 'sku ' => $ transactionExtender ['id ' ],
63
- 'type ' => LineItemType::PRODUCT ,
64
- ]);
65
-
66
- $ transaction = $ this ->createPostFinanceModel (TransactionCreate::class, [
67
- 'currency ' => $ transactionExtender ['currency ' ] ?? null ,
68
- 'language ' => $ transactionExtender ['language ' ] ?? null ,
69
- 'lineItems ' => [$ lineItem ],
70
- 'autoConfirmationEnabled ' => true ,
71
- 'failedUrl ' => $ this ->getFailedUrl ($ returnUrl ),
72
- 'successUrl ' => $ this ->getSuccessUrl ($ returnUrl ),
73
- 'shippingAddress ' => $ shippingAddress ,
74
- 'billingAddress ' => $ billingAddress ,
75
- 'metaData ' => ['paymentToken ' => $ notifyTokenHash ],
76
- 'allowedPaymentMethodBrands ' => $ transactionExtender ['allowedPaymentMethodBrands ' ] ?? [],
77
- 'allowedPaymentMethodConfigurations ' => $ transactionExtender ['allowedPaymentMethodConfigurations ' ] ?? [],
78
- ]);
79
-
80
- return $ this ->getTransactionService ()->create ($ this ->getSpaceId (), $ transaction );
66
+ $ this ->setDefaultsToTransactionCreateObject (
67
+ $ transactionCreateObject ,
68
+ $ transactionConfig ,
69
+ $ returnUrl ,
70
+ $ notifyTokenHash
71
+ );
72
+
73
+ return $ this ->getTransactionService ()->create ($ this ->getSpaceId (), $ transactionCreateObject );
81
74
}
82
75
83
76
public function getEntity ($ entityId ): ModelInterface
@@ -173,20 +166,67 @@ protected function getSpaceId(): mixed
173
166
return $ this ->options ['spaceId ' ];
174
167
}
175
168
176
- public function getIntegrationType (): string
177
- {
178
- return $ this ->options ['integrationType ' ];
179
- }
169
+ private function setDefaultsToTransactionCreateObject (
170
+ TransactionCreate $ transactionCreateObject ,
171
+ array $ transactionConfig ,
172
+ string $ returnUrl ,
173
+ string $ notifyTokenHash
174
+ ): void {
180
175
181
- protected function createPostFinanceModel (string $ model , array $ data ): ModelInterface
182
- {
183
- $ modelClass = new $ model ();
184
- foreach ($ data as $ key => $ value ) {
185
- $ setter = sprintf ('set%s ' , ucfirst ($ key ));
186
- $ modelClass ->$ setter ($ value );
176
+ $ defaults = [
177
+ 'autoConfirmationEnabled ' => true ,
178
+ 'currency ' => $ transactionConfig ['currency ' ] ?? null ,
179
+ 'language ' => $ transactionConfig ['language ' ] ?? null ,
180
+ 'failedUrl ' => $ this ->getFailedUrl ($ returnUrl ),
181
+ 'successUrl ' => $ this ->getSuccessUrl ($ returnUrl ),
182
+ 'metaData ' => function (mixed $ storedValue ) use ($ notifyTokenHash ) {
183
+
184
+ $ data = ['paymentToken ' => $ notifyTokenHash ];
185
+
186
+ if (!is_array ($ storedValue )) {
187
+ return $ data ;
188
+ }
189
+
190
+ return array_merge ($ storedValue , $ data );
191
+ }
192
+ ];
193
+
194
+ foreach ($ defaults as $ defaultKey => $ defaultValue ) {
195
+
196
+ $ getter = sprintf ('get%s ' , ucfirst ($ defaultKey ));
197
+ $ setter = sprintf ('set%s ' , ucfirst ($ defaultKey ));
198
+
199
+ if (is_callable ($ defaultValue )) {
200
+ $ transactionCreateObject ->$ setter ($ defaultValue ($ transactionCreateObject ->$ getter ()));
201
+ } elseif ($ transactionCreateObject ->$ getter () === null ) {
202
+ $ transactionCreateObject ->$ setter ($ defaultValue );
203
+ }
187
204
}
188
205
189
- return $ modelClass ;
206
+ if (empty ($ transactionCreateObject ->getLineItems ())) {
207
+
208
+ /** @var LineItemCreate $defaultLineItem */
209
+ $ defaultLineItem = $ this ->createDefaultLineItem ($ transactionConfig );
210
+
211
+ $ transactionCreateObject ->setLineItems ([
212
+ $ defaultLineItem
213
+ ]);
214
+ }
190
215
}
191
216
217
+ private function createDefaultLineItem (array $ data ): ModelInterface
218
+ {
219
+ $ lineItem = new LineItemCreate ();
220
+
221
+ $ lineItem
222
+ ->setQuantity (1 )
223
+ ->setAmountIncludingTax ($ data ['amount ' ] / 100 )
224
+ ->setTaxes ($ data ['totalTaxes ' ] ?? null )
225
+ ->setUniqueId ($ data ['id ' ])
226
+ ->setName ($ data ['id ' ])
227
+ ->setSku ($ data ['id ' ])
228
+ ->setType (LineItemType::PRODUCT );
229
+
230
+ return $ lineItem ;
231
+ }
192
232
}
0 commit comments