2019-10-15 05:46:42 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
2021-07-10 04:15:14 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
flow,
|
|
|
|
isEmpty,
|
|
|
|
omit,
|
|
|
|
} from 'lodash';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodePropertyOptions,
|
|
|
|
} from 'n8n-workflow';
|
2019-10-15 05:46:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Stripe
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-15 05:46:42 -07:00
|
|
|
try {
|
feat: Add more credentials tests (#3668)
* ✨ Add injection to notion,
Add test to notion in cred
* 🔥 Remove unuse method
* 🎨 Move testing from node file to cred file
* ✨ Add injection and testing in facebook graph
* Add cred injec with testing
* Add Cred injection and cred test
* Add cred injection, and cred testing for typeform, fix issue in clickup
* Add cred injection, move testing inside creds
* Add cred injection and cred testing to SendGrid
* Add cred injection and cred testing to woocommerce
* Add cred injection, add cred test to gitlab
* 🔥 Fix duplicated imports in Mautic cred
* 🔥 removed unused credentials testing in node
* Add cred injection, cred testing, handles slash trailing for Grafana node
* Add cred injection, cred testing to shopify
* Add cred injection , add cred testing to stripe
* changed cred injection, add testing to cred for mattermost
* add cred injection and testing for dropbox
* Add cred injection, cred testing to webflow
* ✨ Add cred injection and cred test to nocodb
* ✨ Add cred injection, cred testing to mailchimp
* 🐛 fix a bug In credentials testing
* ✨ Add cred injection, cred testing to sms77
* ✨ Add cred injection, cred testing to ActiveCampaign
* Add cred injection, cred testing to TheHive
* ✨ Add cred injection, add cred testing to ApiTemplateio
* ✨ Add cred injection, add cred testing for zoom
* ✨ Add cred injection, cred testing to rocketchat
* ✨ Add cred injection, add cred test to getResponse
* 🔥 Remove useless authentcate creds and testing from facebookGraphApp
* 🔥 Remove useless imports in FacebookGrappApp credentials file
* 🔥 Removed useless imports and if statement
* 🐛 Add version to header when testing cred
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
2022-07-15 07:20:41 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'stripeApi', options);
|
2019-10-15 05:46:42 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-10-15 05:46:42 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-10 04:15:14 -07: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,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2021-11-03 17:44:25 -07:00
|
|
|
const adjustedMetadata: Record<string, string> = {};
|
2021-07-10 04:15:14 -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.
|
|
|
|
*/
|
|
|
|
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,
|
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;
|
|
|
|
|
2021-11-03 17:42:57 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const limit = this.getNodeParameter('limit', i, 0) as number;
|
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
|
|
|
}
|