Skip to content

Commit 5b92786

Browse files
committed
external payment statuses from Convertim are now included in isPaid and hasPaymentInProcess methods
1 parent 2164b31 commit 5b92786

File tree

5 files changed

+102
-50
lines changed

5 files changed

+102
-50
lines changed

src/Form/Admin/Order/OrderFormType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Shopsys\FrameworkBundle\Model\Order\Order;
2121
use Shopsys\FrameworkBundle\Model\Order\OrderData;
2222
use Shopsys\FrameworkBundle\Model\Order\Status\OrderStatusFacade;
23-
use Shopsys\FrameworkBundle\Model\Payment\Transaction\ExternalPaymentStatus;
23+
use Shopsys\FrameworkBundle\Model\Payment\Transaction\ExternalPaymentStatusHelper;
2424
use Shopsys\FrameworkBundle\Twig\DateTimeFormatterExtension;
2525
use Symfony\Component\Form\AbstractType;
2626
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
@@ -45,12 +45,14 @@ class OrderFormType extends AbstractType
4545
* @param \Shopsys\FrameworkBundle\Model\Order\Status\OrderStatusFacade $orderStatusFacade
4646
* @param \Shopsys\FrameworkBundle\Twig\DateTimeFormatterExtension $dateTimeFormatterExtension
4747
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
48+
* @param \Shopsys\FrameworkBundle\Model\Payment\Transaction\ExternalPaymentStatusHelper $externalPaymentStatusHelper
4849
*/
4950
public function __construct(
5051
private readonly CountryFacade $countryFacade,
5152
private readonly OrderStatusFacade $orderStatusFacade,
5253
private readonly DateTimeFormatterExtension $dateTimeFormatterExtension,
5354
private readonly Domain $domain,
55+
private readonly ExternalPaymentStatusHelper $externalPaymentStatusHelper,
5456
) {
5557
}
5658

@@ -207,7 +209,7 @@ private function createBasicInformationGroup(FormBuilderInterface $builder, Orde
207209

208210
if ($paymentTransaction !== false) {
209211
if ($paymentTransaction->getExternalPaymentStatus() !== null) {
210-
$translatedPaymentStatus = ExternalPaymentStatus::getTranslatedStatus($paymentTransaction->getExternalPaymentStatus());
212+
$translatedPaymentStatus = $this->externalPaymentStatusHelper->getTranslatedStatus($paymentTransaction->getExternalPaymentStatus());
211213
} else {
212214
$translatedPaymentStatus = t('Order has not been sent to payment gateway');
213215
}

src/Model/Payment/Transaction/ExternalPaymentStatus.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrameworkBundle\Model\Payment\Transaction;
6+
7+
use GoPay\Definition\Response\PaymentStatus;
8+
use Shopsys\ConvertimBundle\Model\Payment\PaymentStatus as ConvertimPaymentStatus;
9+
10+
class ExternalPaymentStatusHelper
11+
{
12+
protected ?ConvertimPaymentStatus $convertimPaymentStatus = null;
13+
14+
public function __construct()
15+
{
16+
if ($this->isConvertimInstalled()) {
17+
$this->convertimPaymentStatus = new ConvertimPaymentStatus();
18+
}
19+
}
20+
21+
/**
22+
* @return bool
23+
*/
24+
public function isConvertimInstalled(): bool
25+
{
26+
return class_exists(ConvertimPaymentStatus::class);
27+
}
28+
29+
/**
30+
* @param string $externalStatus
31+
* @return string
32+
*/
33+
public function getTranslatedStatus(string $externalStatus): string
34+
{
35+
$statusesToTranslate = $this->getStatusesToTranslate();
36+
$externalStatusUppercase = mb_strtoupper($externalStatus);
37+
38+
if (array_key_exists($externalStatusUppercase, $statusesToTranslate)) {
39+
return $statusesToTranslate[$externalStatusUppercase];
40+
}
41+
42+
return $externalStatus;
43+
}
44+
45+
/**
46+
* @param string $paymentStatus
47+
* @return bool
48+
*/
49+
public function isPaid(string $paymentStatus): bool
50+
{
51+
return $paymentStatus === PaymentStatus::PAID ||
52+
($this->convertimPaymentStatus !== null && $this->convertimPaymentStatus->isPaid($paymentStatus));
53+
}
54+
55+
/**
56+
* @param string $paymentStatus
57+
* @return bool
58+
*/
59+
public function hasPaymentInProcess(string $paymentStatus): bool
60+
{
61+
return $paymentStatus === PaymentStatus::PAYMENT_METHOD_CHOSEN ||
62+
($this->convertimPaymentStatus !== null && $this->convertimPaymentStatus->hasPaymentInProcess($paymentStatus));
63+
}
64+
65+
/**
66+
* @return array<string, string>
67+
*/
68+
protected function getStatusesToTranslate(): array
69+
{
70+
$statusesToTranslate = [];
71+
72+
if ($this->convertimPaymentStatus !== null) {
73+
$statusesToTranslate = $this->convertimPaymentStatus->getStatusesToTranslate();
74+
}
75+
76+
return array_merge(
77+
[
78+
PaymentStatus::CREATED => t('Payment created'),
79+
PaymentStatus::PAYMENT_METHOD_CHOSEN => t('Payment method chosen'),
80+
PaymentStatus::PAID => t('Payment paid'),
81+
PaymentStatus::AUTHORIZED => t('Payment authorized'),
82+
PaymentStatus::CANCELED => t('Payment canceled'),
83+
PaymentStatus::TIMEOUTED => t('Payment has expired'),
84+
PaymentStatus::REFUNDED => t('Payment refunded'),
85+
PaymentStatus::PARTIALLY_REFUNDED => t('Payment partially refunded'),
86+
],
87+
$statusesToTranslate,
88+
);
89+
}
90+
}

src/Model/Payment/Transaction/PaymentTransaction.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ class PaymentTransaction
6868
*/
6969
protected $refundedAmount;
7070

71+
protected ExternalPaymentStatusHelper $externalPaymentStatusHelper;
72+
7173
/**
7274
* @param \Shopsys\FrameworkBundle\Model\Payment\Transaction\PaymentTransactionData $paymentTransactionData
7375
*/
7476
public function __construct(PaymentTransactionData $paymentTransactionData)
7577
{
7678
$this->setData($paymentTransactionData);
79+
$this->externalPaymentStatusHelper = new ExternalPaymentStatusHelper();
7780
}
7881

7982
/**
@@ -179,15 +182,15 @@ public function getRefundableAmount(): Money
179182
*/
180183
public function isRefundable(): bool
181184
{
182-
return in_array($this->externalPaymentStatus, [PaymentStatus::PARTIALLY_REFUNDED, PaymentStatus::PAID], true);
185+
return $this->payment->isGoPay() && in_array($this->externalPaymentStatus, [PaymentStatus::PARTIALLY_REFUNDED, PaymentStatus::PAID], true);
183186
}
184187

185188
/**
186189
* @return bool
187190
*/
188191
public function isPartiallyRefunded(): bool
189192
{
190-
return $this->externalPaymentStatus === PaymentStatus::PARTIALLY_REFUNDED;
193+
return $this->payment->isGoPay() && $this->externalPaymentStatus === PaymentStatus::PARTIALLY_REFUNDED;
191194
}
192195

193196
/**
@@ -199,7 +202,7 @@ public function isPaid(): bool
199202
return false;
200203
}
201204

202-
return $this->externalPaymentStatus === PaymentStatus::PAID;
205+
return $this->externalPaymentStatusHelper->isPaid($this->externalPaymentStatus);
203206
}
204207

205208
/**
@@ -211,6 +214,6 @@ public function hasPaymentInProcess(): bool
211214
return false;
212215
}
213216

214-
return $this->externalPaymentStatus === PaymentStatus::PAYMENT_METHOD_CHOSEN;
217+
return $this->externalPaymentStatusHelper->hasPaymentInProcess($this->externalPaymentStatus);
215218
}
216219
}

src/Resources/config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717

1818
Shopsys\FrameworkBundle\:
1919
exclude: '../../{Command,Controller,DependencyInjection,Form,Migrations,Resources,Twig}'
20-
resource: '../../**/*{Calculation,Dispatcher,Enum,Facade,Factory,Formatter,Generator,Handler,InlineEdit,Listener,Loader,Mapper,Middleware,Parser,Provider,Recalculator,Registry,Repository,Resolver,Sender,Service,Scheduler,Subscriber,Transformer,DataFetcher}.php'
20+
resource: '../../**/*{Calculation,Dispatcher,Enum,Facade,Factory,Formatter,Generator,Handler,Helper,InlineEdit,Listener,Loader,Mapper,Middleware,Parser,Provider,Recalculator,Registry,Repository,Resolver,Sender,Service,Scheduler,Subscriber,Transformer,DataFetcher}.php'
2121

2222
Shopsys\FrameworkBundle\Controller\:
2323
resource: '../../Controller/'

0 commit comments

Comments
 (0)