UNPKG

@cocreate/stripe

Version:

A simple stripe component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.

430 lines (408 loc) 14.8 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CoCreate-api Documentation | CoCreateJS</title> <link rel="icon" type="image/png" sizes="32x32" href="https://cocreate.app/images/favicon.ico" /> <meta name="description" content="A simple HTML5 and pure javascript component. Easy configuration using HTML5 attributes or Javscript api and highly styleable." /> <meta name="keywords" content="helper classes, utility classes, css framework, css library, inline style classes" /> <meta name="robots" content="index,follow" /> <meta property="og:image" content="https://cdn.cocreate.app/docs/stripe.png" /> <link rel="stylesheet" href="../index.css" array="files" object="60888216117c640e7596303f" key="src" type="text/css" save="true" /> <link rel="manifest" href="/manifest.webmanifest" /> </head> <body> <!-- Navbar --> <nav class="width:100% display:flex align-items:center background:transparent padding-top:10px padding-bottom:10px" content_id="content" scroll="sticky-nav," scroll-up="5" scroll-down="5" path="../" src="../components/navbar.html"></nav> <sidenav id="menuL" class="position:fixed top:0px left:0px overflow:hidden background:whitesmoke height:100vh width:0px width:300px@xl" resizable resize-query="[content_id='content']" resize-property="margin-left" resize-value="width"> <menu array="files" object="603717b07de7fb350ae9fec8" key="src"></menu> <div resize="right"></div> </sidenav> <main class="padding-top:15px padding:15px@lg@xl" content_id="content" id="cocreate-api"> <div class="display:flex flex-wrap:wrap justify-content:space-between position:relative margin:10px"> <div class="display:flex align-items:center"> <h2>CoCreate-api</h2> </div> <div class="display:flex align-items:center font-size:20px position:absolute right:0 padding:5px background:white"> <div class="display:flex align-items:center transition:0.3s padding-left:10px" share-network="true" share-text="Enter decription here" share-title="testing im a title" share-height="600" share-width="700" share-media="https://cdn.cocreate.app/docs/stripe.png" hover="display:block!important" hover-query=".social-networks"> <div class="display:none social-networks"> <a class="margin-right:15px" share-network="twitter" title="Share on twitter" ><i src="/assets/svg/twitter.svg"></i ></a> <a class="margin-right:15px" share-network="facebook" title="Share on Facebook" ><i src="/assets/svg/facebook.svg"></i ></a> <a class="margin-right:15px" share-network="instagram" title="Share on instagram" ><i src="/assets/svg/instagram.svg"></i ></a> </div> <a class="margin-right:15px" share-network="share" title="Share on share" ><i src="/assets/svg/share-alt.svg"></i ></a> </div> <a href="https://github.com/CoCreate-app/CoCreate-api" target="_blank" class="margin-right:15px" ><i src="/assets/svg/github.svg"></i ></a> </div> </div> <h1 class="max-width:500px margin:20px_10px line-height:1.5 font-size:16px font-weight:100"> Cocreate-actions : Is a library that we use to chain any number of events to a single button </h1> <div id="api-section" class="display:flex flex-wrap:wrap"> <div class="flex-grow:1 width:400px width:300px@xs padding:0px_10px margin-top:60px"> <div id="stripe-install" class="border-bottom:1px_solid_lightgrey" scroll scroll-intersect="color:dodgerblue" scroll-query="#stripe-install-section"> <span class="display:flex align-items:center width:fit-content" hover="display:block!important" hover-query="[href='#stripe-install']"> <h2 class="padding:5px_0px">Install</h2> <a class="margin-left:10px display:none" href="#stripe-install" ><i src="/assets/svg/link.svg"></i ></a> </span> </div> <pre><code class="language-bash">npm i @cocreate/api</code></pre> <p class="padding:10px_0px line-height:1.5"> Or you can use cdn link: </p> <pre><code class="language-html">&lt;script&gt;https://cdn.cocreate.app/stripe/latest/CoCreate-api.min.js&lt;/script&gt;</code></pre> <div id="stripe-usage" class="margin-top:80px border-bottom:1px_solid_lightgrey" scroll scroll-intersect="color:dodgerblue" scroll-query="#stripe-usage-section"> <span class="display:flex align-items:center width:fit-content" hover="display:block!important" hover-query="[href='#stripe-usage']"> <h2 class="padding:5px_0px">Usage</h2> <a class="margin-left:10px display:none" href="#stripe-usage" ><i src="/assets/svg/link.svg"></i ></a> </span> </div> <div class=""> <p class="padding:10px_0px line-height:1.5"> According to the usage of the official documentation of stripe <a href="https://stripe.com/docs/api/customers/create" target="_blank" >CREATE_CUSTOMER</a > and <a href="https://stripe.com/docs/api/cards/create" target="_blank" >CREATE_CARD</a > </p> <div class="flex-grow:1 min-width:300px width:100%"> <pre> <code class="language-js"> Name-file : index-connect.html &#x3C;form&#x3E; &#x3C;input type=&#x22;text&#x22; stripe=&#x22;description&#x22; value=&#x22;descripcion Test Customer&#x22; &#x3E; &#x3C;input type=&#x22;text&#x22; stripe=&#x22;email&#x22; value=&#x22;customertest@cocreate.com&#x22; &#x3E; &#x3C;input type=&#x22;text&#x22; stripe=&#x22;source&#x22; value=&#x22;tok_amex&#x22; &#x3E; &#x3C;button type=&#x22;button&#x22; actions=&#x22;registerCustomerStripe, registerCardStripe&#x22; id=&#x22;register&#x22;&#x3E;create&#x3C;/button&#x3E; &#x3C;/form&#x3E; </code> </pre> </div> <p class="padding:10px_0px line-height:1.5"> An important aspect to take into account is the data attribute actions here the events that will be executed in an orderly manner are defined </p> <div class="flex-grow:1 min-width:300px width:100%"> <pre> <code class="language-js"> Name-file : index-connect.html &#x3C;script type=&#x22;text/javascript&#x22; &#x3E; var SessionStorage = window.sessionStorage function sendDataWebSocket(type,idCustomer=null){ var btn = object.querySelector(&#x27;button#register&#x27;); let dataToSend = cocreateStripeRegisterAPi.getDataJSON(btn,cocreateStripeRegisterAPi); dataToSend[&#x27;type&#x27;] = type; if (idCustomer!=null){ &#x9; dataToSend[&#x27;customer_id&#x27;] = idCustomer; &#x9; //for create CARD &#x9; delete dataToSend[&#x27;description&#x27;]; &#x9; delete dataToSend[&#x27;email&#x27;]; } cocreateStripeRegisterAPi.socket(dataToSend,cocreateStripeRegisterAPi); } function fnRegisterCustomer(){ sendDataWebSocket(&#x27;.createCustomerBtn&#x27;); } function fnRegisterCard(){ sendDataWebSocket(&#x27;.createCardBtn&#x27;,SessionStorage[&#x27;customer_id&#x27;]); } &#x9; CoCreate.actions.registerEvent(&#x27;registerCustomerStripe&#x27;, fnRegisterCustomer, window, &#x27;eventRegisterCustomer&#x27;) CoCreate.actions.registerEvent(&#x27;registerCardStripe&#x27;, fnRegisterCard, window, &#x27;eventRegisterCard&#x27;) &#x3C;/script&#x3E; </code> </pre> </div> <p class="padding:10px_0px line-height:1.5"> The events previously defined in the button must be declared in the javascript code and define the appropriate logic for each event, cocreateActions always waits for an event to finish to start the second event, so we will see the second step below in overwriting the setResult </p> <div class="flex-grow:1 min-width:300px width:100%"> <pre> <code class="language-js"> Name-file WebSocketServer = Cocreate-stripe.js ........ switch (type) { case &#x27;.createCustomerBtn&#x27;: const customer = await stripe.customers.create(data); utils.send_response(that.wsManager,socket,{&#x22;type&#x22;:type,&#x22;response&#x22;:customer},send_response) break; case &#x27;.createCardBtn&#x27;: let customer-id = data[&#x27;customer_id&#x27;]; delete data[&#x27;customer_id&#x27;]; const card = await stripe.customers.createSource( customer-id, data ); utils.send_response(that.wsManager,socket,{&#x22;type&#x22;:type,&#x22;response&#x22;:card},send_response) break; } ........ </code> </pre> </div> <p class="padding:10px_0px line-height:1.5"> Dispatch events to tell cocreate-actions when an event ends </p> <div class="flex-grow:1 min-width:300px width:100%"> <pre> <code class="language-js"> Name-file FrontEnd = Cocreate-stripe.js var CocreateStripeAPi = function() { ........... this.setResult = function(data) { let type = data[&#x22;type&#x22;]; switch(type){ case &#x27;.createCustomerBtn&#x27;: var localstorage = window.sessionStorage; localstorage[&#x27;customer_id&#x27;] = data[&#x27;response&#x27;][&#x22;id&#x22;]; if (data[&#x27;response&#x27;]){ object.dispatchEvent(new CustomEvent(&#x27;eventRegisterCustomer&#x27;)); } break; case &#x27;.createCardBtn&#x27;: object.dispatchEvent(new CustomEvent(&#x27;eventRegisterCard&#x27;)); break; } } ........... } </code> </pre> </div> <p class="padding:10px_0px line-height:1.5"> finally we observe a response, in the console that shows that the events were executed in a chained and correct way using the cocreate-actions bridge </p> <p class="padding:10px_0px line-height:1.5"> <img src="img/create_card.png" width="98%" /> </p> </div> </div> <div class="flex-grow:1 width:300px padding:0px_10px margin-top:60px border-bottom:1px_solid_lightgrey"> <!-- SandBox --> <div id="stripe-demo" class="border-bottom:1px_solid_lightgrey" scroll scroll-intersect="color:dodgerblue" scroll-query="#stripe-demo-section"> <span class="display:flex align-items:center width:fit-content" hover="display:block!important" hover-query="[href='#stripe-demo']"> <h2 class="padding:5px_0px">Demo</h2> <a class="margin-left:10px display:none" href="#stripe-demo" ><i src="/assets/svg/link.svg"></i ></a> </span> </div> <div class="position:sticky top:0 padding:15px_0px height:100vh"> <!-- SandBox --> <div class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px" id="playground"> <div id="demo-code" resizable class="position:relative height:50%"> <textarea type="code" lang="html" array="demos" object="" key="demo" save="false" id="demo" input-query=".demopreview" input-attribute="value" input-events="input, onload" class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea> <div resize="bottom" class="background:lightgrey"></div> </div> <div id="demo-preview" class="position:relative overflow:auto background-color:white"> <div class="demopreview padding:20px"></div> </div> <div class="font-size:20px position:absolute top:10px right:10px opacity:0.6"> <a class="margin-right:10px" id="eye" show="#eye-slash" hide="#eye, #demo-preview" toggle="code-height" toggle-query="#demo-code" ><i class="height:18px" src="/assets/svg/eye.svg"></i ></a> <a class="margin-right:10px" hidden id="eye-slash" show="#eye, #demo-preview" hide="#eye-slash" toggle="code-height" toggle-query="#demo-code" ><i src="/assets/svg/eye-slash.svg"></i ></a> <a class="margin-right:10px" id="code" show="#code-slash" hide="#code, #demo-code" ><i src="/assets/svg/code.svg"></i ></a> <a class="margin-right:10px" hidden id="code-slash" show="#code, #demo-code" hide="#code-slash" ><i class="height:18px" src="/assets/svg/code.svg"></i ></a> <a class="margin-right:5px" fullscreen fullscreen-query="#playground"></a> </div> </div> <!-- End SandBox --> </div> </div> </div> <button href="https://github.com/CoCreate-app/CoCreate-stripe/tree/master/docs/index.html?message=docs%3A%20describe%20your%20change..." target="_blank" class="display:flex justify-content:center align-items:center position:fixed bottom:15px right:15px height:50px width:50px border-radius:50% box-shadow:0px_2px_10px_0px_rgba(0,_0,_0,_0.4)"> <i src="../assets/svg/pencil-alt.svg"></i> </button> </main> <script src="https://CoCreate.app/dist/CoCreate.js"></script> </body> </html>