Commerce: Return Order errors in PaymentsController::actionPay()

Created on 10 Dec 2018  路  3Comments  路  Source: craftcms/commerce

Description

I made a custom field with several validators and attached it to the field layout of an order. It's kinda like Amazons limited time deals so by the time a customer adds the item to the cart the validation may pass but when he/she waits for too long the deal might be already "closed"/out of stock or whatever unfortunately The PaymentsController doesn't return validation errors of the order

if (!$paymentForm->hasErrors() && !$order->hasErrors()) {
    try {
        $plugin->getPayments()->processPayment($order, $paymentForm, $redirect, $transaction);
        $success = true;
    } catch (PaymentException $exception) {
        $customError = $exception->getMessage();
        $success = false;
    }
} else {
    $customError = Craft::t('commerce', 'Invalid payment or order. Please review.');
    $success = false;
}

if (!$success) {
    if ($request->getAcceptsJson()) {
        return $this->asJson(['error' => $customError, 'paymentForm' => $paymentForm->getErrors()]);
    }

    $session->setError($customError);
    Craft::$app->getUrlManager()->setRouteParams(compact('paymentForm'));

    return null;
}

So the only information are the $customError and the payment form. While the payment form might be totally correct the order itself could have errors. Currently I need to either create a 2nd Ajax request just to receive those errors or create a custom controller to change this one line.

Is there any chance to include those errors as well?

if ($request->getAcceptsJson()) {
    return $this->asJson(
        [
            'orderError'  => $order->getErrors(),  // <-- include the order errors
            'error'       => $customError,
            'paymentForm' => $paymentForm->getErrors()
        ]
    );
}
馃悶 bug

Most helpful comment

Hi @Anubarak

An update has been pushed for this to be included in the next release. The JSON response data now includes order and similarly to paymentForm includes the errors for the order (if there are any).

If the request does not accept JSON the order element is now passed back in the route params.

Thanks.

All 3 comments

Yes, this needs to be corrected. Will make a fix soon.

Any update on this?
I currently always overwrite your code and include it manually

Hi @Anubarak

An update has been pushed for this to be included in the next release. The JSON response data now includes order and similarly to paymentForm includes the errors for the order (if there are any).

If the request does not accept JSON the order element is now passed back in the route params.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

palomamtnez picture palomamtnez  路  5Comments

codyjames picture codyjames  路  6Comments

lazlo-bonin picture lazlo-bonin  路  5Comments

AugustMiller picture AugustMiller  路  4Comments

jmauzyk picture jmauzyk  路  5Comments