Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

registration.vue 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <section v-if="allow" 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 trial = ref(false)
  21. const allow = ref(true)
  22. const clientSecret = new URLSearchParams(window.location.search).get(
  23. 'payment_intent_client_secret'
  24. );
  25. function getCookie(name) {
  26. var re = new RegExp(name + "=([^;]+)")
  27. var value = re.exec(document.cookie)
  28. return (value != null) ? unescape(value[1]) : null
  29. }
  30. function getUser() {
  31. token.value = getCookie("skouter")
  32. return fetch(`/api/user`,
  33. {method: 'GET',
  34. headers: {
  35. "Accept": "application/json",
  36. "Authorization": `Bearer ${token.value}`,
  37. },
  38. }).then(response => {
  39. if (response.ok) {
  40. return response.json()
  41. }
  42. })
  43. }
  44. function create(user) {
  45. console.log(user)
  46. fetch(`/api/user`,
  47. {method: 'POST',
  48. body: JSON.stringify(user),
  49. headers: {
  50. "Accept": "application/json",
  51. },
  52. }).then(resp => {
  53. if (resp.ok) {
  54. return resp.json().then(u => { err.value = "" }).
  55. then( () => {
  56. token.value = getCookie("skouter")
  57. intent(user)
  58. } )
  59. } else {
  60. resp.text().then( e => err.value = e)
  61. }
  62. })
  63. }
  64. function intent(u) {
  65. let path = `/api/user/subscribe`
  66. if (trial.value) {
  67. path = `/api/user/trial`
  68. }
  69. return fetch(path,
  70. {method: 'POST',
  71. body: JSON.stringify(u),
  72. headers: {
  73. "Accept": "application/json",
  74. "Authorization": `Bearer ${token.value}`,
  75. },
  76. }).then(resp => {
  77. if (resp.ok) {
  78. resp.json().then(s => {
  79. err.value = ""
  80. console.log(s)
  81. sub.value = s
  82. if (["processing", "succeeded"].includes(s.paymentStatus) &&
  83. clientSecret == s.clientSecret) {
  84. step.value = step.value + 2
  85. } else if (s.paymentStatus == "requires_payment_method") {
  86. step.value++
  87. } else {
  88. step.value = 0
  89. }
  90. })
  91. } else {
  92. resp.text().then( e => err.value = e)
  93. }
  94. })
  95. }
  96. onMounted(() => {
  97. if (window.location.pathname == "/free-trial") {
  98. trial.value = true
  99. }
  100. getUser().then( u => {
  101. if (u) { user.value = u; console.log(u); intent(u) }
  102. // If user's account was not created through a trial form but the
  103. // current path is /free-trial, hide the form.
  104. if (u && u.status != "Trial" && trial.value) {
  105. allow.value == false
  106. }
  107. })
  108. })
  109. </script>
  110. <style scoped>
  111. section {
  112. max-width: 400px;
  113. margin: auto;
  114. }
  115. </style>