n8n/packages/nodes-base/nodes/Stripe/Stripe.node.ts

506 lines
14 KiB
TypeScript
Raw Normal View History

:sparkles: Add Stripe regular node (#1470) * :tada: Register node * :art: Add SVG icon * :zap: Add preliminary node stub * :zap: Add resource description stubs * :art: Fix SVG size and position * :zap: Implement charge operations * :zap: Implement customer operations * :art: Apply cosmetic changes * :zap: Fix customer address fields * :zap: Add stub and fields for invoice * :zap: Add invoice item stubs * :zap: Implement source operations * :zap: Reduce scope per feedback * :zap: Add continueOnFail functionality * :art: Prettify error thrown * :fire: Remove unused resource * :zap: Replace source in card with token * :hammer: Remove logging * :wrench: Fix shipping address in charge:create * :wrench: Load update fields for charge:update * :zap: Implement token:create to ease testing * :zap: Simplify card token fields * :zap: Update description parameters * :wrench: Fix field adjusters * :zap: Remove unused source options * :wrench: Fix shipping fields adjuster * :fire: Remove PNG icon * :fire: Remove logging * :hammer: Reorder address fields * :bug: Fix shipping field in charge:update * :lipstick: Apply cosmetic change * :zap: Small improvements * :zap: Fix lintings in main file * :zap: Lint all descriptions * :zap: Add target="_blank" attribute * :zap: Fix various lintings for charge * :zap: Fix lintings for coupon * :zap: Fix lintings for customer * :zap: Fix lintings for source * :zap: Fix lintings for token * :zap: Reorder address fields * :zap: Fix casing in credentials * :hammer: Place recipient name above address in shipping * :zap: Remove references to string in descriptions * :zap: Apply minor renamings * :fire: Remove logging * :hammer: Simplify error handling * :zap: Fix indentation * :zap: Move cardFields to root level for Token creation Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-07-10 04:15:14 -07:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
isEmpty,
} from 'lodash';
import {
adjustChargeFields,
adjustCustomerFields,
adjustMetadata,
handleListing,
loadResource,
stripeApiRequest,
} from './helpers';
import {
balanceOperations,
chargeFields,
chargeOperations,
couponFields,
couponOperations,
customerCardFields,
customerCardOperations,
customerFields,
customerOperations,
sourceFields,
sourceOperations,
tokenFields,
tokenOperations,
} from './descriptions';
export class Stripe implements INodeType {
description: INodeTypeDescription = {
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',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'stripeApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
refactor: Apply more nodelinting rules (#3324) * :pencil2: Alphabetize lint rules * :fire: Remove duplicates * :zap: Update `lintfix` script * :shirt: Apply `node-param-operation-without-no-data-expression` (#3329) * :shirt: Apply `node-param-operation-without-no-data-expression` * :shirt: Add exceptions * :shirt: Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-value-duplicate` (#3331) * :shirt: Apply `node-param-description-miscased-json` (#3337) * :shirt: Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * :pencil2: Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-simplify` (#3334) * :zap: fix * :zap: exceptions * :zap: changed rule ignoring from file to line * :shirt: Apply `node-param-resource-without-no-data-expression` (#3339) * :shirt: Apply `node-param-display-name-untrimmed` (#3341) * :shirt: Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-resource-with-plural-option` (#3342) * :shirt: Apply `node-param-description-wrong-for-upsert` (#3333) * :zap: fix * :zap: replaced record with contact in description * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-description-identical-to-name` (#3343) * :shirt: Apply `node-param-option-name-containing-star` (#3347) * :shirt: Apply `node-param-display-name-wrong-for-update-fields` (#3348) * :shirt: Apply `node-param-option-name-wrong-for-get-all` (#3345) * :zap: fix * :zap: exceptions * :shirt: Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * :shirt: Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Alphabetize lint rules * :zap: Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
2022-05-20 14:47:24 -07:00
noDataExpression: true,
:sparkles: Add Stripe regular node (#1470) * :tada: Register node * :art: Add SVG icon * :zap: Add preliminary node stub * :zap: Add resource description stubs * :art: Fix SVG size and position * :zap: Implement charge operations * :zap: Implement customer operations * :art: Apply cosmetic changes * :zap: Fix customer address fields * :zap: Add stub and fields for invoice * :zap: Add invoice item stubs * :zap: Implement source operations * :zap: Reduce scope per feedback * :zap: Add continueOnFail functionality * :art: Prettify error thrown * :fire: Remove unused resource * :zap: Replace source in card with token * :hammer: Remove logging * :wrench: Fix shipping address in charge:create * :wrench: Load update fields for charge:update * :zap: Implement token:create to ease testing * :zap: Simplify card token fields * :zap: Update description parameters * :wrench: Fix field adjusters * :zap: Remove unused source options * :wrench: Fix shipping fields adjuster * :fire: Remove PNG icon * :fire: Remove logging * :hammer: Reorder address fields * :bug: Fix shipping field in charge:update * :lipstick: Apply cosmetic change * :zap: Small improvements * :zap: Fix lintings in main file * :zap: Lint all descriptions * :zap: Add target="_blank" attribute * :zap: Fix various lintings for charge * :zap: Fix lintings for coupon * :zap: Fix lintings for customer * :zap: Fix lintings for source * :zap: Fix lintings for token * :zap: Reorder address fields * :zap: Fix casing in credentials * :hammer: Place recipient name above address in shipping * :zap: Remove references to string in descriptions * :zap: Apply minor renamings * :fire: Remove logging * :hammer: Simplify error handling * :zap: Fix indentation * :zap: Move cardFields to root level for Token creation Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-07-10 04:15:14 -07:00
options: [
{
name: 'Balance',
value: 'balance',
},
{
name: 'Charge',
value: 'charge',
},
{
name: 'Coupon',
value: 'coupon',
},
{
name: 'Customer',
value: 'customer',
},
{
name: 'Customer Card',
value: 'customerCard',
},
{
name: 'Source',
value: 'source',
},
{
name: 'Token',
value: 'token',
},
],
default: 'balance',
},
...balanceOperations,
...customerCardOperations,
...customerCardFields,
...chargeOperations,
...chargeFields,
...couponOperations,
...couponFields,
...customerOperations,
...customerFields,
...sourceOperations,
...sourceFields,
...tokenOperations,
...tokenFields,
],
};
methods = {
loadOptions: {
async getCustomers(this: ILoadOptionsFunctions) {
return await loadResource.call(this, 'customer');
},
async getCurrencies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { data } = await 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(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let responseData;
const returnData: IDataObject[] = [];
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 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),
} as IDataObject;
const customerId = this.getNodeParameter('customerId', i);
const endpoint = `/customers/${customerId}/sources`;
responseData = await 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 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 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) as string).toLowerCase(),
amount: this.getNodeParameter('amount', i),
source: this.getNodeParameter('source', i),
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (!isEmpty(additionalFields)) {
Object.assign(body, adjustChargeFields(additionalFields));
}
responseData = await stripeApiRequest.call(this, 'POST', '/charges', body, {});
} else if (operation === 'get') {
// ----------------------------------
// charge: get
// ----------------------------------
const chargeId = this.getNodeParameter('chargeId', i);
responseData = await stripeApiRequest.call(this, 'GET', `/charges/${chargeId}`, {}, {});
} else if (operation === 'getAll') {
// ----------------------------------
// charge: getAll
// ----------------------------------
responseData = await handleListing.call(this, resource, i);
:sparkles: Add Stripe regular node (#1470) * :tada: Register node * :art: Add SVG icon * :zap: Add preliminary node stub * :zap: Add resource description stubs * :art: Fix SVG size and position * :zap: Implement charge operations * :zap: Implement customer operations * :art: Apply cosmetic changes * :zap: Fix customer address fields * :zap: Add stub and fields for invoice * :zap: Add invoice item stubs * :zap: Implement source operations * :zap: Reduce scope per feedback * :zap: Add continueOnFail functionality * :art: Prettify error thrown * :fire: Remove unused resource * :zap: Replace source in card with token * :hammer: Remove logging * :wrench: Fix shipping address in charge:create * :wrench: Load update fields for charge:update * :zap: Implement token:create to ease testing * :zap: Simplify card token fields * :zap: Update description parameters * :wrench: Fix field adjusters * :zap: Remove unused source options * :wrench: Fix shipping fields adjuster * :fire: Remove PNG icon * :fire: Remove logging * :hammer: Reorder address fields * :bug: Fix shipping field in charge:update * :lipstick: Apply cosmetic change * :zap: Small improvements * :zap: Fix lintings in main file * :zap: Lint all descriptions * :zap: Add target="_blank" attribute * :zap: Fix various lintings for charge * :zap: Fix lintings for coupon * :zap: Fix lintings for customer * :zap: Fix lintings for source * :zap: Fix lintings for token * :zap: Reorder address fields * :zap: Fix casing in credentials * :hammer: Place recipient name above address in shipping * :zap: Remove references to string in descriptions * :zap: Apply minor renamings * :fire: Remove logging * :hammer: Simplify error handling * :zap: Fix indentation * :zap: Move cardFields to root level for Token creation Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-07-10 04:15:14 -07:00
} else if (operation === 'update') {
// ----------------------------------
// charge: update
// ----------------------------------
const body = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (isEmpty(updateFields)) {
throw new Error(`Please enter at least one field to update for the ${resource}.`);
}
Object.assign(body, adjustChargeFields(updateFields));
const chargeId = this.getNodeParameter('chargeId', i);
responseData = await 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),
} as IDataObject;
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 stripeApiRequest.call(this, 'POST', '/coupons', body, {});
} else if (operation === 'getAll') {
// ----------------------------------
// coupon: getAll
// ----------------------------------
responseData = await handleListing.call(this, resource, i);
:sparkles: Add Stripe regular node (#1470) * :tada: Register node * :art: Add SVG icon * :zap: Add preliminary node stub * :zap: Add resource description stubs * :art: Fix SVG size and position * :zap: Implement charge operations * :zap: Implement customer operations * :art: Apply cosmetic changes * :zap: Fix customer address fields * :zap: Add stub and fields for invoice * :zap: Add invoice item stubs * :zap: Implement source operations * :zap: Reduce scope per feedback * :zap: Add continueOnFail functionality * :art: Prettify error thrown * :fire: Remove unused resource * :zap: Replace source in card with token * :hammer: Remove logging * :wrench: Fix shipping address in charge:create * :wrench: Load update fields for charge:update * :zap: Implement token:create to ease testing * :zap: Simplify card token fields * :zap: Update description parameters * :wrench: Fix field adjusters * :zap: Remove unused source options * :wrench: Fix shipping fields adjuster * :fire: Remove PNG icon * :fire: Remove logging * :hammer: Reorder address fields * :bug: Fix shipping field in charge:update * :lipstick: Apply cosmetic change * :zap: Small improvements * :zap: Fix lintings in main file * :zap: Lint all descriptions * :zap: Add target="_blank" attribute * :zap: Fix various lintings for charge * :zap: Fix lintings for coupon * :zap: Fix lintings for customer * :zap: Fix lintings for source * :zap: Fix lintings for token * :zap: Reorder address fields * :zap: Fix casing in credentials * :hammer: Place recipient name above address in shipping * :zap: Remove references to string in descriptions * :zap: Apply minor renamings * :fire: Remove logging * :hammer: Simplify error handling * :zap: Fix indentation * :zap: Move cardFields to root level for Token creation Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-07-10 04:15:14 -07:00
}
} else if (resource === 'customer') {
// *********************************************************************
// customer
// *********************************************************************
// https://stripe.com/docs/api/customers
if (operation === 'create') {
// ----------------------------------
// customer: create
// ----------------------------------
const body = {
name: this.getNodeParameter('name', i),
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (!isEmpty(additionalFields)) {
Object.assign(body, adjustCustomerFields(additionalFields));
}
responseData = await stripeApiRequest.call(this, 'POST', '/customers', body, {});
} else if (operation === 'delete') {
// ----------------------------------
// customer: delete
// ----------------------------------
const customerId = this.getNodeParameter('customerId', i);
responseData = await stripeApiRequest.call(this, 'DELETE', `/customers/${customerId}`, {}, {});
} else if (operation === 'get') {
// ----------------------------------
// customer: get
// ----------------------------------
const customerId = this.getNodeParameter('customerId', i);
responseData = await stripeApiRequest.call(this, 'GET', `/customers/${customerId}`, {}, {});
} else if (operation === 'getAll') {
// ----------------------------------
// customer: getAll
// ----------------------------------
const qs = {} as IDataObject;
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (!isEmpty(filters)) {
qs.email = filters.email;
}
responseData = await handleListing.call(this, resource, i, qs);
:sparkles: Add Stripe regular node (#1470) * :tada: Register node * :art: Add SVG icon * :zap: Add preliminary node stub * :zap: Add resource description stubs * :art: Fix SVG size and position * :zap: Implement charge operations * :zap: Implement customer operations * :art: Apply cosmetic changes * :zap: Fix customer address fields * :zap: Add stub and fields for invoice * :zap: Add invoice item stubs * :zap: Implement source operations * :zap: Reduce scope per feedback * :zap: Add continueOnFail functionality * :art: Prettify error thrown * :fire: Remove unused resource * :zap: Replace source in card with token * :hammer: Remove logging * :wrench: Fix shipping address in charge:create * :wrench: Load update fields for charge:update * :zap: Implement token:create to ease testing * :zap: Simplify card token fields * :zap: Update description parameters * :wrench: Fix field adjusters * :zap: Remove unused source options * :wrench: Fix shipping fields adjuster * :fire: Remove PNG icon * :fire: Remove logging * :hammer: Reorder address fields * :bug: Fix shipping field in charge:update * :lipstick: Apply cosmetic change * :zap: Small improvements * :zap: Fix lintings in main file * :zap: Lint all descriptions * :zap: Add target="_blank" attribute * :zap: Fix various lintings for charge * :zap: Fix lintings for coupon * :zap: Fix lintings for customer * :zap: Fix lintings for source * :zap: Fix lintings for token * :zap: Reorder address fields * :zap: Fix casing in credentials * :hammer: Place recipient name above address in shipping * :zap: Remove references to string in descriptions * :zap: Apply minor renamings * :fire: Remove logging * :hammer: Simplify error handling * :zap: Fix indentation * :zap: Move cardFields to root level for Token creation Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-07-10 04:15:14 -07:00
} else if (operation === 'update') {
// ----------------------------------
// customer: update
// ----------------------------------
const body = {} as IDataObject;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (isEmpty(updateFields)) {
throw new Error(`Please enter at least one field to update for the ${resource}.`);
}
Object.assign(body, adjustCustomerFields(updateFields));
const customerId = this.getNodeParameter('customerId', i);
responseData = await 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),
} as IDataObject;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (!isEmpty(additionalFields)) {
Object.assign(body, adjustMetadata(additionalFields));
}
responseData = await stripeApiRequest.call(this, 'POST', '/sources', body, {});
// attach source to customer
const endpoint = `/customers/${customerId}/sources`;
await 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 stripeApiRequest.call(this, 'DELETE', endpoint, {}, {});
} else if (operation === 'get') {
// ----------------------------------
// source: get
// ----------------------------------
const sourceId = this.getNodeParameter('sourceId', i);
responseData = await 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 = {} as IDataObject;
if (type !== 'cardToken') {
throw new Error('Only card token creation implemented.');
}
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 stripeApiRequest.call(this, 'POST', '/tokens', body, {});
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}