22id : ba5c9666-330f-4349-9f1e-da113fb84d27
33slug : create-new-customers-in-stripe-in-a-custom-hook
44title : 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---
99Hooks allow you to trigger your own code under certain conditions. This tutorial will show you how to create a Stripe
1010account 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
5555export 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
7070action (' 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
8787stripe .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
9696If 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
102102stripe .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
111111By 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
117117stripe .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\n Please investigate.\r\n\r\n ID: ${ key} \r\n Email: ${ 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\n Please investigate.\r\n\r\n ID: ${ key} \r\n Email: ${ payload .email_address } ` , // [!code ++]
127+ }); // [!code ++]
128+ });
129129```
130130
131131Build the hook with the latest changes.
@@ -168,30 +168,30 @@ other endpoints that Stripe has to offer.
168168import Stripe from ' stripe' ;
169169
170170export 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\n Please investigate.\r\n\r\n ID: ${ key} \r\n Email: ${ 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\n Please investigate.\r\n\r\n ID: ${ key} \r\n Email: ${ payload .email_address } ` ,
193+ });
194+ });
195+ });
196196};
197197```
0 commit comments