Magento2: Braintree and Custom Payment Method Product 400 Bad Request on purchase

Created on 1 Jul 2016  路  19Comments  路  Source: magento/magento2

Steps to reproduce

  1. Install Magento 2.1.0.
  2. Enable Braintree and /or build custom payment method
  3. Add product to cart
  4. Complete credit card details
  5. Submit

    Expected result

  6. Puchase completed and Transaction recorded in PSP

    Actual result

  7. Ajax 400 Bad Request, Unable to place order.

  8. Debug.log - [2016-07-01 16:07:41] main.DEBUG: cache_invalidate: {"method":"POST","url":"http://dev.domain.com/rest/default/V1/carts/mine/payment-information","invalidateInfo":{"tags":["catalog_product_14","FPC"],"mode":"matchingTag"},"is_exception":false} []
  9. [Screenshot, logs]
    screen shot 2016-07-01 at 17 40 52
  10. Trace:
    "#0 /var/www/vhosts/domain.com/dev.domain.com/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Checkout\Model\PaymentInformationManagement->savePaymentInformationAndPlaceOrder(53, Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\QuoteAddress))
    #1 /var/www/vhosts/domain.com/dev.domain.com/var/generation/Magento/Checkout/Model/PaymentInformationManagement/Interceptor.php(26): Magento\Checkout\Model\PaymentInformationManagement\Interceptor->___callPlugins('savePaymentInfo...', Array, Array)
    #2 [internal function]: Magento\Checkout\Model\PaymentInformationManagement\Interceptor->savePaymentInformationAndPlaceOrder(53, Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\QuoteAddress))
    #3 /var/www/vhosts/domain.com/dev.domain.com/vendor/magento/module-webapi/Controller/Rest.php(307): call_user_func_array(Array, Array)
    #4 /var/www/vhosts/domain.com/dev.domain.com/vendor/magento/module-webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest->processApiRequest()
    #5 /var/www/vhosts/domain.com/dev.domain.com/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\FrameworkApp\Request\Http))
    #6 /var/www/vhosts/domain.com/dev.domain.com/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\FrameworkApp\Request\Http))
    #7 /var/www/vhosts/domain.com/dev.domain.com/vendor/magento/framework/App/Bootstrap.php(258): Magento\FrameworkApp\Http->launch()
    #8 /var/www/vhosts/domain.com/dev.domain.com/index.php(39): Magento\FrameworkApp\Bootstrap->run(Object(Magento\FrameworkApp\Http))
    #9 {main}"
needs update

Most helpful comment

Hi, @romeof1980, you can use xdebug to find the reason of error. You need to set a breakpoint on this line and you will see original exception message.

All 19 comments

What type of payment action (Authorization or Authorize and Capture) did you use? Also, you use fresh Magento installation or upgrade version from some previous release?

It's using Sandbox with Authorize and Capture. The install is originally a 2.0.6 that has been upgraded on each point step to 2.1.0. It is also using the demo data and Luma Theme.

Platform: CentOS7, PHP7.0.7, FastCGI

Any Ideas on were to start debugging this one? Apart from following the Interceptor Class back up the tree.... Hope you've got a solution as I only had three days left to solve this before Client drops the platform....
screen shot 2016-07-03 at 18 21 16

@stevewinni, this is known issue and related to incorrect upgrade scripts for Sales module. On the fresh install, it should work.
Closed as duplicate for https://github.com/magento/magento2/issues/4785 .

Opppss.... OK for those who come after the fix is;

ALTER TABLE sales_invoice_grid ADD base_grand_total decimal(12,4) AFTER grand_total;

For now, there is no workaround, because problem is related to db schema changes between upgrades.

Thank Joni... Looks like the DB amend listed in #4785 works.... I have success payment posts via Braintree after I amend DB..

@joni-jones On the fresh install, it still doesn't work.

@Nerogee: are you using table prefixing?
If so see https://github.com/magento/magento2/issues/5902.

@flecxie. I don't use table prefixing

@Nerogee: Do you have debugging enabled / do you see anything in var/report/debug.log?

