Reconcile Stripe payments (from payment intent)
Category:
Serverless Function,
CMS Enterprise
ETL:
Load
Notes
First, ensure that you are sending success webhooks from Stripe to your serverless function. This can be configured in the Stripe dashboard by clicking Developers>Webhooks.
Code
Language: JavaScript
const axios = require('axios');
const accessToken = `Bearer 1234`; // You'll want to use secrets to dynamically insert this token
exports.main = async (context, sendResponse) => {
// Extract the body from the incoming request
let objectId;
let objectType = "contact";
const requestBody = context.body;
const stripePaymentIntentId = requestBody.data.object.id;
console.log("The stripePaymentIntentId is: " + stripePaymentIntentId);
// Tell Stripe that the webhook was received
sendResponse({ statusCode: 200, body: { received: true } });
searchHubSpot();
// See if the record exists in HubSpot
function searchHubSpot() {
const data = {
filterGroups: [
{
filters: [
{
propertyName: 'stripe_transaction_id',
operator: 'EQ',
value: stripePaymentIntentId
}
]
}
]
};
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 > 0) {
console.log("Transaction exists");
objectId = results[0].properties.hs_object_id;
updateObject();
} else {
console.log("Transaction doesn't exist");
return;
}
})
.catch(error => {
console.log("Error while checking if transaction exists");
});
}
// If the object exists, update it in HubSpot
function updateObject() {
const data = {
properties: {
payment_status: 'Complete'
}
};
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 ${objectId} as Complete`);
})
.catch(error => {
console.log("Error while updating record");
});
}
};
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