Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Order errors in PaymentsController::actionPay() #601

Closed
Anubarak opened this issue Dec 10, 2018 · 3 comments
Closed

Return Order errors in PaymentsController::actionPay() #601

Anubarak opened this issue Dec 10, 2018 · 3 comments
Assignees
Labels

Comments

@Anubarak
Copy link

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()
        ]
    );
}
@lukeholder
Copy link
Member

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

@lukeholder lukeholder added the bug label Jan 31, 2019
@Anubarak
Copy link
Author

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

@nfourtythree
Copy link
Contributor

nfourtythree commented Aug 22, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants