Refund of Outstanding Advance Payment.

Step =

1/ Advance payment created

(Dr) Cash 100
(Cr) Advance payment account 100

2/ Invoice created:

(Dr) Account Receivables 80
(Cr) Sales 80

3/ Apply Advance payment: (Partially)

(Dr) Advance payment account 80
(Cr) Account Receivables 80

In some cases, we need to refund the outstanding advance payment,

4/ Create an Invoice with non-tracked item called “Refund of Prepayment” - sales account set to "Contra Advance payment", say $20

(Dr) Account Receivables 20
(Cr) Contra Advance payment 20

5/ Apply the Advance payment to the Invoice

(Dr) Advance payment Account 20
(Cr) Account Receivables 20

6/ Create Direct Expense

(Dr) Contra Advance payment 20
(Cr) Cash 20

Above is the simulation on the Sales flow (Advance Payment).
The similar concept can be applied to the Buy flow for the Pre-payment case as well.