Imported code from LDEngine/Billing.git#1491ffea2 and enhanced for standalone project