Skip to content

Commit 433a9e4

Browse files
Extension tutorial authors
1 parent 57c07b1 commit 433a9e4

20 files changed

+1927
-1903
lines changed

content/tutorials/extensions/check-permissions-in-a-custom-endpoint.md

Lines changed: 254 additions & 254 deletions
Large diffs are not rendered by default.

content/tutorials/extensions/create-collection-items-in-custom-panels.md

Lines changed: 307 additions & 305 deletions
Large diffs are not rendered by default.

content/tutorials/extensions/create-comments-in-custom-operations.md

Lines changed: 178 additions & 176 deletions
Large diffs are not rendered by default.

content/tutorials/extensions/create-new-customers-in-stripe-in-a-custom-hook.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
id: ba5c9666-330f-4349-9f1e-da113fb84d27
33
slug: create-new-customers-in-stripe-in-a-custom-hook
44
title: Create New Customers in Stripe in a Custom Hook
5-
authors:
6-
- name: Kevin Lewis
7-
title: Director Developer Experience
5+
authors:
6+
- name: Tim Butterfield
7+
title: Guest Author
88
---
99
Hooks allow you to trigger your own code under certain conditions. This tutorial will show you how to create a Stripe
1010
account when an item is created in Directus and write the customer ID back to the record.
@@ -53,7 +53,7 @@ the `MailService` to send yourself an email if the Stripe API fails.
5353

5454
```js
5555
export default ({ action }, { env, services }) => {
56-
const { MailService, ItemsService } = services; // [!code ++]
56+
const { MailService, ItemsService } = services; // [!code ++]
5757
};
5858
```
5959

@@ -68,7 +68,7 @@ restrictions. Inside the action, exclude anything that’s not in the customers
6868

6969
```js
7070
action('items.create', async ({ key, collection, payload }, { schema }) => {
71-
if (collection !== 'customers') return; // [!code ++]
71+
if (collection !== 'customers') return; // [!code ++]
7272
});
7373
```
7474

@@ -85,12 +85,12 @@ Create a new customer with the customer's name and email as the input values.
8585

8686
```js
8787
stripe.customers
88-
.create({
89-
name: `${payload.first_name} ${payload.last_name}`,
90-
email: payload.email_address,
91-
})
92-
.then((customer) => {})
93-
.catch((error) => {});
88+
.create({
89+
name: `${payload.first_name} ${payload.last_name}`,
90+
email: payload.email_address,
91+
})
92+
.then((customer) => {})
93+
.catch((error) => {});
9494
```
9595

9696
If successful, update the record with the new customer id from stripe. The API call returns the customer object into the
@@ -100,12 +100,12 @@ Use the `ItemsService` to update the customer record. Initialize the service and
100100

101101
```js
102102
stripe.customers
103-
.create({})
104-
.then((customer) => {
105-
const customers = new ItemsService(collection, { schema }); // [!code ++]
106-
customers.updateByQuery({ filter: { id: key } }, { stripe_id: customer.id }, { emitEvents: false }); // [!code ++]
107-
})
108-
.catch((error) => {});
103+
.create({})
104+
.then((customer) => {
105+
const customers = new ItemsService(collection, { schema }); // [!code ++]
106+
customers.updateByQuery({ filter: { id: key } }, { stripe_id: customer.id }, { emitEvents: false }); // [!code ++]
107+
})
108+
.catch((error) => {});
109109
```
110110

111111
By setting `emitEvents` to `false`, the `items.update` event will not trigger, which prevents flows or hooks from
@@ -115,17 +115,17 @@ Add an exception if the Stripe API fails.
115115

116116
```js
117117
stripe.customers
118-
.create({})
119-
.then((customer) => {})
120-
.catch((error) => {
121-
const mailService = new MailService({ schema });
122-
mailService.send({ // [!code ++]
123-
to: '[email protected]', // [!code ++]
124-
from: '[email protected]', // [!code ++]
125-
subject: `An error has occurred with Stripe API`, // [!code ++]
126-
text: `The following error occurred for ${payload.first_name} ${payload.last_name} when attempting to create an account in Stripe.\r\n\r\n${error}\r\n\r\nPlease investigate.\r\n\r\nID: ${key}\r\nEmail: ${payload.email_address}`, // [!code ++]
127-
}); // [!code ++]
128-
});
118+
.create({})
119+
.then((customer) => {})
120+
.catch((error) => {
121+
const mailService = new MailService({ schema });
122+
mailService.send({ // [!code ++]
123+
to: '[email protected]', // [!code ++]
124+
from: '[email protected]', // [!code ++]
125+
subject: `An error has occurred with Stripe API`, // [!code ++]
126+
text: `The following error occurred for ${payload.first_name} ${payload.last_name} when attempting to create an account in Stripe.\r\n\r\n${error}\r\n\r\nPlease investigate.\r\n\r\nID: ${key}\r\nEmail: ${payload.email_address}`, // [!code ++]
127+
}); // [!code ++]
128+
});
129129
```
130130

131131
Build the hook with the latest changes.
@@ -168,30 +168,30 @@ other endpoints that Stripe has to offer.
168168
import Stripe from 'stripe';
169169

170170
export default ({ action }, { env, services }) => {
171-
const { MailService, ItemsService } = services;
172-
173-
action('items.create', async ({ key, collection, payload }, { schema }) => {
174-
if (collection !== 'customers') return;
175-
const stripe = new Stripe(env.STRIPE_TOKEN);
176-
177-
stripe.customers
178-
.create({
179-
name: `${payload.first_name} ${payload.last_name}`,
180-
email: payload.email_address,
181-
})
182-
.then((customer) => {
183-
const customers = new ItemsService(collection, { schema });
184-
customers.updateByQuery({ filter: { id: key } }, { stripe_id: customer.id }, { emitEvents: false });
185-
})
186-
.catch((error) => {
187-
const mailService = new MailService({ schema });
188-
mailService.send({
189-
190-
191-
subject: `An error has occurred with Stripe API`,
192-
text: `The following error occurred for ${payload.first_name} ${payload.last_name} when attempting to create an account in Stripe.\r\n\r\n${error}\r\n\r\nPlease investigate.\r\n\r\nID: ${key}\r\nEmail: ${payload.email_address}`,
193-
});
194-
});
195-
});
171+
const { MailService, ItemsService } = services;
172+
173+
action('items.create', async ({ key, collection, payload }, { schema }) => {
174+
if (collection !== 'customers') return;
175+
const stripe = new Stripe(env.STRIPE_TOKEN);
176+
177+
stripe.customers
178+
.create({
179+
name: `${payload.first_name} ${payload.last_name}`,
180+
email: payload.email_address,
181+
})
182+
.then((customer) => {
183+
const customers = new ItemsService(collection, { schema });
184+
customers.updateByQuery({ filter: { id: key } }, { stripe_id: customer.id }, { emitEvents: false });
185+
})
186+
.catch((error) => {
187+
const mailService = new MailService({ schema });
188+
mailService.send({
189+
190+
191+
subject: `An error has occurred with Stripe API`,
192+
text: `The following error occurred for ${payload.first_name} ${payload.last_name} when attempting to create an account in Stripe.\r\n\r\n${error}\r\n\r\nPlease investigate.\r\n\r\nID: ${key}\r\nEmail: ${payload.email_address}`,
193+
});
194+
});
195+
});
196196
};
197197
```

0 commit comments

Comments
 (0)