Skip to content

Commit a61c84c

Browse files
committed
Identify completed order by PayPal order id rather than autoincremented ID
1 parent 956e1ce commit a61c84c

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"php": "^7.3",
99

10-
"sylius/sylius": "^1.7",
10+
"sylius/sylius": "1.7.*",
1111
"nyholm/append-query-string": "^0.1.1",
1212
"phpseclib/phpseclib": "^2.0"
1313
},

src/Controller/CompletePayPalOrderAction.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Sylius\Component\Core\OrderCheckoutTransitions;
1111
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
1212
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
13+
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
@@ -23,6 +24,9 @@ final class CompletePayPalOrderAction
2324
/** @var UrlGeneratorInterface */
2425
private $router;
2526

27+
/** @var PaymentProviderInterface */
28+
private $paymentProvider;
29+
2630
/** @var OrderProviderInterface */
2731
private $orderProvider;
2832

@@ -35,24 +39,25 @@ final class CompletePayPalOrderAction
3539
public function __construct(
3640
PaymentStateManagerInterface $paymentStateManager,
3741
UrlGeneratorInterface $router,
42+
PaymentProviderInterface $paymentProvider,
3843
OrderProviderInterface $orderProvider,
3944
FactoryInterface $stateMachineFactory,
4045
ObjectManager $orderManager
4146
) {
4247
$this->paymentStateManager = $paymentStateManager;
4348
$this->router = $router;
49+
$this->paymentProvider = $paymentProvider;
4450
$this->orderProvider = $orderProvider;
4551
$this->stateMachineFactory = $stateMachineFactory;
4652
$this->orderManager = $orderManager;
4753
}
4854

4955
public function __invoke(Request $request): Response
5056
{
51-
$id = (int) $request->attributes->get('id');
52-
$order = $this->orderProvider->provideOrderById($id);
57+
$id = $request->query->get('id');
58+
$payment = $this->paymentProvider->getByPayPalOrderId($id);
59+
$order = $payment->getOrder();
5360

54-
/** @var PaymentInterface $payment */
55-
$payment = $order->getLastPayment(PaymentInterface::STATE_PROCESSING);
5661
$this->paymentStateManager->complete($payment);
5762

5863
$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);

src/Resources/config/services/controller.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<service id="Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction">
2929
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
3030
<argument type="service" id="router" />
31+
<argument type="service" id="Sylius\PayPalPlugin\Provider\PaymentProviderInterface" />
3132
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
3233
<argument type="service" id="sm.factory" />
3334
<argument type="service" id="sylius.manager.order" />

src/Resources/config/shop_routing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ sylius_paypal_plugin_create_paypal_order_from_cart:
1111
_controller: Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromCartAction
1212

1313
sylius_paypal_plugin_complete_paypal_order:
14-
path: /complete-pay-pal-order/{id}
14+
path: /complete-pay-pal-order
1515
methods: [POST]
1616
defaults:
1717
_controller: Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction

src/Resources/views/payWithPaypal.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
<script src="https://www.paypal.com/sdk/js?components=hosted-fields,buttons,funding-eligibility&locale={{ locale }}&currency={{ currency }}&vault=false&client-id={{ client_id }}&merchant-id={{ merchant_id }}&intent=capture" data-partner-attribution-id="{{ partner_attribution_id }}" data-enable-3ds data-client-token="{{ client_token }}"></script>
183183
<script>
184184
let createPayPalOrderUrl = "{{ path('sylius_paypal_plugin_create_paypal_order_from_payment_page', { 'id': order_id }) }}";
185-
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order', { 'id': order_id }) }}"
185+
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order') }}"
186186
let errorPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_payment_error') }}";
187187
let availableCountries = {{ available_countries|json_encode|raw }};
188188
let cancelPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_cancel_payment') }}";
@@ -203,7 +203,7 @@
203203
}).then(data => data.order_id);
204204
},
205205
onApprove: function(data, actions) {
206-
return fetch(completePayPalOrderUrl, {
206+
return fetch(completePayPalOrderUrl+'?id='+data.orderID, {
207207
method: 'post'
208208
}).then(res => res.json()).then(details => window.location.href = details.return_url);
209209
},

0 commit comments

Comments
 (0)