This example component shows how you might attach an event handler within componentDidMount()
that updates the component's state with the referral UUID:
import React, { useState, useEffect } from "react";
const SignupForm = () => {
const [referral, setReferral] = useState(null);
// When the component mounts, wire up a callback for when Rewardful is ready.
// If `window.Rewardful.referral` is present, it means this visitor was referred
// and we should update state with the referral UUID so it's rendered in the form.
useEffect(() => {
window.rewardful('ready', () => {
setReferral(window.Rewardful.referral)
})
}, []);
// If referral is set, render the referral UUID into a hidden input named "referral".
return (
<form>
<input type="text" name="customer_name" />
<input type="email" name="customer_email" />
{ referral ? <input type="hidden" name="referral" value={referral} /> : null }
</form>
);
}
Copy
When the form data is submitted to your server from React, you then add the referral
value to the Stripe customer's metadata:
// Gather params submitted with <form>
const customerParams = {
email: req.body.email,
source: 'tok_visa' // Obtained with stripe.js
}
// Add the `referral` parameter from Rewardful to the Stripe customer metadata, if present.
if (req.body.referral) {
customerParams.metadata = { referral: req.body.referral }
}
// Create the customer in Stripe.
stripe.customers.create(customerParams)
Copy
Adding the referral UUID to the Stripe customer's metadata
converts the referral and associates it with the affiliate.
Whenever that customer is charged, a commission will be paid to the affiliate according to your campaign rules.
Please see the
Stripe API Reference
for full documentation on creating a customer with Node.js.