Skip to content

Instantly share code, notes, and snippets.

@nwaughachukwuma
Created December 23, 2019 15:32
Show Gist options
  • Save nwaughachukwuma/b561a6a0f9078b878bd22f6257f913ac to your computer and use it in GitHub Desktop.
Save nwaughachukwuma/b561a6a0f9078b878bd22f6257f913ac to your computer and use it in GitHub Desktop.
Create a charge from a customer transaction
import { stripe } from './stripe-util'
export async function preAuthorizeCharge(customer: string, amount: number, accountId: string, metadata: any = {}) {
const percentageOfTransaction = 0.05 // percentage charge on a business for using the platform
const application_fee_amount = Math.round((amount * percentageOfTransaction) * 100)
const cost = Math.round(amount * 100);
const descriptor = 'brief description of transaction';
const description = 'a more detailed description'
try {
const charge = await stripe.charges.create({
customer: customer.stripe_customer_id,
amount: cost,
application_fee_amount,
description,
statement_descriptor: descriptor,
currency: 'usd',
transfer_data: {
destination: accountId // <-- this is where you specify the business accountId
},
metadata,
capture: false,
receipt_email: customer.email
})
return charge
} catch (err) {
throw err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment