Skip to content

Fix empty fields in payment output - #57

Open
alvarolivie wants to merge 2 commits into
mainfrom
fix-empty-fields
Open

Fix empty fields in payment output#57
alvarolivie wants to merge 2 commits into
mainfrom
fix-empty-fields

Conversation

@alvarolivie

@alvarolivie alvarolivie commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • newCreditTransferAccount now returns nil instead of an empty FinancialAccount when no fields are set
  • CardAccount.PrimaryAccountNumberID only set when Last4 is non-empty
  • PaymentMandate.ID only set when DirectDebit.Ref is non-empty
  • PaymentTerms note only appended when Terms.Notes is non-empty

Prevent blank XML elements from being emitted when source data
has no meaningful values for credit transfer, card, mandate, and
payment terms fields.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Mar 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.09%. Comparing base (59d3ef8) to head (79609de).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
payment.go 71.42% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #57      +/-   ##
==========================================
+ Coverage   76.12%   77.09%   +0.97%     
==========================================
  Files          26       26              
  Lines        1889     1895       +6     
==========================================
+ Hits         1438     1461      +23     
+ Misses        329      312      -17     
  Partials      122      122              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce empty/meaningless XML output in generated UBL payment structures by omitting optional elements/fields when the corresponding GOBL payment data is empty.

Changes:

  • Return nil from newCreditTransferAccount when no account fields are set, avoiding empty financial account output.
  • Make card PAN and direct-debit mandate ID conditional on non-empty source values (and move direct-debit payer account under PaymentMandate).
  • Only append PaymentTerms notes when Terms.Notes is non-empty.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread payment.go
Comment on lines +134 to 137
mandate := &PaymentMandate{}
if instr.DirectDebit.Ref != "" {
mandate.ID = IDType{Value: instr.DirectDebit.Ref}
}

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PaymentMandate.ID is a non-pointer IDType, so it will still be marshaled as <cbc:ID> with an empty value even when instr.DirectDebit.Ref == "". The conditional assignment here doesn’t actually prevent an empty ID from appearing in the XML; consider making PaymentMandate.ID a *IDType (or implementing custom marshaling) so it can be omitted when unset, and update the parsing side accordingly.

Copilot uses AI. Check for mistakes.
Comment thread payment.go
Comment on lines 133 to 144
if instr.DirectDebit != nil {
ui.PaymentMeans[0].PaymentMandate = &PaymentMandate{
ID: IDType{Value: instr.DirectDebit.Ref},
mandate := &PaymentMandate{}
if instr.DirectDebit.Ref != "" {
mandate.ID = IDType{Value: instr.DirectDebit.Ref}
}
if instr.DirectDebit.Account != "" {
ui.PaymentMeans[0].PayerFinancialAccount = &FinancialAccount{
mandate.PayerFinancialAccount = &FinancialAccount{
ID: &instr.DirectDebit.Account,
}
}
ui.PaymentMeans[0].PaymentMandate = mandate
}

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PaymentMandate is always allocated and assigned whenever instr.DirectDebit != nil, even if both Ref and Account are empty. That will still emit a <cac:PaymentMandate> element (and currently an empty <cbc:ID>), which seems to contradict the goal of avoiding empty output fields. Consider only setting ui.PaymentMeans[0].PaymentMandate when at least one meaningful DirectDebit field is present, otherwise leave it nil.

Copilot uses AI. Check for mistakes.
Comment thread payment.go
ui.PaymentMeans[0].CardAccount.HolderName = &instr.Card.Holder
card.HolderName = &instr.Card.Holder
}
ui.PaymentMeans[0].CardAccount = card

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CardAccount is always allocated and assigned whenever instr.Card != nil, even when both Last4 and Holder are empty. This can still produce an empty <cac:CardAccount/> element in the XML. To fully avoid empty payment output, consider only setting ui.PaymentMeans[0].CardAccount when at least one card field is non-empty.

Suggested change
ui.PaymentMeans[0].CardAccount = card
if card.PrimaryAccountNumberID != nil || card.HolderName != nil {
ui.PaymentMeans[0].CardAccount = card
}

Copilot uses AI. Check for mistakes.
Comment thread payment.go
Comment on lines +171 to +173
if pfa.ID == nil && pfa.Name == nil && pfa.FinancialInstitutionBranch == nil {
return nil
}

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds new behavior where newCreditTransferAccount returns nil when no fields are set. There are existing payment conversion tests, but none assert the nil/omission behavior for an empty credit transfer; adding a unit test/fixture covering an empty pay.CreditTransfer would help prevent regressions in the generated XML.

Copilot uses AI. Check for mistakes.
Comment thread payment.go
Comment on lines +203 to 206
} else if pymt.Terms.Notes != "" {
ui.PaymentTerms = append(ui.PaymentTerms, PaymentTerms{
Note: []string{pymt.Terms.Notes},
})

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PaymentTerms are now appended only when pymt.Terms.Notes is non-empty. Since this changes output behavior (previously an empty <cbc:Note/> could be emitted), it would be good to add a conversion test that covers the case where due dates are absent and Terms.Notes is empty to ensure ui.PaymentTerms stays empty.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants