Help -- Code for Fundraising Platform

Hello,

I am developing a fundraising platform of sorts and could use some help.

Assume I have set out to raise $100 (hard capped at $100, not a cent more) and currently have an array of 10 commitments of $15 each towards the goal. Since the total commitments exceed the $100, I need to structure the loop so that 100% of each commitment is charged and then only a partial final charge to get exactly the goal amount on the remaining commitment.

In the example, it would be 6, $15 charges to get to $90 then one final charge amount for the remainder of $10 on the 7th commitment. The others are never made.

Assume I already have the code to make the charges, this is just about structuring the loop to get the proper amounts billed to match the exact fundraising goal.

Any thoughts would be appreciated.

Thanks!

Hello David,

See my code below

function charge(sumToCharge) {
    //a function that charges sumToCharge
    console.log('Charged $' + sumToCharge);
}

const ar = [ 15, 15, 14, 15, 15, 15, 16, 15, 14, 16, 15, 14, 16]; //donation array
let toCharge = 100; //donation amount left to charge
for (let i = 0; i < ar.length ; i++) {
    if ((toCharge - ar[i]) > 0) {
        charge(ar[i]);  
        toCharge -= ar[i];
        //add some indication that this specific donation was used, probably by removing it from the array
    } 
    else {
        charge(toCharge); 
        //add some indication that this (ar[i]) donation was partially used.
        break;
    }
}

This looks great! Appears to be exactly what I needed and it’s simple. Thanks Ido. I will toy with it and hopefully should be able to figure out the rest.

Thanks

Ido – Quick follow-up. I’ve built out the code and it works, but now I’ve added one more layer and it’s messing it up. To recap, the below code operates as you previously outlined: If we are trying to raise $100 for an event, it will start charging the available commitments, according to what is available and un-used until it reaches exactly $100, or the commitments available run out. It then tracks all of this in the database, updating which commitments were used or partially used.

However, if I try to loop this, I run into issues. Let’s take the same exact example of above, but run it twice to raise money for two events.

I believe the issue is that the future iterations of the loop start executing before the dataset has saved and recognized that some donations were already used. I’m slightly familiar with async/await, which I thought would solve this issue, but I couldn’t figure it out.

THoughts?

for (let i = 0; i < 2; i++) {
	chargeDonations(proj_sub_id, projItem.price, applications.length, project_fees);
}

function chargeDonations(proj_sub_id, price, quantity, fees) {
	wixData.query("Sponsorships")
	.eq("proj_sub_id", proj_sub_id)
	.ne("allFunds", true)
	.find()
	.then( (results) => {	
		let sponsorships = results.
		let toCharge = (price + fees);
			
	for (let i = 0; i < sponsorships.length; i++) {
		let amount_charged = sponsorships[i].amountCharged;
		amount_charged = amount_charged || 0;
		let remaining_sponsorship = (sponsorships[i].amount - amount_charged);
		let net_amount = (remaining_sponsorship - calculateFeesStripePhil(remaining_sponsorship, 1));
		let net_toCharge = (toCharge + calculateFeesStripePhil(toCharge));
		let sponsorships_sum = sponsorships.sum("amount");
					
	if ((toCharge - net_amount) > 0) {
		console.log("Donation Amount" + remaining_sponsorship);
		console.log("Net Amount" + net_amount);
		toCharge -= net_amount;
		console.log("Still to be Charged: " + toCharge);
		sponsorships_sum -= remaining_sponsorship;
					
	$w("#dataset2").setFilter( wixData.filter()
		.eq("_id", sponsorships[i]._id)) 
		.then( () => {
		$w("#dataset2").setFieldValue('amountCharged', (remaining_sponsorship + amount_charged));
		$w("#dataset2").setFieldValue('allFundsUsed', true);
		console.log("Saved Amount Charged");
		$w("#dataset2").save();
		});
					
		}
	 else {				 	
		console.log("Sponsorship Amount " + (toCharge / .921 - 0.3).toFixed(2));
		console.log("Net Amount: " + toCharge);
					
        	$w("#dataset2").setFilter( wixData.filter()
		.eq("_id", sponsorships[i]._id)) 
		.then( () => {
		$w("#dataset2").setFieldValue('amountCharged', (toCharge + amount_charged));						
		console.log("Saved Amount Charged");
		$w("#dataset2").save();	        			
    		});	
		break;
    			}
				}

		});

}