Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ jobs:

-
name: Upload Behat logs
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: failure()
with:
name: Behat logs
path: etc/build/
if-no-files-found: ignore
overwrite: true
18 changes: 18 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
### UPGRADE FROM 1.6.1 to 1.6.2

1. The following constructor signatures have been changed:

`Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromCartAction`:
```diff
public function __construct(
private readonly ?Payum $payum,
private readonly ?OrderRepositoryInterface $orderRepository,
private readonly ?FactoryInterface $stateMachineFactory,
private readonly ObjectManager $paymentManager,
private readonly OrderProviderInterface $orderProvider,
private readonly CapturePaymentResolverInterface $capturePaymentResolver,
+ private readonly ?OrderPaymentsRemoverInterface $orderPaymentsRemover = null,
+ private readonly ?OrderProcessorInterface $orderProcessor = null,
)
```

### UPGRADE FROM 1.6.0 to 1.6.1

1. The following constructor signatures have been changed:
Expand Down
50 changes: 46 additions & 4 deletions src/Controller/CreatePayPalOrderFromCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
use GuzzleHttp\Exception\GuzzleException;
use Payum\Core\Payum;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Payment\Remover\OrderPaymentsRemoverInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
use Sylius\PayPalPlugin\Resolver\CapturePaymentResolverInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -35,6 +39,8 @@ public function __construct(
private readonly ObjectManager $paymentManager,
private readonly OrderProviderInterface $orderProvider,
private readonly CapturePaymentResolverInterface $capturePaymentResolver,
private readonly ?OrderPaymentsRemoverInterface $orderPaymentsRemover = null,
private readonly ?OrderProcessorInterface $orderProcessor = null,
) {
if (null !== $this->payum) {
trigger_deprecation(
Expand Down Expand Up @@ -66,19 +72,33 @@ public function __construct(
),
);
}
if (null === $this->orderPaymentsRemover) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Not passing an $orderPaymentsRemover to %s constructor is deprecated and will be prohibited in 3.0',
self::class,
);
}
if (null === $this->orderProcessor) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Not passing an $orderProcessor to %s constructor is deprecated and will be prohibited in 3.0',
self::class,
);
}
}

public function __invoke(Request $request): Response
{
$id = $request->attributes->getInt('id');
$order = $this->orderProvider->provideOrderById($id);

/** @var PaymentInterface $payment */
$payment = $order->getLastPayment(PaymentInterface::STATE_CART);

try {
$payment = $this->getPayment($order);
$this->capturePaymentResolver->resolve($payment);
} catch (GuzzleException $exception) {
} catch (\DomainException|GuzzleException) {
/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('error', 'sylius.pay_pal.something_went_wrong');
Expand All @@ -94,4 +114,26 @@ public function __invoke(Request $request): Response
'status' => $payment->getState(),
]);
}

private function getPayment(OrderInterface $order): PaymentInterface
{
/** @var PaymentInterface $payment */
$payment = $order->getLastPayment(PaymentInterface::STATE_CART);
/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $payment->getMethod();
$factoryName = $paymentMethod?->getGatewayConfig()?->getFactoryName();

if ($factoryName === 'sylius.pay_pal') {
return $payment;
}

if ($this->orderPaymentsRemover === null || $this->orderProcessor === null) {
throw new \DomainException('OrderPaymentsRemover and OrderProcessor must be provided to create a new payment.');
}

$this->orderPaymentsRemover->removePayments($order);
$this->orderProcessor->process($order);

return $order->getLastPayment(PaymentInterface::STATE_CART);
}
}
3 changes: 2 additions & 1 deletion src/Factory/PayPalPaymentMethodNewResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\PayPalPlugin\Onboarding\Processor\OnboardingProcessorInterface;
use Sylius\Resource\Factory\FactoryInterface;

