UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

423 lines 23.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stripe = void 0; const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const n8n_workflow_1 = require("n8n-workflow"); const descriptions_1 = require("./descriptions"); const helpers_1 = require("./helpers"); class Stripe { description = { displayName: 'Stripe', name: 'stripe', icon: 'file:stripe.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume the Stripe API', defaults: { name: 'Stripe', }, usableAsTool: true, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'stripeApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Balance', value: 'balance', }, { name: 'Charge', value: 'charge', }, { name: 'Coupon', value: 'coupon', }, { name: 'Customer', value: 'customer', }, { name: 'Customer Card', value: 'customerCard', }, { name: 'Meter Event', value: 'meterEvent', }, { name: 'Source', value: 'source', }, { name: 'Token', value: 'token', }, ], default: 'balance', }, ...descriptions_1.balanceOperations, ...descriptions_1.customerCardOperations, ...descriptions_1.customerCardFields, ...descriptions_1.chargeOperations, ...descriptions_1.chargeFields, ...descriptions_1.couponOperations, ...descriptions_1.couponFields, ...descriptions_1.customerOperations, ...descriptions_1.customerFields, ...descriptions_1.meterEventOperations, ...descriptions_1.meterEventFields, ...descriptions_1.sourceOperations, ...descriptions_1.sourceFields, ...descriptions_1.tokenOperations, ...descriptions_1.tokenFields, ], }; methods = { loadOptions: { async getCustomers() { return await helpers_1.loadResource.call(this, 'customer'); }, async getCurrencies() { const returnData = []; const { data } = await helpers_1.stripeApiRequest.call(this, 'GET', '/country_specs', {}); for (const currency of data[0].supported_payment_currencies) { returnData.push({ name: currency.toUpperCase(), value: currency, }); } return returnData; }, }, }; async execute() { const items = this.getInputData(); const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); let responseData; const returnData = []; for (let i = 0; i < items.length; i++) { try { if (resource === 'balance') { // ********************************************************************* // balance // ********************************************************************* // https://stripe.com/docs/api/balance if (operation === 'get') { // ---------------------------------- // balance: get // ---------------------------------- responseData = await helpers_1.stripeApiRequest.call(this, 'GET', '/balance', {}, {}); } } else if (resource === 'customerCard') { // ********************************************************************* // customer card // ********************************************************************* // https://stripe.com/docs/api/cards if (operation === 'add') { // ---------------------------------- // customerCard: add // ---------------------------------- const body = { source: this.getNodeParameter('token', i), }; const customerId = this.getNodeParameter('customerId', i); const endpoint = `/customers/${customerId}/sources`; responseData = await helpers_1.stripeApiRequest.call(this, 'POST', endpoint, body, {}); } else if (operation === 'remove') { // ---------------------------------- // customerCard: remove // ---------------------------------- const customerId = this.getNodeParameter('customerId', i); const cardId = this.getNodeParameter('cardId', i); const endpoint = `/customers/${customerId}/sources/${cardId}`; responseData = await helpers_1.stripeApiRequest.call(this, 'DELETE', endpoint, {}, {}); } else if (operation === 'get') { // ---------------------------------- // customerCard: get // ---------------------------------- const customerId = this.getNodeParameter('customerId', i); const sourceId = this.getNodeParameter('sourceId', i); const endpoint = `/customers/${customerId}/sources/${sourceId}`; responseData = await helpers_1.stripeApiRequest.call(this, 'GET', endpoint, {}, {}); } } else if (resource === 'charge') { // ********************************************************************* // charge // ********************************************************************* // https://stripe.com/docs/api/charges if (operation === 'create') { // ---------------------------------- // charge: create // ---------------------------------- const body = { customer: this.getNodeParameter('customerId', i), currency: this.getNodeParameter('currency', i).toLowerCase(), amount: this.getNodeParameter('amount', i), source: this.getNodeParameter('source', i), }; const additionalFields = this.getNodeParameter('additionalFields', i); if (!(0, isEmpty_1.default)(additionalFields)) { Object.assign(body, (0, helpers_1.adjustChargeFields)(additionalFields)); } responseData = await helpers_1.stripeApiRequest.call(this, 'POST', '/charges', body, {}); } else if (operation === 'get') { // ---------------------------------- // charge: get // ---------------------------------- const chargeId = this.getNodeParameter('chargeId', i); responseData = await helpers_1.stripeApiRequest.call(this, 'GET', `/charges/${chargeId}`, {}, {}); } else if (operation === 'getAll') { // ---------------------------------- // charge: getAll // ---------------------------------- responseData = await helpers_1.handleListing.call(this, resource, i); } else if (operation === 'update') { // ---------------------------------- // charge: update // ---------------------------------- const body = {}; const updateFields = this.getNodeParameter('updateFields', i); if ((0, isEmpty_1.default)(updateFields)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`, { itemIndex: i }); } Object.assign(body, (0, helpers_1.adjustChargeFields)(updateFields)); const chargeId = this.getNodeParameter('chargeId', i); responseData = await helpers_1.stripeApiRequest.call(this, 'POST', `/charges/${chargeId}`, body, {}); } } else if (resource === 'coupon') { // ********************************************************************* // coupon // ********************************************************************* // https://stripe.com/docs/api/coupons if (operation === 'create') { // ---------------------------------- // coupon: create // ---------------------------------- const body = { duration: this.getNodeParameter('duration', i), }; const type = this.getNodeParameter('type', i); if (type === 'fixedAmount') { body.amount_off = this.getNodeParameter('amountOff', i); body.currency = this.getNodeParameter('currency', i); } else { body.percent_off = this.getNodeParameter('percentOff', i); } responseData = await helpers_1.stripeApiRequest.call(this, 'POST', '/coupons', body, {}); } else if (operation === 'getAll') { // ---------------------------------- // coupon: getAll // ---------------------------------- responseData = await helpers_1.handleListing.call(this, resource, i); } } else if (resource === 'customer') { // ********************************************************************* // customer // ********************************************************************* // https://stripe.com/docs/api/customers if (operation === 'create') { // ---------------------------------- // customer: create // ---------------------------------- const body = { name: this.getNodeParameter('name', i), }; const additionalFields = this.getNodeParameter('additionalFields', i); if (!(0, isEmpty_1.default)(additionalFields)) { Object.assign(body, (0, helpers_1.adjustCustomerFields)(additionalFields)); } responseData = await helpers_1.stripeApiRequest.call(this, 'POST', '/customers', body, {}); } else if (operation === 'delete') { // ---------------------------------- // customer: delete // ---------------------------------- const customerId = this.getNodeParameter('customerId', i); responseData = await helpers_1.stripeApiRequest.call(this, 'DELETE', `/customers/${customerId}`, {}, {}); } else if (operation === 'get') { // ---------------------------------- // customer: get // ---------------------------------- const customerId = this.getNodeParameter('customerId', i); responseData = await helpers_1.stripeApiRequest.call(this, 'GET', `/customers/${customerId}`, {}, {}); } else if (operation === 'getAll') { // ---------------------------------- // customer: getAll // ---------------------------------- const qs = {}; const filters = this.getNodeParameter('filters', i); if (!(0, isEmpty_1.default)(filters)) { qs.email = filters.email; } responseData = await helpers_1.handleListing.call(this, resource, i, qs); } else if (operation === 'update') { // ---------------------------------- // customer: update // ---------------------------------- const body = {}; const updateFields = this.getNodeParameter('updateFields', i); if ((0, isEmpty_1.default)(updateFields)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`, { itemIndex: i }); } Object.assign(body, (0, helpers_1.adjustCustomerFields)(updateFields)); const customerId = this.getNodeParameter('customerId', i); responseData = await helpers_1.stripeApiRequest.call(this, 'POST', `/customers/${customerId}`, body, {}); } } else if (resource === 'source') { // ********************************************************************* // source // ********************************************************************* // https://stripe.com/docs/api/sources if (operation === 'create') { // ---------------------------------- // source: create // ---------------------------------- const customerId = this.getNodeParameter('customerId', i); const body = { type: this.getNodeParameter('type', i), amount: this.getNodeParameter('amount', i), currency: this.getNodeParameter('currency', i), }; const additionalFields = this.getNodeParameter('additionalFields', i); if (!(0, isEmpty_1.default)(additionalFields)) { Object.assign(body, (0, helpers_1.adjustMetadata)(additionalFields)); } responseData = await helpers_1.stripeApiRequest.call(this, 'POST', '/sources', body, {}); // attach source to customer const endpoint = `/customers/${customerId}/sources`; await helpers_1.stripeApiRequest.call(this, 'POST', endpoint, { source: responseData.id }, {}); } else if (operation === 'delete') { // ---------------------------------- // source: delete // ---------------------------------- const sourceId = this.getNodeParameter('sourceId', i); const customerId = this.getNodeParameter('customerId', i); const endpoint = `/customers/${customerId}/sources/${sourceId}`; responseData = await helpers_1.stripeApiRequest.call(this, 'DELETE', endpoint, {}, {}); } else if (operation === 'get') { // ---------------------------------- // source: get // ---------------------------------- const sourceId = this.getNodeParameter('sourceId', i); responseData = await helpers_1.stripeApiRequest.call(this, 'GET', `/sources/${sourceId}`, {}, {}); } } else if (resource === 'token') { // ********************************************************************* // token // ********************************************************************* // https://stripe.com/docs/api/tokens if (operation === 'create') { // ---------------------------------- // token: create // ---------------------------------- const type = this.getNodeParameter('type', i); const body = {}; if (type !== 'cardToken') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Only card token creation implemented.', { itemIndex: i }); } body.card = { number: this.getNodeParameter('number', i), exp_month: this.getNodeParameter('expirationMonth', i), exp_year: this.getNodeParameter('expirationYear', i), cvc: this.getNodeParameter('cvc', i), }; responseData = await helpers_1.stripeApiRequest.call(this, 'POST', '/tokens', body, {}); } } else if (resource === 'meterEvent') { // ********************************************************************* // meter event // ********************************************************************* // https://stripe.com/docs/api/billing/meter-event if (operation === 'create') { // ---------------------------------- // meterEvent: create // ---------------------------------- const eventName = this.getNodeParameter('eventName', i); const customerId = this.getNodeParameter('customerId', i); const value = this.getNodeParameter('value', i); const payload = { stripe_customer_id: customerId, value, }; const body = { event_name: eventName, payload, }; const additionalFields = this.getNodeParameter('additionalFields', i); if (!(0, isEmpty_1.default)(additionalFields)) { if (additionalFields.identifier) { body.identifier = additionalFields.identifier; } if (additionalFields.timestamp) { // Convert ISO date string to Unix timestamp const timestamp = new Date(additionalFields.timestamp).getTime() / 1000; body.timestamp = Math.floor(timestamp); } if (additionalFields.customPayload) { const customPayloadData = additionalFields.customPayload; if (customPayloadData.properties && customPayloadData.properties.length > 0) { customPayloadData.properties.forEach((prop) => { // Guard against overwriting required fields if (prop.key !== 'stripe_customer_id' && prop.key !== 'value') { payload[prop.key] = prop.value; } }); } } } responseData = await helpers_1.stripeApiRequest.call(this, 'POST', '/billing/meter_events', body, {}); } } } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } return [returnData]; } } exports.Stripe = Stripe; //# sourceMappingURL=Stripe.node.js.map