From 5b0f710c27c104dfa21f15abd9a168be920a6cef Mon Sep 17 00:00:00 2001 From: Nicolas Bonduel Date: Mon, 27 Jan 2020 11:09:48 +0100 Subject: [PATCH 1/4] Better error handling. --- src/collection/objects/_objects.js | 2 +- src/collection/resources/_resources.js | 2 +- src/utils/error.js | 14 +++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/collection/objects/_objects.js b/src/collection/objects/_objects.js index a7ecef0..9b01d6d 100644 --- a/src/collection/objects/_objects.js +++ b/src/collection/objects/_objects.js @@ -59,7 +59,7 @@ class Component { }).then((res) => { const obj = parseJson(res, ResConstructor, json); return Promise.resolve({ res, obj }); - }).catch((err) => Promise.reject(err)); + }).catch((err) => Promise.reject(new Error.BadRequest(err))); } catch (err) { return Promise.reject(err); } diff --git a/src/collection/resources/_resources.js b/src/collection/resources/_resources.js index 007df31..7483442 100644 --- a/src/collection/resources/_resources.js +++ b/src/collection/resources/_resources.js @@ -46,7 +46,7 @@ class Components { }).then((res) => { const obj = parseJson(res, ResConstructor, json); return Promise.resolve({ res, obj }); - }).catch((err) => Promise.reject(err)); + }).catch((err) => Promise.reject(new Error.BadRequest(err))); } catch (err) { return Promise.reject(err); } diff --git a/src/utils/error.js b/src/utils/error.js index c427bba..9f5c8c3 100644 --- a/src/utils/error.js +++ b/src/utils/error.js @@ -1,9 +1,14 @@ /* eslint-disable max-classes-per-file */ class ExtendableError extends Error { - constructor(message) { + constructor(message, metadata) { super(); this.message = message; this.name = this.constructor.name; + Object.entries(metadata).forEach(([key, value]) => { + if (!this[key]) { + this[key] = value; + } + }); } } @@ -31,9 +36,16 @@ class AbstractClass extends ExtendableError { } } +class BadRequest extends ExtendableError { + constructor(requestError) { + super(requestError.response.body.message || 'Bad request', requestError); + } +} + module.exports = { MethodNeedsId, AbstractClass, MethodNeedsArg, NotImplemented, + BadRequest, }; From 072688b61e03c7bc8b06135b3fd41943380c8ecf Mon Sep 17 00:00:00 2001 From: Nicolas Bonduel Date: Mon, 27 Jan 2020 11:12:45 +0100 Subject: [PATCH 2/4] Stripe & Vipps integration. --- .../stripe-firebase-full-order/index.html | 229 ++++++++++++++++++ src/collection/resources/paymentMethods.js | 30 +++ src/collection/resources/payments.js | 64 +++++ src/functionality/paymentProvider/stripe.js | 9 + src/functionality/paymentProvider/vipps.js | 7 + src/utils/error.js | 7 + 6 files changed, 346 insertions(+) create mode 100644 examples/stripe-firebase-full-order/index.html create mode 100644 src/functionality/paymentProvider/stripe.js create mode 100644 src/functionality/paymentProvider/vipps.js diff --git a/examples/stripe-firebase-full-order/index.html b/examples/stripe-firebase-full-order/index.html new file mode 100644 index 0000000..2a4630b --- /dev/null +++ b/examples/stripe-firebase-full-order/index.html @@ -0,0 +1,229 @@ + + + + + + Builton SDK & Firebase example + + + + + + + + + +
+
+ +
+
+
Please use a Stripe test card
+
+ +
+
+

