-
Notifications
You must be signed in to change notification settings - Fork 65
[Shop] Fix paypal checkout when another payment method was selected during checkout #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b329228
b9cea8b
b8e8417
6b6c8c3
ebef1ac
18c95f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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( | ||
|
|
@@ -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', | ||
Wojdylak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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'); | ||
|
|
@@ -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
|
||
|
|
||
| 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); | ||
|
||
|
|
||
| $payment = $order->getLastPayment(PaymentInterface::STATE_CART); | ||
| } | ||
|
|
||
| return $payment; | ||
| } | ||
Wojdylak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| 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" |
Uh oh!
There was an error while loading. Please reload this page.