banco-alimentar / alimentestaideia.pt

Site doações alimente esta ideia
http://alimentestaideia.pt
Other
10 stars 0 forks source link

Donation is Paid but cannot generate invoice #748

Open tiagonmas opened 10 months ago

tiagonmas commented 10 months ago

Describe the bug A user paid for a donation and did not receive the invoice. The Generate Invoice is not working for this donation.

The log shows a "CreateInvoice-ConfirmedFailedPaymentStatus" event. This is because the Donation record is false in ConfirmedPayment. SQL select top 10 from Donations where Id=27131 select top 10 from Payments where DonationId=27131 select top 10 * from Invoices where DonationId=27131

Returns Donation

Payment Status=1 ConfirmedPaymentId=NULL

Payments

Status = Success Completed = Null Invoices Not found

Code


                if ((donation != null && donation.ConfirmedPayment == null) || (donation != null && PaymentStatusMessages.FailedPaymentMessages.Any(p => p == donation.ConfirmedPayment?.Status)))
                {
                    this.TelemetryClient.TrackEvent(
                       "CreateInvoice-ConfirmedFailedPaymentStatus",
                       new Dictionary<string, string>()
                       {
                            { "DonationId", donationId.ToString() },
                            { "UserId", user.Id },
                            { "ConfirmedPaymentStatusId", donation.ConfirmedPayment?.Id.ToString() },
                            { "Function", nameof(this.GetOrCreateInvoiceByDonation) },
                       });
                    return null;
                }

Investigation By looking at

select top 100 * from Payments where Discriminator like 'MBWayPayment' order by Created desc`

we see several payments that have sucess, but not a completed date.

select distinct(Discriminator), count(1) from Payments where Status='Success' and Completed is null group by Discriminator

Discriminator Total MultiBankPayment 1601 MBWayPayment 97 CreditCardPayment 206

tiagonmas commented 10 months ago

Easypay has the payment confirmation for generic but skip for payment message:

image

guerrerotook commented 10 months ago

Let me take a look

guerrerotook commented 10 months ago

So, what has been happening is that as part of the payload that easypay sent us for the payment and generic notification they have been including a new field account with an empy id. Like this:

"account": {
        "id": ""
    },

That empty id was making the json parser to fail because the property wasn't set to nullable in the model. So instead of changing the value I set this to Guid.Empty for now because that may potentially affect a lot of model classes.

I'm going to push a change into master adding a new class GuidConverter and add this converter to the configuration in the API. This solve the issue when simulating a easypay payment notification in my local PC.

tiagonmas commented 10 months ago

we're having Null exceptions in production, and that is because Donation.ConfirmedPaymentId is not being set, and then when we try to fetch donation.ConfirmedPayment.Created we get the null exception.

DateTime donationPaidDate = donation.ConfirmedPayment.Created;

tiagonmas commented 10 months ago

Fixed in bee04c360f711a37ae78749b6ad4edde2b648a3c

tiagonmas commented 10 months ago

made a new payment and still got an error when generating the invoice. Needs more investigation. FYI @guerrerotook

tiagonmas commented 10 months ago

When I try to generate the invoice again (going to donation history) I can see the invoice.

tiagonmas commented 10 months ago

There are still some situations throwing an error. For ex:

  select top 10 * from Donations where Id=27027
 select top 100 * from Payments where DonationId=27027
 select top 100 * from Invoices where DonationId=27027
tiagonmas commented 10 months ago

I'm still seeing the issue @guerrerotook . I can repro in dev. It seems to be time based ... If I donate and try to click on "To claim the invoice click [here .]" immediately, it throws an error. If I refresh it continues to give an error for some seconds, and then it works ...

guerrerotook commented 10 months ago

I see, I tested a couple of times and it work for me, but I see what you said, because it could be a delay between you click and easypay sent us the invoice.

tiagonmas commented 10 months ago

might not be related but I'm seeing this exception "An error occurred while saving the entity changes. See the inner exception for details. Cannot insert duplicate key row in object 'dbo.Invoices' with unique index 'IX_Invoices_Sequence_FoodBank_Id_Year'. The duplicate key value is (420, 19, 2023). "

tiagonmas commented 10 months ago

above message is not related. When I get the error in generating the invoice, I see that event log has "CreateInvoice-InvoiceWithPaymentStatusNotPayed" fired. After a while the issue goes away. This happens after we are presented the Thanks page. How can the payment status not be updated yet and we are at the Thanks page ?

To repro this, make a donation (issue happens with MbWay at least) and when you get to thanks page very quickly follow "To claim the invoice click [here .]"

tiagonmas commented 10 months ago

I added a bit more telemetry to better understand what is happening. In the example below I refreshed 5 times until I got the invoice. We are showing the Thanks page before actually receiving the confirmation of payment. image

tiagonmas commented 10 months ago

added more telemetry in dev to trace easypay generic and payment notifications. Here is the flow ... I click ed on invoice, got a error, waited and then it worked. image