I have tried setting up a Braintree account on a vanilla Magento CE 2.1.0 installation (without table prefixing) and it did work for me (see #5902). The "400 Bad Request" can be caused by a large variety of reasons, you will probably have to debug with Xdebug (see https://www.fontis.com.au/blog/debugging-magento-with-xdebug) to figure out where things go wrong exactly...

@flecxie sorry for late reply. I don't use table prefixing. Magento2.1 is fresh installed. I got message like "can't place order, please try again later.". I did notice for the debug.log that the Invoice ID is not updated which result in paypal keep receiving the same Invoice ID and thus fail to pass through or this may even cause problem when try to save order in system due the duplicate order ID (duplicated key).

hi there,
We've been experiencing the same Braintree issue both with Sandbox and Production (using both 2.0.7 and 2.1.0) with credit card processing (works as expected with PayPal through Braintrere)
We can see that the transactions are carried out in Braintree "Transaction" as expected.

But we also always get the '400 Bad Request' in the console and the message "Unable to place order. Please try again later".

Nothing worth to be mentioned in debug.log (see attachment).
Nothing in exception.log

thanks a lot,
romeo

debug.log works on Braintree - not on Magento 2.1.0.txt

Hi, @romeof1980, you can use xdebug to find the reason of error. You need to set a breakpoint on this line and you will see original exception message.

hi @joni-jones , thanks a lot for your reply.
I can indeed use xdebug. I took a look at the suggested link
and set the breakpoint as you suggested.

No when trying to checkout I get the following message from the console:
"NetworkError: 400 Bad Request - http://www.xxxxxx.net/rest/default/V1/guest-carts/3092d4cd6cf9cbb7ae4ef68cd3782dd8/payment-information" and
{"message":"%fieldName is a required field.","parameters":{"fieldName":"paymentMethod"}}
(but then when retrying the checkout only the original message {"message":"Unable to place order. Please try again later."} appears in the console)

About xdebug.. still trying to figure it out the right procedure to debug this issue.
when we append ?XDEBUG_PROFILE=1 to checkout/ we get the cachegrind file, but in the following step (checkout/#payment) we can't append the ?XDEBUG_PROFILE=1 so no cachegrind file is generated.

any hint will be highly appreciated.

thanks a lot,
romeo

You should be able to set xdebug.profiler_enable to 1 in your php.ini configuration file to always enable profiling, even if a request does not include XDEBUG_PROFILE as part of the request data.
See the xdebug configuration settings for more information.

thanks @joni-jones and @vinai for pointing us in the right direction: debugging with xdebug has given the following js errors:

Blocked a frame with origin "https://assets.braintreegateway.com" from accessing a frame with origin "https://www.paypal.com". Protocols, domains, and ports must match. (05:32:22:446 | error, javascript)
at a (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:3:13074)
at n (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:2:23832)
at p (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:2:23502)
at f (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:2:23332)

Blocked a frame with origin "https://assets.braintreegateway.com" from accessing a frame with origin "https://www.paypal.com". Protocols, domains, and ports must match. (05:32:22:531 | error, javascript)
at a (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:3:13074)
at n (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:2:23832)
at p (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:2:23502)
at f (https://assets.braintreegateway.com/hosted-fields/2.17.6/braintree-hosted-fields-internal.min.js:2:23332)

trying to figure it out why the frame was blocked...

Just to add a note (since I just fixed it after 3 full days), renaming the tables and removing the table prefix also solved the 400 error for me using PayFlow Pro.

Issue was fixed for us following the solution suggested in #5902 (deleting table prefixing and updating table sales_sequence_meta)

Hi,
This issue is coming in Magento ver. 2.1.3 also, while placing order, getting error 404 Bad Request.
I debugged in file app/code/Corra/Quote/Model/QuoteManagement.php; The error is cached after code
$order = $this->orderManagement->place($order);
Customers are charged multiple times when they use payment method as BrainTree credit card.
The following information was logged:
2017-02-23T21:33:14+00:00 DEBUG (7): ===========PLACE ORDER START=========== {"data":"{\"additional_data\":{\"device_data\":\"{\\"correlation_id\\":\\"c80ae18c244c4d27a0b52cca03e24f72\\",\\"device_session_id\\":\\"73923DFD39BE4CFB9D97A74214CE6A68\\",\\"fraud_merchant_id\\":\\"600000\\"}\",\"payment_method_nonce\":\"96c5de0f-fd23-0f69-2c49-d62fdf6ba364\"},\"method\":\"braintree\",\"checks\":[\"checkout\",\"country\",\"currency\",\"total\",\"zero_total\"]}"}

2017-02-23T21:33:14+00:00 DEBUG (7): Quote Before Quote Submit {"Reserved Order ID":null,"Quote ID":"324944","Customer Email":"[email protected]","Checkout Session":"{\"shipper_global\":{\"dateFormat\":\"mm/dd/yyyy\",\"currencyCode\":\"usd\",\"weightUnit\":\"lb\",\"dropshipShowMergedRates\":true,\"dropshipShowItemDesc\":false,\"dropshipDescription\":\"Multiple Warehouses\",\"dropshipTitle\":\"Shipping\",\"distanceUnit\":\"MI\",\"carrierGroupDescription\":\"Origin\",\"calendarConfirm\":false,\"shippingTooltipText\":\"\",\"showPickupMulti\":false,\"cityRequired\":false,\"sortBasedPrice\":true,\"dropshipShowWeight\":false,\"preferredLocale\":\"en-US\",\"transaction\":\"SHQ_REQUEST_20170223_2133_XXX.95881385\"},\"custom_method_code\":\"shqFDX_FDX_2DA\"}"}

2017-02-23T21:33:17+00:00 DEBUG (7): Error on place order {"error":"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'braintree-923835-mf5krd2' for key 'UNQ_54DCE14AEAEA03B587F9EF723EB10A10', query was: INSERT INTO vault_payment_token (customer_id, public_hash, payment_method_code, type, expires_at, gateway_token, details, is_active) VALUES (?, ?, ?, ?, '2018-07-01 00:00:00', ?, ?, ?)"}

Given that the user get's many Magento order ids, it would appear that Magento is trying to charge the users many times.
We have tried every steps mentioned in this ticket as well as #5902, both.
Plz suggest any solution.

Was this page helpful?
0 / 5 - 0 ratings