{"version":3,"file":"financialGateway.js","sources":["../../../../Framework/Core/Controls/financialGateway.ts"],"sourcesContent":["// \n// Copyright by the Spark Development Network\n//\n// Licensed under the Rock Community License (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.rockrms.com/license\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n// \n//\n\nimport { inject, InjectionKey, provide } from \"vue\";\n\n// #region Submit Payment\n\n/** The function signature that will be called when the payment can be submitted. */\ntype SubmitPaymentFunction = () => void;\n\n/**\n * The object to be provided by a parent so that it can interact with the\n * financial gateway.\n */\ntype SubmitPaymentObject = {\n /** The callback to use when submitting the payment. */\n callback?: SubmitPaymentFunction;\n};\n\n/** The unique symbol that holds our custom data. */\nconst submitPaymentCallbackSymbol: InjectionKey = Symbol(\"gateway-submit-payment-callback\");\n\n/**\n * Prepares the gateway control for use. This provides a custom object into the\n * calling controls namespace and then returns the function that can be used to\n * attempt to submit the payment.\n */\nexport function provideSubmitPayment(): SubmitPaymentFunction {\n const container: SubmitPaymentObject = {};\n provide(submitPaymentCallbackSymbol, container);\n\n return () => {\n if (container.callback) {\n container.callback();\n }\n else {\n throw \"Submit payment callback has not been defined.\";\n }\n };\n}\n\n/**\n * Provides the callback from the gateway component that should be called when\n * the user presses a button to initiate payment.\n *\n * @param callback The function to be called when the payment should be attempted.\n */\nexport function onSubmitPayment(callback: SubmitPaymentFunction): void {\n const container = inject(submitPaymentCallbackSymbol);\n\n if (!container) {\n throw \"Gateway control has not been properly initialized.\";\n }\n\n container.callback = callback;\n}\n\n// #endregion\n\n// #region Payment Events\n\n/** Event for when payment is about to be submitted .*/\nexport type PaymentSubmittingEvent = { readonly type: \"submitting\"; };\n\n/** Event for when payment succeeds. */\nexport type PaymentSuccessEvent = { readonly type: \"success\"; };\n\n/** Event for when payment fails. */\nexport type PaymentFailureEvent = { readonly type: \"failure\"; errorMessage: string; };\n\n/** Union of all payment events */\ntype PaymentEvent =\n | PaymentSubmittingEvent\n | PaymentSuccessEvent\n | PaymentFailureEvent;\n\ntype PaymentEventHandler = (event: T) => void;\n\n/**\n * Keeps track of registered event handlers (callbacks) that will be\n * invoked when the associated event is dispatched.\n */\ntype PaymentEventHandlers = {\n submittingCallbacks: PaymentEventHandler[];\n successCallbacks: PaymentEventHandler[];\n failureCallbacks: PaymentEventHandler[];\n};\n\n/**\n * Dispatches payment events.\n */\ntype PaymentEventDispatcher = (event: PaymentEvent) => void;\n\n/**\n * The unique symbol that holds our custom event data.\n */\nconst paymentEventsSymbol: InjectionKey = Symbol(\"gateway-payment-events\");\n\n/**\n * Provides payment event functionality to this component as an event dispatcher,\n * and allows subcomponents to register to payment events either by\n * calling the `onPaymentSubmitting(callback)` functions or by using\n * event bindings with the Obsidian component ``.\n */\nexport function usePaymentEvents(): PaymentEventDispatcher {\n const container: PaymentEventHandlers = {\n submittingCallbacks: [],\n successCallbacks: [],\n failureCallbacks: []\n };\n\n provide(paymentEventsSymbol, container);\n\n return (event: PaymentEvent) => {\n switch (event.type) {\n case \"submitting\":\n if (container.submittingCallbacks) {\n container.submittingCallbacks.forEach(callback => {\n callback(event);\n });\n }\n break;\n\n case \"success\":\n if (container.successCallbacks) {\n container.successCallbacks.forEach(callback => {\n callback(event);\n });\n }\n break;\n\n case \"failure\":\n if (container.failureCallbacks) {\n container.failureCallbacks.forEach(callback => {\n callback(event);\n });\n }\n break;\n\n default:\n console.warn(`Unknown payment event: ${event[\"type\"]}`);\n }\n };\n}\n\n/**\n * Provides a callback that is invoked when a payment is about to be submitted.\n *\n * @returns A function to deregister this callback.\n */\nexport function onPaymentSubmitting(callback: PaymentEventHandler): () => void {\n const container = inject(paymentEventsSymbol);\n\n if (!container) {\n throw \"Gateway control has not been properly initialized.\";\n }\n\n container.submittingCallbacks.push(callback);\n\n return () => {\n container.submittingCallbacks = container.submittingCallbacks.filter(cb => cb !== callback);\n };\n}\n\n/**\n * Provides a callback that is invoked when a payment succeeds.\n *\n * @returns A function to deregister this callback.\n */\nexport function onPaymentSuccess(callback: PaymentEventHandler): () => void {\n const container = inject(paymentEventsSymbol);\n\n if (!container) {\n throw \"Gateway control has not been properly initialized.\";\n }\n\n container.successCallbacks.push(callback);\n\n return () => {\n container.successCallbacks = container.successCallbacks.filter(cb => cb !== callback);\n };\n}\n\n/**\n * Provides a callback that is invoked when a payment fails.\n *\n * @returns A function to deregister this callback.\n */\nexport function onPaymentFailure(callback: PaymentEventHandler): () => void {\n const container = inject(paymentEventsSymbol);\n\n if (!container) {\n throw \"Gateway control has not been properly initialized.\";\n }\n\n container.failureCallbacks.push(callback);\n\n return () => {\n container.failureCallbacks = container.failureCallbacks.filter(cb => cb !== callback);\n };\n}\n\n// #endregion Payment Events"],"names":["submitPaymentCallbackSymbol","Symbol","provideSubmitPayment","container","provide","callback","onSubmitPayment","inject","paymentEventsSymbol","usePaymentEvents","submittingCallbacks","successCallbacks","failureCallbacks","event","type","forEach","console","warn","concat","onPaymentSubmitting","push","filter","cb","onPaymentSuccess","onPaymentFailure"],"mappings":";;;;;;;;;;;;;;;;;;;YAkCA,IAAMA,2BAA8D,GAAGC,MAAM,CAAC,iCAAiC,CAAC,CAAA;YAOzG,SAASC,oBAAoBA,GAA0B;cAC1D,IAAMC,SAA8B,GAAG,EAAE,CAAA;YACzCC,EAAAA,OAAO,CAACJ,2BAA2B,EAAEG,SAAS,CAAC,CAAA;YAE/C,EAAA,OAAO,MAAM;gBACT,IAAIA,SAAS,CAACE,QAAQ,EAAE;kBACpBF,SAAS,CAACE,QAAQ,EAAE,CAAA;YACxB,KAAC,MACI;YACD,MAAA,MAAM,+CAA+C,CAAA;YACzD,KAAA;eACH,CAAA;YACL,CAAA;YAQO,SAASC,eAAeA,CAACD,QAA+B,EAAQ;YACnE,EAAA,IAAMF,SAAS,GAAGI,MAAM,CAACP,2BAA2B,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