Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
 
 
 
 
 
 

111 lines
2.5 KiB

  1. <template>
  2. <section class="shadowbox">
  3. <h2>Register</h2>
  4. <account v-if="step == 1" :err="err" @submit="create" />
  5. <billing v-if="step == 2" :err="err" :sub="sub"/>
  6. <completed v-if="step == 3" :err="err" :status="sub.paymentStatus"/>
  7. </section>
  8. </template>
  9. <script setup>
  10. import { ref, onMounted } from "vue"
  11. import Account from "./account.vue"
  12. import Billing from "./billing.vue"
  13. import Completed from "./completed.vue"
  14. let err = ref("")
  15. const stripe = Stripe(process.env.STRIPE_KEY)
  16. const step = ref(1)
  17. const token = ref("")
  18. const user = ref(null)
  19. const sub = ref(null)
  20. const clientSecret = new URLSearchParams(window.location.search).get(
  21. 'payment_intent_client_secret'
  22. );
  23. function getCookie(name) {
  24. var re = new RegExp(name + "=([^;]+)")
  25. var value = re.exec(document.cookie)
  26. return (value != null) ? unescape(value[1]) : null
  27. }
  28. function getUser() {
  29. token.value = getCookie("skouter")
  30. return fetch(`/api/user`,
  31. {method: 'GET',
  32. headers: {
  33. "Accept": "application/json",
  34. "Authorization": `Bearer ${token.value}`,
  35. },
  36. }).then(response => {
  37. if (response.ok) {
  38. return response.json()
  39. }
  40. })
  41. }
  42. function create(user) {
  43. console.log(user)
  44. fetch(`/api/user`,
  45. {method: 'POST',
  46. body: JSON.stringify(user),
  47. headers: {
  48. "Accept": "application/json",
  49. },
  50. }).then(resp => {
  51. if (resp.ok) {
  52. return resp.json().then(u => { err.value = "" }).
  53. then( () => intent(user) )
  54. } else {
  55. resp.text().then( e => err.value = e)
  56. }
  57. })
  58. }
  59. function intent(user) {
  60. return fetch(`/api/user/subscribe`,
  61. {method: 'POST',
  62. body: JSON.stringify(user),
  63. headers: {
  64. "Accept": "application/json",
  65. "Authorization": `Bearer ${token.value}`,
  66. },
  67. }).then(resp => {
  68. if (resp.ok) {
  69. resp.json().then(s => {
  70. err.value = ""
  71. console.log(s)
  72. sub.value = s
  73. step.value++
  74. if (["processing", "succeeded"].includes(s.paymentStatus) &&
  75. clientSecret == s.clientSecret) {
  76. step.value++
  77. } else {
  78. step.value = 0
  79. }
  80. })
  81. } else {
  82. resp.text().then( e => err.value = e)
  83. }
  84. })
  85. }
  86. onMounted(() => {
  87. getUser().then( u => {
  88. if (u) { user.value = u; intent(u) }
  89. })
  90. })
  91. </script>
  92. <style scoped>
  93. section {
  94. max-width: 400px;
  95. margin: auto;
  96. }
  97. </style>