// Exit if we already posted loyalty points for this sales order.
select firstOnly RecId from trans
where trans.SalesId == salesTable.SalesId
&& trans.CardNumber == loyaltyCard.CardNumber;
Because loyalty point trans posted in MPOS don't contain Sales Id, this validation code would by passed. Then this loyalty points is posted double times.
The proposed fix update the posted loyalty points with SalesId during statement posting. That would make the validation logic works.