Skip to content

[releases/28.x] [Shopify] Skip refund credit memo while transaction is pending#9666

Open
onbuyuka wants to merge 1 commit into
releases/28.xfrom
bugs/643243-backport
Open

[releases/28.x] [Shopify] Skip refund credit memo while transaction is pending#9666
onbuyuka wants to merge 1 commit into
releases/28.xfrom
bugs/643243-backport

Conversation

@onbuyuka

@onbuyuka onbuyuka commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Backport of bug #640432 to releases/28.x.

Fixes AB#643243

Original PR: #9112

Cherry-picked #9112's merge commit. Two files needed manual conflict resolution because releases/28.x predates a later ""return order"" feature: kept the 28.x wording of the existing RefundCantCreateCreditMemoErr label and omitted the unrelated Shop lookup that isn't present on this branch. The fix itself (new HasPendingRefundTransactions helper, the two guard calls, and the Refund Id/Type/Status key) is applied unchanged.

Test plan

Includes the tests added in #9112:

  • UnitTestDoesNotCreateCrMemoFromRefundWithPendingTransaction
  • UnitTestCreatesCrMemoFromRefundWithSucceededTransaction
  • UnitTestVerifyRefundCanCreateCreditMemoErrorsWithPendingTransaction

@onbuyuka
onbuyuka requested a review from a team July 22, 2026 11:48
@onbuyuka
onbuyuka requested a review from a team as a code owner July 22, 2026 11:48
@github-actions github-actions Bot added AL: Apps (W1) Add-on apps for W1 Integration GitHub request for Integration area labels Jul 22, 2026
@github-actions github-actions Bot added this to the Version 28.4 milestone Jul 22, 2026
darjoo
darjoo previously approved these changes Jul 23, 2026
Fixes
[AB#640432](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/640432)

When a Shopify refund is synced while its payment transaction is still
**pending**, Shopify's `totalRefundedSet` returns 0 (it only counts
`SUCCESS` transactions). If a credit memo is created at that moment, the
balancing line in `CreateSalesLinesFromRemainingAmount` calculates `Unit
Price = 0 - <item total>`, producing a credit memo whose total is 0 — a
financially useless document.

`ShpfyCreateSalesDocRefund.Codeunit.al` balances the document against
`RefundHeader."Total Refunded Amount"`, which is 0 while the refund
transaction is still pending, even though the item/return lines already
sum to the real amount.

Block credit memo / return order creation while the refund still has a
pending transaction, mirroring the existing `"Can Create Credit Memo"`
dual-guard pattern:

- **`ShpfyRefundsAPI.Codeunit.al`** — new internal helper
`HasPendingRefundTransactions(RefundId)` that checks for refund
transactions with `Type = Refund` and `Status = Pending`.
`VerifyRefundCanCreateCreditMemo` now throws a clear error when a
pending transaction exists (manual **Create Sales Document** path).
- **`ShpfyRetRefProcCrMemo.Codeunit.al`** — `CreateSalesDocument` now
exits early (silent skip) when the refund still has a pending
transaction (auto-sync path). The refund stays unprocessed and is
retried on the next sync; once the transaction reaches `SUCCESS`,
`totalRefundedSet` is final and the credit memo is created correctly.

This is safe because the auto-processing loop (`ProcessShopifyRefunds`)
retries all unprocessed refunds on every sync, and no data is guessed or
approximated.

Added to `ShpfyOrderRefundTest.Codeunit.al` (with a
`CreateRefundTransaction` helper in
`ShpfyOrderRefundsHelper.Codeunit.al`):

- `UnitTestDoesNotCreateCrMemoFromRefundWithPendingTransaction` — no
credit memo is created while the refund has a pending transaction.
- `UnitTestCreatesCrMemoFromRefundWithSucceededTransaction` — the guard
is specific to pending; a succeeded transaction still creates the credit
memo.
- `UnitTestVerifyRefundCanCreateCreditMemoErrorsWithPendingTransaction`
— the manual path throws the pending-transaction error.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b1f006ac-3ace-495d-8b5c-9b76510cf117
@onbuyuka
onbuyuka force-pushed the bugs/643243-backport branch from 8f4084b to 62c2704 Compare July 23, 2026 12:10
@onbuyuka
onbuyuka enabled auto-merge (squash) July 24, 2026 08:54
@onbuyuka onbuyuka closed this Jul 24, 2026
auto-merge was automatically disabled July 24, 2026 11:54

Pull request was closed

@onbuyuka onbuyuka reopened this Jul 24, 2026
@onbuyuka
onbuyuka enabled auto-merge (squash) July 24, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AL: Apps (W1) Add-on apps for W1 Integration GitHub request for Integration area

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants