Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a3ac72

Browse files
return only used payment method for refund form
1 parent 339d1e8 commit 1a3ac72

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

features/admin/refunding_payplug_payment.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Feature: Refunding order's PayPlug payment
6262
Scenario: Should be able to refund using payplug payment
6363
When I want to refund some units of order "00000001"
6464
Then there should be "PayPlug" payment method
65-
Then there should be "Cash on delivery" payment method
65+
Then there should not be "Cash on delivery" payment method
6666
Then I should still be able to refund order shipment with "PayPlug" payment
6767
When For this order I decide to refund 1st "Green Arrow" product with "PayPlug" payment
6868
Then this order refunded total should be "$10.00"
@@ -72,7 +72,7 @@ Feature: Refunding order's PayPlug payment
7272
Scenario: Should be able to refund using payplug payment
7373
When I want to refund some units of order "00000001"
7474
Then there should be "PayPlug" payment method
75-
Then there should be "Cash on delivery" payment method
75+
Then there should not be "Cash on delivery" payment method
7676
Then I should still be able to refund order shipment with "PayPlug" payment
7777
When For this order I decide to refund 1st "Green Arrow" product with "PayPlug" payment
7878
Then this order refunded total should be "$10.00"

src/Provider/SupportedRefundPaymentMethodsProviderDecorator.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PayPlug\SyliusPayPlugPlugin\Provider;
66

77
use PayPlug\SyliusPayPlugPlugin\PayPlugGatewayFactory;
8+
use Payum\Core\Model\GatewayConfigInterface;
89
use Sylius\Component\Core\Model\ChannelInterface;
910
use Sylius\Component\Core\Model\OrderInterface;
1011
use Sylius\Component\Core\Model\PaymentInterface;
@@ -57,10 +58,25 @@ public function findForChannel(ChannelInterface $channel): array
5758
/** @var OrderInterface|null $order */
5859
$order = $this->orderRepository->findOneByNumber($request->get('orderNumber'));
5960

60-
if (!$order instanceof OrderInterface || $this->isPayPlugPayment($order)) {
61+
if (!$order instanceof OrderInterface) {
6162
return $paymentMethods;
6263
}
6364

65+
if ($this->isPayPlugPayment($order)) {
66+
return array_filter($paymentMethods, function (PaymentMethodInterface $paymentMethod) use ($order): bool {
67+
$lastPayment = $order->getLastPayment();
68+
if (!$lastPayment instanceof PaymentInterface) {
69+
return false;
70+
}
71+
$lastPaymentMethod = $lastPayment->getMethod();
72+
if (!$lastPaymentMethod instanceof PaymentMethodInterface) {
73+
return false;
74+
}
75+
76+
return $paymentMethod->getId() === $lastPaymentMethod->getId();
77+
});
78+
}
79+
6480
if (null !== $order->getLastPayment() &&
6581
null !== $order->getLastPayment()->getMethod() &&
6682
$order->getLastPayment()->getMethod()->getCode() === PayPlugGatewayFactory::FACTORY_NAME &&
@@ -80,17 +96,23 @@ public function findForChannel(ChannelInterface $channel): array
8096

8197
private function isPayPlugPayment(OrderInterface $order): bool
8298
{
83-
$firstPayment = $order->getPayments()->first();
84-
if (!$firstPayment instanceof PaymentInterface) {
99+
$lastPayment = $order->getLastPayment();
100+
if (!$lastPayment instanceof PaymentInterface) {
101+
return false;
102+
}
103+
104+
$paymentMethod = $lastPayment->getMethod();
105+
if (!$paymentMethod instanceof PaymentMethodInterface) {
85106
return false;
86107
}
87108

88-
$firstPaymentMethod = $firstPayment->getMethod();
89-
if (!$firstPaymentMethod instanceof PaymentMethodInterface) {
109+
$gatewayConfig = $paymentMethod->getGatewayConfig();
110+
111+
if (!$gatewayConfig instanceof GatewayConfigInterface) {
90112
return false;
91113
}
92114

93-
if (PayPlugGatewayFactory::FACTORY_NAME !== $firstPaymentMethod->getCode()) {
115+
if (PayPlugGatewayFactory::FACTORY_NAME !== $gatewayConfig->getFactoryName()) {
94116
return false;
95117
}
96118

0 commit comments

Comments
 (0)