dmjio / stripe

:moneybag: Stripe API
http://hackage.haskell.org/package/stripe-haskell
177 stars 74 forks source link

`mzero` error on refunds #105

Closed dariooddenino closed 5 years ago

dariooddenino commented 5 years ago

Hello! I'm on version 2.4.0.

When I try to refund a charge, I get this error: (StripeError {errorType = ParseFailure, errorMsg = "mzero", errorCode = Nothing, errorParam = Nothing, errorHTTP = Nothing})

The transaction is actually refunded and everything looks fine on stripe side. Any idea of how I can debug this, since the error message is not helping at all?

Thanks!

dmjio commented 5 years ago

It looks like that (extremely unhelpful) error is originating here: https://github.com/dmjio/stripe/blob/master/stripe-core/src/Web/Stripe/Types.hs#L215

The first thing we should do is remove all the calls to mzero and replace them with withObject:

instance FromJSON Refund where
   parseJSON = withObject "Refund" $ \o ->
        Refund <$> (RefundId <$> o .: "id")
               <*> o .: "amount"
               <*> o .: "currency"
               <*> (fromSeconds <$> o .: "created")
               <*> o .: "object"
               <*> o .: "charge"
               <*> o .: "balance_transaction"
               <*> o .: "metadata"

This way we'll at least see what field / object it failed to decode on. Another thing to do is look at the wayback machine of stripe's API docs circa 07/2014 to see if there are any fields missing / added.

https://web.archive.org/web/20140625062328/http://stripe.com/docs/api#refunds

This looks close enough. So let's see if I did anything wrong.

screen shot 2018-11-12 at 5 25 12 am

So far it all looks good, the balance_transaction field might have been expanded though, this means a BalanceTransaction could have failed to parse.

https://github.com/dmjio/stripe/blob/master/stripe-core/src/Web/Stripe/Types.hs#L1564

and docs https://web.archive.org/web/20140625062328/http://stripe.com/docs/api#balance

EDIT: The best thing we could do is put a trace statement before the withObject call, to see the json we received.

So, I'd continue on this route...

dariooddenino commented 5 years ago

Thanks for the info! I will check what's failing to parse as soon as I have some time :)

dmjio commented 5 years ago

@dariooddenino another thing we could do is check your stripe API logs, and try to decode them into the Refund type. Go to https://dashboard.stripe.com/your-account/logs and try to find the response body you received and paste it here.

dariooddenino commented 5 years ago

Here's the response body:

{
  "id": "re_1DVboWD7I8eVU4d2RZjhG9li",
  "object": "refund",
  "amount": 19620,
  "balance_transaction": null,
  "charge": "ch_1DVbmBD7I8eVU4d22XJhgoTs",
  "created": 1542015276,
  "currency": "eur",
  "metadata": {
  },
  "reason": null,
  "receipt_number": null,
  "source_transfer_reversal": null,
  "status": "succeeded"
}
dmjio commented 5 years ago

109