final class PayPalPaymentMethodNewResourceFactory implements NewResourceFactoryInterface
{
Expand All @@ -36,6 +36,7 @@ public function __construct(

public function create(RequestConfiguration $requestConfiguration, FactoryInterface $factory): ResourceInterface
{
/** @var ResourceInterface $resource */
$resource = $this->newResourceFactory->create($requestConfiguration, $factory);

if (!$resource instanceof PaymentMethodInterface) {
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
<argument type="service" id="sylius.manager.payment" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Resolver\CapturePaymentResolverInterface" />
<argument type="service" id="Sylius\Component\Core\Payment\Remover\OrderPaymentsRemoverInterface" />
<argument type="service" id="sylius.order_processing.order_processor" />
</service>

<service id="Sylius\PayPalPlugin\Controller\PayPalButtonsController">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Sylius\Component\Core\Model\Order:
new_cart:
channel: "@channel_web"
items: ["@sw_mug_item"]
currencyCode: "USD"
localeCode: "en_US"
customer: "@customer_oliver"
state: "cart"
checkoutState: "shipping_selected"
tokenValue: "TOKEN"
payments: ["@paypal_payment"]

Sylius\Component\Core\Model\OrderItem:
sw_mug_item:
units: ["@sw_mug_item_unit1", "@sw_mug_item_unit2"]
variant: "@mug_sw"
order: "@new_cart"

Sylius\Component\Core\Model\OrderItemUnit:
sw_mug_item_unit1:
__construct: ["@sw_mug_item"]
sw_mug_item_unit2:
__construct: ["@sw_mug_item"]

Sylius\Component\Core\Model\Payment:
paypal_payment:
method: "@cash_on_delivery"
currencyCode: "USD"
amount: 40
state: "cart"
17 changes: 17 additions & 0 deletions tests/DataFixtures/ORM/resources/shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ Sylius\Component\Core\Model\PaymentMethod:
translations:
- "@paypal_translation"
channels: ["@channel_web"]
cash_on_delivery:
code: CASH_ON_DELIVERY
enabled: true
gatewayConfig: "@cash_on_delivery_config"
currentLocale: en_US
translations:
- "@cash_on_delivery_translation"
channels: ["@channel_web"]

Sylius\Bundle\PayumBundle\Model\GatewayConfig:
paypal_config:
Expand All @@ -102,10 +110,19 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
sylius_merchant_id: "SYLIUS_MERCHANT_ID"
partner_attribution_id: "PARTNER_ATTRIBUTION_ID"
use_authorize: true
cash_on_delivery_config:
gatewayName: "offline"
factoryName: "sylius.offline"
config: []

Sylius\Component\Payment\Model\PaymentMethodTranslation:
paypal_translation:
name: "PayPal"
locale: "en_US"
description: <paragraph(2)>
translatable: "@paypal"
cash_on_delivery_translation:
name: "Cash on delivery"
locale: "en_US"
description: <paragraph(2)>
translatable: "@cash_on_delivery"
17 changes: 17 additions & 0 deletions tests/Functional/CreatePayPalOrderFromCartActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ public function it_creates_pay_pal_order_from_cart_and_returns_its_data(): void
$this->assertSame($content['orderID'], 'PAYPAL_ORDER_ID');
$this->assertSame($content['status'], 'cart');
}

/** @test */
public function it_creates_pay_pal_order_from_cart_and_returns_its_data_if_payment_method_is_different_then_pay_pal(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function it_creates_pay_pal_order_from_cart_and_returns_its_data_if_payment_method_is_different_then_pay_pal(): void
public function it_creates_paypal_order_from_cart_and_returns_its_data_if_payment_method_is_different_than_paypal(): void

{
$order = $this->loadFixturesFromFiles(['resources/shop.yaml', 'resources/new_cart_with_cash_on_delivery_method.yaml']);
/** @var int $orderId */
$orderId = $order['new_cart']->getId();

$this->client->request('POST', '/en_US/create-pay-pal-order-from-cart/' . $orderId);

$response = $this->client->getResponse();
$content = (array) json_decode($response->getContent(), true);

$this->assertSame($content['id'], $orderId);
$this->assertSame($content['orderID'], 'PAYPAL_ORDER_ID');
$this->assertSame($content['status'], 'cart');
}
}