Skip to content

Commit 03755eb

Browse files
committed
Create Contact Payment action
1 parent 00da8bb commit 03755eb

File tree

2 files changed

+295
-0
lines changed

2 files changed

+295
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import app from "../../sage_accounting.app.mjs";
2+
3+
export default {
4+
key: "sage_accounting-create-contact-payment",
5+
name: "Create Contact Payment",
6+
description: "Creates a new contact payment in Sage Accounting. [See the documentation](https://developer.sage.com/accounting/reference/payments/#operation/postContactPayments)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
transactionTypeId: {
12+
propDefinition: [
13+
app,
14+
"transactionTypeId",
15+
],
16+
},
17+
contactId: {
18+
propDefinition: [
19+
app,
20+
"contactId",
21+
],
22+
},
23+
bankAccountId: {
24+
propDefinition: [
25+
app,
26+
"bankAccountId",
27+
],
28+
},
29+
date: {
30+
propDefinition: [
31+
app,
32+
"date",
33+
],
34+
},
35+
totalAmount: {
36+
propDefinition: [
37+
app,
38+
"totalAmount",
39+
],
40+
},
41+
paymentMethodId: {
42+
propDefinition: [
43+
app,
44+
"paymentMethodId",
45+
],
46+
},
47+
netAmount: {
48+
propDefinition: [
49+
app,
50+
"netAmount",
51+
],
52+
},
53+
taxAmount: {
54+
propDefinition: [
55+
app,
56+
"taxAmount",
57+
],
58+
},
59+
currencyId: {
60+
propDefinition: [
61+
app,
62+
"currencyId",
63+
],
64+
},
65+
exchangeRate: {
66+
propDefinition: [
67+
app,
68+
"exchangeRate",
69+
],
70+
},
71+
baseCurrencyNetAmount: {
72+
propDefinition: [
73+
app,
74+
"baseCurrencyNetAmount",
75+
],
76+
},
77+
baseCurrencyTaxAmount: {
78+
propDefinition: [
79+
app,
80+
"baseCurrencyTaxAmount",
81+
],
82+
},
83+
baseCurrencyTotalAmount: {
84+
propDefinition: [
85+
app,
86+
"baseCurrencyTotalAmount",
87+
],
88+
},
89+
baseCurrencyCurrencyCharge: {
90+
propDefinition: [
91+
app,
92+
"baseCurrencyCurrencyCharge",
93+
],
94+
},
95+
paymentReference: {
96+
propDefinition: [
97+
app,
98+
"paymentReference",
99+
],
100+
},
101+
taxRateId: {
102+
propDefinition: [
103+
app,
104+
"taxRateId",
105+
],
106+
},
107+
},
108+
async run({ $ }) {
109+
const data = {
110+
transaction_type_id: this.transactionTypeId,
111+
contact_id: this.contactId,
112+
bank_account_id: this.bankAccountId,
113+
date: this.date,
114+
total_amount: parseFloat(this.totalAmount),
115+
payment_method_id: this.paymentMethodId,
116+
net_amount: this.netAmount
117+
? parseFloat(this.netAmount)
118+
: undefined,
119+
tax_amount: this.taxAmount
120+
? parseFloat(this.taxAmount)
121+
: undefined,
122+
currency_id: this.currencyId,
123+
exchange_rate: this.exchangeRate
124+
? parseFloat(this.exchangeRate)
125+
: undefined,
126+
base_currency_net_amount: this.baseCurrencyNetAmount
127+
? parseFloat(this.baseCurrencyNetAmount)
128+
: undefined,
129+
base_currency_tax_amount: this.baseCurrencyTaxAmount
130+
? parseFloat(this.baseCurrencyTaxAmount)
131+
: undefined,
132+
base_currency_total_amount: this.baseCurrencyTotalAmount
133+
? parseFloat(this.baseCurrencyTotalAmount)
134+
: undefined,
135+
base_currency_currency_charge: this.baseCurrencyCurrencyCharge
136+
? parseFloat(this.baseCurrencyCurrencyCharge)
137+
: undefined,
138+
reference: this.paymentReference,
139+
tax_rate_id: this.taxRateId,
140+
};
141+
142+
const response = await this.app.createContactPayment({
143+
$,
144+
data,
145+
});
146+
147+
$.export("$summary", `Successfully created contact payment with ID: ${response.id}`);
148+
return response;
149+
},
150+
};

