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:
- User clicks paypal express button
- paypal modal opens, user completes payment, modal closes
- paypal executes the callback (hitting this
onReturn
) - payment is applied to order
- order is moved to complete
- 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 :).