2

On my order_information page I have a Paypal Express checkout button. Using this hacked together route, I can successfully save the payment, apply it to the order, and then move the order to complete:

public function onReturn() {
  $method = $this->requestStack->getCurrentRequest()->getMethod();
    if ($this->requestStack->getCurrentRequest()->getMethod() == 'POST') {
      $request = $this->requestStack->getCurrentRequest();
      $test = $request->get('test');
      $data = $this->requestStack->getParentRequest()->query->all();
      $carts = $this->getCarts();
      $payment_method_storage = $this->entityTypeManager()
        ->getStorage('commerce_payment_method');

      // @todo can't be right
      /** @var Order $order */
      $order = array_pop($carts);
      $order->set('payment_gateway', $this->paymentGateway);
      try {
         $this->paymentPlugin->onReturn($order, $request);
      } catch (PaymentGatewayException $exception) {
        // do something - like cancel moving the order into complete state.
      }
      // I prefer the "throw-it-all-at-the-wall-and-see-what-sticks" method
      $transition = $order->getState()->getWorkflow()->getTransition('place');
      $order->getState()->applyTransition($transition);
      $order->save();
      throw new NeedsRedirectException(Url::fromRoute('commerce_checkout.form', [
        'commerce_order' => $order->id(),
        'step' => 'complete',
      ])->toString());
    }
  }

The flow here is:

  1. User clicks paypal express button
  2. paypal modal opens, user completes payment, modal closes
  3. paypal executes the callback (hitting this onReturn)
  4. payment is applied to order
  5. order is moved to complete
  6. user is still on order_information page.

There are some obvious gaps here (hard coded stuff, not sure how to deal with carts, what if payment isn't paid in full) but what I'm trying to solve right now is: how do I move someone to the order complete page? (or any other page for that matter).

The throw is only my latest attempt, I've tried redirecting to the CartController and returning RedirectResponse - just can't get it to move :).

  • Does the Commerce PayPal module not support Express Checkout in 8? In 7, you can do this with the module out of the box. Even if you're coding it yourself for some reason, looking at that module's code should be helpful. – Patrick Kenny Oct 4 '17 at 13:07
  • 1
    @PatrickKenny not in the way I'd expect it to, see drupal.org/node/2912593 – Chris Rockwell Oct 4 '17 at 13:09
  • 1
    Ah, I see. That's unfortunate. In 7 you could just hide the address and skip the cart for the same effect, but 8 has issues with both. (Hide the address: drupal.stackexchange.com/questions/246923/… / Skip the cart: drupal.org/node/2810723) – Patrick Kenny Oct 4 '17 at 13:22
  • Hmm, I wonder now if it's not moving to the next step because there is no address/billing info – Chris Rockwell Oct 4 '17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.