2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
import { flow, isEmpty, omit } from 'lodash';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
|
2019-10-15 05:46:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Stripe
|
|
|
|
*
|
|
|
|
*/
|
2021-07-10 04:15:14 -07:00
|
|
|
export async function stripeApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
query?: object,
|
|
|
|
) {
|
2019-10-15 05:46:42 -07:00
|
|
|
const options = {
|
|
|
|
method,
|
|
|
|
form: body,
|
|
|
|
qs: query,
|
|
|
|
uri: `https://api.stripe.com/v1${endpoint}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-10-15 05:46:42 -07:00
|
|
|
};
|
|
|
|
|
2021-07-10 04:15:14 -07:00
|
|
|
if (options.qs && Object.keys(options.qs).length === 0) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, 'stripeApi', options);
|
2019-10-15 05:46:42 -07:00
|
|
|
}
|
2021-07-10 04:15:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert n8n's address object into a Stripe API request shipping object.
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
function adjustAddress(addressFields: { address: { details: IDataObject } }) {
|
2021-07-10 04:15:14 -07:00
|
|
|
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.
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export function adjustMetadata(fields: {
|
|
|
|
metadata?: { metadataProperties: Array<{ key: string; value: string }> };
|
|
|
|
}) {
|
2021-07-10 04:15:14 -07:00
|
|
|
if (!fields.metadata || isEmpty(fields.metadata)) return fields;
|
|
|
|
|
2021-11-03 17:44:25 -07:00
|
|
|
const adjustedMetadata: Record<string, string> = {};
|
2021-07-10 04:15:14 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
fields.metadata.metadataProperties.forEach((pair) => {
|
2021-11-03 17:44:25 -07:00
|
|
|
adjustedMetadata[pair.key] = pair.value;
|
2021-07-10 04:15:14 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...omit(fields, ['metadata']),
|
|
|
|
metadata: adjustedMetadata,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert n8n's shipping object into a Stripe API request shipping object.
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
function adjustShipping(shippingFields: {
|
|
|
|
shipping?: { shippingProperties: Array<{ address: { details: IDataObject }; name: string }> };
|
|
|
|
}) {
|
2021-07-10 04:15:14 -07:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
|
2021-07-10 04:15:14 -07:00
|
|
|
/**
|
|
|
|
* 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`, {}, {});
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
return responseData.data.map(({ name, id }: { name: string; id: string }) => ({
|
2021-07-10 04:15:14 -07:00
|
|
|
name,
|
|
|
|
value: id,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Stripe listing by returning all items or up to a limit.
|
|
|
|
*/
|
|
|
|
export async function handleListing(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
resource: string,
|
2021-11-03 17:42:57 -07:00
|
|
|
i: number,
|
2021-07-10 04:15:14 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2021-11-03 17:42:57 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
2021-07-10 04:15:14 -07:00
|
|
|
let responseData;
|
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i, 0);
|
2021-07-10 04:15:14 -07:00
|
|
|
|
2021-11-03 17:42:57 -07:00
|
|
|
do {
|
|
|
|
responseData = await stripeApiRequest.call(this, 'GET', `/${resource}s`, {}, qs);
|
|
|
|
returnData.push(...responseData.data);
|
2021-07-10 04:15:14 -07:00
|
|
|
|
2021-11-03 17:42:57 -07:00
|
|
|
if (!returnAll && returnData.length >= limit) {
|
|
|
|
return returnData.slice(0, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
qs.starting_after = returnData[returnData.length - 1].id;
|
|
|
|
} while (responseData.has_more);
|
2021-07-10 04:15:14 -07:00
|
|
|
|
2021-11-03 17:42:57 -07:00
|
|
|
return returnData;
|
2021-07-10 04:15:14 -07:00
|
|
|
}
|