Skip to content

Commit

Permalink
payum-postfinance-flex:2.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Jan 21, 2025
1 parent b7c9203 commit dc2ccee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 34 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CoreShop >= 3.0
## Installation

```json
"dachcom-digital/payum-postfinance-flex-bundle": "~1.3.0"
"dachcom-digital/payum-postfinance-flex-bundle": "~2.0.0"
```

Add Bundle to `bundles.php`:
Expand All @@ -29,6 +29,9 @@ Go to CoreShop -> PaymentProvider and add a new Provider. Choose `postfinance_fl

## Changelog

### 2.0.0
- Dependency `dachcom-digital/payum-postfinance-flex` requirement set to `^2.0`

### 1.3.0
- [LICENSE] Dual-License with GPL and Dachcom Commercial License (DCL) added

Expand All @@ -55,6 +58,6 @@ Go to CoreShop -> PaymentProvider and add a new Provider. Choose `postfinance_fl
## License
**DACHCOM.DIGITAL AG**, Löwenhofstrasse 15, 9424 Rheineck, Schweiz
[dachcom.com](https://www.dachcom.com), [email protected]
Copyright © 2024 DACHCOM.DIGITAL. All rights reserved.
Copyright © 2025 DACHCOM.DIGITAL. All rights reserved.

For licensing details please visit [LICENSE.md](LICENSE.md)
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
"email": "[email protected]",
"homepage": "https://www.dachcom.com",
"role": "Developer"
},
{
"name": "DACHCOM.DIGITAL Stefan Hagspiel",
"email": "[email protected]",
"homepage": "https://www.dachcom.com",
"role": "Developer"
}
],
"require": {
"payum/core": "^1.6",
"dachcom-digital/payum-postfinance-flex": "^1.2"
"dachcom-digital/payum-postfinance-flex": "^2.0"
},
"require-dev": {
"symfony/http-client": "^6.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CoreShop\Component\Core\Model\PaymentProviderInterface;
use CoreShop\Component\Taxation\Model\TaxItemInterface;
use DachcomDigital\Payum\PostFinance\Flex\Request\Api\TransactionExtender;
use DachcomDigital\Payum\PostFinance\Flex\Transaction\Transaction;
use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\Model\Payment;
Expand Down Expand Up @@ -54,28 +55,29 @@ public function onPostExecute(Context $context)
return;
}

/** @var Payment $payment */
$payment = $request->getFirstModel();

$paymentEntity = $this->paymentRepository->createQueryBuilder('p')
->where('p.number = :orderNumber')
->setParameter('orderNumber', $payment->getNumber())
->getQuery()
->getSingleResult();

if (!$paymentEntity instanceof \Coreshop\Component\Core\Model\Payment) {
$payment = $this->getPayment($request);
if (!$payment instanceof \Coreshop\Component\Core\Model\Payment) {
return;
}

$order = $paymentEntity->getOrder();
$order = $this->getOrder($payment);
if (!$order instanceof OrderInterface) {
return;
}

$gatewayLanguage = 'en_EN';

$transaction = $request->getTransaction();

$this->addLocaleToTransaction($order, $transaction);
$this->addTaxesToTransaction($order, $transaction);
$this->addPaymentConfigurationToTransaction($payment, $transaction);

$request->setTransaction($transaction);
}

private function addLocaleToTransaction(OrderInterface $order, Transaction $transaction): void
{
$gatewayLanguage = 'en_EN';

if (!empty($order->getLocaleCode())) {
$orderLanguage = $order->getLocaleCode();
// post finance always requires a full language ISO Code
Expand All @@ -87,24 +89,10 @@ public function onPostExecute(Context $context)
}

$transaction->setLanguage($gatewayLanguage);
}

/** @var PaymentProviderInterface $paymentProvider */
$paymentProvider = $paymentEntity->getPaymentProvider();
$gatewayConfig = $paymentProvider->getGatewayConfig()->getConfig();

$optionalParameters = [];
if (is_array($gatewayConfig) && array_key_exists('optionalParameters', $gatewayConfig)) {
$optionalParameters = $gatewayConfig['optionalParameters'];
}

if (array_key_exists('allowedPaymentMethodBrands', $optionalParameters) && !empty($optionalParameters['allowedPaymentMethodBrands'])) {
$transaction->setAllowedPaymentMethodBrands(explode(',', $optionalParameters['allowedPaymentMethodBrands']));
}

if (array_key_exists('allowedPaymentMethodConfigurations', $optionalParameters) && !empty($optionalParameters['allowedPaymentMethodConfigurations'])) {
$transaction->setAllowedPaymentMethodConfigurations(explode(',', $optionalParameters['allowedPaymentMethodConfigurations']));
}

private function addTaxesToTransaction(OrderInterface $order, Transaction $transaction): void
{
$taxes = [];
foreach ($order->getTaxes() as $tax) {
if (!$tax instanceof TaxItemInterface) {
Expand All @@ -127,7 +115,53 @@ public function onPostExecute(Context $context)
if (count($taxes) > 0) {
$transaction->setTotalTaxes($taxes);
}
}

private function addPaymentConfigurationToTransaction(\Coreshop\Component\Core\Model\Payment $payment, Transaction $transaction): void
{
/** @var PaymentProviderInterface $paymentProvider */
$paymentProvider = $payment->getPaymentProvider();
$gatewayConfig = $paymentProvider->getGatewayConfig()->getConfig();

$optionalParameters = [];
if (is_array($gatewayConfig) && array_key_exists('optionalParameters', $gatewayConfig)) {
$optionalParameters = $gatewayConfig['optionalParameters'];
}

$request->setTransaction($transaction);
if (array_key_exists('allowedPaymentMethodBrands', $optionalParameters) && !empty($optionalParameters['allowedPaymentMethodBrands'])) {
$transaction->getTransactionCreateObject()->setAllowedPaymentMethodBrands(explode(',', $optionalParameters['allowedPaymentMethodBrands']));
}

if (array_key_exists('allowedPaymentMethodConfigurations', $optionalParameters) && !empty($optionalParameters['allowedPaymentMethodConfigurations'])) {
$transaction->getTransactionCreateObject()->setAllowedPaymentMethodConfigurations(explode(',', $optionalParameters['allowedPaymentMethodConfigurations']));
}
}

private function getOrder(\Coreshop\Component\Core\Model\Payment $payment): ?OrderInterface
{
$order = $payment->getOrder();
if (!$order instanceof OrderInterface) {
return null;
}

return $order;
}

private function getPayment(TransactionExtender $request): ?\Coreshop\Component\Core\Model\Payment
{
/** @var Payment $payment */
$payment = $request->getFirstModel();

$paymentEntity = $this->paymentRepository->createQueryBuilder('p')
->where('p.number = :orderNumber')
->setParameter('orderNumber', $payment->getNumber())
->getQuery()
->getSingleResult();

if (!$paymentEntity instanceof \Coreshop\Component\Core\Model\Payment) {
return null;
}

return $paymentEntity;
}
}

0 comments on commit dc2ccee

Please sign in to comment.