Skip to content

Commit 362fde3

Browse files
authored
Merge pull request #10 from dachcom-digital/postfinance_objects
add TransactionCreate object
2 parents 4ae3995 + a12b5ce commit 362fde3

File tree

6 files changed

+125
-106
lines changed

6 files changed

+125
-106
lines changed

Action/Api/TransactionExtenderAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Payum\Core\Action\ActionInterface;
88
use Payum\Core\Exception\RequestNotSupportedException;
99
use Payum\Core\Model\PaymentInterface;
10+
use PostFinanceCheckout\Sdk\Model\TransactionCreate;
1011

1112
class TransactionExtenderAction implements ActionInterface
1213
{
@@ -20,7 +21,7 @@ public function execute($request)
2021
/** @var PaymentInterface $payment */
2122
$payment = $request->getFirstModel();
2223

23-
$transaction = new Transaction();
24+
$transaction = new Transaction(new TransactionCreate());
2425

2526
$transaction->setId($payment->getNumber());
2627
$transaction->setCurrency($payment->getCurrencyCode());

Action/ConvertPaymentAction.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function execute($request)
4949
$details['transaction_extender'] = $transactionExtender->toArray();
5050

5151
$request->setResult((array)$details);
52-
5352
}
5453

5554
public function supports($request): bool

Api.php

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Payum\Core\Bridge\Spl\ArrayObject;
66
use PostFinanceCheckout\Sdk\ApiClient;
7-
use PostFinanceCheckout\Sdk\Model\AddressCreate;
87
use PostFinanceCheckout\Sdk\Model\LineItemCreate;
98
use PostFinanceCheckout\Sdk\Model\LineItemType;
109
use PostFinanceCheckout\Sdk\Model\ModelInterface;
1110
use PostFinanceCheckout\Sdk\Model\Transaction;
1211
use PostFinanceCheckout\Sdk\Model\TransactionCreate;
12+
use PostFinanceCheckout\Sdk\ObjectSerializer;
1313
use PostFinanceCheckout\Sdk\Service\TransactionIframeService;
1414
use PostFinanceCheckout\Sdk\Service\TransactionLightboxService;
1515
use PostFinanceCheckout\Sdk\Service\TransactionPaymentPageService;
@@ -24,6 +24,11 @@ public function __construct(array $options)
2424
$this->options = $options;
2525
}
2626

27+
public function getIntegrationType(): string
28+
{
29+
return $this->options['integrationType'];
30+
}
31+
2732
public function getPaymentPageUrl(int $transactionId): string
2833
{
2934
return $this->getTransactionPaymentPageService()->paymentPageUrl($this->getSpaceId(), $transactionId);
@@ -45,39 +50,27 @@ public function getIframeUrl(int $transactionId): string
4550

4651
public function prepareTransaction(ArrayObject $details, string $returnUrl, string $notifyTokenHash): Transaction
4752
{
48-
$transactionExtender = [];
53+
$transactionConfig = [];
54+
$detailsArray = $details->toUnsafeArray();
55+
4956
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();
5164
}
5265

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);
8174
}
8275

