mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-17 01:54:06 -08:00
b4f4ecc77e
* 🎉 Register node * 🎨 Add SVG icon * ⚡ Add preliminary node stub * ⚡ Add resource description stubs * 🎨 Fix SVG size and position * ⚡ Implement charge operations * ⚡ Implement customer operations * 🎨 Apply cosmetic changes * ⚡ Fix customer address fields * ⚡ Add stub and fields for invoice * ⚡ Add invoice item stubs * ⚡ Implement source operations * ⚡ Reduce scope per feedback * ⚡ Add continueOnFail functionality * 🎨 Prettify error thrown * 🔥 Remove unused resource * ⚡ Replace source in card with token * 🔨 Remove logging * 🔧 Fix shipping address in charge:create * 🔧 Load update fields for charge:update * ⚡ Implement token:create to ease testing * ⚡ Simplify card token fields * ⚡ Update description parameters * 🔧 Fix field adjusters * ⚡ Remove unused source options * 🔧 Fix shipping fields adjuster * 🔥 Remove PNG icon * 🔥 Remove logging * 🔨 Reorder address fields * 🐛 Fix shipping field in charge:update * 💄 Apply cosmetic change * ⚡ Small improvements * ⚡ Fix lintings in main file * ⚡ Lint all descriptions * ⚡ Add target="_blank" attribute * ⚡ Fix various lintings for charge * ⚡ Fix lintings for coupon * ⚡ Fix lintings for customer * ⚡ Fix lintings for source * ⚡ Fix lintings for token * ⚡ Reorder address fields * ⚡ Fix casing in credentials * 🔨 Place recipient name above address in shipping * ⚡ Remove references to string in descriptions * ⚡ Apply minor renamings * 🔥 Remove logging * 🔨 Simplify error handling * ⚡ Fix indentation * ⚡ Move cardFields to root level for Token creation Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
173 lines
3.8 KiB
TypeScript
173 lines
3.8 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
flow,
|
|
isEmpty,
|
|
omit,
|
|
} from 'lodash';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodePropertyOptions,
|
|
} from 'n8n-workflow';
|
|
|
|
/**
|
|
* Make an API request to Stripe
|
|
*
|
|
* @param {IHookFunctions} this
|
|
* @param {string} method
|
|
* @param {string} url
|
|
* @param {object} body
|
|
* @returns {Promise<any>}
|
|
*/
|
|
export async function stripeApiRequest(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: object,
|
|
query?: object,
|
|
) {
|
|
const credentials = this.getCredentials('stripeApi');
|
|
if (credentials === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
}
|
|
|
|
const options = {
|
|
method,
|
|
auth: {
|
|
user: credentials.secretKey as string,
|
|
},
|
|
form: body,
|
|
qs: query,
|
|
uri: `https://api.stripe.com/v1${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
if (options.qs && Object.keys(options.qs).length === 0) {
|
|
delete options.qs;
|
|
}
|
|
|
|
try {
|
|
return await this.helpers.request!.call(this, options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make n8n's charge fields compliant with the Stripe API request object.
|
|
*/
|
|
export const adjustChargeFields = flow([
|
|
adjustShipping,
|
|
adjustMetadata,
|
|
]);
|
|
|
|
/**
|
|
* Make n8n's customer fields compliant with the Stripe API request object.
|
|
*/
|
|
export const adjustCustomerFields = flow([
|
|
adjustShipping,
|
|
adjustAddress,
|
|
adjustMetadata,
|
|
]);
|
|
|
|
/**
|
|
* Convert n8n's address object into a Stripe API request shipping object.
|
|
*/
|
|
function adjustAddress(
|
|
addressFields: { address: { details: IDataObject } },
|
|
) {
|
|
if (!addressFields.address) return addressFields;
|
|
|
|
return {
|
|
...omit(addressFields, ['address']),
|
|
address: addressFields.address.details,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert n8n's `fixedCollection` metadata object into a Stripe API request metadata object.
|
|
*/
|
|
export function adjustMetadata(
|
|
fields: { metadata?: { metadataProperties: Array<{ key: string; value: string }> } },
|
|
) {
|
|
if (!fields.metadata || isEmpty(fields.metadata)) return fields;
|
|
|
|
let adjustedMetadata = {};
|
|
|
|
fields.metadata.metadataProperties.forEach(pair => {
|
|
adjustedMetadata = { ...adjustedMetadata, ...pair };
|
|
});
|
|
|
|
return {
|
|
...omit(fields, ['metadata']),
|
|
metadata: adjustedMetadata,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert n8n's shipping object into a Stripe API request shipping object.
|
|
*/
|
|
function adjustShipping(
|
|
shippingFields: { shipping?: { shippingProperties: Array<{ address: { details: IDataObject }; name: string }> } },
|
|
) {
|
|
const shippingProperties = shippingFields.shipping?.shippingProperties[0];
|
|
|
|
if (!shippingProperties?.address || isEmpty(shippingProperties.address)) return shippingFields;
|
|
|
|
return {
|
|
...omit(shippingFields, ['shipping']),
|
|
shipping: {
|
|
...omit(shippingProperties, ['address']),
|
|
address: shippingProperties.address.details,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Load a resource so it can be selected by name from a dropdown.
|
|
*/
|
|
export async function loadResource(
|
|
this: ILoadOptionsFunctions,
|
|
resource: 'charge' | 'customer' | 'source',
|
|
): Promise<INodePropertyOptions[]> {
|
|
const responseData = await stripeApiRequest.call(this, 'GET', `/${resource}s`, {}, {});
|
|
|
|
return responseData.data.map(({ name, id }: { name: string, id: string }) => ({
|
|
name,
|
|
value: id,
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Handles a Stripe listing by returning all items or up to a limit.
|
|
*/
|
|
export async function handleListing(
|
|
this: IExecuteFunctions,
|
|
resource: string,
|
|
qs: IDataObject = {},
|
|
) {
|
|
let responseData;
|
|
|
|
responseData = await stripeApiRequest.call(this, 'GET', `/${resource}s`, qs, {});
|
|
responseData = responseData.data;
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
|
|
if (!returnAll) {
|
|
const limit = this.getNodeParameter('limit', 0) as number;
|
|
responseData = responseData.slice(0, limit);
|
|
}
|
|
|
|
return responseData;
|
|
}
|