Skip to content

Instantly share code, notes, and snippets.

@alphanumeric0101
Created May 19, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphanumeric0101/0d93fc83f0b1f61d229201f82ae209c0 to your computer and use it in GitHub Desktop.
Save alphanumeric0101/0d93fc83f0b1f61d229201f82ae209c0 to your computer and use it in GitHub Desktop.
Sample of creating a stripe charge
// generate the payment blob we will base the charge on
this.createJobBrief = function(shift, worker, confirmedShiftData, employer, employerReview) {
// in the case of ch_1AGu3MCOsXOJGjx5HT3TBu2n, shift.pay was 16$
// employers can update the hours worked, hence confirmedShiftData
// there's an excessive amount of coercing to Number() here...
// I should probably switch these to Math.round() ;P
let amount_owed = Number((Number(shift.pay) * Number(confirmedShiftData.updatedHours)).toFixed(2))
let taxes = Number((Number(amount_owed) * 0.05).toFixed(2));
let fee = Number(Number(amount_owed * 0.20).toFixed(2));
let totalCharge = (amount_owed + fee + taxes + 0.3)/(1-0.029);
let stripeFee = totalCharge - (amount_owed + fee + taxes);
return { // returned object is then stored in employer.jobs_to_be_paid
amount_owed: amount_owed,
amount_owed_tasker: amount_owed_tasker,
fee: fee,
stripeFee: Number((stripeFee).toFixed(2)),
fees: Number((Number(fee) + Number(stripeFee)).toFixed(2)),
taxes: Number(Number((amount_owed + fee + stripeFee) * 0.05).toFixed(2)),
total: Number(Number((amount_owed + fee + stripeFee) * 1.05).toFixed(2))
}
};
// Creat the actual charge on stripe
return Promise.all(employer.jobs_to_be_paid.map( shift => {
emailTotal += shift.total; // for dispatching receipt
let metadata = { tasker: shift.worker_name, tasker_id: shift.userid, amount_owed: Number(shift.amount_owed), hours: Number(shift.hours), company: shift.employer_name, employer_id: shift.employer_id, eventName: shift.eventName, jobid: shift.jobid, paymentTotal: Number(shift.total) };
let destinationAmount = Math.round(Number(shift.amount_owed)*100);
return stripe.charges.create({
amount: Math.round(Number(shift.total)*100),
currency: 'cad',
customer: employer.stripe_cus_id,
destination: { account: shift.worker_stripe_account, amount: destinationAmount },
metadata: metadata
})
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment