Collecting Payments and Consents
Requirements
In order to collect payments and patient consent, you'll need the following
- A CareValidate Public API Key
- A page in your storefront that can be used as a checkout flow
- A backend endpoint capable of creating a Payment Intent or Setup Intent
- A backend endpoint capable of creating a Case
1. Setting up cv.js
To collect payment and consent, you'll need to use cv.js. cv.js is a JavaScript library built, maintained, and hosted by CareValidate
that provides out-of-the-box elements for creating frontends that implement CareValidate. It can be added to your storefront using a single script tag in
your checkout page's HTML:
<script src="https://assets.carevalidate.com/cv.js"></script>
This script registers a set of custom elements used to interact with the CareValidate Public API. The only additional requirement to use these elements is to set the required global configuration options:
<script>
window.careValidate = {
publicKey: "cv_pk_XXXXXXXX_XXXX_XXXX_XXXX_XXXXXXXXXXXX",
};
</script>
2. Capturing Patient Treatment Consent
To securely capture patient treatment consent, you'll need to instantiate a <cv-consent> element with a type attribute that contains the corresponding
treatment. For example:
<cv-consent type="TREATMENT_GLP"></cv-consent>
Available consent document types are as follows:
TREATMENT_ANTI_NAUSEATREATMENT_CBDTREATMENT_GLPTREATMENT_HAIR_CARETREATMENT_HRTTREATMENT_PEPTIDESTREATMENT_SEXUAL_HEALTHTREATMENT_SKINCARETREATMENT_TRT
Once your consent element has been instantiated, you'll need to listen for submit events on the element and capture its outputted consent token for use
during case creation. For example:
<script>
let treatmentConsentSignature = null;
document.querySelector("cv-consent").addEventListener("submit", (event) => {
treatmentConsentSignature = event.detail.consentSignature;
});
</script>
3. Capturing Patient Payment Information and Payment Consent
Much like capturing treatment consent, payment information / consent are captured via a custom element. This time, instantiate a <cv-checkout> element with a
redirect-url attribute to capture redirects for complex checkout flows such as BN/PL. For example:
<cv-checkout redirect-url="https://my-storefront.care/intake/checkout"></cv-checkout>
Once your checkoput element has been instantiated, you'll need to give it a payment intent or setup intent. This can be either a static object or a promise containing the exact CareValidate API output for the intent. For example:
<script>
const createPaymentIntent = async () => {
const res = await fetch("/api/payment-intent", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
metadata: {
email: "someone@example.net",
phone: "+5555555555",
},
}),
});
const data = await res.json();
return {
type: "payment",
data,
};
};
document.querySelector("cv-checkout").intent = createPaymentIntent();
</script>
After setting the payment / setup intent, you'll need to listen for submit events on the element and capture its outputted consent token and payment
information for use during case creation. For example:
<script>
document.querySelector("cv-checkout").addEventListener("submit", (event) => {
// Send event.detail to your storefront's API endpoint that's in charge of case creation
});
</script>
Putting It All Together
A complete example that first shows patient consent and then shows checkout might look something like this:
<!doctype html>
<html>
<head>
<title>My Storefront Intake</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://assets.carevalidate.com/cv.js"></script>
<script>
window.careValidate = { publicKey: "cv_pk_XXXXXXXX_XXXX_XXXX_XXXX_XXXXXXXXXXXX" };
</script>
</head>
<body>
<cv-consent type="TREATMENT_GLP"></cv-consent>
<script>
let treatmentConsentSignature = null;
const createPaymentIntent = async () => {
const res = await fetch("/api/payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
metadata: {
email: "example.patient.1@carevalidate.com",
phone: "+5555555555",
},
}),
});
const data = await res.json();
return {
type: "payment",
data,
};
};
const handleTreatmentConsentSubmit = (event) => {
treatmentConsentSignature = event.detail.consentSignature;
document.querySelector("cv-consent").remove();
const checkoutEl = document.createElement("cv-checkout");
checkoutEl.setAttribute("redirect-url", "https://my-storefront.care/intake/checkout");
checkoutEl.intent = createPaymentIntent();
checkoutEl.addEventListener("submit", handleCheckoutSubmit);
document.body.append(checkoutEl);
};
const handleCheckoutSubmit = async (event) => {
const res = await fetch("/api/create-case", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
treatmentConsentSignature,
...event.detail,
}),
});
if (res.ok) {
window.location.href = "https://my-storefront.care/intake/success";
} else {
// TODO: Add your own error handling
}
};
document.querySelector("cv-consent").addEventListener("submit", handleTreatmentConsentSubmit);
</script>
</body>
</html>
And just like that, you're ready to securely capture your customer's payment and consent!
Customization
We've built cv.js so that your storefront can apply their own branding / styles to our elements so that our elements can truly feel like a part of your
storefront. The following options are available to be passed into the theme property of window.careValidate:
export interface ThemeFontStyle {
letterSpacing: string;
lineHeight: string;
size: string;
weight: string;
}
export interface Theme {
borderRadius: string;
colors: {
background: string;
border: string;
foreground: string;
primary: string;
};
font: {
family: string;
url?: string;
};
fontStyles: {
h4: ThemeFontStyle;
base: ThemeFontStyle;
small: ThemeFontStyle;
};
spacing: {
base: number;
unit: string;
};
}