Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ jobs:

-
name: Upload Behat logs
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: failure()
with:
name: Behat logs
Expand Down
47 changes: 43 additions & 4 deletions src/Controller/CreatePayPalOrderFromCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
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\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 +38,8 @@
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 +71,33 @@
),
);
}
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 2.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 2.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 +113,24 @@
'status' => $payment->getState(),
]);
}

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

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.2, Symfony ^5.4.21, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^5.4.21, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.1, Symfony ^5.4.21, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^6.4, MySQL 8.0, State Machine Adapter symfony_workflow

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^5.4.21, MySQL 8.0, State Machine Adapter symfony_workflow

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^6.4, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.2, Symfony ^5.4.21, MySQL 8.0, State Machine Adapter symfony_workflow

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^5.4.21, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.2, Symfony ^6.4, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.3, Symfony ^6.4, MySQL 8.0, State Machine Adapter symfony_workflow

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.2, Symfony ^6.4, MySQL 8.0, State Machine Adapter symfony_workflow

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

Check failure on line 121 in src/Controller/CreatePayPalOrderFromCartAction.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.1, Symfony ^6.4, MySQL 8.0, State Machine Adapter winzou_state_machine

Call to an undefined method Sylius\Component\Payment\Model\PaymentMethodInterface::getGatewayConfig().

if ($factoryName !== 'sylius.pay_pal') {
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment as to why this is necessary/wat it do

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the test I added provides a good comment explaining the code I added.


$payment = $order->getLastPayment(PaymentInterface::STATE_CART);
}

return $payment;
}
}
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_cash_on_delivery(): 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');
}
}
Loading