Skip to content

Commit 12d6e2d

Browse files
committed
init
1 parent a72fc41 commit 12d6e2d

File tree

7 files changed

+453
-5
lines changed

7 files changed

+453
-5
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import chargify from "../../chargify.app.mjs";
2+
3+
export default {
4+
key: "chargify-create-customer",
5+
name: "Create Customer",
6+
description: "Creates a new customer in Chargify",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
chargify,
11+
name: {
12+
type: "string",
13+
label: "Name",
14+
description: "The name of the customer",
15+
},
16+
email: {
17+
type: "string",
18+
label: "Email",
19+
description: "The email of the customer",
20+
optional: true,
21+
},
22+
organization: {
23+
type: "string",
24+
label: "Organization",
25+
description: "The organization of the customer",
26+
optional: true,
27+
},
28+
},
29+
async run({ $ }) {
30+
const response = await this.chargify.createCustomer({
31+
data: {
32+
customer: {
33+
first_name: this.name,
34+
email: this.email,
35+
organization: this.organization,
36+
},
37+
},
38+
});
39+
$.export("$summary", `Successfully created customer ${this.name}`);
40+
return response;
41+
},
42+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
js
2+
import chargify from "../../chargify.app.mjs";
3+
4+
export default {
5+
key: "chargify-create-subscription",
6+
name: "Create Subscription",
7+
description: "Establishes a new subscription for a given customer in Chargify",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
chargify,
12+
customerId: {
13+
propDefinition: [
14+
chargify,
15+
"customerId"
16+
]
17+
},
18+
productId: {
19+
propDefinition: [
20+
chargify,
21+
"productId"
22+
]
23+
},
24+
couponCode: {
25+
propDefinition: [
26+
chargify,
27+
"couponCode"
28+
],
29+
optional: true
30+
},
31+
nextBillingAt: {
32+
propDefinition: [
33+
chargify,
34+
"nextBillingAt"
35+
],
36+
optional: true
37+
},
38+
},
39+
async run({ $ }) {
40+
const response = await this.chargify.createSubscription({
41+
data: {
42+
subscription: {
43+
customer_id: this.customerId,
44+
product_id: this.productId,
45+
coupon_code: this.couponCode,
46+
next_billing_at: this.nextBillingAt
47+
}
48+
}
49+
});
50+
$.export("$summary", `Successfully created subscription with ID: ${response.id}`);
51+
return response;
52+
},
53+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import chargify from "../../chargify.app.mjs";
2+
3+
export default {
4+
key: "chargify-update-subscription",
5+
name: "Update Subscription",
6+
description: "Modifies an existing subscription in Chargify using its unique 'subscription_id'.",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
chargify,
11+
subscriptionId: {
12+
propDefinition: [
13+
chargify,
14+
"subscriptionId",
15+
],
16+
},
17+
productId: {
18+
propDefinition: [
19+
chargify,
20+
"productId",
21+
],
22+
},
23+
nextBillingAt: {
24+
propDefinition: [
25+
chargify,
26+
"nextBillingAt",
27+
],
28+
},
29+
couponCode: {
30+
propDefinition: [
31+
chargify,
32+
"couponCode",
33+
],
34+
},
35+
},
36+
async run({ $ }) {
37+
const response = await this.chargify.updateSubscription({
38+
subscriptionId: this.subscriptionId,
39+
data: {
40+
subscription: {
41+
product_id: this.productId,
42+
next_billing_at: this.nextBillingAt,
43+
coupon_code: this.couponCode,
44+
},
45+
},
46+
});
47+
$.export("$summary", `Successfully updated subscription ${this.subscriptionId}`);
48+
return response;
49+
},
50+
};
Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,103 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "chargify",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
customerId: {
8+
type: "string",
9+
label: "Customer ID",
10+
description: "The ID of the customer",
11+
},
12+
subscriptionId: {
13+
type: "string",
14+
label: "Subscription ID",
15+
description: "The ID of the subscription",
16+
},
17+
name: {
18+
type: "string",
19+
label: "Name",
20+
description: "The name of the customer",
21+
optional: true,
22+
},
23+
email: {
24+
type: "string",
25+
label: "Email",
26+
description: "The email of the customer",
27+
optional: true,
28+
},
29+
organization: {
30+
type: "string",
31+
label: "Organization",
32+
description: "The organization of the customer",
33+
optional: true,
34+
},
35+
productId: {
36+
type: "string",
37+
label: "Product ID",
38+
description: "The ID of the product",
39+
},
40+
couponCode: {
41+
type: "string",
42+
label: "Coupon Code",
43+
description: "The coupon code",
44+
optional: true,
45+
},
46+
nextBillingAt: {
47+
type: "string",
48+
label: "Next Billing At",
49+
description: "The next billing date",
50+
optional: true,
51+
},
52+
},
553
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
54+
_baseUrl() {
55+
return "https://api.chargify.com/api/v2";
56+
},
57+
async _makeRequest(opts = {}) {
58+
const {
59+
$ = this,
60+
method = "GET",
61+
path,
62+
headers,
63+
...otherOpts
64+
} = opts;
65+
return axios($, {
66+
...otherOpts,
67+
method,
68+
url: this._baseUrl() + path,
69+
headers: {
70+
...headers,
71+
Authorization: `Bearer ${this.$auth.api_token}`,
72+
},
73+
});
74+
},
75+
async createCustomer(opts = {}) {
76+
return this._makeRequest({
77+
method: "POST",
78+
path: "/customers",
79+
...opts,
80+
});
81+
},
82+
async getSubscriptions(opts = {}) {
83+
return this._makeRequest({
84+
path: "/subscriptions",
85+
...opts,
86+
});
87+
},
88+
async createSubscription(opts = {}) {
89+
return this._makeRequest({
90+
method: "POST",
91+
path: "/subscriptions",
92+
...opts,
93+
});
94+
},
95+
async updateSubscription({ subscriptionId, ...opts }) {
96+
return this._makeRequest({
97+
method: "PUT",
98+
path: `/subscriptions/${subscriptionId}`,
99+
...opts,
100+
});
9101
},
10102
},
11-
};
103+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import chargify from "../../chargify.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "chargify-new-customer",
6+
name: "New Customer",
7+
description: "Emits an event when a new customer is added in Chargify",
8+
version: "0.0.{{ts}}",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
chargify,
13+
db: "$.service.db",
14+
timer: {
15+
type: "$.interface.timer",
16+
default: {
17+
intervalSeconds: 60 * 15, // 15 minutes
18+
},
19+
},
20+
},
21+
methods: {
22+
_getCustomerId(customer) {
23+
return customer.id;
24+
},
25+
_getTimestamp(customer) {
26+
return +new Date(customer.created_at);
27+
},
28+
},
29+
hooks: {
30+
async deploy() {
31+
const customers = await this.chargify._makeRequest({
32+
path: "/customers",
33+
params: { order: "desc", per_page: 1 },
34+
});
35+
if (customers.length > 0) {
36+
const lastProcessedCustomerId = this._getCustomerId(customers[0]);
37+
this.db.set("lastProcessedCustomerId", lastProcessedCustomerId);
38+
const lastTimestamp = this._getTimestamp(customers[0]);
39+
this.db.set("lastTimestamp", lastTimestamp);
40+
}
41+
},
42+
},
43+
async run() {
44+
const lastCustomerId = this.db.get("lastProcessedCustomerId") || 0;
45+
const lastTimestamp = this.db.get("lastTimestamp") || 0;
46+
const params = {
47+
page: 1,
48+
per_page: 100,
49+
};
50+
51+
while (true) {
52+
const { data: customers } = await this.chargify._makeRequest({
53+
path: "/customers",
54+
params,
55+
});
56+
57+
if (customers.length === 0) {
58+
console.log("No new customers found, exiting");
59+
break;
60+
}
61+
62+
for (const customer of customers) {
63+
const customerId = this._getCustomerId(customer);
64+
const timestamp = this._getTimestamp(customer);
65+
66+
if (customerId > lastCustomerId && timestamp > lastTimestamp) {
67+
this.$emit(customer, {
68+
id: customerId,
69+
summary: `New Customer: ${customer.first_name} ${customer.last_name}`,
70+
ts: timestamp,
71+
});
72+
} else {
73+
console.log("No new customers found, exiting");
74+
break;
75+
}
76+
}
77+
78+
params.page++;
79+
}
80+
81+
const { data: latestCustomer } = await this.chargify._makeRequest({
82+
path: "/customers",
83+
params: { order: "desc", per_page: 1 },
84+
});
85+
86+
this.db.set("lastCustomerId", this._getCustomerId(latestCustomer[0]));
87+
this.db.set("lastTimestamp", this._getTimestamp(latestCustomer[0]));
88+
},
89+
};

0 commit comments

Comments
 (0)