Advertisement
wurtz

stripeJS

May 14th, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. <script src="https://js.stripe.com/terminal/v1/"></script>
  2. <script>
  3.  
  4. $(document).ready(function()
  5. {
  6. discoverReaders();
  7. });
  8.  
  9. var storage = window.localStorage;
  10.  
  11. var terminal = StripeTerminal.create({
  12. onFetchConnectionToken: fetchConnectionToken,
  13. onUnexpectedReaderDisconnect: unexpectedDisconnect,
  14. });
  15.  
  16. function unexpectedDisconnect() {
  17. // You might want to display UI to notify the user and start re-discovering readers
  18. }
  19.  
  20. function fetchConnectionToken() {
  21. // Your backend should call /v1/terminal/connection_tokens and return the JSON response from Stripe
  22. return fetch("{{ route('location-stripe-connection') }}?useCaseId={{ $useCaseId }}&organizationId={{ $organizationId }}&locationId={{ $locationId }}", { method: "POST" })
  23. .then(function(response) {
  24. return response.json();
  25. })
  26. .then(function(data) {
  27. return data.secret;
  28. });
  29. }
  30.  
  31. function checkout()
  32. {
  33. // clientSecret is the client_secret from the PaymentIntent you created in Step 1.
  34. terminal.collectPaymentMethod('{{ $clientSecret }}').then(function(result)
  35. {
  36. if(result.error)
  37. {
  38. // Placeholder for handling result.error
  39. console.log('stripe error');
  40. console.log(result);
  41. }
  42. else
  43. {
  44. // Placeholder for processing result.paymentIntent
  45. console.log('stripe success');
  46. console.log(result);
  47.  
  48. terminal.processPayment(result.paymentIntent).then(function(processPaymentResult)
  49. {
  50. if (processPaymentResult.error)
  51. {
  52. // Placeholder for handling result.error
  53. console.log('processPayment error');
  54. console.log(processPaymentResult);
  55. console.log(processPaymentResult.error.message);
  56. }
  57. else if (processPaymentResult.paymentIntent)
  58. {
  59. // Placeholder for notifying your backend to capture result.paymentIntent.id
  60. console.log('processPayment success');
  61. console.log(processPaymentResult);
  62. Livewire.emit('capturePaymentIntent');
  63. }
  64. });
  65. }
  66. });
  67. }
  68.  
  69. function discoverReaders() {
  70. const config = {simulated: false}
  71. terminal.discoverReaders(config).then(function(discoverResult) {
  72. if (discoverResult.error) {
  73. console.log('Failed to discover: ', discoverResult.error);
  74. } else if (discoverResult.discoveredReaders.length === 0) {
  75. console.log('No available readers.');
  76. } else {
  77. // You should show the list of discoveredReaders to the
  78. // cashier here and let them select which to connect to (see below).
  79. console.log(discoverResult);
  80. connectReader(discoverResult);
  81. }
  82. });
  83. }
  84.  
  85. function connectReader(discoverResult)
  86. {
  87. // first, check for saved readerId
  88. // if not saved, show a dialog to select the reader
  89.  
  90. var savedReaderId = storage.getItem('readerId');
  91.  
  92. var readers = discoverResult.discoveredReaders;
  93.  
  94. if(savedReaderId != null)
  95. {
  96. // a reader has already been selected
  97. // look for it in the discoverResult array
  98. for(var i=0; i<readers.length; i++)
  99. {
  100. var id = readers[i]['id'];
  101.  
  102. if(id == savedReaderId)
  103. {
  104. connectToReader(discoverResult.discoveredReaders[i]);
  105. }
  106. }
  107. }
  108. else
  109. {
  110. // no reader has been selected yet
  111. // show available readers
  112. Livewire.emit('showStripeReaders', readers);
  113. }
  114. }
  115.  
  116. function connectToReader(selectedReader)
  117. {
  118. terminal.connectReader(selectedReader).then(function(connectResult)
  119. {
  120. if (connectResult.error)
  121. {
  122. console.log('Failed to connect: ', connectResult.error);
  123. }
  124. else
  125. {
  126. storage.setItem('readerId', selectedReader['id']);
  127. Livewire.emit('closeStripeLoading');
  128. console.log('Connected to reader: ', connectResult.reader.label);
  129. }
  130. });
  131. }
  132.  
  133. function connectFromSelection(readerId)
  134. {
  135. const config = {simulated: false};
  136. terminal.discoverReaders(config).then(function(discoverResult)
  137. {
  138. if (discoverResult.error)
  139. {
  140. // need error handling
  141. console.log('Failed to discover: ', discoverResult.error);
  142. }
  143. else if (discoverResult.discoveredReaders.length === 0)
  144. {
  145. // need error handling
  146. console.log('No available readers.');
  147. }
  148. else
  149. {
  150. // search for selected reader and connect
  151. var readers = discoverResult.discoveredReaders;
  152.  
  153. for(var i=0; i<readers.length; i++)
  154. {
  155. var id = readers[i]['id'];
  156.  
  157. if(id == readerId)
  158. {
  159. connectToReader(discoverResult.discoveredReaders[i]);
  160. }
  161. }
  162.  
  163. // need error handling if this point is reached
  164. }
  165. });
  166. }
  167.  
  168. // updates the screen on the card reader for the customer
  169. window.addEventListener('prices-updated', event => {
  170.  
  171. console.log('prices-updated');
  172. console.log(event.detail.info);
  173.  
  174. var info = [];
  175.  
  176. $(event.detail.info).each(function($index)
  177. {
  178. info[$index] = {
  179. description: this[0],
  180. quantity: this[1],
  181. amount: this[2] * 100,
  182. }
  183. });
  184.  
  185. var subtotal = $('#subtotal').val();
  186. var tax = $('#tax').val();
  187. var total = $('#total').val();
  188.  
  189. terminal.setReaderDisplay({
  190.  
  191. type: 'cart',
  192. cart: {
  193. line_items: info,
  194. tax: tax * 100,
  195. total: total * 100,
  196. currency: 'usd',
  197. }
  198.  
  199. });
  200.  
  201. });
  202.  
  203. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement