Create Stripe checkout session
Category:
Serverless Function,
CMS Enterprise
ETL:
Extract,
Transform,
Load
Notes
The Stripe API can seem complex and there are many brilliant integrations on the market that will help you to get the most from HubSpot and Stripe.
But, sometimes, all you want to do is send a user to Stripe to make a payment. Perhaps after they've submitted a form to indicate what they want to purchase.
This code snippet will create a Stripe checkout page and send information to the front end that you can use to redirect the user there. We will typically use this when a user has just submitted a form and we'll trigger this for the form's On Form Submitted callback.
Code
Language: JavaScript
let stripeTransactionId;let customerEmail = "bob@bobness.co.uk";let successUrl = "FULL URL OF THE SUCCESS URL GOES HERE";processCardPayment(10, 1, "Line item name goes here", customerEmail, successUrl);function processCardPayment(pricePerUnit, quantOfUnit, unitName, customerEmail, successUrl) {$.ajax({url: 'FULL URL OF SERVERLESS FUNCTION GOES HERE',method: 'GET',data: {pricePerUnit: pricePerUnit,quantOfUnit: quantOfUnit,unitName: unitName,customerEmail: customerEmail,successUrl: successUrl},success: function(response) {// Handle the success response hereconsole.log(response);stripeTransactionId = response.payment_intent;searchHubSpot("contact", "email", customerEmail);window.location.href = response.url;},error: function(xhr, status, error) {// Handle any errors hereconsole.error(error);}});}
// See if the record exists in HubSpotfunction searchHubSpot(objectType, propertyName, searchTerm) {const data = {filterGroups: [{filters: [{propertyName: propertyName,operator: 'EQ',value: searchTerm}]}]};const config = {headers: {'Content-Type': 'application/json','Authorization': accessToken}};axios.post(`https://api.hubapi.com/crm/v3/objects/${objectType}/search`, data, config).then(response => {const { total, results } = response.data;if (total === 1) {console.log("Record exists");objectId = results[0].properties.hs_object_id;updateRecord("stripe_transaction_id", stripeTransactionId, "contact", objectId);// Call processing function for single record} else {console.log("Record doesn't exist");// Call processing function for no recordscreateRecord("contact", stripeTransactionId);}}).catch(error => {console.log("Error while checking if record exists");});}
function createRecord(objectType, stripeTransactionId) {const data = {properties: {email: customerEmail,stripeTransactionId: stripeTransactionId},
associations: [{to: {id: associationRecord},types: [{associationCategory: associationCategory,associationTypeId: associationType}]}]};
console.log("The data is: " + JSON.stringify(data));axios.post(`https://api.hubapi.com/crm/v3/objects/objectType`, data, {headers: {'Authorization': accessToken,'content-type': 'application/json'}}).then(response => {console.log("Success creating record");}).catch(error => {console.log(`Error creating record. The error message from HubSpot was ${error.message}`);});}function updateObject(propertyName, propertyValue, objectType, objectId) {const data = {properties: {`${propertyName}: '${propertyValue}'`}};const config = {headers: {'Content-Type': 'application/json','Authorization': accessToken}};axios.patch(`https://api.hubapi.com/crm/v3/objects/${objectType}/${objectId}`, data, config).then(response => {console.log(`Successfully updated the ${objectType} with ID of ${objectId}. ${propertyName} is now has a value of ${propertyValue}`);}).catch(error => {console.log(`Error while updating the ${objectType} with ID of ${objectId}. Received this error message from HubSpot: ${error.message}`);});}
How can SpotDev support your business?
HubSpot Migrations
Move from Salesforce, Dynamics, Pipedrive or any CRM to HubSpot with SpotDev.
Learn moreHubSpot Integrations
Add advanced functionality to your HubSpot portal with API development services.
Learn moreRevOps
Align your sales, marketing and service teams to break down silos and trigger growth.
Learn more