@tutorbook/payments
v0.6.5
Published
Tutorbook PWA (Progressive Web App) sub-package that manages client-side payment operations (primarily for tutors to manage their business(es)).
Downloads
66
Readme
Tutorbook Payments
Tutorbook PWA (Progressive Web App) sub-package that manages client-side payment operations (primarily for tutors to manage their business(es)).
Payment Flow
We use Stripe Connect to process all of the payments on Tutorbook. Paid tutors setup their accounts as follows:
- Paid tutors are prompted by a dashboard card to setup a Stripe Connect payouts account. A link is generated using our unique Stripe Client ID that sends the user to https://connect.stripe.com to complete their account setup. Upon completion, they are sent back to our return URI (usually https://tutorbook.app/app) with their auth token included as a query param (e.g. https://tutorbook.app/app/home?token=ABCDEFG).
- That token is then sent to our server via an HTTPS GET request that triggers
the creation of a Firestore document containing that new Stripe Account's
information. Our server then sends an
accountURL
(where the tutor can view and manage their payout settings) back to the client.
Once the paid tutor has finished setting up their account, they can then receive paid lesson requests as follows:
- Pupils create paid requests via a PCI-compliant Stripe Element (included in
the
StripeRequestDialog
in@tutorbook/dialogs
) that returns a payment method token which is sent to the server in thenewRequest
data
REST API request. - The server's
data
cloud function then creates asentPayment
Firestore document which triggers theprocessAuthPayment
cloud function. - The
processAuthPayment
cloud function:- Adds a payment method to the Stripe Customer using the token generated by the Stripe Element.
- Ensures that the
payment.from
user has a validstripeCustomer
document and thepayment.to
user has a validstripeAccount
document. - Authorizes payment from the Stripe Customer to the Stripe Connect Account while deducting a 10% fee.
- Creates
authPayment
documents in both user'sauthPayments
subcollections and one document (that contains the Stripe Payment object) in thestripeCustomer
authPayments
subcollection. - Deletes the original
sentPayment
document. - Note: If anything errors, the function deletes the
requestOut
,requestIn
, andsentPayment
documents and sends an email to thefromUser
asking for a valid form of payment.
After the tutoring lesson, tutors are able to request payments from their pupils
by creating pastAppointment
documents:
- The tutor clicks a FAB on the
ViewApptDialog
(included in@tutorbook/dialogs
) indicating that they are ready to "Request Payment". - Clicking that button creates a
requestedPayment
document (via thedata
REST API) that asks the pupil to confirm the lesson occurred (via a dialog created in@tutorbook/listener
). - Once payment approval has been granted by the pupil (which leads to the
creation of
approvedPayment
documents), our server'sprocessPayment
cloud function captures the correct amount from the originalauthorizedPayment
. - Capturing the payment leads to the creation of
pastPayment
documents which triggers another function to increase the tutor's balance (in their Firestore profile document) to match their Stripe Account balance.
Tutors then receive payouts from Stripe (that trigger a webhook that lowers their account balance in their Firestore profile document) on a daily basis.