Page MenuHomePhabricator

Ensure gross value is not empty in the refund messages
Closed, ResolvedPublic

Description

Adyen sometimes send us chargeback notifications with an empty gross value before sending the complete notifications. Here's an example of one of those:

Oct 13 20:24:03 civi1002 civicrm.wmf[3762931]: civicrm.wmf.INFO: RefundQueueConsumer array{"gross":"","date":1697133875,"type":"chargeback","gateway":"adyen","gateway_refund_id":"BK4GJ678DX5MPK42","gateway_parent_id":"GVZ9SNZSG95CKMG2","gross_currency":"JPY","source_name":" Recon Auditor","source_type":"audit","source_host":"civi1002","source_run_id":3750834,"source_version":"unknown","source_enqueued_time":1697228587}
Oct 13 20:24:03 civi1002 civicrm.wmf[3762931]: civicrm.wmf.INFO: refund BK4GJ678DX5MPK42: Marking as refunded
Oct 13 20:24:03 civi1002 civicrm.wmf[3762931]: civicrm.wmf.INFO: refund BK4GJ678DX5MPK42: Successfully marked as refunded
Oct 13 20:24:04 civi1002 civicrm.wmf[3762931]: civicrm.wmf.INFO: RefundQueueConsumer array{"gross":"13.43","date":1691500282,"type":"chargeback","gateway":"adyen","gateway_refund_id":"BK4GJ678DX5MPK42","gateway_parent_id":"GVZ9SNZSG95CKMG2","gross_currency":"USD","source_name":" Recon Auditor","source_type":"audit","source_host":"civi1002","source_run_id":3750834,"source_version":"unknown","source_enqueued_time":1697228587}
Oct 13 20:24:04 civi1002 civicrm.wmf[3762931]: civicrm.wmf.INFO: refund BK4GJ678DX5MPK42: Marking as refunded

In our check for the difference between donated and refunded here, for notifications with gross set to '', we save a value equal to the original value where instead we are suppose to save value equal to the difference caused by fluctuations in FX.

From the query provided by @JMando in T351651, we can see that this year we've had 568 transactions from Adyen due to this notification.

We need to ensure empty gross values throws an error in the refund queue consumer.

Event Timeline

Here's a slight modification of @JMando SQL to fetch the trxn ids for the odd adyen refund messages:

select
    ft.trxn_id as "Transaction ID",
    ft.id as trxn_id,
    ft.trxn_date, 
    ft.total_amount as trxn_amount, 
    c.total_amount,
    ft.currency,
    c.contact_id,
    c.contribution_status_id,
    c.financial_type_id,
    c.id as civi_id,
    c.receive_date,
    CASE
        WHEN (extra.gateway IS NULL OR extra.gateway = '') AND cov.name = 'Tides' THEN 'Tides'
        WHEN extra.gateway IS NULL OR extra.gateway = '' THEN 'NULL'
        WHEN extra.gateway = 'globalcollect' THEN 'ingenico'
        WHEN extra.gateway = 'astropay' THEN 'Dlocal'
        WHEN extra.gateway = 'paypal_ec' THEN 'paypal'
        ELSE extra.gateway
    END AS gateway_not_null
FROM civicrm.civicrm_financial_trxn ft
LEFT JOIN civicrm.civicrm_entity_financial_trxn eft ON ft.id=eft.financial_trxn_id AND entity_table='civicrm_contribution'
LEFT JOIN civicrm.civicrm_contribution c on c.id = eft.entity_id
LEFT JOIN civicrm.civicrm_option_value AS cov ON CAST(c.payment_instrument_id AS UNSIGNED) = CAST(cov.value AS UNSIGNED) AND CAST(cov.option_group_id AS UNSIGNED) = CAST(10 AS UNSIGNED)
LEFT JOIN civicrm.civicrm_value_1_gift_data_7 AS gift ON c.id = gift.entity_id
LEFT JOIN civicrm.wmf_contribution_extra AS extra ON c.id = extra.entity_id
LEFT JOIN civicrm.civicrm_contribution_tracking AS t ON c.id = t.contribution_id
WHERE ft.trxn_date >'2023-06-30'
    AND c.id is not null
    AND ft.is_payment=1
    AND c.financial_type_id=14
    AND c.total_amount>0
    AND extra.gateway="adyen";
greg triaged this task as High priority.Nov 22 2023, 5:21 PM

Change 978632 had a related patch set uploaded (by Damilare Adedoyin; author: Damilare Adedoyin):

[wikimedia/fundraising/crm@master] Ensure gross value is not empty in the refund messages

https://gerrit.wikimedia.org/r/978632

Ah, looks like we're parsing the correct one out of the settlement_detail batch and the incorrect one out of the payments_accounting batch

Change 978632 merged by jenkins-bot:

[wikimedia/fundraising/crm@master] Ensure gross value is not empty in the refund messages

https://gerrit.wikimedia.org/r/978632

Change 979166 had a related patch set uploaded (by Ejegg; author: Ejegg):

[wikimedia/fundraising/SmashPig@master] Fix chargeback parsing in Adyen accounting report

https://gerrit.wikimedia.org/r/979166

Change 979166 merged by jenkins-bot:

[wikimedia/fundraising/SmashPig@master] Fix chargeback parsing in Adyen accounting report

https://gerrit.wikimedia.org/r/979166

AKanji-WMF set Final Story Points to 4.

Hey all, not sure this is fixed. I checked the query I provided in T351651 and still see them marked refund without a corresponding original charge:

select
	ft.id as trxn_id,
    ft.trxn_date, 
    CASE
		WHEN ft.total_amount > 0 THEN 'inflow'
        ELSE 'outflow'
	END as designation,
    ft.total_amount as trxn_amount, 
    ft.currency,
    c.contact_id,
    c.contribution_status_id,
    c.financial_type_id,
    c.id as civi_id,
    c.receive_date
FROM civicrm.civicrm_financial_trxn ft
LEFT JOIN civicrm.civicrm_entity_financial_trxn eft ON ft.id=eft.financial_trxn_id AND entity_table='civicrm_contribution'
LEFT JOIN civicrm.civicrm_contribution c on c.id = eft.entity_id
LEFT JOIN civicrm.civicrm_option_value AS cov ON CAST(c.payment_instrument_id AS UNSIGNED) = CAST(cov.value AS UNSIGNED) AND CAST(cov.option_group_id AS UNSIGNED) = CAST(10 AS UNSIGNED)
LEFT JOIN civicrm.civicrm_value_1_gift_data_7 AS gift ON c.id = gift.entity_id
LEFT JOIN civicrm.wmf_contribution_extra AS extra ON c.id = extra.entity_id
LEFT JOIN civicrm.civicrm_contribution_tracking AS t ON c.id = t.contribution_id
WHERE c.contact_id = 57805094
    AND c.id is not null
    AND ft.is_payment=1
     AND c.financial_type_id=14;

The Civi UI also still shows the error:

Screenshot 2023-12-11 at 7.17.51 AM.png (689×384 px, 41 KB)

Hi @JMando the underlying cause for this has been fixed. So there shouldn't be new rows like this anymore, @Cstone is working on a data cleanup to remove the old rows as described here.