|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import { |
| 3 | + BANKING_PROVIDER_OPTIONS, |
| 4 | + LANGUAGE_OPTIONS, |
| 5 | + PROVIDER_FIELD_NAMES_OPTIONS, |
| 6 | +} from "../../common/constants.mjs"; |
| 7 | +import { parseObject } from "../../common/utils.mjs"; |
| 8 | +import pennylane from "../../pennylane.app.mjs"; |
| 9 | + |
| 10 | +export default { |
| 11 | + key: "pennylane-create-customer-invoice", |
| 12 | + name: "Create Customer Invoice", |
| 13 | + description: "Generates a new invoice for a customer using Pennylane. [See the documentation](https://pennylane.readme.io/reference/customer_invoices-post-1)", |
| 14 | + version: "0.0.1", |
| 15 | + type: "action", |
| 16 | + props: { |
| 17 | + pennylane, |
| 18 | + date: { |
| 19 | + type: "string", |
| 20 | + label: "Date", |
| 21 | + description: "Invoice date (ISO 8601)", |
| 22 | + }, |
| 23 | + deadline: { |
| 24 | + type: "string", |
| 25 | + label: "Deadline", |
| 26 | + description: "Invoice payment deadline (ISO 8601)", |
| 27 | + }, |
| 28 | + externalId: { |
| 29 | + type: "string", |
| 30 | + label: "External Id", |
| 31 | + description: "An id you can use to refer to the invoice from outside of Pennylane", |
| 32 | + optional: true, |
| 33 | + }, |
| 34 | + pdfInvoiceFreeText: { |
| 35 | + type: "string", |
| 36 | + label: "PDF Invoice Free Text", |
| 37 | + description: "For example, the contact details of the person to contact", |
| 38 | + optional: true, |
| 39 | + }, |
| 40 | + pdfInvoiceSubject: { |
| 41 | + type: "string", |
| 42 | + label: "PDF Invoice Subject", |
| 43 | + description: "Invoice title", |
| 44 | + optional: true, |
| 45 | + }, |
| 46 | + draft: { |
| 47 | + type: "boolean", |
| 48 | + label: "Draft", |
| 49 | + description: "Do you wish to create a draft invoice (otherwise it is a finalized invoice)? Reminder, once created, a finalized invoice cannot be edited!", |
| 50 | + }, |
| 51 | + currency: { |
| 52 | + type: "string", |
| 53 | + label: "Currency", |
| 54 | + description: "Invoice Currency (ISO 4217). Default is EUR.", |
| 55 | + optional: true, |
| 56 | + }, |
| 57 | + specialMention: { |
| 58 | + type: "string", |
| 59 | + label: "Special Mention", |
| 60 | + description: "Additional details", |
| 61 | + optional: true, |
| 62 | + }, |
| 63 | + discount: { |
| 64 | + type: "integer", |
| 65 | + label: "Discount", |
| 66 | + description: "Invoice discount (in percent)", |
| 67 | + optional: true, |
| 68 | + }, |
| 69 | + language: { |
| 70 | + type: "string", |
| 71 | + label: "Language", |
| 72 | + description: "invoice pdf language", |
| 73 | + options: LANGUAGE_OPTIONS, |
| 74 | + optional: true, |
| 75 | + }, |
| 76 | + bankingProvider: { |
| 77 | + type: "string", |
| 78 | + label: "Banking Provider", |
| 79 | + description: "The banking provider for the transaction", |
| 80 | + options: BANKING_PROVIDER_OPTIONS, |
| 81 | + reloadProps: true, |
| 82 | + }, |
| 83 | + providerFieldName: { |
| 84 | + type: "string", |
| 85 | + label: "Provider Field Name", |
| 86 | + description: "Name of the field that you want to match", |
| 87 | + options: PROVIDER_FIELD_NAMES_OPTIONS, |
| 88 | + hidden: true, |
| 89 | + }, |
| 90 | + providerFieldValue: { |
| 91 | + type: "string", |
| 92 | + label: "Provider Field Value", |
| 93 | + description: "Value that you want to match", |
| 94 | + }, |
| 95 | + customerId: { |
| 96 | + propDefinition: [ |
| 97 | + pennylane, |
| 98 | + "customerId", |
| 99 | + ], |
| 100 | + }, |
| 101 | + lineItemsSectionsAttributes: { |
| 102 | + propDefinition: [ |
| 103 | + pennylane, |
| 104 | + "lineItemsSectionsAttributes", |
| 105 | + ], |
| 106 | + optional: true, |
| 107 | + }, |
| 108 | + lineItems: { |
| 109 | + propDefinition: [ |
| 110 | + pennylane, |
| 111 | + "lineItems", |
| 112 | + ], |
| 113 | + }, |
| 114 | + categories: { |
| 115 | + type: "string[]", |
| 116 | + label: "Categories", |
| 117 | + description: "A list of objects of categories", |
| 118 | + optional: true, |
| 119 | + }, |
| 120 | + startDate: { |
| 121 | + type: "string", |
| 122 | + label: "Start Date", |
| 123 | + description: "Start date of the imputation period (ISO 8601)", |
| 124 | + }, |
| 125 | + endDate: { |
| 126 | + type: "string", |
| 127 | + label: "End Date", |
| 128 | + description: "End date of the imputation period (ISO 8601)", |
| 129 | + }, |
| 130 | + }, |
| 131 | + async additionalProps(props) { |
| 132 | + if (this.bankingProvider === "stripe") { |
| 133 | + props.providerFieldName.hidden = false; |
| 134 | + } |
| 135 | + return {}; |
| 136 | + }, |
| 137 | + async run({ $ }) { |
| 138 | + try { |
| 139 | + const invoice = await this.pennylane.createInvoice({ |
| 140 | + $, |
| 141 | + data: { |
| 142 | + create_customer: false, |
| 143 | + create_products: false, |
| 144 | + invoice: { |
| 145 | + date: this.date, |
| 146 | + deadline: this.deadline, |
| 147 | + external_id: this.externalId, |
| 148 | + pdf_invoice_free_text: this.pdfInvoiceFreeText, |
| 149 | + pdf_invoice_subject: this.pdfInvoiceSubject, |
| 150 | + draft: this.draft, |
| 151 | + currency: this.currency, |
| 152 | + special_mention: this.specialMention, |
| 153 | + discount: this.discount, |
| 154 | + language: this.language, |
| 155 | + transactions_reference: { |
| 156 | + banking_provider: this.bankingProvider, |
| 157 | + provider_field_name: (this.bankingProvider === "gocardless") |
| 158 | + ? "payment_id" |
| 159 | + : this.providerFieldName, |
| 160 | + provider_field_value: this.providerFieldValue, |
| 161 | + }, |
| 162 | + customer: { |
| 163 | + source_id: this.customerId, |
| 164 | + }, |
| 165 | + line_items_sections_attributes: parseObject(this.lineItemsSectionsAttributes), |
| 166 | + line_items: parseObject(this.lineItems), |
| 167 | + categories: parseObject(this.categories), |
| 168 | + imputation_dates: { |
| 169 | + start_date: this.startDate, |
| 170 | + end_date: this.endDate, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }); |
| 175 | + |
| 176 | + $.export("$summary", `Created invoice with ID ${invoice.invoice.id}`); |
| 177 | + return invoice; |
| 178 | + } catch ({ response }) { |
| 179 | + throw new ConfigurationError(response?.data?.message || response?.data?.error); |
| 180 | + } |
| 181 | + }, |
| 182 | +}; |
0 commit comments