+    
+ + + + diff --git a/src/collection/resources/paymentMethods.js b/src/collection/resources/paymentMethods.js index 4cdf76e..251ae1f 100644 --- a/src/collection/resources/paymentMethods.js +++ b/src/collection/resources/paymentMethods.js @@ -1,5 +1,8 @@ +/* eslint-disable max-classes-per-file */ const Components = require('./_resources'); const PaymentMethod = require('../objects/paymentMethod'); +const StripePaymentProvider = require('../../functionality/paymentProvider/stripe'); +const VippsPaymentProvider = require('../../functionality/paymentProvider/vipps'); const { create, del, @@ -16,6 +19,33 @@ class PaymentMethods extends Components { this.request = request; this.apiPath = 'payment_methods'; this.ResConstructor = PaymentMethod; + this.StripePaymentProvider = StripePaymentProvider; + this.VippsPaymentProvider = VippsPaymentProvider; + } + + createWithProvider(provider, { urlParams, json = false } = {}, done) { + const createWithStripe = (stripe, cardElement) => this.create({ + payment_method: 'stripe', + }).then((holderPaymentMethod) => stripe.handleCardSetup( + holderPaymentMethod.setup_intent.client_secret, + cardElement, + ).then((stripeResponse) => holderPaymentMethod.update({ + payment_method_id: stripeResponse.setupIntent.payment_method, + }, { urlParams, json }, done))); + const createWithVipps = () => this.create({ + payment_method: 'vipps', + }); + switch (provider.name) { + case 'StripePaymentProvider': + if (provider.stripe && provider.element) { + return createWithStripe(provider.stripe, provider.element); + } + break; + case 'VippsPaymentProvider': + return createWithVipps(); + default: + throw Error('error'); + } } } diff --git a/src/collection/resources/payments.js b/src/collection/resources/payments.js index 6ef5ea9..220e104 100644 --- a/src/collection/resources/payments.js +++ b/src/collection/resources/payments.js @@ -1,5 +1,6 @@ const Components = require('./_resources'); const Payment = require('../objects/payment'); +const Error = require('../../utils/error'); const { create, getFromId, @@ -17,6 +18,69 @@ class Payments extends Components { this.ResConstructor = Payment; } + createWithProvider(provider, body, { urlParams, json = false } = {}, done) { + const createWithStripe = (stripe) => this.create( + body, + ) + .catch((createError) => { + if (createError.status === 422) { + const payments = createError.response.body; + const paymentList = []; + return new Promise((resolve) => { + let result = Promise.resolve(); + payments.forEach((payment) => { + if ( + payment.current_state + && payment.current_state.toUpperCase() === 'PENDING' + && payment.metadata + && payment.metadata.intent_client_secret + && payment.metadata.intent_id + ) { + result = result + .then(() => stripe.handleCardAction(payment.metadata.intent_client_secret)) + .catch((error) => { throw new Error.StripeError(error); }) + .then((stripeResponse) => { + if (stripeResponse.error) { + throw new Error.StripeError(stripeResponse.error); + } + // eslint-disable-next-line camelcase + const { id, client_secret } = stripeResponse.paymentIntent; + return this.confirm(payment._id.$oid, { + payment_intent_id: id, + payment_client_secret: client_secret, + }, { urlParams, json }, done); + }) + .then((SCAPayment) => { + paymentList.push(new Payment(this.request, SCAPayment)); + }) + .catch((error) => { + paymentList.push(new Payment(this.request, { + ...payment, + error, + })); + }); + } else { + result = result.then(() => paymentList.push(new Payment(this.request, payment))); + } + }); + result.then(() => resolve(paymentList)); + }); + } + }); + + switch (provider.name) { + case 'StripePaymentProvider': + if (provider.stripe && provider.element) { + return createWithStripe(provider.stripe, provider.element); + } + break; + case 'VippsPaymentProvider': + return create(body, { json }, done); + default: + throw Error('error'); + } + } + pay(id, ...params) { const obj = new Payment(this.request, id); return obj.pay(...params); diff --git a/src/functionality/paymentProvider/stripe.js b/src/functionality/paymentProvider/stripe.js new file mode 100644 index 0000000..6853e32 --- /dev/null +++ b/src/functionality/paymentProvider/stripe.js @@ -0,0 +1,9 @@ +class StripePaymentProvider { + constructor(stripe, element) { + this.name = 'StripePaymentProvider'; + this.stripe = stripe; + this.element = element; + } +} + +module.exports = StripePaymentProvider; diff --git a/src/functionality/paymentProvider/vipps.js b/src/functionality/paymentProvider/vipps.js new file mode 100644 index 0000000..db39419 --- /dev/null +++ b/src/functionality/paymentProvider/vipps.js @@ -0,0 +1,7 @@ +class VippsPaymentProvider { + constructor() { + this.name = 'VippsPaymentProvider'; + } +} + +module.exports = VippsPaymentProvider; diff --git a/src/utils/error.js b/src/utils/error.js index 9f5c8c3..8661c97 100644 --- a/src/utils/error.js +++ b/src/utils/error.js @@ -42,10 +42,17 @@ class BadRequest extends ExtendableError { } } +class StripeError extends ExtendableError { + constructor(stripeError) { + super(stripeError.message || 'Payment error', stripeError); + } +} + module.exports = { MethodNeedsId, AbstractClass, MethodNeedsArg, NotImplemented, BadRequest, + StripeError, }; From 1f53b0c4402bf623e0811b52c2c52bd15628e7ec Mon Sep 17 00:00:00 2001 From: Nicolas Bonduel Date: Mon, 17 Feb 2020 14:35:12 +0100 Subject: [PATCH 3/4] Better error handling + fix tests --- dist/main.bundle.js | 4 +- .../stripe-firebase-full-order/index.html | 16 ++++++-- src/collection/resources/paymentMethods.js | 40 +++++++++++++++---- src/collection/resources/payments.js | 8 +++- src/utils/error.js | 7 ++++ test/architecture/architecture.json | 8 +++- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/dist/main.bundle.js b/dist/main.bundle.js index cdbaa2b..a7b962f 100644 --- a/dist/main.bundle.js +++ b/dist/main.bundle.js @@ -1,8 +1,8 @@ -var Builton=function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=12)}([function(t,e,r){function n(){return(n=Object.assign||function(t){for(var e=1;e=Math.floor(c.paginationTotal/c.size)?Promise.resolve(c.current):(c.page+=1,c._query(t))},this.previous=function(t){return c.page<=0?Promise.resolve(c.current):(c.page-=1,c._query(t))},this.goToPage=function(t,e){return c.page=t,c._query(e)}}},function(t,e,r){function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var r=0;rr)}catch(t){return!1}}},{key:"serialize",value:function(t){var e=[];return Object.keys(t).forEach(function(r){void 0!==t[r]&&e.push("".concat(encodeURIComponent(r),"=").concat(encodeURIComponent(t[r])))}),e.length?"?".concat(e.join("&")):""}}],(r=[{key:"_constructHeaders",value:function(){var t={"X-Builton-API-Key":this.apiKey};return this.bearerToken&&(t.Authorization="Bearer ".concat(this.bearerToken)),t}},{key:"getHeaders",value:function(){var e=this;return this.bearerToken&&!t.isJWTAlive(this.bearerToken)&&this.refreshBearerFn?(this.refreshTokenPromise||(this.refreshTokenPromise=this.refreshBearerFn().then(function(t){return e.bearerToken=t,e.refreshTokenPromise=null,e._constructHeaders()}).catch(function(t){throw t})),this.refreshTokenPromise):Promise.resolve(this._constructHeaders())}},{key:"query",value:function(e){var r=0=this._maxRetries)return!1;if(this._retryCallback)try{var r=this._retryCallback(t,e);if(!0===r)return!0;if(!1===r)return!1}catch(t){console.error(t)}if(e&&e.status&&500<=e.status&&501!==e.status)return!0;if(t){if(t.code&&-1!==u.indexOf(t.code))return!0;if(t.timeout&&"ECONNABORTED"===t.code)return!0;if(t.crossDomain)return!0}return!1},i.prototype._retry=function(){return this.clearTimeout(),this.req&&(this.req=null,this.req=this.request()),this._aborted=!1,this.timedout=!1,this._end()},i.prototype.then=function(t,e){var r=this;if(!this._fullfilledPromise){var n=this;this._endCalled&&console.warn("Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises"),this._fullfilledPromise=new Promise(function(t,e){n.on("abort",function(){var t=new Error("Aborted");t.code="ABORTED",t.status=r.status,t.method=r.method,t.url=r.url,e(t)}),n.end(function(r,n){r?e(r):t(n)})})}return this._fullfilledPromise.then(t,e)},i.prototype.catch=function(t){return this.then(void 0,t)},i.prototype.use=function(t){return t(this),this},i.prototype.ok=function(t){if("function"!=typeof t)throw new Error("Callback required");return this._okCallback=t,this},i.prototype._isResponseOK=function(t){return!!t&&(this._okCallback?this._okCallback(t):200<=t.status&&t.status<300)},i.prototype.getHeader=i.prototype.get=function(t){return this._header[t.toLowerCase()]},i.prototype.set=function(t,e){if(o(t)){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&this.set(r,t[r]);return this}return this._header[t.toLowerCase()]=e,this.header[t]=e,this},i.prototype.unset=function(t){return delete this._header[t.toLowerCase()],delete this.header[t],this},i.prototype.field=function(t,e){if(null==t)throw new Error(".field(name, val) name can not be empty");if(this._data)throw new Error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");if(o(t)){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&this.field(r,t[r]);return this}if(Array.isArray(e)){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&this.field(t,e[n]);return this}if(null==e)throw new Error(".field(name, val) val can not be empty");return"boolean"==typeof e&&(e=String(e)),this._getFormData().append(t,e),this},i.prototype.abort=function(){return this._aborted||(this._aborted=!0,this.xhr&&this.xhr.abort(),this.req&&this.req.abort(),this.clearTimeout(),this.emit("abort")),this},i.prototype._auth=function(t,e,r,n){switch(r.type){case"basic":this.set("Authorization","Basic ".concat(n("".concat(t,":").concat(e))));break;case"auto":this.username=t,this.password=e;break;case"bearer":this.set("Authorization","Bearer ".concat(t))}return this},i.prototype.withCredentials=function(t){return void 0===t&&(t=!0),this._withCredentials=t,this},i.prototype.redirects=function(t){return this._maxRedirects=t,this},i.prototype.maxResponseSize=function(t){if("number"!=typeof t)throw new TypeError("Invalid argument");return this._maxResponseSize=t,this},i.prototype.toJSON=function(){return{method:this.method,url:this.url,data:this._data,headers:this._header}},i.prototype.send=function(t){var e=o(t),r=this._header["content-type"];if(this._formData)throw new Error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");if(e&&!this._data)Array.isArray(t)?this._data=[]:this._isHost(t)||(this._data={});else if(t&&this._data&&this._isHost(this._data))throw new Error("Can't merge these send calls");if(e&&o(this._data))for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(this._data[n]=t[n]);else"string"==typeof t?(r||this.type("form"),r=this._header["content-type"],this._data="application/x-www-form-urlencoded"===r?this._data?"".concat(this._data,"&").concat(t):t:(this._data||"")+t):this._data=t;return!e||this._isHost(t)||r||this.type("json"),this},i.prototype.sortQuery=function(t){return this._sort=void 0===t||t,this},i.prototype._finalizeQueryString=function(){var t=this._query.join("&");if(t&&(this.url+=(0<=this.url.indexOf("?")?"&":"?")+t),this._query.length=0,this._sort){var e=this.url.indexOf("?");if(0<=e){var r=this.url.substring(e+1).split("&");"function"==typeof this._sort?r.sort(this._sort):r.sort(),this.url=this.url.substring(0,e)+"?"+r.join("&")}}},i.prototype._appendQueryString=function(){console.warn("Unsupported")},i.prototype._timeoutError=function(t,e,r){if(!this._aborted){var n=new Error("".concat(t+e,"ms exceeded"));n.timeout=e,n.code="ECONNABORTED",n.errno=r,this.timedout=!0,this.abort(),this.callback(n)}},i.prototype._setTimeouts=function(){var t=this;this._timeout&&!this._timer&&(this._timer=setTimeout(function(){t._timeoutError("Timeout of ",t._timeout,"ETIME")},this._timeout)),this._responseTimeout&&!this._responseTimeoutTimer&&(this._responseTimeoutTimer=setTimeout(function(){t._timeoutError("Response timeout of ",t._responseTimeout,"ETIMEDOUT")},this._responseTimeout))}},function(t,e,r){"use strict";var n=r(20);function o(t){if(t)return function(t){for(var e in o.prototype)Object.prototype.hasOwnProperty.call(o.prototype,e)&&(t[e]=o.prototype[e]);return t}(t)}(t.exports=o).prototype.get=function(t){return this.header[t.toLowerCase()]},o.prototype._setHeaderProperties=function(t){var e=t["content-type"]||"";this.type=n.type(e);var r=n.params(e);for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(this[o]=r[o]);this.links={};try{t.link&&(this.links=n.parseLinks(t.link))}catch(t){}},o.prototype._setStatusProperties=function(t){var e=t/100|0;this.statusCode=t,this.status=this.statusCode,this.statusType=e,this.info=1==e,this.ok=2==e,this.redirect=3==e,this.clientError=4==e,this.serverError=5==e,this.error=(4==e||5==e)&&this.toError(),this.created=201===t,this.accepted=202===t,this.noContent=204===t,this.badRequest=400===t,this.unauthorized=401===t,this.notAcceptable=406===t,this.forbidden=403===t,this.notFound=404===t,this.unprocessableEntity=422===t}},function(t,e,r){"use strict";e.type=function(t){return t.split(/ *; */).shift()},e.params=function(t){return t.split(/ *; */).reduce(function(t,e){var r=e.split(/ *= */),n=r.shift(),o=r.shift();return n&&o&&(t[n]=o),t},{})},e.parseLinks=function(t){return t.split(/ *, */).reduce(function(t,e){var r=e.split(/ *; */),n=r[0].slice(1,-1);return t[r[1].split(/ *= */)[1].slice(1,-1)]=n,t},{})},e.cleanHeader=function(t,e){return delete t["content-type"],delete t["content-length"],delete t["transfer-encoding"],delete t.host,e&&(delete t.authorization,delete t.cookie),t}},function(t,e,r){"use strict";function n(){this._defaults=[]}["use","on","once","set","query","type","accept","auth","withCredentials","sortQuery","retry","ok","redirects","timeout","buffer","serialize","parse","ca","key","pfx","cert"].forEach(function(t){n.prototype[t]=function(){for(var e=arguments.length,r=new Array(e),n=0;n>(-2*i&6))))o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(o);return s}},function(t,e,r){function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var r=0;r=Math.floor(c.paginationTotal/c.size)?Promise.resolve(c.current):(c.page+=1,c._query(t))},this.previous=function(t){return c.page<=0?Promise.resolve(c.current):(c.page-=1,c._query(t))},this.goToPage=function(t,e){return c.page=t,c._query(e)}}},function(t,e,r){function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var r=0;rr)}catch(t){return!1}}},{key:"serialize",value:function(t){var e=[];return Object.keys(t).forEach(function(r){void 0!==t[r]&&e.push("".concat(encodeURIComponent(r),"=").concat(encodeURIComponent(t[r])))}),e.length?"?".concat(e.join("&")):""}}],(r=[{key:"_constructHeaders",value:function(){var t={"X-Builton-API-Key":this.apiKey};return this.bearerToken&&(t.Authorization="Bearer ".concat(this.bearerToken)),t}},{key:"getHeaders",value:function(){var e=this;return this.bearerToken&&!t.isJWTAlive(this.bearerToken)&&this.refreshBearerFn?(this.refreshTokenPromise||(this.refreshTokenPromise=this.refreshBearerFn().then(function(t){return e.bearerToken=t,e.refreshTokenPromise=null,e._constructHeaders()}).catch(function(t){throw t})),this.refreshTokenPromise):Promise.resolve(this._constructHeaders())}},{key:"query",value:function(e){var r=0=this._maxRetries)return!1;if(this._retryCallback)try{var r=this._retryCallback(t,e);if(!0===r)return!0;if(!1===r)return!1}catch(t){console.error(t)}if(e&&e.status&&500<=e.status&&501!==e.status)return!0;if(t){if(t.code&&-1!==u.indexOf(t.code))return!0;if(t.timeout&&"ECONNABORTED"===t.code)return!0;if(t.crossDomain)return!0}return!1},i.prototype._retry=function(){return this.clearTimeout(),this.req&&(this.req=null,this.req=this.request()),this._aborted=!1,this.timedout=!1,this._end()},i.prototype.then=function(t,e){var r=this;if(!this._fullfilledPromise){var n=this;this._endCalled&&console.warn("Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises"),this._fullfilledPromise=new Promise(function(t,e){n.on("abort",function(){var t=new Error("Aborted");t.code="ABORTED",t.status=r.status,t.method=r.method,t.url=r.url,e(t)}),n.end(function(r,n){r?e(r):t(n)})})}return this._fullfilledPromise.then(t,e)},i.prototype.catch=function(t){return this.then(void 0,t)},i.prototype.use=function(t){return t(this),this},i.prototype.ok=function(t){if("function"!=typeof t)throw new Error("Callback required");return this._okCallback=t,this},i.prototype._isResponseOK=function(t){return!!t&&(this._okCallback?this._okCallback(t):200<=t.status&&t.status<300)},i.prototype.getHeader=i.prototype.get=function(t){return this._header[t.toLowerCase()]},i.prototype.set=function(t,e){if(o(t)){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&this.set(r,t[r]);return this}return this._header[t.toLowerCase()]=e,this.header[t]=e,this},i.prototype.unset=function(t){return delete this._header[t.toLowerCase()],delete this.header[t],this},i.prototype.field=function(t,e){if(null==t)throw new Error(".field(name, val) name can not be empty");if(this._data)throw new Error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");if(o(t)){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&this.field(r,t[r]);return this}if(Array.isArray(e)){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&this.field(t,e[n]);return this}if(null==e)throw new Error(".field(name, val) val can not be empty");return"boolean"==typeof e&&(e=String(e)),this._getFormData().append(t,e),this},i.prototype.abort=function(){return this._aborted||(this._aborted=!0,this.xhr&&this.xhr.abort(),this.req&&this.req.abort(),this.clearTimeout(),this.emit("abort")),this},i.prototype._auth=function(t,e,r,n){switch(r.type){case"basic":this.set("Authorization","Basic ".concat(n("".concat(t,":").concat(e))));break;case"auto":this.username=t,this.password=e;break;case"bearer":this.set("Authorization","Bearer ".concat(t))}return this},i.prototype.withCredentials=function(t){return void 0===t&&(t=!0),this._withCredentials=t,this},i.prototype.redirects=function(t){return this._maxRedirects=t,this},i.prototype.maxResponseSize=function(t){if("number"!=typeof t)throw new TypeError("Invalid argument");return this._maxResponseSize=t,this},i.prototype.toJSON=function(){return{method:this.method,url:this.url,data:this._data,headers:this._header}},i.prototype.send=function(t){var e=o(t),r=this._header["content-type"];if(this._formData)throw new Error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");if(e&&!this._data)Array.isArray(t)?this._data=[]:this._isHost(t)||(this._data={});else if(t&&this._data&&this._isHost(this._data))throw new Error("Can't merge these send calls");if(e&&o(this._data))for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(this._data[n]=t[n]);else"string"==typeof t?(r||this.type("form"),r=this._header["content-type"],this._data="application/x-www-form-urlencoded"===r?this._data?"".concat(this._data,"&").concat(t):t:(this._data||"")+t):this._data=t;return!e||this._isHost(t)||r||this.type("json"),this},i.prototype.sortQuery=function(t){return this._sort=void 0===t||t,this},i.prototype._finalizeQueryString=function(){var t=this._query.join("&");if(t&&(this.url+=(0<=this.url.indexOf("?")?"&":"?")+t),this._query.length=0,this._sort){var e=this.url.indexOf("?");if(0<=e){var r=this.url.substring(e+1).split("&");"function"==typeof this._sort?r.sort(this._sort):r.sort(),this.url=this.url.substring(0,e)+"?"+r.join("&")}}},i.prototype._appendQueryString=function(){console.warn("Unsupported")},i.prototype._timeoutError=function(t,e,r){if(!this._aborted){var n=new Error("".concat(t+e,"ms exceeded"));n.timeout=e,n.code="ECONNABORTED",n.errno=r,this.timedout=!0,this.abort(),this.callback(n)}},i.prototype._setTimeouts=function(){var t=this;this._timeout&&!this._timer&&(this._timer=setTimeout(function(){t._timeoutError("Timeout of ",t._timeout,"ETIME")},this._timeout)),this._responseTimeout&&!this._responseTimeoutTimer&&(this._responseTimeoutTimer=setTimeout(function(){t._timeoutError("Response timeout of ",t._responseTimeout,"ETIMEDOUT")},this._responseTimeout))}},function(t,e,r){"use strict";var n=r(20);function o(t){if(t)return function(t){for(var e in o.prototype)Object.prototype.hasOwnProperty.call(o.prototype,e)&&(t[e]=o.prototype[e]);return t}(t)}(t.exports=o).prototype.get=function(t){return this.header[t.toLowerCase()]},o.prototype._setHeaderProperties=function(t){var e=t["content-type"]||"";this.type=n.type(e);var r=n.params(e);for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(this[o]=r[o]);this.links={};try{t.link&&(this.links=n.parseLinks(t.link))}catch(t){}},o.prototype._setStatusProperties=function(t){var e=t/100|0;this.statusCode=t,this.status=this.statusCode,this.statusType=e,this.info=1==e,this.ok=2==e,this.redirect=3==e,this.clientError=4==e,this.serverError=5==e,this.error=(4==e||5==e)&&this.toError(),this.created=201===t,this.accepted=202===t,this.noContent=204===t,this.badRequest=400===t,this.unauthorized=401===t,this.notAcceptable=406===t,this.forbidden=403===t,this.notFound=404===t,this.unprocessableEntity=422===t}},function(t,e,r){"use strict";e.type=function(t){return t.split(/ *; */).shift()},e.params=function(t){return t.split(/ *; */).reduce(function(t,e){var r=e.split(/ *= */),n=r.shift(),o=r.shift();return n&&o&&(t[n]=o),t},{})},e.parseLinks=function(t){return t.split(/ *, */).reduce(function(t,e){var r=e.split(/ *; */),n=r[0].slice(1,-1);return t[r[1].split(/ *= */)[1].slice(1,-1)]=n,t},{})},e.cleanHeader=function(t,e){return delete t["content-type"],delete t["content-length"],delete t["transfer-encoding"],delete t.host,e&&(delete t.authorization,delete t.cookie),t}},function(t,e,r){"use strict";function n(){this._defaults=[]}["use","on","once","set","query","type","accept","auth","withCredentials","sortQuery","retry","ok","redirects","timeout","buffer","serialize","parse","ca","key","pfx","cert"].forEach(function(t){n.prototype[t]=function(){for(var e=arguments.length,r=new Array(e),n=0;n>(-2*i&6))))o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(o);return s}},function(t,e,r){function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var r=0;r * @license MIT */ -var n=r(42),o=r(43),i=r(44);function u(){return a.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function s(t,e){if(u()=u())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+u().toString(16)+" bytes");return 0|t}function y(t,e){if(a.isBuffer(t))return t.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(t)||t instanceof ArrayBuffer))return t.byteLength;"string"!=typeof t&&(t=""+t);var r=t.length;if(0===r)return 0;for(var n=!1;;)switch(e){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return L(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return z(t).length;default:if(n)return L(t).length;e=(""+e).toLowerCase(),n=!0}}function b(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function d(t,e,r,n,o){if(0===t.length)return-1;if("string"==typeof r?(n=r,r=0):2147483647=t.length){if(o)return-1;r=t.length-1}else if(r<0){if(!o)return-1;r=0}if("string"==typeof e&&(e=a.from(e,n)),a.isBuffer(e))return 0===e.length?-1:v(t,e,r,n,o);if("number"==typeof e)return e&=255,a.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?o?Uint8Array.prototype.indexOf.call(t,e,r):Uint8Array.prototype.lastIndexOf.call(t,e,r):v(t,[e],r,n,o);throw new TypeError("val must be string, number or Buffer")}function v(t,e,r,n,o){var i,u=1,s=t.length,a=e.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||e.length<2)return-1;s/=u=2,a/=2,r/=2}function c(t,e){return 1===u?t[e]:t.readUInt16BE(e*u)}if(o){var f=-1;for(i=r;i>8,o=r%256,i.push(o),i.push(n);return i}(e,t.length-r),t,r,n)}function j(t,e,r){return 0===e&&r===t.length?n.fromByteArray(t):n.fromByteArray(t.slice(e,r))}function S(t,e,r){r=Math.min(t.length,r);for(var n=[],o=e;o>>10&1023|55296),f=56320|1023&f),n.push(f),o+=l}return function(t){var e=t.length;if(e<=E)return String.fromCharCode.apply(String,t);for(var r="",n=0;nthis.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if((r>>>=0)<=(e>>>=0))return"";for(t=t||"utf8";;)switch(t){case"hex":return k(this,e,r);case"utf8":case"utf-8":return S(this,e,r);case"ascii":return T(this,e,r);case"latin1":case"binary":return A(this,e,r);case"base64":return j(this,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return R(this,e,r);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}.apply(this,arguments)},a.prototype.equals=function(t){if(!a.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===a.compare(this,t)},a.prototype.inspect=function(){var t="",r=e.INSPECT_MAX_BYTES;return 0r&&(t+=" ... ")),""},a.prototype.compare=function(t,e,r,n,o){if(!a.isBuffer(t))throw new TypeError("Argument must be a Buffer");if(void 0===e&&(e=0),void 0===r&&(r=t?t.length:0),void 0===n&&(n=0),void 0===o&&(o=this.length),e<0||r>t.length||n<0||o>this.length)throw new RangeError("out of range index");if(o<=n&&r<=e)return 0;if(o<=n)return-1;if(r<=e)return 1;if(this===t)return 0;for(var i=(o>>>=0)-(n>>>=0),u=(r>>>=0)-(e>>>=0),s=Math.min(i,u),c=this.slice(n,o),f=t.slice(e,r),l=0;lthis.length)throw new RangeError("Attempt to write outside buffer bounds");n=n||"utf8";for(var i=!1;;)switch(n){case"hex":return g(this,t,e,r);case"utf8":case"utf-8":return m(this,t,e,r);case"ascii":return w(this,t,e,r);case"latin1":case"binary":return _(this,t,e,r);case"base64":return O(this,t,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return P(this,t,e,r);default:if(i)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),i=!0}},a.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var E=4096;function T(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;ot.length)throw new RangeError("Index out of range")}function q(t,e,r,n){e<0&&(e=65535+e+1);for(var o=0,i=Math.min(t.length-r,2);o>>8*(n?o:1-o)}function I(t,e,r,n){e<0&&(e=4294967295+e+1);for(var o=0,i=Math.min(t.length-r,4);o>>8*(n?o:3-o)&255}function D(t,e,r,n){if(r+n>t.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function B(t,e,r,n,i){return i||D(t,0,r,4),o.write(t,e,r,n,23,4),r+4}function U(t,e,r,n,i){return i||D(t,0,r,8),o.write(t,e,r,n,52,8),r+8}a.prototype.slice=function(t,e){var r,n=this.length;if((t=~~t)<0?(t+=n)<0&&(t=0):n>>8):q(this,t,e,!0),e+2},a.prototype.writeUInt16BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,2,65535,0),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):q(this,t,e,!1),e+2},a.prototype.writeUInt32LE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,4294967295,0),a.TYPED_ARRAY_SUPPORT?(this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t):I(this,t,e,!0),e+4},a.prototype.writeUInt32BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,4294967295,0),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):I(this,t,e,!1),e+4},a.prototype.writeIntLE=function(t,e,r,n){if(t=+t,e|=0,!n){var o=Math.pow(2,8*r-1);C(this,t,e,r,o-1,-o)}var i=0,u=1,s=0;for(this[e]=255&t;++i>0)-s&255;return e+r},a.prototype.writeIntBE=function(t,e,r,n){if(t=+t,e|=0,!n){var o=Math.pow(2,8*r-1);C(this,t,e,r,o-1,-o)}var i=r-1,u=1,s=0;for(this[e+i]=255&t;0<=--i&&(u*=256);)t<0&&0===s&&0!==this[e+i+1]&&(s=1),this[e+i]=(t/u>>0)-s&255;return e+r},a.prototype.writeInt8=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,1,127,-128),a.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),t<0&&(t=255+t+1),this[e]=255&t,e+1},a.prototype.writeInt16LE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,2,32767,-32768),a.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):q(this,t,e,!0),e+2},a.prototype.writeInt16BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,2,32767,-32768),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):q(this,t,e,!1),e+2},a.prototype.writeInt32LE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,2147483647,-2147483648),a.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24):I(this,t,e,!0),e+4},a.prototype.writeInt32BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):I(this,t,e,!1),e+4},a.prototype.writeFloatLE=function(t,e,r){return B(this,t,e,!0,r)},a.prototype.writeFloatBE=function(t,e,r){return B(this,t,e,!1,r)},a.prototype.writeDoubleLE=function(t,e,r){return U(this,t,e,!0,r)},a.prototype.writeDoubleBE=function(t,e,r){return U(this,t,e,!1,r)},a.prototype.copy=function(t,e,r,n){if(r=r||0,n||0===n||(n=this.length),e>=t.length&&(e=t.length),e=e||0,0=this.length)throw new RangeError("sourceStart out of bounds");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-e>>=0,r=void 0===r?this.length:r>>>0,"number"==typeof(t=t||0))for(i=e;i>6|192,63&r|128)}else if(r<65536){if((e-=3)<0)break;i.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return i}function z(t){return n.toByteArray(function(t){if((t=function(t){return t.trim?t.trim():t.replace(/^\s+|\s+$/g,"")}(t).replace(M,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function Y(t,e,r,n){for(var o=0;o=e.length||o>=t.length);++o)e[o+r]=t[o];return o}}).call(this,r(41))},function(t,e){var r;r=function(){return this}();try{r=r||new Function("return this")()}catch(t){"object"==typeof window&&(r=window)}t.exports=r},function(t,e,r){"use strict";e.byteLength=function(t){var e=c(t),r=e[0],n=e[1];return 3*(r+n)/4-n},e.toByteArray=function(t){var e,r,n=c(t),u=n[0],s=n[1],a=new i(function(t,e,r){return 3*(e+r)/4-r}(0,u,s)),f=0,l=0>16&255,a[f++]=e>>8&255,a[f++]=255&e;return 2===s&&(e=o[t.charCodeAt(r)]<<2|o[t.charCodeAt(r+1)]>>4,a[f++]=255&e),1===s&&(e=o[t.charCodeAt(r)]<<10|o[t.charCodeAt(r+1)]<<4|o[t.charCodeAt(r+2)]>>2,a[f++]=e>>8&255,a[f++]=255&e),a},e.fromByteArray=function(t){for(var e,r=t.length,o=r%3,i=[],u=0,s=r-o;u>2]+n[e<<4&63]+"==")):2==o&&(e=(t[r-2]<<8)+t[r-1],i.push(n[e>>10]+n[e>>4&63]+n[e<<2&63]+"=")),i.join("")};for(var n=[],o=[],i="undefined"!=typeof Uint8Array?Uint8Array:Array,u="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",s=0,a=u.length;s>18&63]+n[i>>12&63]+n[i>>6&63]+n[63&i]);return u.join("")}o["-".charCodeAt(0)]=62,o["_".charCodeAt(0)]=63},function(t,e){e.read=function(t,e,r,n,o){var i,u,s=8*o-n-1,a=(1<>1,f=-7,l=r?o-1:0,p=r?-1:1,h=t[e+l];for(l+=p,i=h&(1<<-f)-1,h>>=-f,f+=s;0>=-f,f+=n;0>1,p=23===o?Math.pow(2,-24)-Math.pow(2,-77):0,h=n?0:i-1,y=n?1:-1,b=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(s=isNaN(e)?1:0,u=f):(u=Math.floor(Math.log(e)/Math.LN2),e*(a=Math.pow(2,-u))<1&&(u--,a*=2),2<=(e+=1<=u+l?p/a:p*Math.pow(2,1-l))*a&&(u++,a/=2),f<=u+l?(s=0,u=f):1<=u+l?(s=(e*a-1)*Math.pow(2,o),u+=l):(s=e*Math.pow(2,l-1)*Math.pow(2,o),u=0));8<=o;t[r+h]=255&s,h+=y,s/=256,o-=8);for(u=u<o.length(this._area)&&(n--,r--)}return e||this},keys:function(t){return this.each(function(t,e,r){r.push(t)},t||[])},get:function(t,e){var r=o.get(this._area,this._in(t));return null!==r?o.parse(r):e||r},getAll:function(t){return this.each(function(t,e,r){r[t]=e},t||{})},transact:function(t,e,r){var n=this.get(t,r),o=e(n);return this.set(t,void 0===o?n:o),this},set:function(t,e,r){var n=this.get(t);return null!=n&&!1===r?e:o.set(this._area,this._in(t),o.stringify(e),r)||n},setAll:function(t,e){var r,n;for(var o in t)n=t[o],this.set(o,n,e)!==n&&(r=!0);return r},add:function(t,e){var r=this.get(t);if(r instanceof Array)e=r.concat(e);else if(null!==r){var n=typeof r;if(n==typeof e&&"object"==n){for(var i in e)r[i]=e[i];e=r}else e=r+e}return o.set(this._area,this._in(t),o.stringify(e)),e},remove:function(t,e){var r=this.get(t,e);return o.remove(this._area,this._in(t)),r},clear:function(){return this._ns?this.each(function(t){o.remove(this._area,this._in(t))},1):o.clear(this._area),this},clearAll:function(){var t=this._area;for(var e in o.areas)o.areas.hasOwnProperty(e)&&(this._area=o.areas[e],this.clear());return this._area=t,this},_in:function(t){return"string"!=typeof t&&(t=o.stringify(t)),this._ns?this._ns+t:t},_out:function(t){return this._ns?t&&0===t.indexOf(this._ns)?t.substring(this._ns.length):void 0:t}},storageAPI:{length:0,has:function(t){return this.items.hasOwnProperty(t)},key:function(t){var e=0;for(var r in this.items)if(this.has(r)&&t===e++)return r},setItem:function(t,e){this.has(t)||this.length++,this.items[t]=e},removeItem:function(t){this.has(t)&&(delete this.items[t],this.length--)},getItem:function(t){return this.has(t)?this.items[t]:null},clear:function(){for(var t in this.items)this.removeItem(t)},toString:function(){return this.length+" items in "+this.name+"Storage"}}}).Store("local",function(){try{return localStorage}catch(t){}}())).local=i)._=o,i.area("session",function(){try{return sessionStorage}catch(t){}}()),"function"==typeof n&&void 0!==n.amd?n("store2",[],function(){return i}):t.exports?t.exports=i:(this.store&&(o.conflict=this.store),this.store=i)}]); \ No newline at end of file +var n=r(44),o=r(45),i=r(46);function u(){return a.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function s(t,e){if(u()=u())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+u().toString(16)+" bytes");return 0|t}function y(t,e){if(a.isBuffer(t))return t.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(t)||t instanceof ArrayBuffer))return t.byteLength;"string"!=typeof t&&(t=""+t);var r=t.length;if(0===r)return 0;for(var n=!1;;)switch(e){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return L(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return z(t).length;default:if(n)return L(t).length;e=(""+e).toLowerCase(),n=!0}}function d(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function b(t,e,r,n,o){if(0===t.length)return-1;if("string"==typeof r?(n=r,r=0):2147483647=t.length){if(o)return-1;r=t.length-1}else if(r<0){if(!o)return-1;r=0}if("string"==typeof e&&(e=a.from(e,n)),a.isBuffer(e))return 0===e.length?-1:v(t,e,r,n,o);if("number"==typeof e)return e&=255,a.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?o?Uint8Array.prototype.indexOf.call(t,e,r):Uint8Array.prototype.lastIndexOf.call(t,e,r):v(t,[e],r,n,o);throw new TypeError("val must be string, number or Buffer")}function v(t,e,r,n,o){var i,u=1,s=t.length,a=e.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||e.length<2)return-1;s/=u=2,a/=2,r/=2}function c(t,e){return 1===u?t[e]:t.readUInt16BE(e*u)}if(o){var f=-1;for(i=r;i>8,o=r%256,i.push(o),i.push(n);return i}(e,t.length-r),t,r,n)}function j(t,e,r){return 0===e&&r===t.length?n.fromByteArray(t):n.fromByteArray(t.slice(e,r))}function S(t,e,r){r=Math.min(t.length,r);for(var n=[],o=e;o>>10&1023|55296),f=56320|1023&f),n.push(f),o+=l}return function(t){var e=t.length;if(e<=E)return String.fromCharCode.apply(String,t);for(var r="",n=0;nthis.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if((r>>>=0)<=(e>>>=0))return"";for(t=t||"utf8";;)switch(t){case"hex":return A(this,e,r);case"utf8":case"utf-8":return S(this,e,r);case"ascii":return T(this,e,r);case"latin1":case"binary":return k(this,e,r);case"base64":return j(this,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return R(this,e,r);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}.apply(this,arguments)},a.prototype.equals=function(t){if(!a.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===a.compare(this,t)},a.prototype.inspect=function(){var t="",r=e.INSPECT_MAX_BYTES;return 0r&&(t+=" ... ")),""},a.prototype.compare=function(t,e,r,n,o){if(!a.isBuffer(t))throw new TypeError("Argument must be a Buffer");if(void 0===e&&(e=0),void 0===r&&(r=t?t.length:0),void 0===n&&(n=0),void 0===o&&(o=this.length),e<0||r>t.length||n<0||o>this.length)throw new RangeError("out of range index");if(o<=n&&r<=e)return 0;if(o<=n)return-1;if(r<=e)return 1;if(this===t)return 0;for(var i=(o>>>=0)-(n>>>=0),u=(r>>>=0)-(e>>>=0),s=Math.min(i,u),c=this.slice(n,o),f=t.slice(e,r),l=0;lthis.length)throw new RangeError("Attempt to write outside buffer bounds");n=n||"utf8";for(var i=!1;;)switch(n){case"hex":return g(this,t,e,r);case"utf8":case"utf-8":return m(this,t,e,r);case"ascii":return w(this,t,e,r);case"latin1":case"binary":return _(this,t,e,r);case"base64":return P(this,t,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return O(this,t,e,r);default:if(i)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),i=!0}},a.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var E=4096;function T(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;ot.length)throw new RangeError("Index out of range")}function q(t,e,r,n){e<0&&(e=65535+e+1);for(var o=0,i=Math.min(t.length-r,2);o>>8*(n?o:1-o)}function I(t,e,r,n){e<0&&(e=4294967295+e+1);for(var o=0,i=Math.min(t.length-r,4);o>>8*(n?o:3-o)&255}function D(t,e,r,n){if(r+n>t.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function B(t,e,r,n,i){return i||D(t,0,r,4),o.write(t,e,r,n,23,4),r+4}function U(t,e,r,n,i){return i||D(t,0,r,8),o.write(t,e,r,n,52,8),r+8}a.prototype.slice=function(t,e){var r,n=this.length;if((t=~~t)<0?(t+=n)<0&&(t=0):n>>8):q(this,t,e,!0),e+2},a.prototype.writeUInt16BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,2,65535,0),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):q(this,t,e,!1),e+2},a.prototype.writeUInt32LE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,4294967295,0),a.TYPED_ARRAY_SUPPORT?(this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t):I(this,t,e,!0),e+4},a.prototype.writeUInt32BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,4294967295,0),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):I(this,t,e,!1),e+4},a.prototype.writeIntLE=function(t,e,r,n){if(t=+t,e|=0,!n){var o=Math.pow(2,8*r-1);C(this,t,e,r,o-1,-o)}var i=0,u=1,s=0;for(this[e]=255&t;++i>0)-s&255;return e+r},a.prototype.writeIntBE=function(t,e,r,n){if(t=+t,e|=0,!n){var o=Math.pow(2,8*r-1);C(this,t,e,r,o-1,-o)}var i=r-1,u=1,s=0;for(this[e+i]=255&t;0<=--i&&(u*=256);)t<0&&0===s&&0!==this[e+i+1]&&(s=1),this[e+i]=(t/u>>0)-s&255;return e+r},a.prototype.writeInt8=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,1,127,-128),a.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),t<0&&(t=255+t+1),this[e]=255&t,e+1},a.prototype.writeInt16LE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,2,32767,-32768),a.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):q(this,t,e,!0),e+2},a.prototype.writeInt16BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,2,32767,-32768),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):q(this,t,e,!1),e+2},a.prototype.writeInt32LE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,2147483647,-2147483648),a.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24):I(this,t,e,!0),e+4},a.prototype.writeInt32BE=function(t,e,r){return t=+t,e|=0,r||C(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),a.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):I(this,t,e,!1),e+4},a.prototype.writeFloatLE=function(t,e,r){return B(this,t,e,!0,r)},a.prototype.writeFloatBE=function(t,e,r){return B(this,t,e,!1,r)},a.prototype.writeDoubleLE=function(t,e,r){return U(this,t,e,!0,r)},a.prototype.writeDoubleBE=function(t,e,r){return U(this,t,e,!1,r)},a.prototype.copy=function(t,e,r,n){if(r=r||0,n||0===n||(n=this.length),e>=t.length&&(e=t.length),e=e||0,0=this.length)throw new RangeError("sourceStart out of bounds");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-e>>=0,r=void 0===r?this.length:r>>>0,"number"==typeof(t=t||0))for(i=e;i>6|192,63&r|128)}else if(r<65536){if((e-=3)<0)break;i.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return i}function z(t){return n.toByteArray(function(t){if((t=function(t){return t.trim?t.trim():t.replace(/^\s+|\s+$/g,"")}(t).replace(M,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function Y(t,e,r,n){for(var o=0;o=e.length||o>=t.length);++o)e[o+r]=t[o];return o}}).call(this,r(43))},function(t,e){var r;r=function(){return this}();try{r=r||new Function("return this")()}catch(t){"object"==typeof window&&(r=window)}t.exports=r},function(t,e,r){"use strict";e.byteLength=function(t){var e=c(t),r=e[0],n=e[1];return 3*(r+n)/4-n},e.toByteArray=function(t){var e,r,n=c(t),u=n[0],s=n[1],a=new i(function(t,e,r){return 3*(e+r)/4-r}(0,u,s)),f=0,l=0>16&255,a[f++]=e>>8&255,a[f++]=255&e;return 2===s&&(e=o[t.charCodeAt(r)]<<2|o[t.charCodeAt(r+1)]>>4,a[f++]=255&e),1===s&&(e=o[t.charCodeAt(r)]<<10|o[t.charCodeAt(r+1)]<<4|o[t.charCodeAt(r+2)]>>2,a[f++]=e>>8&255,a[f++]=255&e),a},e.fromByteArray=function(t){for(var e,r=t.length,o=r%3,i=[],u=0,s=r-o;u>2]+n[e<<4&63]+"==")):2==o&&(e=(t[r-2]<<8)+t[r-1],i.push(n[e>>10]+n[e>>4&63]+n[e<<2&63]+"=")),i.join("")};for(var n=[],o=[],i="undefined"!=typeof Uint8Array?Uint8Array:Array,u="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",s=0,a=u.length;s>18&63]+n[i>>12&63]+n[i>>6&63]+n[63&i]);return u.join("")}o["-".charCodeAt(0)]=62,o["_".charCodeAt(0)]=63},function(t,e){e.read=function(t,e,r,n,o){var i,u,s=8*o-n-1,a=(1<>1,f=-7,l=r?o-1:0,p=r?-1:1,h=t[e+l];for(l+=p,i=h&(1<<-f)-1,h>>=-f,f+=s;0>=-f,f+=n;0>1,p=23===o?Math.pow(2,-24)-Math.pow(2,-77):0,h=n?0:i-1,y=n?1:-1,d=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(s=isNaN(e)?1:0,u=f):(u=Math.floor(Math.log(e)/Math.LN2),e*(a=Math.pow(2,-u))<1&&(u--,a*=2),2<=(e+=1<=u+l?p/a:p*Math.pow(2,1-l))*a&&(u++,a/=2),f<=u+l?(s=0,u=f):1<=u+l?(s=(e*a-1)*Math.pow(2,o),u+=l):(s=e*Math.pow(2,l-1)*Math.pow(2,o),u=0));8<=o;t[r+h]=255&s,h+=y,s/=256,o-=8);for(u=u<o.length(this._area)&&(n--,r--)}return e||this},keys:function(t){return this.each(function(t,e,r){r.push(t)},t||[])},get:function(t,e){var r=o.get(this._area,this._in(t));return null!==r?o.parse(r):e||r},getAll:function(t){return this.each(function(t,e,r){r[t]=e},t||{})},transact:function(t,e,r){var n=this.get(t,r),o=e(n);return this.set(t,void 0===o?n:o),this},set:function(t,e,r){var n=this.get(t);return null!=n&&!1===r?e:o.set(this._area,this._in(t),o.stringify(e),r)||n},setAll:function(t,e){var r,n;for(var o in t)n=t[o],this.set(o,n,e)!==n&&(r=!0);return r},add:function(t,e){var r=this.get(t);if(r instanceof Array)e=r.concat(e);else if(null!==r){var n=typeof r;if(n==typeof e&&"object"==n){for(var i in e)r[i]=e[i];e=r}else e=r+e}return o.set(this._area,this._in(t),o.stringify(e)),e},remove:function(t,e){var r=this.get(t,e);return o.remove(this._area,this._in(t)),r},clear:function(){return this._ns?this.each(function(t){o.remove(this._area,this._in(t))},1):o.clear(this._area),this},clearAll:function(){var t=this._area;for(var e in o.areas)o.areas.hasOwnProperty(e)&&(this._area=o.areas[e],this.clear());return this._area=t,this},_in:function(t){return"string"!=typeof t&&(t=o.stringify(t)),this._ns?this._ns+t:t},_out:function(t){return this._ns?t&&0===t.indexOf(this._ns)?t.substring(this._ns.length):void 0:t}},storageAPI:{length:0,has:function(t){return this.items.hasOwnProperty(t)},key:function(t){var e=0;for(var r in this.items)if(this.has(r)&&t===e++)return r},setItem:function(t,e){this.has(t)||this.length++,this.items[t]=e},removeItem:function(t){this.has(t)&&(delete this.items[t],this.length--)},getItem:function(t){return this.has(t)?this.items[t]:null},clear:function(){for(var t in this.items)this.removeItem(t)},toString:function(){return this.length+" items in "+this.name+"Storage"}}}).Store("local",function(){try{return localStorage}catch(t){}}())).local=i)._=o,i.area("session",function(){try{return sessionStorage}catch(t){}}()),"function"==typeof n&&void 0!==n.amd?n("store2",[],function(){return i}):t.exports?t.exports=i:(this.store&&(o.conflict=this.store),this.store=i)}]); \ No newline at end of file diff --git a/examples/stripe-firebase-full-order/index.html b/examples/stripe-firebase-full-order/index.html index 2a4630b..92746ef 100644 --- a/examples/stripe-firebase-full-order/index.html +++ b/examples/stripe-firebase-full-order/index.html @@ -146,13 +146,13 @@ addPaymentMethod: () => { document.getElementById('cardDetails').style.visibility = "visible"; let cardButton = document.getElementById('cardButton'); - return new Promise((resolve) => { + return new Promise((resolve, reject) => { cardButton.addEventListener('click', (ev) => { builton.paymentMethods.createWithProvider(stripeProvider).then(paymentMethod => { paymentMethodRef = paymentMethod; document.getElementById('cardDetails').style.visibility = "hidden"; resolve(paymentMethod); - }); + }).catch(err => reject(err)); }); }); }, @@ -175,7 +175,14 @@ orders: [orderRef.id], payment_method: paymentMethodRef.id } - ) + ).then(payments => { + for (let payment of payments) { + if (payment.error) { + return Promise.reject(payments); + } + } + return payments; + }); } } @@ -202,6 +209,9 @@ } else { stepButton.innerHTML = 'Done!'; } + }).catch((error) => { + displayResponse(error); + addStep(steps[currentStep], false); }); } diff --git a/src/collection/resources/paymentMethods.js b/src/collection/resources/paymentMethods.js index 251ae1f..92aba3c 100644 --- a/src/collection/resources/paymentMethods.js +++ b/src/collection/resources/paymentMethods.js @@ -1,6 +1,7 @@ /* eslint-disable max-classes-per-file */ const Components = require('./_resources'); const PaymentMethod = require('../objects/paymentMethod'); +const Error = require('../../utils/error'); const StripePaymentProvider = require('../../functionality/paymentProvider/stripe'); const VippsPaymentProvider = require('../../functionality/paymentProvider/vipps'); const { @@ -26,12 +27,37 @@ class PaymentMethods extends Components { createWithProvider(provider, { urlParams, json = false } = {}, done) { const createWithStripe = (stripe, cardElement) => this.create({ payment_method: 'stripe', - }).then((holderPaymentMethod) => stripe.handleCardSetup( - holderPaymentMethod.setup_intent.client_secret, - cardElement, - ).then((stripeResponse) => holderPaymentMethod.update({ - payment_method_id: stripeResponse.setupIntent.payment_method, - }, { urlParams, json }, done))); + }) + .catch((err) => { + if (done) { + done(err); + } + return Promise.reject(err); + }) + .then((holderPaymentMethod) => stripe.handleCardSetup( + holderPaymentMethod.setup_intent.client_secret, + cardElement, + ) + .catch((err) => { + const error = new Error.StripeError(err); + if (done) { + done(error); + } + return Promise.reject(error); + }) + .then((stripeResponse) => { + if (stripeResponse.error) { + const error = new Error.StripeError(stripeResponse.error); + if (done) { + done(error); + } + return Promise.reject(error); + } + return holderPaymentMethod.update({ + payment_method_id: stripeResponse.setupIntent.payment_method, + }, { urlParams, json }, done); + })); + const createWithVipps = () => this.create({ payment_method: 'vipps', }); @@ -44,7 +70,7 @@ class PaymentMethods extends Components { case 'VippsPaymentProvider': return createWithVipps(); default: - throw Error('error'); + throw new Error.UnknownPaymentProvider(); } } } diff --git a/src/collection/resources/payments.js b/src/collection/resources/payments.js index 220e104..25f08de 100644 --- a/src/collection/resources/payments.js +++ b/src/collection/resources/payments.js @@ -41,7 +41,11 @@ class Payments extends Components { .catch((error) => { throw new Error.StripeError(error); }) .then((stripeResponse) => { if (stripeResponse.error) { - throw new Error.StripeError(stripeResponse.error); + const error = new Error.StripeError(stripeResponse.error); + if (done) { + done(error); + } + return Promise.reject(error); } // eslint-disable-next-line camelcase const { id, client_secret } = stripeResponse.paymentIntent; @@ -77,7 +81,7 @@ class Payments extends Components { case 'VippsPaymentProvider': return create(body, { json }, done); default: - throw Error('error'); + throw new Error.UnknownPaymentProvider(); } } diff --git a/src/utils/error.js b/src/utils/error.js index 2cb60ca..71f1ef9 100644 --- a/src/utils/error.js +++ b/src/utils/error.js @@ -54,6 +54,12 @@ class ImageUpload extends ExtendableError { } } +class UnknownPaymentProvider extends ExtendableError { + constructor() { + super('This payment method name doesn\'t exist'); + } +} + module.exports = { MethodNeedsId, AbstractClass, @@ -62,4 +68,5 @@ module.exports = { BadRequest, StripeError, ImageUpload, + UnknownPaymentProvider, }; diff --git a/test/architecture/architecture.json b/test/architecture/architecture.json index f3f61b0..faa0407 100644 --- a/test/architecture/architecture.json +++ b/test/architecture/architecture.json @@ -134,7 +134,8 @@ "create", "getAll", "getFromId", - "set" + "set", + "createWithProvider" ] } } @@ -158,7 +159,10 @@ "create", "getFromId", "getAll", - "set" + "set", + "createWithProvider", + "StripePaymentProvider", + "VippsPaymentProvider" ] } } From 50fd27590168c78d3a390b8965657939ec449988 Mon Sep 17 00:00:00 2001 From: Nicolas Bonduel Date: Tue, 18 Feb 2020 11:41:25 +0100 Subject: [PATCH 4/4] removed useless body on authentication --- examples/stripe-firebase-full-order/index.html | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/stripe-firebase-full-order/index.html b/examples/stripe-firebase-full-order/index.html index 92746ef..9edd331 100644 --- a/examples/stripe-firebase-full-order/index.html +++ b/examples/stripe-firebase-full-order/index.html @@ -122,9 +122,6 @@ bearerToken, refreshTokenFn: async () => { return await authResult.user.getIdToken(); - }, - body: { - foo: "bar" } }); })