8376
public function getEntity($entityId): ModelInterface
@@ -173,20 +166,67 @@ protected function getSpaceId(): mixed
173166
return $this->options['spaceId'];
174167
}
175168

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 {
180175

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+
}
187204
}
188205

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+
}
190215
}
191216

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+
}
192232
}

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ You need to define a global webhook: `https://your-domain.com/payment/notify/uns
2323

2424
## Changelog
2525

26+
### 2.0.0
27+
- [NEW FEATURE | **BC BREAK**] `TransactionCreate` Object added: The object `DachcomDigital\Payum\PostFinance\Flex\Transaction\Transaction`
28+
now provides a `getTransactionCreateObject` method, which gives you full control over the transaction data.
29+
Therefor we've removed several methods within the `Transaction` object itself.
30+
Use to the `TransactionCreate` object directly, to add additional data.
31+
- Removed methods:
32+
- `(get|set)AllowedPaymentMethodBrands`
33+
- `(get|set)AllowedPaymentMethodConfigurations`
34+
- `(get|set)ShippingAddress`
35+
- `(get|set)BillingAddress`
36+
- Signature of `transaction_extender` within the payment `details` has changed:
37+
- `transactionCreate` added
38+
- `allowedPaymentMethodBrands`, `allowedPaymentMethodConfigurations`, `shippingAddress`, `billingAddress` removed
39+
40+
***
41+
2642
### 1.3.0
2743
- introduce `GetTransactionDetailsAction`
2844

Transaction/Transaction.php

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace DachcomDigital\Payum\PostFinance\Flex\Transaction;
44

5-
use PostFinanceCheckout\Sdk\Model\AddressCreate;
5+
use PostFinanceCheckout\Sdk\Model\TransactionCreate;
66
use PostFinanceCheckout\Sdk\ObjectSerializer;
77

88
class Transaction
@@ -12,11 +12,15 @@ class Transaction
1212
protected ?string $currency;
1313
protected ?string $language = null;
1414
protected ?array $totalTaxes = null;
15-
protected ?array $allowedPaymentMethodBrands = null;
16-
protected ?array $allowedPaymentMethodConfigurations = null;
1715

18-
protected ?AddressCreate $shippingAddress = null;
19-
protected ?AddressCreate $billingAddress = null;
16+
public function __construct(protected TransactionCreate $transactionCreate)
17+
{
18+
}
19+
20+
public function getTransactionCreateObject(): TransactionCreate
21+
{
22+
return $this->transactionCreate;
23+
}
2024

2125
public function getId(): mixed
2226
{
@@ -68,62 +72,21 @@ public function setTotalTaxes(?array $totalTaxes): void
6872
$this->totalTaxes = $totalTaxes;
6973
}
7074

71-
public function getAllowedPaymentMethodBrands(): ?array
72-
{
73-
return $this->allowedPaymentMethodBrands;
74-
}
75-
76-
public function setAllowedPaymentMethodBrands(?array $allowedPaymentMethodBrands): void
77-
{
78-
$this->allowedPaymentMethodBrands = $allowedPaymentMethodBrands;
79-
}
80-
81-
public function getAllowedPaymentMethodConfigurations(): ?array
82-
{
83-
return $this->allowedPaymentMethodConfigurations;
84-
}
85-
86-
public function setAllowedPaymentMethodConfigurations(?array $allowedPaymentMethodConfigurations): void
87-
{
88-
$this->allowedPaymentMethodConfigurations = $allowedPaymentMethodConfigurations;
89-
}
90-
91-
public function getShippingAddress(): ?AddressCreate
92-
{
93-
return $this->shippingAddress;
94-
}
95-
96-
public function setShippingAddress(?AddressCreate $shippingAddress): void
97-
{
98-
$this->shippingAddress = $shippingAddress;
99-
}
100-
101-
public function getBillingAddress(): ?AddressCreate
102-
{
103-
return $this->billingAddress;
104-
}
105-
106-
public function setBillingAddress(?AddressCreate $billingAddress): void
107-
{
108-
$this->billingAddress = $billingAddress;
109-
}
110-
11175
public function toArray(): array
11276
{
113-
$data = [
114-
'id' => $this->getId(),
115-
'amount' => $this->getAmount(),
116-
'currency' => $this->getCurrency(),
117-
'language' => $this->getLanguage(),
118-
'totalTaxes' => $this->getTotalTaxes(),
119-
'allowedPaymentMethodBrands' => $this->getAllowedPaymentMethodBrands(),
120-
'allowedPaymentMethodConfigurations' => $this->getAllowedPaymentMethodConfigurations(),
121-
'shippingAddress' => $this->shippingAddress === null ? [] : (array) ObjectSerializer::sanitizeForSerialization($this->shippingAddress),
122-
'billingAddress' => $this->billingAddress === null ? [] : (array) ObjectSerializer::sanitizeForSerialization($this->billingAddress)
77+
$transactionCreateJson = ObjectSerializer::json_encode(
78+
ObjectSerializer::sanitizeForSerialization(
79+
$this->getTransactionCreateObject()
80+
)
81+
);
82+
83+
return [
84+
'id' => $this->getId(),
85+
'amount' => $this->getAmount(),
86+
'currency' => $this->getCurrency(),
87+
'language' => $this->getLanguage(),
88+
'totalTaxes' => $this->getTotalTaxes(),
89+
'transactionCreate' => ObjectSerializer::json_decode($transactionCreateJson)
12390
];
124-
125-
return array_filter($data, static function ($row) {
126-
return $row !== null;
127-
});
12891
}
12992
}

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
{
1313
"name": "DACHCOM.DIGITAL Robert Kaczmarczyk",
1414
"email": "rkaczmarczyk@dachcom.ch",
15-
"homepage": "http://www.dachcom.com",
15+
"homepage": "https://www.dachcom.com",
1616
"role": "Developer"
1717
},
1818
{
1919
"name": "DACHCOM.DIGITAL Stefan Hagspiel",
2020
"email": "shagspiel@dachcom.ch",
21-
"homepage": "http://www.dachcom.com",
21+
"homepage": "https://www.dachcom.com",
2222
"role": "Developer"
2323
}
2424
],
@@ -28,7 +28,7 @@
2828
},
2929
"extra": {
3030
"branch-alias": {
31-
"dev-main": "1.1.x-dev"
31+
"dev-main": "3.0.x-dev"
3232
}
3333
},
3434
"autoload": {

0 commit comments

Comments
 (0)