magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Braintree and Custom Payment Method Product 400 Bad Request on purchase

Open stevewinni opened this issue 10 years ago • 19 comments

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

  1. Puchase completed and Transaction recorded in PSP

Actual result

  1. Ajax 400 Bad Request, Unable to place order.
  2. 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} []
  3. [Screenshot, logs] screen shot 2016-07-01 at 17 40 52
  4. 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\Quote\Address)) #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\Quote\Address)) #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\Framework\App\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\Framework\App\Request\Http)) #7 /var/www/vhosts/domain.com/dev.domain.com/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch() #8 /var/www/vhosts/domain.com/dev.domain.com/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http)) #9 {main}"

stevewinni avatar Jul 01 '16 16:07 stevewinni

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?

YevSent avatar Jul 03 '16 10:07 YevSent

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 avatar Jul 03 '16 17:07 stevewinni

@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 .

YevSent avatar Jul 04 '16 06:07 YevSent

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;

stevewinni avatar Jul 04 '16 08:07 stevewinni

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

YevSent avatar Jul 04 '16 08:07 YevSent

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

stevewinni avatar Jul 04 '16 08:07 stevewinni

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

Nerogee avatar Jul 05 '16 07:07 Nerogee

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

flecxie avatar Jul 30 '16 05:07 flecxie

@flecxie. I don't use table prefixing

Nerogee avatar Aug 01 '16 07:08 Nerogee

@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 avatar Aug 01 '16 07:08 flecxie

@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).

Nerogee avatar Aug 09 '16 14:08 Nerogee

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

rumbdev avatar Aug 10 '16 16:08 rumbdev

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.

YevSent avatar Aug 10 '16 17:08 YevSent

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

rumbdev avatar Aug 10 '16 19:08 rumbdev

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.

Vinai avatar Aug 11 '16 03:08 Vinai

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...

rumbdev avatar Aug 13 '16 12:08 rumbdev

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.

gabriel-sf avatar Aug 16 '16 11:08 gabriel-sf

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

rumbdev avatar Aug 17 '16 10:08 rumbdev

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.

singh190 avatar Feb 27 '17 10:02 singh190