components/sage_accounting/sage_accounting.app.mjs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,115 @@ export default {
164164
description: "Identifies a contact should be blocked due to destination VAT",
165165
optional: true,
166166
},
167+
// Contact Payment propDefinitions
168+
transactionTypeId: {
169+
type: "string",
170+
label: "Transaction Type ID",
171+
description: "The transaction type of the payment",
172+
async options() {
173+
const transactionTypes = await this.listTransactionTypes();
174+
return transactionTypes.map((transactionType) => ({
175+
label: transactionType.displayed_as,
176+
value: transactionType.id,
177+
}));
178+
},
179+
},
180+
bankAccountId: {
181+
type: "string",
182+
label: "Bank Account ID",
183+
description: "The bank account of the payment",
184+
async options() {
185+
const bankAccounts = await this.listBankAccounts();
186+
return bankAccounts.map((bankAccount) => ({
187+
label: bankAccount.displayed_as,
188+
value: bankAccount.id,
189+
}));
190+
},
191+
},
192+
paymentMethodId: {
193+
type: "string",
194+
label: "Payment Method ID",
195+
description: "The ID of the Payment Method",
196+
async options() {
197+
const paymentMethods = await this.listPaymentMethods();
198+
return paymentMethods.map((paymentMethod) => ({
199+
label: paymentMethod.displayed_as,
200+
value: paymentMethod.id,
201+
}));
202+
},
203+
optional: true,
204+
},
205+
taxRateId: {
206+
type: "string",
207+
label: "Tax Rate ID",
208+
description: "The ID of the Tax Rate",
209+
async options() {
210+
const taxRates = await this.listTaxRates();
211+
return taxRates.map((taxRate) => ({
212+
label: taxRate.displayed_as,
213+
value: taxRate.id,
214+
}));
215+
},
216+
optional: true,
217+
},
218+
date: {
219+
type: "string",
220+
label: "Date",
221+
description: "The date the payment was made (YYYY-MM-DD format)",
222+
},
223+
totalAmount: {
224+
type: "string",
225+
label: "Total Amount",
226+
description: "The total amount of the payment",
227+
},
228+
netAmount: {
229+
type: "string",
230+
label: "Net Amount",
231+
description: "The net amount of the payment",
232+
optional: true,
233+
},
234+
taxAmount: {
235+
type: "string",
236+
label: "Tax Amount",
237+
description: "The tax amount of the payment",
238+
optional: true,
239+
},
240+
exchangeRate: {
241+
type: "string",
242+
label: "Exchange Rate",
243+
description: "The exchange rate of the payment",
244+
optional: true,
245+
},
246+
baseCurrencyNetAmount: {
247+
type: "string",
248+
label: "Base Currency Net Amount",
249+
description: "The net amount of the payment in base currency",
250+
optional: true,
251+
},
252+
baseCurrencyTaxAmount: {
253+
type: "string",
254+
label: "Base Currency Tax Amount",
255+
description: "The tax amount of the payment in base currency",
256+
optional: true,
257+
},
258+
baseCurrencyTotalAmount: {
259+
type: "string",
260+
label: "Base Currency Total Amount",
261+
description: "The total amount of the payment in base currency",
262+
optional: true,
263+
},
264+
baseCurrencyCurrencyCharge: {
265+
type: "string",
266+
label: "Base Currency Currency Charge",
267+
description: "The currency conversion charges in base currency",
268+
optional: true,
269+
},
270+
paymentReference: {
271+
type: "string",
272+
label: "Payment Reference",
273+
description: "A reference for the payment",
274+
optional: true,
275+
},
167276
},
168277
methods: {
169278
// this.$auth contains connected account data
@@ -245,5 +354,41 @@ export default {
245354
...opts,
246355
});
247356
},
357+
// Contact Payment API methods
358+
async listTransactionTypes(opts = {}) {
359+
const response = await this._makeRequest({
360+
path: "/transaction_types",
361+
...opts,
362+
});
363+
return response.$items || response.items || [];
364+
},
365+
async listBankAccounts(opts = {}) {
366+
const response = await this._makeRequest({
367+
path: "/bank_accounts",
368+
...opts,
369+
});
370+
return response.$items || response.items || [];
371+
},
372+
async listPaymentMethods(opts = {}) {
373+
const response = await this._makeRequest({
374+
path: "/payment_methods",
375+
...opts,
376+
});
377+
return response.$items || response.items || [];
378+
},
379+
async listTaxRates(opts = {}) {
380+
const response = await this._makeRequest({
381+
path: "/tax_rates",
382+
...opts,
383+
});
384+
return response.$items || response.items || [];
385+
},
386+
async createContactPayment(opts = {}) {
387+
return this._makeRequest({
388+
method: "POST",
389+
path: "/contact_payments",
390+
...opts,
391+
});
392+
},
248393
},
249394
};

0 commit comments

Comments
 (0)