Skip to content

Commit

Permalink
Improves error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Jan 15, 2018
1 parent dafef90 commit 8d1cf3e
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions stripe-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,31 +303,38 @@
* @param {Event} event Submit event.
*/
submit(event) {
const fire = (type, detail) => this.dispatchEvent(new CustomEvent(type, {
bubbles: true,
composed: true,
detail,
}));

const tokenReceived = ({token}) => {
this._setToken(token);
fire('stripe-token', token);
// Submit the form
return this.action ? this.$.form.submit() : true;
const bubbles = true;
const composed = true;
const handleResponse = ({error, token}) => {
if (error) {
this._setError(error);
this.dispatchEvent(
new ErrorEvent('stripe-error', {error, bubbles, composed})
);
} else {
this._setToken(token);
this.dispatchEvent(
new CustomEvent('stripe-token', {token, bubbles, composed})
);
// Submit the form
return this.action ? this.$.form.submit() : true;
}
};

const tokenError = (error) => {
const handleError = (error) => {
// Show error in UI
this.$.toast.fitInto = this.$.form;
this.$.toast.fitInto = this;
this._setError(error.message);
fire('stripe-error', error);
this.dispatchEvent(
new ErrorEvent('stripe-error', {error, bubbles, composed})
);
return false;
};

// Get the token
this._stripe.createToken(this._card)
.then(tokenReceived)
.catch(tokenError);
.then(handleResponse)
.catch(handleError);
}

reset() {
Expand All @@ -354,7 +361,7 @@
/** EVENT HANDLERS */

onChange(event) {
this.error = event.error;
this._setError(event.error);
}

/** PRIVATE METHODS */
Expand Down

0 comments on commit 8d1cf3e

Please sign in to comment.