Skip to content

Commit

Permalink
Identify completed order by PayPal order id rather than autoincrement…
Browse files Browse the repository at this point in the history
…ed ID
  • Loading branch information
Zales0123 committed Nov 13, 2020
1 parent 02a8c04 commit 6f72e02
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^7.3",

"sylius/sylius": "^1.7",
"sylius/sylius": "1.7.*",
"nyholm/append-query-string": "^0.1.1",
"phpseclib/phpseclib": "^2.0"
},
Expand Down
13 changes: 9 additions & 4 deletions src/Controller/CompletePayPalOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -23,6 +24,9 @@ final class CompletePayPalOrderAction
/** @var UrlGeneratorInterface */
private $router;

/** @var PaymentProviderInterface */
private $paymentProvider;

/** @var OrderProviderInterface */
private $orderProvider;

Expand All @@ -35,24 +39,25 @@ final class CompletePayPalOrderAction
public function __construct(
PaymentStateManagerInterface $paymentStateManager,
UrlGeneratorInterface $router,
PaymentProviderInterface $paymentProvider,
OrderProviderInterface $orderProvider,
FactoryInterface $stateMachineFactory,
ObjectManager $orderManager
) {
$this->paymentStateManager = $paymentStateManager;
$this->router = $router;
$this->paymentProvider = $paymentProvider;
$this->orderProvider = $orderProvider;
$this->stateMachineFactory = $stateMachineFactory;
$this->orderManager = $orderManager;
}

public function __invoke(Request $request): Response
{
$id = (int) $request->attributes->get('id');
$order = $this->orderProvider->provideOrderById($id);
$id = $request->query->get('id');
$payment = $this->paymentProvider->getByPayPalOrderId($id);
$order = $payment->getOrder();

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

$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<service id="Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction">
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
<argument type="service" id="router" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\PaymentProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.manager.order" />
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/shop_routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sylius_paypal_plugin_create_paypal_order_from_cart:
_controller: Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromCartAction

sylius_paypal_plugin_complete_paypal_order:
path: /complete-pay-pal-order/{id}
path: /complete-pay-pal-order
methods: [POST]
defaults:
_controller: Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/payWithPaypal.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<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>
<script>
let createPayPalOrderUrl = "{{ path('sylius_paypal_plugin_create_paypal_order_from_payment_page', { 'id': order_id }) }}";
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order', { 'id': order_id }) }}"
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order') }}"
let errorPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_payment_error') }}";
let availableCountries = {{ available_countries|json_encode|raw }};
let cancelPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_cancel_payment') }}";
Expand All @@ -203,7 +203,7 @@
}).then(data => data.order_id);
},
onApprove: function(data, actions) {
return fetch(completePayPalOrderUrl, {
return fetch(completePayPalOrderUrl+'?id='+data.orderID, {
method: 'post'
}).then(res => res.json()).then(details => window.location.href = details.return_url);
},
Expand Down

0 comments on commit 6f72e02

Please sign in to comment.