Skip to content

Commit 080d445

Browse files
authored
Braintree - new components (#19407)
* new components * pnpm-lock.yaml * updates * error handling
1 parent e2854bf commit 080d445

File tree

10 files changed

+340
-6
lines changed

10 files changed

+340
-6
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import braintree from "../../braintree.app.mjs";
2+
import mutations from "../../common/mutations.mjs";
3+
4+
export default {
5+
key: "braintree-create-customer",
6+
name: "Create Customer",
7+
description: "Create a new customer in Braintree. [See the documentation](https://developer.paypal.com/braintree/graphql/guides/customers/#create)",
8+
version: "0.0.1",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: false,
13+
},
14+
type: "action",
15+
props: {
16+
braintree,
17+
firstName: {
18+
type: "string",
19+
label: "First Name",
20+
description: "First name of the customer",
21+
optional: true,
22+
},
23+
lastName: {
24+
type: "string",
25+
label: "Last Name",
26+
description: "Last name of the customer",
27+
optional: true,
28+
},
29+
company: {
30+
type: "string",
31+
label: "Company",
32+
description: "Company name of the customer",
33+
optional: true,
34+
},
35+
email: {
36+
type: "string",
37+
label: "Email",
38+
description: "Email of the customer",
39+
optional: true,
40+
},
41+
phoneNumber: {
42+
type: "string",
43+
label: "Phone Number",
44+
description: "Phone number of the customer",
45+
optional: true,
46+
},
47+
fax: {
48+
type: "string",
49+
label: "Fax",
50+
description: "Fax number of the customer",
51+
optional: true,
52+
},
53+
website: {
54+
type: "string",
55+
label: "Website",
56+
description: "Website of the customer",
57+
optional: true,
58+
},
59+
},
60+
async run({ $ }) {
61+
const response = await this.braintree.makeGraphQLRequest({
62+
$,
63+
data: {
64+
query: mutations.createCustomer,
65+
variables: {
66+
input: {
67+
customer: {
68+
firstName: this.firstName,
69+
lastName: this.lastName,
70+
company: this.company,
71+
email: this.email,
72+
phoneNumber: this.phoneNumber,
73+
fax: this.fax,
74+
website: this.website,
75+
},
76+
},
77+
},
78+
},
79+
});
80+
$.export("$summary", `Customer successfully created with ID ${response.data.createCustomer.customer.id}`);
81+
return response;
82+
},
83+
};
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
import {
2+
axios, ConfigurationError,
3+
} from "@pipedream/platform";
4+
15
export default {
26
type: "app",
37
app: "braintree",
48
propDefinitions: {},
59
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
10+
async makeGraphQLRequest({
11+
$ = this, ...opts
12+
}) {
13+
const response = await axios($, {
14+
method: "post",
15+
url: `https://${this.$auth.environment}.braintree-api.com/graphql`,
16+
headers: {
17+
"Braintree-Version": "2019-01-01",
18+
"Content-Type": "application/json",
19+
},
20+
auth: {
21+
username: this.$auth.public_key,
22+
password: this.$auth.private_key,
23+
},
24+
...opts,
25+
});
26+
if (response.errors) {
27+
throw new ConfigurationError(response.errors[0].message);
28+
}
29+
return response;
930
},
1031
},
1132
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const createCustomer = `
2+
mutation createCustomer($input: CreateCustomerInput!) {
3+
createCustomer(input: $input) {
4+
customer {
5+
id
6+
firstName
7+
lastName
8+
company
9+
email
10+
phoneNumber
11+
fax
12+
website
13+
createdAt
14+
}
15+
}
16+
}
17+
`;
18+
19+
export default {
20+
createCustomer,
21+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const searchCustomers = `
2+
query Search($input: CustomerSearchInput!) {
3+
search {
4+
customers(input: $input) {
5+
edges {
6+
node {
7+
id
8+
firstName
9+
lastName
10+
company
11+
email
12+
phoneNumber
13+
fax
14+
website
15+
createdAt
16+
customFields {
17+
name
18+
value
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
`;
26+
27+
const searchTransactions = `
28+
query ($input: TransactionSearchInput!) {
29+
search {
30+
transactions(input: $input) {
31+
edges {
32+
node {
33+
id
34+
status
35+
amount {
36+
value
37+
currencyCode
38+
}
39+
paymentMethod {
40+
id
41+
}
42+
orderId
43+
merchantId
44+
merchantName
45+
customFields {
46+
name
47+
value
48+
}
49+
channel
50+
customer {
51+
id
52+
}
53+
lineItems {
54+
name
55+
quantity
56+
totalAmount
57+
}
58+
createdAt
59+
}
60+
}
61+
}
62+
}
63+
}
64+
`;
65+
66+
export default {
67+
searchCustomers,
68+
searchTransactions,
69+
};

components/braintree/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/braintree",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Pipedream Braintree Components",
55
"main": "braintree.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.1"
1417
}
1518
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import braintree from "../../braintree.app.mjs";
2+
import {
3+
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError,
4+
} from "@pipedream/platform";
5+
6+
export default {
7+
props: {
8+
braintree,
9+
db: "$.service.db",
10+
timer: {
11+
type: "$.interface.timer",
12+
default: {
13+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
14+
},
15+
},
16+
},
17+
methods: {
18+
_yesterday() {
19+
const now = new Date();
20+
const yesterday = new Date(now);
21+
yesterday.setDate(now.getDate() - 1);
22+
return yesterday.toISOString();
23+
},
24+
_getLastCreatedAt() {
25+
return this.db.get("createdAt") || this._yesterday();
26+
},
27+
_setLastCreatedAt(lastCreatedAt) {
28+
this.db.set("createdAt", lastCreatedAt);
29+
},
30+
emitEvent(result) {
31+
const meta = this.generateMeta(result);
32+
this.$emit(result, meta);
33+
},
34+
generateMeta(result) {
35+
return {
36+
id: result.id,
37+
summary: this.getSummary(result),
38+
ts: Date.parse(result.createdAt),
39+
};
40+
},
41+
getResults() {
42+
throw new ConfigurationError("getResults is not implemented");
43+
},
44+
getSummary() {
45+
throw new ConfigurationError("getSummary is not implemented");
46+
},
47+
},
48+
async run() {
49+
const lastCreatedAt = this._getLastCreatedAt();
50+
let maxCreatedAt = lastCreatedAt;
51+
52+
const results = await this.getResults(lastCreatedAt);
53+
for (const result of results) {
54+
const createdAt = result.createdAt;
55+
this.emitEvent(result);
56+
if (Date.parse(createdAt) > Date.parse(maxCreatedAt)) {
57+
maxCreatedAt = createdAt;
58+
}
59+
}
60+
61+
this._setLastCreatedAt(maxCreatedAt);
62+
},
63+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import base from "../common/base-polling.mjs";
2+
import queries from "../../common/queries.mjs";
3+
4+
export default {
5+
...base,
6+
key: "braintree-new-customer-created",
7+
name: "New Customer Created",
8+
description: "Emit new event when a new customer is created.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...base.methods,
14+
async getResults(lastCreatedAt) {
15+
const { braintree } = this;
16+
const response = await braintree.makeGraphQLRequest({
17+
data: {
18+
query: queries.searchCustomers,
19+
variables: {
20+
input: {
21+
createdAt: {
22+
greaterThanOrEqualTo: lastCreatedAt,
23+
},
24+
},
25+
},
26+
},
27+
});
28+
const edges = response?.data?.search?.customers?.edges ?? [];
29+
return edges.map((edge) => edge.node);
30+
},
31+
getSummary(result) {
32+
return `New Customer Created: ${result.id}`;
33+
},
34+
},
35+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import common from "../common/base-polling.mjs";
2+
import queries from "../../common/queries.mjs";
3+
4+
export default {
5+
...common,
6+
key: "braintree-new-transaction-created",
7+
name: "New Transaction Created",
8+
description: "Emit new event when a new transaction is created.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
async getResults(lastCreatedAt) {
15+
const { braintree } = this;
16+
const response = await braintree.makeGraphQLRequest({
17+
data: {
18+
query: queries.searchTransactions,
19+
variables: {
20+
input: {
21+
createdAt: {
22+
greaterThanOrEqualTo: lastCreatedAt,
23+
},
24+
},
25+
},
26+
},
27+
});
28+
const edges = response?.data?.search?.transactions?.edges ?? [];
29+
return edges.map((edge) => edge.node);
30+
},
31+
getSummary(result) {
32+
return `New Transaction Created: ${result.id}`;
33+
},
34+
},
35+
};

components/xero_payroll/xero_payroll.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)